on 4fdc19d help sign in
178 lines 5.6 kB history raw
1+++
2title = "Main-loop"
3weight = 3
4template = "doc.html"
5+++
7`main-loop` is a useful function included in `strandio` that:
91. lets you create a loop
102. lets you try the same input against multiple functions
113. queues input on `%skip` and then dequeues from the beginning on `%done`
13`main-loop` takes a list of functions as its argument but only moves to the next item in the list on a `[%fail %ignore ~]` (whose usage we'll describe in the second example). In other cases it restarts from the top, so providing multiple functions is only useful for trying the same input against multiple functions.
15## Create a loop
17This is useful if you want to (for example) take an arbitrary number of facts.
19Here's an example of a thread that subscribes to `graph-store` for updates and nicely prints the messages (an extremely basic chat reader):
21#### `chat-watch.hoon`
23```hoon
24/- spider
25/+ *strandio, *graph-store
26=, strand=strand:spider
27=>
28|%
29++ watcher
30 =/ m (strand ,~)
31 ^- form:m
32 %- (main-loop ,~)
33 :~ |= ~
34 ^- form:m
35 ;< =cage bind:m (take-fact /graph-store)
36 =/ up=update !< update q.cage
37 ?. ?=(%add-nodes -.q.up)
38 (pure:m ~)
39 =/ res=tape "{(scow %p entity.resource.q.up)}/{(scow %tas name.resource.q.up)}"
40 =/ node-list `(list (pair index node))`~(tap by nodes.q.up)
41 ?~ node-list
42 (pure:m ~)
43 ?: (gth (lent node-list) 1)
44 %- (slog leaf+"{res}: <multi-node update skipped>" ~)
45 (pure:m ~)
46 =/ from=tape (scow %p author.post.q.i.node-list)
47 =/ conts `(list content)`contents.post.q.i.node-list
48 ?~ conts
49 (pure:m ~)
50 ?: (gth (lent conts) 1)
51 %- (slog leaf+"{res}: [{from}] <mixed-type message skipped>" ~)
52 (pure:m ~)
53 ?. ?=(%text -.i.conts)
54 %- (slog leaf+"{res}: [{from}] <non-text message skipped>" ~)
55 (pure:m ~)
56 =/ msg=tape (trip text.i.conts)
57 %- (slog leaf+"{res}: [{from}] {msg}" ~)
58 (pure:m ~)
59 ==
60--
61^- thread:spider
62|= arg=vase
63=/ m (strand ,vase)
64^- form:m
65;< now=@da bind:m get-time
66;< ~ bind:m (watch-our /graph-store %graph-store /updates)
67;< ~ bind:m watcher
68(pure:m !>(~))
69```
71Save it in `/ted`, `|commit %base`, and run it with `-chat-watch`. Now try typing some messages in the chat and you should see them printed like:
73```
74~zod/test-8488: [~zod] x
75~zod/test-8488: [~zod] blah blah blah
76~zod/test-8488: [~zod] foo
77~zod/test-8488: [~zod] some text
78~zod/test-8488: [~zod] .
79```
81To stop this hit backspace and then run `:spider|kill`.
83First we subscribe to graph-store for updates with `watch-our`, then we call the watcher arm of the core we have added. Watcher just calls `main-loop`:
85```hoon
86=/ m (strand ,~)
87^- form:m
88%- (main-loop ,~)
89...
90```
92...with a list of functions. In this case we've just given it one. Our function first calls `take-fact`:
94```hoon
95;< =cage bind:m (take-fact /graph-store)
96```
98...to receive the fact and then the rest is just processing & printing logic which isn't too important.
100Once this is done, main-loop will just call the same function again which will again wait for a fact and so on. So you see how it creates a loop. The only way to exit the loop is with a `%fail` or else by poking spider with a `%spider-stop` and the thread's `tid`.
102## Try input against multiple functions
104To try the same input against multiple function you must use another `strandio` function `handle`. Handle converts a `%skip` into a `[fail %ignore ~]`. When `main-loop` sees a `[fail %ignore ~]` it tries the next function in its list with the same input.
106Here are two files: `tester.hoon` and `tested.hoon`. Save them both to `/ted` in the `%base` desk, `|commit %base` and run `-tester`. You should see:
108```
109> -tester
110baz
112```
114#### `tester.hoon`
116```hoon
117/- spider
118/+ *strandio
119=, strand=strand:spider
120^- thread:spider
121|= arg=vase
122=/ m (strand ,vase)
123^- form:m
124;< tid=tid:spider bind:m (start-thread %tested)
125;< our=ship bind:m get-our
126;< ~ bind:m %- poke
127 :- [our %spider]
128 [%spider-input !>([tid `cage`[%baz !>("baz")]])]
129(pure:m !>(~))
130```
132#### `tested.hoon`
134```hoon
135/- spider
136/+ *strandio
137=, strand=strand:spider
138=>
139|%
140++ looper
141 =/ m (strand ,~)
142 ^- form:m
143 %- (main-loop ,~)
144 :~ |= ~
145 ^- form:m
146 ;< =vase bind:m ((handle ,vase) (take-poke %foo))
147 =/ msg=tape !<(tape vase)
148 %- (slog leaf+"{msg}" ~)
149 (pure:m ~)
150 ::
151 |= ~
152 ^- form:m
153 ;< =vase bind:m ((handle ,vase) (take-poke %bar))
154 =/ msg=tape !<(tape vase)
155 %- (slog leaf+"{msg}" ~)
156 (pure:m ~)
157 ::
158 |= ~
159 ^- form:m
160 ;< =vase bind:m ((handle ,vase) (take-poke %baz))
161 =/ msg=tape !<(tape vase)
162 %- (slog leaf+"{msg}" ~)
163 (pure:m ~)
164 ==
165--
166^- thread:spider
167|= arg=vase
168=/ m (strand ,vase)
169^- form:m
170;< ~ bind:m looper
171(pure:m !>(~))
172```
174The first thread (tester.hoon) just starts the second thread and pokes it with a vase containing `"baz"` and the mark `%baz`.
176The second thread (tested.hoon) has a `main-loop` with a list of three `take-poke` strands. As you can see it's the third one expecting a mark of `%baz` but yet it still successfully prints the message. This is because it tried the previous two which each saw the wrong mark and said `%skip`.
178Notice we've wrapped the `take-poke`s in `handle` to convert the `%skip`s into `[%fail %ignore ~]`s, which `main-loop` takes to mean it should try the next function with the same input.