Take Facts
Most of the time you'll just want the thread final result. Sometimes, though, you might want to send out facts while the thread runs rather than just at the end.
Here's an example agent with another card to subscribe for any facts sent by the thread and some small extra logic in +on-agent
to handle them:
We've also made some changes to the thread:
/+ *strandio
|= arg=vase
=/ m (strand:rand ,vase)
^- form:m
;< =path bind:m take-watch
;< ~ bind:m (send-raw-card [%give %fact ~[path] %update !>("message 1")])
;< ~ bind:m %- send-raw-cards
:~ [%give %fact ~[path] %update !>("message 2")]
[%give %fact ~[path] %update !>("message 3")]
[%give %fact ~[path] %update !>("message 4")]
==
;< ~ bind:m (send-raw-card [%give %kick ~[path] ~])
|= strand-input:rand
?+ q.arg [~ %fail %not-foo ~]
%foo
[~ %done arg]
==
Save & |commit
, then run :thread-starter [%test-thread %foo]
. You should see:
Thread started successfully
message 1
message 2
message 3
message 4
Result: foo
Now try :thread-starter [%test-thread %bar]
. You should see:
Thread started successfully
message 1
message 2
message 3
message 4
Thread failed: not-foo
Analysis
In our agent's on-poke
arm we've added a card to subscribe to /thread/[tid]/updates
:
[%pass /thread/updates/[ta-now] %agent [our.bowl %spider] %watch /thread/[tid]/updates]
Note: In practice you'll want to include some kind of tag in the wire so you can differentiate particular threads and subscriptions and test for it in on-agent
.
Threads always send facts on /thread/[tid]/some-path
. The thread itself will see the incoming subscription on /some-path
though, not the full thing.
In the thread we've first added:
;< =path bind:m take-watch
...to take the subscription. Without something like this to handle the incoming %watch
, spider will reject the subscription.
Then we've added:
;< ~ bind:m (send-raw-card [%give %fact ~[path] %update !>("message 1")])
;< ~ bind:m %- send-raw-cards
:~ [%give %fact ~[path] %update !>("message 2")]
[%give %fact ~[path] %update !>("message 3")]
[%give %fact ~[path] %update !>("message 4")]
==
...to send some facts out to subscribers. Here we've used both send-raw-card
and send-raw-cards
to demonstrate both ways.
Note: in practice you'd probably want to send facts on a predetermined path and just test the path of the incoming subscription rather than just accepting anything.
Finally we've added:
;< ~ bind:m (send-raw-card [%give %kick ~[path] ~])
...to kick subscribers. This is important because, unlike on /thread-result
, spider will not automatically kick subscribers when the thread ends. You have to do it explicitly so your agent doesn't accumulate wires with repeated executions.
Back in our agent, in the +on-agent
arm, we've added:
%update
=/ msg !< tape q.cage.sign
%- (slog leaf+msg ~)
`this
... to take the facts and print them.
One last thing: Notice how when we gave the thread an argument of %bar
and made it fail, we still got the facts we sent. This is because such cards are sent immediately as the thread runs, they don't depend on the final result.
Last updated