trebol 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/README.md +2 -0
  2. package/dist/cli.js +10040 -7519
  3. package/dist/lib/core/boot.js +10 -7
  4. package/dist/lib/core/eval.js +2 -2
  5. package/dist/lib/core/machine/checks.d.ts +1 -0
  6. package/dist/lib/core/machine/checks.js +250 -0
  7. package/dist/lib/core/machine/events.d.ts +1 -0
  8. package/dist/lib/core/machine/events.js +365 -0
  9. package/dist/lib/core/machine/running.d.ts +1 -0
  10. package/dist/lib/core/machine/running.js +180 -0
  11. package/dist/lib/core/machine/shared.d.ts +1 -0
  12. package/dist/lib/core/machine/shared.js +123 -0
  13. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  14. package/dist/lib/core/machine/snapshot.js +244 -0
  15. package/dist/lib/core/machine/spec.d.ts +1 -0
  16. package/dist/lib/core/machine/spec.js +274 -0
  17. package/dist/lib/core/machine/words.d.ts +1 -0
  18. package/dist/lib/core/machine/words.js +98 -0
  19. package/dist/lib/core/machine.d.ts +1 -0
  20. package/dist/lib/core/machine.js +17 -0
  21. package/dist/lib/core/mezz.js +31 -0
  22. package/dist/lib/core/natives/control.js +1 -1
  23. package/dist/lib/core/natives/data.js +6 -1
  24. package/dist/lib/core/natives/series.js +49 -9
  25. package/dist/lib/core/natives/text.js +0 -1
  26. package/dist/lib/host/browser/calendar.d.ts +34 -0
  27. package/dist/lib/host/browser/calendar.js +268 -0
  28. package/dist/lib/host/browser/count.d.ts +15 -0
  29. package/dist/lib/host/browser/count.js +48 -0
  30. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  31. package/dist/lib/host/browser/dialogs.js +443 -0
  32. package/dist/lib/host/browser/drag.d.ts +70 -0
  33. package/dist/lib/host/browser/drag.js +268 -0
  34. package/dist/lib/host/browser/drawings.d.ts +30 -0
  35. package/dist/lib/host/browser/drawings.js +176 -0
  36. package/dist/lib/host/browser/events.d.ts +16 -0
  37. package/dist/lib/host/browser/events.js +125 -0
  38. package/dist/lib/host/browser/faces.d.ts +35 -0
  39. package/dist/lib/host/browser/faces.js +131 -0
  40. package/dist/lib/host/browser/fields.d.ts +16 -0
  41. package/dist/lib/host/browser/fields.js +322 -0
  42. package/dist/lib/host/browser/keys.d.ts +29 -0
  43. package/dist/lib/host/browser/keys.js +151 -0
  44. package/dist/lib/host/browser/lookup.d.ts +25 -0
  45. package/dist/lib/host/browser/lookup.js +275 -0
  46. package/dist/lib/host/browser/menus.d.ts +20 -0
  47. package/dist/lib/host/browser/menus.js +149 -0
  48. package/dist/lib/host/browser/mount.d.ts +92 -0
  49. package/dist/lib/host/browser/mount.js +448 -0
  50. package/dist/lib/host/browser/orbits.d.ts +90 -0
  51. package/dist/lib/host/browser/orbits.js +379 -0
  52. package/dist/lib/host/browser/page.d.ts +13 -0
  53. package/dist/lib/host/browser/page.js +35 -0
  54. package/dist/lib/host/browser/richtext.d.ts +19 -0
  55. package/dist/lib/host/browser/richtext.js +379 -0
  56. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  57. package/dist/lib/host/browser/row-faces.js +74 -0
  58. package/dist/lib/host/browser/screens.d.ts +37 -0
  59. package/dist/lib/host/browser/screens.js +140 -0
  60. package/dist/lib/host/browser/swipe.d.ts +31 -0
  61. package/dist/lib/host/browser/swipe.js +178 -0
  62. package/dist/lib/host/browser/sync.d.ts +24 -0
  63. package/dist/lib/host/browser/sync.js +450 -0
  64. package/dist/lib/host/browser/tables.d.ts +65 -0
  65. package/dist/lib/host/browser/tables.js +505 -0
  66. package/dist/lib/host/browser/times.d.ts +14 -0
  67. package/dist/lib/host/browser/times.js +114 -0
  68. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  69. package/dist/lib/host/browser/toolbars.js +129 -0
  70. package/dist/lib/host/browser/trees.d.ts +20 -0
  71. package/dist/lib/host/browser/trees.js +235 -0
  72. package/dist/lib/host/browser/uploads.d.ts +29 -0
  73. package/dist/lib/host/browser/uploads.js +370 -0
  74. package/dist/lib/host/browser/widget.d.ts +13 -0
  75. package/dist/lib/host/browser/widget.js +105 -0
  76. package/dist/lib/host/browser.d.ts +25 -0
  77. package/dist/lib/host/browser.js +84 -4812
  78. package/dist/lib/host/devtools/console.d.ts +9 -0
  79. package/dist/lib/host/devtools/console.js +104 -0
  80. package/dist/lib/host/devtools/faces.d.ts +25 -0
  81. package/dist/lib/host/devtools/faces.js +169 -0
  82. package/dist/lib/host/devtools/log.d.ts +3 -0
  83. package/dist/lib/host/devtools/log.js +32 -0
  84. package/dist/lib/host/devtools/machines.d.ts +34 -0
  85. package/dist/lib/host/devtools/machines.js +212 -0
  86. package/dist/lib/host/devtools/read.d.ts +13 -0
  87. package/dist/lib/host/devtools/read.js +52 -0
  88. package/dist/lib/host/devtools/state.d.ts +90 -0
  89. package/dist/lib/host/devtools/state.js +123 -0
  90. package/dist/lib/host/devtools.d.ts +15 -19
  91. package/dist/lib/host/devtools.js +39 -427
  92. package/dist/lib/host/fonts.js +4 -3
  93. package/dist/lib/host/icons.js +4 -0
  94. package/dist/lib/host/index.d.ts +3 -1
  95. package/dist/lib/host/index.js +107 -4
  96. package/dist/lib/host/locale.d.ts +18 -0
  97. package/dist/lib/host/locale.js +75 -5
  98. package/dist/lib/host/markdown.d.ts +4 -0
  99. package/dist/lib/host/markdown.js +8 -0
  100. package/dist/lib/host/modules.js +118 -19
  101. package/dist/lib/host/motion.d.ts +32 -0
  102. package/dist/lib/host/motion.js +190 -0
  103. package/dist/lib/host/net.js +5 -2
  104. package/dist/lib/host/palette.d.ts +17 -0
  105. package/dist/lib/host/palette.js +131 -0
  106. package/dist/lib/host/scheduler.d.ts +2 -0
  107. package/dist/lib/host/scheduler.js +4 -0
  108. package/dist/lib/host/share.d.ts +31 -0
  109. package/dist/lib/host/share.js +134 -0
  110. package/dist/lib/host/theme/classic.d.ts +1 -0
  111. package/dist/lib/host/theme/classic.js +120 -0
  112. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  113. package/dist/lib/host/theme/cupertino.js +65 -0
  114. package/dist/lib/host/theme/data.d.ts +1 -0
  115. package/dist/lib/host/theme/data.js +109 -0
  116. package/dist/lib/host/theme/devtools.d.ts +1 -0
  117. package/dist/lib/host/theme/devtools.js +166 -0
  118. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  119. package/dist/lib/host/theme/dialogs.js +63 -0
  120. package/dist/lib/host/theme/fluent.d.ts +1 -0
  121. package/dist/lib/host/theme/fluent.js +64 -0
  122. package/dist/lib/host/theme/forms.d.ts +1 -0
  123. package/dist/lib/host/theme/forms.js +239 -0
  124. package/dist/lib/host/theme/fusion.d.ts +1 -0
  125. package/dist/lib/host/theme/fusion.js +66 -0
  126. package/dist/lib/host/theme/layout.d.ts +1 -0
  127. package/dist/lib/host/theme/layout.js +166 -0
  128. package/dist/lib/host/theme/media.d.ts +1 -0
  129. package/dist/lib/host/theme/media.js +106 -0
  130. package/dist/lib/host/theme/navigation.d.ts +1 -0
  131. package/dist/lib/host/theme/navigation.js +36 -0
  132. package/dist/lib/host/theme/panes.d.ts +1 -0
  133. package/dist/lib/host/theme/panes.js +99 -0
  134. package/dist/lib/host/theme/popups.d.ts +1 -0
  135. package/dist/lib/host/theme/popups.js +149 -0
  136. package/dist/lib/host/theme/responsive.d.ts +1 -0
  137. package/dist/lib/host/theme/responsive.js +103 -0
  138. package/dist/lib/host/theme/toasts.d.ts +1 -0
  139. package/dist/lib/host/theme/toasts.js +62 -0
  140. package/dist/lib/host/theme.js +19 -1384
  141. package/dist/lib/ui/dialect/events.trb +276 -0
  142. package/dist/lib/ui/dialect/faces.trb +135 -0
  143. package/dist/lib/ui/dialect/layout.trb +283 -0
  144. package/dist/lib/ui/dialect/palette.trb +87 -0
  145. package/dist/lib/ui/dialect/problems.trb +174 -0
  146. package/dist/lib/ui/dialect/reactions.trb +198 -0
  147. package/dist/lib/ui/dialect/rows.trb +208 -0
  148. package/dist/lib/ui/dialect/screens.trb +293 -0
  149. package/dist/lib/ui/dialect/steps.trb +203 -0
  150. package/dist/lib/ui/dialect/validation.trb +137 -0
  151. package/dist/lib/ui/dialect/view.trb +110 -0
  152. package/dist/lib/ui/dialect/walking.trb +98 -0
  153. package/dist/lib/ui/dialect/words.trb +70 -0
  154. package/dist/lib/ui/i18n.trb +17 -0
  155. package/dist/lib/ui/test.trb +3 -556
  156. package/dist/lib/ui/testing/actions.trb +421 -0
  157. package/dist/lib/ui/testing/faces.trb +148 -0
  158. package/dist/lib/ui/testing/server.trb +73 -0
  159. package/dist/lib/ui/ui.trb +25 -1675
  160. package/dist/trebol.js +4463 -1891
  161. package/package.json +1 -1
  162. package/src/core/boot.ts +9 -6
  163. package/src/core/eval.ts +2 -2
  164. package/src/core/machine/checks.ts +251 -0
  165. package/src/core/machine/events.ts +366 -0
  166. package/src/core/machine/running.ts +181 -0
  167. package/src/core/machine/shared.ts +124 -0
  168. package/src/core/machine/snapshot.ts +245 -0
  169. package/src/core/machine/spec.ts +275 -0
  170. package/src/core/machine/words.ts +99 -0
  171. package/src/core/machine.ts +20 -0
  172. package/src/core/mezz.ts +31 -0
  173. package/src/core/natives/control.ts +1 -1
  174. package/src/core/natives/data.ts +6 -1
  175. package/src/core/natives/parse.ts +1 -1
  176. package/src/core/natives/series.ts +47 -11
  177. package/src/core/natives/text.ts +0 -1
  178. package/src/host/browser/calendar.ts +264 -0
  179. package/src/host/browser/count.ts +48 -0
  180. package/src/host/browser/dialogs.ts +439 -0
  181. package/src/host/browser/drag.ts +278 -0
  182. package/src/host/browser/drawings.ts +178 -0
  183. package/src/host/browser/events.ts +117 -0
  184. package/src/host/browser/faces.ts +143 -0
  185. package/src/host/browser/fields.ts +315 -0
  186. package/src/host/browser/keys.ts +145 -0
  187. package/src/host/browser/lookup.ts +262 -0
  188. package/src/host/browser/menus.ts +146 -0
  189. package/src/host/browser/mount.ts +420 -0
  190. package/src/host/browser/orbits.ts +418 -0
  191. package/src/host/browser/page.ts +43 -0
  192. package/src/host/browser/richtext.ts +346 -0
  193. package/src/host/browser/row-faces.ts +75 -0
  194. package/src/host/browser/screens.ts +142 -0
  195. package/src/host/browser/swipe.ts +168 -0
  196. package/src/host/browser/sync.ts +354 -0
  197. package/src/host/browser/tables.ts +461 -0
  198. package/src/host/browser/times.ts +115 -0
  199. package/src/host/browser/toolbars.ts +126 -0
  200. package/src/host/browser/trees.ts +205 -0
  201. package/src/host/browser/uploads.ts +367 -0
  202. package/src/host/browser/widget.ts +103 -0
  203. package/src/host/browser.ts +82 -4612
  204. package/src/host/devtools/console.ts +102 -0
  205. package/src/host/devtools/faces.ts +160 -0
  206. package/src/host/devtools/log.ts +34 -0
  207. package/src/host/devtools/machines.ts +208 -0
  208. package/src/host/devtools/read.ts +58 -0
  209. package/src/host/devtools/state.ts +148 -0
  210. package/src/host/devtools.ts +39 -442
  211. package/src/host/fonts.ts +4 -3
  212. package/src/host/icons.ts +4 -0
  213. package/src/host/index.ts +107 -4
  214. package/src/host/locale.ts +71 -5
  215. package/src/host/markdown.ts +9 -0
  216. package/src/host/modules.ts +117 -21
  217. package/src/host/motion.ts +204 -0
  218. package/src/host/net.ts +4 -2
  219. package/src/host/palette.ts +139 -0
  220. package/src/host/scheduler.ts +5 -0
  221. package/src/host/share.ts +168 -0
  222. package/src/host/theme/classic.ts +121 -0
  223. package/src/host/theme/cupertino.ts +66 -0
  224. package/src/host/theme/data.ts +110 -0
  225. package/src/host/theme/devtools.ts +167 -0
  226. package/src/host/theme/dialogs.ts +64 -0
  227. package/src/host/theme/fluent.ts +65 -0
  228. package/src/host/theme/forms.ts +240 -0
  229. package/src/host/theme/fusion.ts +67 -0
  230. package/src/host/theme/layout.ts +167 -0
  231. package/src/host/theme/media.ts +107 -0
  232. package/src/host/theme/navigation.ts +37 -0
  233. package/src/host/theme/panes.ts +100 -0
  234. package/src/host/theme/popups.ts +150 -0
  235. package/src/host/theme/responsive.ts +104 -0
  236. package/src/host/theme/toasts.ts +63 -0
  237. package/src/host/theme.ts +19 -1384
  238. package/src/ui/dialect/events.trb +276 -0
  239. package/src/ui/dialect/faces.trb +135 -0
  240. package/src/ui/dialect/layout.trb +283 -0
  241. package/src/ui/dialect/palette.trb +87 -0
  242. package/src/ui/dialect/problems.trb +174 -0
  243. package/src/ui/dialect/reactions.trb +198 -0
  244. package/src/ui/dialect/rows.trb +208 -0
  245. package/src/ui/dialect/screens.trb +293 -0
  246. package/src/ui/dialect/steps.trb +203 -0
  247. package/src/ui/dialect/validation.trb +137 -0
  248. package/src/ui/dialect/view.trb +110 -0
  249. package/src/ui/dialect/walking.trb +98 -0
  250. package/src/ui/dialect/words.trb +70 -0
  251. package/src/ui/i18n.trb +17 -0
  252. package/src/ui/test.trb +3 -556
  253. package/src/ui/testing/actions.trb +421 -0
  254. package/src/ui/testing/faces.trb +148 -0
  255. package/src/ui/testing/server.trb +73 -0
  256. package/src/ui/ui.trb +25 -1675
@@ -0,0 +1,181 @@
1
+ // Machines in Trebol (see machine.ts). The engine, part three: running, the states the machine is in, entering
2
+ // and leaving them, and its timers.
3
+
4
+ export const RUNNING = String.raw`
5
+ ; --- running ----------------------------------------------------------------
6
+
7
+ chain: function [m top leaf] [
8
+ out: copy []
9
+ id: leaf
10
+ while [true] [
11
+ insert out id
12
+ if id = top [return out]
13
+ id: select node m id 'parent
14
+ ]
15
+ ]
16
+
17
+ lca: function [m a b] [
18
+ up: chain m 1 a
19
+ while [not find up b] [b: select node m b 'parent]
20
+ b
21
+ ]
22
+
23
+ holds?: function [m guard] [
24
+ if none? guard [return true]
25
+ r: try guard
26
+ either error? :r [false] [either :r [true] [false]]
27
+ ]
28
+
29
+ ; The target and the sets of a transition, as they are now; none when a choose has nowhere to go.
30
+ ; The block of a select runs once, here: a value no branch takes is a mistake.
31
+ route: function [m t] [
32
+ unless t/choices [return reduce [t/target t/set]]
33
+ if t/select [
34
+ v: do t/select
35
+ foreach c t/choices [if :v = first first c [return reduce [second c third c]]]
36
+ if t/other [return reduce [t/other t/other-set]]
37
+ fail rejoin ["select gave " mold :v ", which no branch takes"] t/place
38
+ ]
39
+ foreach c t/choices [if holds? m first c [return reduce [second c third c]]]
40
+ if t/other [return reduce [t/other t/other-set]]
41
+ none
42
+ ]
43
+
44
+ ; want: none = a transition with no event, true = any, a word = that event.
45
+ ; The route of the one that fits is kept in m/taken, for go. A way out of a parallel
46
+ ; with no event sees in event how each of its states ended: a to waits for all of them.
47
+ fits?: function [m t want] [
48
+ case [
49
+ want = true [true]
50
+ none? want [
51
+ if t/events [return false]
52
+ if select node m t/source 'parallel [
53
+ if all [none? t/choices inside? m t/source] [return false]
54
+ m/scope/event: results m t/source
55
+ ]
56
+ to logic! m/taken: route m t
57
+ ]
58
+ true [all [t/events find t/events want holds? m t/guard m/taken: route m t]]
59
+ ]
60
+ ]
61
+
62
+ ; The transition from state id that fits: its own first, then, when outer, those of the
63
+ ; states around it, up to the state stop (none: up to the top).
64
+ find-from: function [m id want outer stop] [
65
+ while [all [id id <> 1 id <> stop]] [
66
+ n: node m id
67
+ foreach t select node m n/parent 'transitions [
68
+ if all [t/source = id fits? m t want] [return t]
69
+ ]
70
+ unless outer [return none]
71
+ id: n/parent
72
+ ]
73
+ none
74
+ ]
75
+
76
+ ; --- the states the machine is in: m/on holds each, from the top down to the leaves ---
77
+
78
+ on?: function [m id] [to logic! find m/on id]
79
+
80
+ ; Whether a state has one of its own states on.
81
+ inside?: function [m id] [
82
+ foreach c select node m id 'children [if on? m c [return true]]
83
+ false
84
+ ]
85
+
86
+ ; The innermost states on, in the order the block gives them.
87
+ leaves: function [m] [
88
+ out: copy []
89
+ foreach id m/on [unless inside? m id [append out id]]
90
+ sort out
91
+ ]
92
+
93
+ ; The nearest parallel around a state, or none.
94
+ region-of: function [m id] [
95
+ while [id: select node m id 'parent] [if select node m id 'parallel [return id]]
96
+ none
97
+ ]
98
+
99
+ ; How each state of a parallel ended: its outcome, or the name of its final state; none while on.
100
+ results: function [m id] [
101
+ make object! collect [
102
+ foreach c select node m id 'children [
103
+ keep to set-word! select node m c 'name
104
+ v: either on? m c [none] [select m/results c]
105
+ keep either v [to lit-word! v] ['none]
106
+ ]
107
+ ]
108
+ ]
109
+
110
+ hook: function [m id which] [
111
+ n: node m id
112
+ if b: select n which [
113
+ either which = 'exit [
114
+ m/exiting: true
115
+ do b
116
+ m/exiting: false
117
+ ] [do b]
118
+ ]
119
+ ]
120
+
121
+ enter: function [m id] [
122
+ m/active: id
123
+ append m/on id
124
+ arm m id
125
+ work m id
126
+ hook m id 'enter
127
+ ]
128
+
129
+ leave: function [m id] [
130
+ m/active: id
131
+ disarm m id
132
+ disarm m negate id
133
+ hook m id 'exit
134
+ remove find m/on id
135
+ ]
136
+
137
+ ; --- time: a timer per state with a timeout, and one (key 0) for the ttl ---
138
+
139
+ ; The timer sends the machine its own event; a serial tells a timer that was stopped.
140
+ clock: function [m key time name] [clock-at m key now/precise + to time! time name]
141
+
142
+ ; A timer that goes off at a moment, kept with it for snapshot; one already past goes off at once.
143
+ clock-at: function [m key at name] [
144
+ s: serial m
145
+ left: difference at now/precise
146
+ if left < 0:00 [left: 0:00]
147
+ put m/timers key reduce [s delay left compose [machine-engine/post (m) (to lit-word! name) (key) (s)] at]
148
+ ]
149
+
150
+ arm: function [m id] [
151
+ unless v: select node m id 'timeout [exit]
152
+ if path? v [
153
+ v: get in m/data second v
154
+ unless any [number? :v time? :v] [
155
+ do make error! rejoin ["machine: the timeout of " select node m id 'name " is not seconds or a time: " mold :v]
156
+ ]
157
+ ]
158
+ clock m id v 'timeout
159
+ ]
160
+
161
+ disarm: function [m key] [
162
+ if x: select m/timers key [
163
+ drop second x
164
+ put m/timers key none
165
+ ]
166
+ ]
167
+
168
+ ; Stops a timer, a task or a machine invoked; none is one that has already ended.
169
+ drop: function [x] [
170
+ case [
171
+ object? x [
172
+ stop x
173
+ x/caller: none
174
+ x/done: true
175
+ ]
176
+ block? x [foreach c x [drop c]]
177
+ x [cancel x]
178
+ ]
179
+ ]
180
+
181
+ `;
@@ -0,0 +1,124 @@
1
+ // Machines in Trebol (see machine.ts). The engine, part six: one machine for every tab, run by the tab that
2
+ // holds its lock; it closes the engine object.
3
+
4
+ export const SHARED = String.raw`
5
+ ; --- shared: one machine for every tab of the app, run by the tab that owns it ---
6
+ ; The owner holds a lock named after the machine and tells the other tabs its
7
+ ; snapshot after each event; they keep a copy and send their events to it.
8
+
9
+ share-name: function [m] [rejoin ["trebol-machine-" m/shared]]
10
+
11
+ ; Makes m the owner, or a copy of the owner another tab has.
12
+ share-start: function [m saved] [
13
+ ; a host with no tabs has no share-open: the machine is only this one's
14
+ m/channel: all [value? 'share-open share-open share-name m compose [machine-engine/heard (m) message asked]]
15
+ either any [
16
+ none? m/channel
17
+ share-lock share-name m compose [machine-engine/took (m)]
18
+ ] [
19
+ either saved [resume m saved] [start m]
20
+ ] [
21
+ m/owner: false
22
+ share-ask m/channel mold/all [hello]
23
+ ]
24
+ m
25
+ ]
26
+
27
+ ; The snapshot to the other tabs, answering the asks done since the last one.
28
+ tell: function [m fresh] [
29
+ asks: m/asks
30
+ m/asks: copy []
31
+ share-post m/channel mold/all reduce ['state snapshot m] asks fresh
32
+ ]
33
+
34
+ ; What another tab says: to the owner an event, restart or hello; to a copy the owner's snapshot.
35
+ heard: function [m message asked] [
36
+ x: load message
37
+ unless m/owner [
38
+ if 'state = first x [
39
+ m/seen: second x
40
+ clean m
41
+ restore m m/seen
42
+ ]
43
+ exit
44
+ ]
45
+ if 'state = first x [exit]
46
+ if asked [append m/asks asked]
47
+ obey m x
48
+ unless m/busy [tell m false]
49
+ ]
50
+
51
+ ; An event or a restart that came from another tab.
52
+ obey: function [m x] [
53
+ switch first x [
54
+ event [
55
+ append/only m/queue reduce [second x object-of third x]
56
+ unless m/busy [run m]
57
+ ]
58
+ restart [again m]
59
+ rewind [rewind m second x]
60
+ ]
61
+ ]
62
+
63
+ ; The owner's tab has gone: this one goes on from the last snapshot it was told.
64
+ took: function [m] [
65
+ m/owner: true
66
+ clean m
67
+ either m/seen [resume m m/seen] [start m]
68
+ foreach [id message] share-asks m/channel [
69
+ append m/asks id
70
+ obey m load message
71
+ ]
72
+ tell m true
73
+ ]
74
+
75
+ ; restart, for the owner or a machine of one tab.
76
+ again: function [m] [
77
+ stop m
78
+ fresh m
79
+ m/history: make map! []
80
+ m/outcome: m/error: m/active: m/last: none
81
+ m/on: copy []
82
+ m/results: make map! []
83
+ m/done: false
84
+ clear m/queue
85
+ start m
86
+ ]
87
+
88
+ make-machine: function [spec [block!]] [
89
+ spec: copy/deep spec
90
+ m: make object! [
91
+ state: data: outcome: error: active: data-spec: ttl: taken: none
92
+ done: false
93
+ nodes: copy [] code: copy [] history: make map! [] queue: copy []
94
+ timers: make map! [] serial: 0 deferred: copy []
95
+ on: copy [] results: make map! [] last: caller: none
96
+ busy: false exiting: false on-save: saver: none
97
+ shared: channel: seen: none owner: true asks: copy []
98
+ scope: make object! [data: event: none]
99
+ ]
100
+ add-node m none none
101
+ level m node m 1 spec
102
+ ; targets once every state is known: one inside may lead to one further out
103
+ foreach n m/nodes [foreach t n/transitions [resolve m n t]]
104
+ if empty? select node m 1 'children [fail "a machine needs a state" spec]
105
+ check-timeouts m
106
+ check-data m
107
+ check-reach m
108
+ check-hang m
109
+ m
110
+ ]
111
+
112
+ ; The data as the machine starts with it.
113
+ fresh: function [m] [
114
+ m/data: make object! copy/deep any [m/data-spec []]
115
+ m/scope/data: m/data
116
+ ]
117
+
118
+ start: function [m] [
119
+ append/only m/queue reduce [none make object! []]
120
+ run m
121
+ ]
122
+ ]
123
+
124
+ `;
@@ -0,0 +1,245 @@
1
+ // Machines in Trebol (see machine.ts). The engine, part five: snapshot and resume, can, the graph of the states,
2
+ // and on-save.
3
+
4
+ export const SNAPSHOT = String.raw`
5
+ ; --- snapshot: the machine as plain data, which machine/from resumes ---
6
+
7
+ ; A state as the block of the names from the top down to it, and back.
8
+ names-of: function [m id] [collect [foreach x next chain m 1 id [keep select node m x 'name]]]
9
+
10
+ id-of: function [m names] [
11
+ id: 1
12
+ foreach w names [
13
+ unless id: child m id w [do make error! rejoin ["machine/from: no state " mold to path! names suggest w state-names m]]
14
+ ]
15
+ id
16
+ ]
17
+
18
+ ; An object from a block of field: value, the values taken as they are.
19
+ object-of: function [body] [
20
+ o: make object! append collect [foreach [f v] body [keep to set-word! f]] [none]
21
+ foreach [f v] body [set in o to word! f :v]
22
+ o
23
+ ]
24
+
25
+ snapshot: function [m] [
26
+ timers: copy [] tasks: copy [] ttl: none
27
+ foreach [key x] m/timers [
28
+ if x [
29
+ case [
30
+ key = 0 [ttl: third x]
31
+ key > 0 [append/only timers names-of m key append timers third x]
32
+ true [
33
+ w: second x
34
+ append/only tasks names-of m negate key
35
+ append/only tasks case [
36
+ object? w [snapshot w]
37
+ block? w [
38
+ reduce [
39
+ 'outcomes copy third x 'data copy fourth x
40
+ 'machines collect [foreach c w [keep/only either c/done [none] [snapshot c]]]
41
+ ]
42
+ ]
43
+ true ['run]
44
+ ]
45
+ ]
46
+ ]
47
+ ]
48
+ ]
49
+ reduce [
50
+ 'on collect [foreach id m/on [keep/only names-of m id]]
51
+ 'data body-of m/data
52
+ 'history collect [foreach [k v] m/history [if v [keep/only names-of m k keep/only names-of m v]]]
53
+ 'results collect [foreach [k v] m/results [keep/only names-of m k keep :v]]
54
+ 'deferred collect [foreach e m/deferred [keep/only reduce [first e body-of second e]]]
55
+ 'timers timers 'ttl ttl 'tasks tasks
56
+ 'done m/done 'outcome m/outcome 'last m/last
57
+ ]
58
+ ]
59
+
60
+ ; Puts a machine made from its spec where the snapshot left it: no on-enter runs again, the
61
+ ; timers go off when they were going to, a run starts over and a machine invoked resumes.
62
+ resume: function [m saved] [
63
+ restore m saved
64
+ unless m/done [
65
+ if t: select saved 'ttl [clock-at m 0 t 'expired]
66
+ foreach [names t] select saved 'timers [clock-at m id-of m names t 'timeout]
67
+ foreach [names w] select saved 'tasks [revive m names w]
68
+ ]
69
+ m/state: where m
70
+ m
71
+ ]
72
+
73
+ ; The plain part of a snapshot: states, data, history, results, the events held.
74
+ restore: function [m saved] [
75
+ unless block? :saved [do make error! "machine/from: a snapshot is a block"]
76
+ foreach names select saved 'on [append m/on id-of m names]
77
+ foreach [f v] select saved 'data [
78
+ unless in m/data to word! f [do make error! rejoin ["machine/from: data has no " to word! f suggest to word! f words-of m/data]]
79
+ set in m/data to word! f :v
80
+ ]
81
+ foreach [k v] select saved 'history [put m/history id-of m k id-of m v]
82
+ foreach [k v] select saved 'results [put m/results id-of m k :v]
83
+ foreach e select saved 'deferred [append/only m/deferred reduce [first e object-of second e]]
84
+ m/done: true = select saved 'done
85
+ m/outcome: select saved 'outcome
86
+ m/last: select saved 'last
87
+ m/state: where m
88
+ ]
89
+
90
+ ; The work of a state as the snapshot left it: a run starts over, a machine invoked resumes.
91
+ revive: function [m names w] [
92
+ id: id-of m names
93
+ n: node m id
94
+ case [
95
+ n/run [work m id]
96
+ all [n/invoke n/for block? :w] [
97
+ s: serial m
98
+ kids: copy []
99
+ machines: select w 'machines
100
+ repeat i length? machines [
101
+ if c: pick machines i [
102
+ c: resumed n c
103
+ c/caller: reduce [m id s i]
104
+ append kids c
105
+ ]
106
+ ]
107
+ put m/timers negate id reduce [s kids copy select w 'outcomes copy select w 'data length? kids]
108
+ ]
109
+ all [n/invoke block? :w] [
110
+ c: resumed n w
111
+ s: serial m
112
+ c/caller: reduce [m id s]
113
+ put m/timers negate id reduce [s c]
114
+ ]
115
+ ]
116
+ ]
117
+
118
+ ; Forgets the states, history and results, before a snapshot is put in.
119
+ clean: function [m] [
120
+ stop m
121
+ m/on: copy []
122
+ m/history: make map! []
123
+ m/results: make map! []
124
+ clear m/queue
125
+ ]
126
+
127
+ ; The machine back where a snapshot of it left it, in place: what it was doing
128
+ ; is stopped, and nothing it does on entering a state runs again. A form in
129
+ ; steps goes back with it. A copy of a shared machine asks the owner's tab.
130
+ rewind: function [m saved] [
131
+ unless m/owner [
132
+ share-ask m/channel mold/all reduce ['rewind saved]
133
+ return m
134
+ ]
135
+ clean m
136
+ m/done: false
137
+ m/outcome: m/error: m/active: m/last: none
138
+ resume m saved
139
+ saved m
140
+ m
141
+ ]
142
+
143
+ ; Whether a state the machine is in, or one around it, has a way out on an event.
144
+ can: function [m name] [
145
+ foreach leaf leaves m [
146
+ id: leaf
147
+ while [all [id id <> 1]] [
148
+ n: node m id
149
+ foreach t select node m n/parent 'transitions [
150
+ if all [t/source = id t/events find t/events name] [return true]
151
+ ]
152
+ id: n/parent
153
+ ]
154
+ ]
155
+ false
156
+ ]
157
+
158
+ ; --- graph: the states and ways as plain data, for tools that draw them ---
159
+
160
+ key-of: function [m id] [
161
+ names: names-of m id
162
+ either 1 = length? names [first names] [to path! names]
163
+ ]
164
+
165
+ graph: function [m] [
166
+ states: copy []
167
+ foreach n next m/nodes [
168
+ parent: node m n/parent
169
+ append states key-of m n/id
170
+ append/only states reduce [
171
+ 'kind case [n/final ['final] n/parallel ['parallel] true ['state]]
172
+ 'initial n/id = parent/initial
173
+ 'on on? m n/id
174
+ 'history n/history
175
+ 'outcome n/outcome
176
+ 'timeout :n/timeout
177
+ 'run either n/run [copy/deep n/run] [none]
178
+ 'invoke either block? :n/invoke [graph make-machine n/invoke] [:n/invoke]
179
+ 'for n/for
180
+ 'in :n/in
181
+ 'defer copy n/defer
182
+ ]
183
+ ]
184
+ ways: copy []
185
+ foreach n m/nodes [
186
+ foreach t n/transitions [
187
+ way: reduce ['from key-of m t/source]
188
+ case [
189
+ t/target [append way reduce ['to key-of m t/target]]
190
+ t/select [append way reduce ['select copy/deep t/select]]
191
+ true [append way 'choose]
192
+ ]
193
+ append way reduce ['on either t/events [copy t/events] [none]]
194
+ append way reduce ['where either t/guard [copy/deep t/guard] [none]]
195
+ append way reduce ['set either t/set [copy/deep t/set] [none]]
196
+ if t/choices [
197
+ branches: copy []
198
+ foreach c t/choices [
199
+ append/only branches reduce [
200
+ either t/select ['value] ['when] copy/deep first c
201
+ 'to key-of m second c
202
+ 'set either third c [copy/deep third c] [none]
203
+ ]
204
+ ]
205
+ if t/other [
206
+ append/only branches reduce ['else none 'to key-of m t/other 'set either t/other-set [copy/deep t/other-set] [none]]
207
+ ]
208
+ append way reduce ['branches branches]
209
+ ]
210
+ append/only ways way
211
+ ]
212
+ ]
213
+ reduce [
214
+ 'state m/state
215
+ 'data collect [foreach v any [m/data-spec []] [if set-word? :v [keep to word! v]]]
216
+ 'ttl m/ttl
217
+ 'shared m/shared
218
+ 'done m/done
219
+ 'states states
220
+ 'ways ways
221
+ ]
222
+ ]
223
+
224
+ ; The machine of an invoke, resumed from its own snapshot.
225
+ resumed: function [n saved] [
226
+ spec: n/invoke
227
+ unless block? spec [spec: get spec]
228
+ c: make-machine spec
229
+ fresh c
230
+ resume c saved
231
+ ]
232
+
233
+ ; After each event, the on-save of the outermost machine, with its snapshot.
234
+ saved: function [m] [
235
+ top: m
236
+ while [top/caller] [top: first top/caller]
237
+ if any [top/busy not top/owner] [exit]
238
+ if top/on-save [
239
+ top/saver/snapshot: snapshot top
240
+ do top/on-save
241
+ ]
242
+ if top/channel [tell top false]
243
+ ]
244
+
245
+ `;