trebol 0.1.1 → 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 (300) hide show
  1. package/README.md +12 -1
  2. package/dist/cli.js +17097 -12238
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/boot.js +12 -8
  12. package/dist/lib/core/eval.js +2 -2
  13. package/dist/lib/core/func.d.ts +4 -0
  14. package/dist/lib/core/machine/checks.d.ts +1 -0
  15. package/dist/lib/core/machine/checks.js +250 -0
  16. package/dist/lib/core/machine/events.d.ts +1 -0
  17. package/dist/lib/core/machine/events.js +365 -0
  18. package/dist/lib/core/machine/running.d.ts +1 -0
  19. package/dist/lib/core/machine/running.js +180 -0
  20. package/dist/lib/core/machine/shared.d.ts +1 -0
  21. package/dist/lib/core/machine/shared.js +123 -0
  22. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  23. package/dist/lib/core/machine/snapshot.js +244 -0
  24. package/dist/lib/core/machine/spec.d.ts +1 -0
  25. package/dist/lib/core/machine/spec.js +274 -0
  26. package/dist/lib/core/machine/words.d.ts +1 -0
  27. package/dist/lib/core/machine/words.js +98 -0
  28. package/dist/lib/core/machine.d.ts +1 -0
  29. package/dist/lib/core/machine.js +17 -0
  30. package/dist/lib/core/mezz.js +31 -0
  31. package/dist/lib/core/natives/control.js +2 -2
  32. package/dist/lib/core/natives/data.js +6 -1
  33. package/dist/lib/core/natives/series.js +59 -9
  34. package/dist/lib/core/natives/text.js +3 -1
  35. package/dist/lib/host/browser/calendar.d.ts +34 -0
  36. package/dist/lib/host/browser/calendar.js +268 -0
  37. package/dist/lib/host/browser/count.d.ts +15 -0
  38. package/dist/lib/host/browser/count.js +48 -0
  39. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  40. package/dist/lib/host/browser/dialogs.js +443 -0
  41. package/dist/lib/host/browser/drag.d.ts +70 -0
  42. package/dist/lib/host/browser/drag.js +268 -0
  43. package/dist/lib/host/browser/drawings.d.ts +30 -0
  44. package/dist/lib/host/browser/drawings.js +176 -0
  45. package/dist/lib/host/browser/events.d.ts +16 -0
  46. package/dist/lib/host/browser/events.js +125 -0
  47. package/dist/lib/host/browser/faces.d.ts +35 -0
  48. package/dist/lib/host/browser/faces.js +131 -0
  49. package/dist/lib/host/browser/fields.d.ts +16 -0
  50. package/dist/lib/host/browser/fields.js +322 -0
  51. package/dist/lib/host/browser/keys.d.ts +29 -0
  52. package/dist/lib/host/browser/keys.js +151 -0
  53. package/dist/lib/host/browser/lookup.d.ts +25 -0
  54. package/dist/lib/host/browser/lookup.js +275 -0
  55. package/dist/lib/host/browser/menus.d.ts +20 -0
  56. package/dist/lib/host/browser/menus.js +149 -0
  57. package/dist/lib/host/browser/mount.d.ts +92 -0
  58. package/dist/lib/host/browser/mount.js +448 -0
  59. package/dist/lib/host/browser/orbits.d.ts +90 -0
  60. package/dist/lib/host/browser/orbits.js +379 -0
  61. package/dist/lib/host/browser/page.d.ts +13 -0
  62. package/dist/lib/host/browser/page.js +35 -0
  63. package/dist/lib/host/browser/richtext.d.ts +19 -0
  64. package/dist/lib/host/browser/richtext.js +379 -0
  65. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  66. package/dist/lib/host/browser/row-faces.js +74 -0
  67. package/dist/lib/host/browser/screens.d.ts +37 -0
  68. package/dist/lib/host/browser/screens.js +140 -0
  69. package/dist/lib/host/browser/swipe.d.ts +31 -0
  70. package/dist/lib/host/browser/swipe.js +178 -0
  71. package/dist/lib/host/browser/sync.d.ts +24 -0
  72. package/dist/lib/host/browser/sync.js +450 -0
  73. package/dist/lib/host/browser/tables.d.ts +65 -0
  74. package/dist/lib/host/browser/tables.js +505 -0
  75. package/dist/lib/host/browser/times.d.ts +14 -0
  76. package/dist/lib/host/browser/times.js +114 -0
  77. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  78. package/dist/lib/host/browser/toolbars.js +129 -0
  79. package/dist/lib/host/browser/trees.d.ts +20 -0
  80. package/dist/lib/host/browser/trees.js +235 -0
  81. package/dist/lib/host/browser/uploads.d.ts +29 -0
  82. package/dist/lib/host/browser/uploads.js +370 -0
  83. package/dist/lib/host/browser/widget.d.ts +13 -0
  84. package/dist/lib/host/browser/widget.js +105 -0
  85. package/dist/lib/host/browser.d.ts +25 -0
  86. package/dist/lib/host/browser.js +129 -3964
  87. package/dist/lib/host/devlog.d.ts +11 -0
  88. package/dist/lib/host/devlog.js +13 -0
  89. package/dist/lib/host/devtools/console.d.ts +9 -0
  90. package/dist/lib/host/devtools/console.js +104 -0
  91. package/dist/lib/host/devtools/faces.d.ts +25 -0
  92. package/dist/lib/host/devtools/faces.js +169 -0
  93. package/dist/lib/host/devtools/log.d.ts +3 -0
  94. package/dist/lib/host/devtools/log.js +32 -0
  95. package/dist/lib/host/devtools/machines.d.ts +34 -0
  96. package/dist/lib/host/devtools/machines.js +212 -0
  97. package/dist/lib/host/devtools/read.d.ts +13 -0
  98. package/dist/lib/host/devtools/read.js +52 -0
  99. package/dist/lib/host/devtools/state.d.ts +90 -0
  100. package/dist/lib/host/devtools/state.js +123 -0
  101. package/dist/lib/host/devtools.d.ts +19 -0
  102. package/dist/lib/host/devtools.js +238 -0
  103. package/dist/lib/host/fonts/OFL.txt +93 -0
  104. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  105. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  106. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  107. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  108. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  109. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  110. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  111. package/dist/lib/host/fonts.d.ts +11 -0
  112. package/dist/lib/host/fonts.js +30 -0
  113. package/dist/lib/host/icons.js +4 -0
  114. package/dist/lib/host/index.d.ts +5 -1
  115. package/dist/lib/host/index.js +110 -5
  116. package/dist/lib/host/js.d.ts +5 -0
  117. package/dist/lib/host/js.js +130 -0
  118. package/dist/lib/host/locale.d.ts +18 -0
  119. package/dist/lib/host/locale.js +75 -5
  120. package/dist/lib/host/markdown.d.ts +6 -0
  121. package/dist/lib/host/markdown.js +18 -0
  122. package/dist/lib/host/modules.d.ts +4 -0
  123. package/dist/lib/host/modules.js +157 -20
  124. package/dist/lib/host/motion.d.ts +32 -0
  125. package/dist/lib/host/motion.js +190 -0
  126. package/dist/lib/host/net.js +14 -3
  127. package/dist/lib/host/orbit.d.ts +121 -0
  128. package/dist/lib/host/orbit.js +374 -0
  129. package/dist/lib/host/palette.d.ts +17 -0
  130. package/dist/lib/host/palette.js +131 -0
  131. package/dist/lib/host/rows.d.ts +14 -0
  132. package/dist/lib/host/rows.js +29 -0
  133. package/dist/lib/host/scheduler.d.ts +8 -0
  134. package/dist/lib/host/scheduler.js +64 -36
  135. package/dist/lib/host/share.d.ts +31 -0
  136. package/dist/lib/host/share.js +134 -0
  137. package/dist/lib/host/theme/classic.d.ts +1 -0
  138. package/dist/lib/host/theme/classic.js +120 -0
  139. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  140. package/dist/lib/host/theme/cupertino.js +65 -0
  141. package/dist/lib/host/theme/data.d.ts +1 -0
  142. package/dist/lib/host/theme/data.js +109 -0
  143. package/dist/lib/host/theme/devtools.d.ts +1 -0
  144. package/dist/lib/host/theme/devtools.js +166 -0
  145. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  146. package/dist/lib/host/theme/dialogs.js +63 -0
  147. package/dist/lib/host/theme/fluent.d.ts +1 -0
  148. package/dist/lib/host/theme/fluent.js +64 -0
  149. package/dist/lib/host/theme/forms.d.ts +1 -0
  150. package/dist/lib/host/theme/forms.js +239 -0
  151. package/dist/lib/host/theme/fusion.d.ts +1 -0
  152. package/dist/lib/host/theme/fusion.js +66 -0
  153. package/dist/lib/host/theme/layout.d.ts +1 -0
  154. package/dist/lib/host/theme/layout.js +166 -0
  155. package/dist/lib/host/theme/media.d.ts +1 -0
  156. package/dist/lib/host/theme/media.js +106 -0
  157. package/dist/lib/host/theme/navigation.d.ts +1 -0
  158. package/dist/lib/host/theme/navigation.js +36 -0
  159. package/dist/lib/host/theme/panes.d.ts +1 -0
  160. package/dist/lib/host/theme/panes.js +99 -0
  161. package/dist/lib/host/theme/popups.d.ts +1 -0
  162. package/dist/lib/host/theme/popups.js +149 -0
  163. package/dist/lib/host/theme/responsive.d.ts +1 -0
  164. package/dist/lib/host/theme/responsive.js +103 -0
  165. package/dist/lib/host/theme/toasts.d.ts +1 -0
  166. package/dist/lib/host/theme/toasts.js +62 -0
  167. package/dist/lib/host/theme.js +19 -1179
  168. package/dist/lib/ui/dialect/events.trb +276 -0
  169. package/dist/lib/ui/dialect/faces.trb +135 -0
  170. package/dist/lib/ui/dialect/layout.trb +283 -0
  171. package/dist/lib/ui/dialect/palette.trb +87 -0
  172. package/dist/lib/ui/dialect/problems.trb +174 -0
  173. package/dist/lib/ui/dialect/reactions.trb +198 -0
  174. package/dist/lib/ui/dialect/rows.trb +208 -0
  175. package/dist/lib/ui/dialect/screens.trb +293 -0
  176. package/dist/lib/ui/dialect/steps.trb +203 -0
  177. package/dist/lib/ui/dialect/validation.trb +137 -0
  178. package/dist/lib/ui/dialect/view.trb +110 -0
  179. package/dist/lib/ui/dialect/walking.trb +98 -0
  180. package/dist/lib/ui/dialect/words.trb +70 -0
  181. package/dist/lib/ui/i18n.trb +23 -0
  182. package/dist/lib/ui/test.trb +12 -0
  183. package/dist/lib/ui/testing/actions.trb +421 -0
  184. package/dist/lib/ui/testing/faces.trb +148 -0
  185. package/dist/lib/ui/testing/server.trb +73 -0
  186. package/dist/lib/ui/ui.trb +25 -1234
  187. package/dist/trebol.js +4882 -1088
  188. package/editors/vscode/README.md +15 -0
  189. package/editors/vscode/language-configuration.json +28 -0
  190. package/editors/vscode/package.json +42 -0
  191. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  192. package/package.json +7 -3
  193. package/src/core/boot.ts +10 -7
  194. package/src/core/eval.ts +2 -2
  195. package/src/core/func.ts +4 -0
  196. package/src/core/machine/checks.ts +251 -0
  197. package/src/core/machine/events.ts +366 -0
  198. package/src/core/machine/running.ts +181 -0
  199. package/src/core/machine/shared.ts +124 -0
  200. package/src/core/machine/snapshot.ts +245 -0
  201. package/src/core/machine/spec.ts +275 -0
  202. package/src/core/machine/words.ts +99 -0
  203. package/src/core/machine.ts +20 -0
  204. package/src/core/mezz.ts +31 -0
  205. package/src/core/natives/control.ts +2 -2
  206. package/src/core/natives/data.ts +6 -1
  207. package/src/core/natives/parse.ts +1 -1
  208. package/src/core/natives/series.ts +55 -11
  209. package/src/core/natives/text.ts +2 -1
  210. package/src/host/browser/calendar.ts +264 -0
  211. package/src/host/browser/count.ts +48 -0
  212. package/src/host/browser/dialogs.ts +439 -0
  213. package/src/host/browser/drag.ts +278 -0
  214. package/src/host/browser/drawings.ts +178 -0
  215. package/src/host/browser/events.ts +117 -0
  216. package/src/host/browser/faces.ts +143 -0
  217. package/src/host/browser/fields.ts +315 -0
  218. package/src/host/browser/keys.ts +145 -0
  219. package/src/host/browser/lookup.ts +262 -0
  220. package/src/host/browser/menus.ts +146 -0
  221. package/src/host/browser/mount.ts +420 -0
  222. package/src/host/browser/orbits.ts +418 -0
  223. package/src/host/browser/page.ts +43 -0
  224. package/src/host/browser/richtext.ts +346 -0
  225. package/src/host/browser/row-faces.ts +75 -0
  226. package/src/host/browser/screens.ts +142 -0
  227. package/src/host/browser/swipe.ts +168 -0
  228. package/src/host/browser/sync.ts +354 -0
  229. package/src/host/browser/tables.ts +461 -0
  230. package/src/host/browser/times.ts +115 -0
  231. package/src/host/browser/toolbars.ts +126 -0
  232. package/src/host/browser/trees.ts +205 -0
  233. package/src/host/browser/uploads.ts +367 -0
  234. package/src/host/browser/widget.ts +103 -0
  235. package/src/host/browser.ts +128 -3747
  236. package/src/host/devlog.ts +16 -0
  237. package/src/host/devtools/console.ts +102 -0
  238. package/src/host/devtools/faces.ts +160 -0
  239. package/src/host/devtools/log.ts +34 -0
  240. package/src/host/devtools/machines.ts +208 -0
  241. package/src/host/devtools/read.ts +58 -0
  242. package/src/host/devtools/state.ts +148 -0
  243. package/src/host/devtools.ts +238 -0
  244. package/src/host/fonts/OFL.txt +93 -0
  245. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  246. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  247. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  248. package/src/host/fonts/inter-greek.woff2 +0 -0
  249. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  250. package/src/host/fonts/inter-latin.woff2 +0 -0
  251. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  252. package/src/host/fonts.ts +40 -0
  253. package/src/host/icons.ts +4 -0
  254. package/src/host/index.ts +110 -5
  255. package/src/host/js.ts +127 -0
  256. package/src/host/locale.ts +71 -5
  257. package/src/host/markdown.ts +18 -0
  258. package/src/host/modules.ts +150 -22
  259. package/src/host/motion.ts +204 -0
  260. package/src/host/net.ts +13 -3
  261. package/src/host/orbit.ts +432 -0
  262. package/src/host/palette.ts +139 -0
  263. package/src/host/rows.ts +30 -0
  264. package/src/host/scheduler.ts +67 -36
  265. package/src/host/share.ts +168 -0
  266. package/src/host/theme/classic.ts +121 -0
  267. package/src/host/theme/cupertino.ts +66 -0
  268. package/src/host/theme/data.ts +110 -0
  269. package/src/host/theme/devtools.ts +167 -0
  270. package/src/host/theme/dialogs.ts +64 -0
  271. package/src/host/theme/fluent.ts +65 -0
  272. package/src/host/theme/forms.ts +240 -0
  273. package/src/host/theme/fusion.ts +67 -0
  274. package/src/host/theme/layout.ts +167 -0
  275. package/src/host/theme/media.ts +107 -0
  276. package/src/host/theme/navigation.ts +37 -0
  277. package/src/host/theme/panes.ts +100 -0
  278. package/src/host/theme/popups.ts +150 -0
  279. package/src/host/theme/responsive.ts +104 -0
  280. package/src/host/theme/toasts.ts +63 -0
  281. package/src/host/theme.ts +19 -1179
  282. package/src/ui/dialect/events.trb +276 -0
  283. package/src/ui/dialect/faces.trb +135 -0
  284. package/src/ui/dialect/layout.trb +283 -0
  285. package/src/ui/dialect/palette.trb +87 -0
  286. package/src/ui/dialect/problems.trb +174 -0
  287. package/src/ui/dialect/reactions.trb +198 -0
  288. package/src/ui/dialect/rows.trb +208 -0
  289. package/src/ui/dialect/screens.trb +293 -0
  290. package/src/ui/dialect/steps.trb +203 -0
  291. package/src/ui/dialect/validation.trb +137 -0
  292. package/src/ui/dialect/view.trb +110 -0
  293. package/src/ui/dialect/walking.trb +98 -0
  294. package/src/ui/dialect/words.trb +70 -0
  295. package/src/ui/i18n.trb +23 -0
  296. package/src/ui/test.trb +12 -0
  297. package/src/ui/testing/actions.trb +421 -0
  298. package/src/ui/testing/faces.trb +148 -0
  299. package/src/ui/testing/server.trb +73 -0
  300. package/src/ui/ui.trb +25 -1234
@@ -0,0 +1,365 @@
1
+ // Machines in Trebol (see machine.ts). The engine, part four: run and invoke, events and how a transition moves
2
+ // the machine, one step at a time.
3
+ export const EVENTS = String.raw `
4
+ ; --- run and invoke: work of a state (key -id), its end sent back as done or error ---
5
+
6
+ work: function [m id] [
7
+ n: node m id
8
+ case [
9
+ n/run [
10
+ s: serial m
11
+ put m/timers negate id reduce [s spawn compose/only [machine-engine/worked (m) (id) (s) (n/run)]]
12
+ ]
13
+ n/invoke [
14
+ spec: n/invoke
15
+ unless block? spec [spec: get spec]
16
+ unless block? :spec [
17
+ do make error! rejoin ["machine: invoke in " n/name " takes the block of a machine: " mold :spec]
18
+ ]
19
+ s: serial m
20
+ either n/for [
21
+ items: get in m/data second n/in
22
+ unless block? :items [
23
+ do make error! rejoin ["machine: invoke in " n/name " goes over a block: data/" second n/in " is " mold :items]
24
+ ]
25
+ ; [serial children outcomes data left]: the outcomes and data in the order of the items
26
+ x: reduce [s copy [] array length? items array length? items length? items]
27
+ put m/timers negate id x
28
+ either empty? items [
29
+ post-event m 'done make object! [outcomes: copy [] data: copy []] negate id s
30
+ ] [
31
+ repeat i length? items [
32
+ ; the word of for, seen only by with
33
+ it: make object! reduce [to set-word! n/for none]
34
+ set in it n/for pick items i
35
+ c: invoked m n spec all [n/with bind copy/deep n/with it]
36
+ c/caller: reduce [m id s i]
37
+ append second x c
38
+ ]
39
+ foreach c second x [start c]
40
+ ]
41
+ ] [
42
+ c: invoked m n spec n/with
43
+ c/caller: reduce [m id s]
44
+ put m/timers negate id reduce [s c]
45
+ start c
46
+ ]
47
+ ]
48
+ ]
49
+ ]
50
+
51
+ ; A machine made for invoke, its data seeded by with.
52
+ invoked: function [m n spec with] [
53
+ c: make-machine spec
54
+ if c/shared [do make error! rejoin ["machine: the machine invoked in " n/name " cannot be shared"]]
55
+ fresh c
56
+ if with [
57
+ foreach [f v] values m reduce [with] [
58
+ unless in c/data f [do make error! rejoin ["machine: the machine invoked in " n/name " has no data " f suggest f words-of c/data]]
59
+ set in c/data f :v
60
+ ]
61
+ ]
62
+ c
63
+ ]
64
+
65
+ ; The task of a run: the value it gives comes back as done, a failure as error.
66
+ worked: function [m id s b] [
67
+ r: try b
68
+ x: select m/timers negate id
69
+ if all [x s = first x] [poke x 2 none]
70
+ either error? :r [
71
+ post-event m 'error r negate id s
72
+ ] [
73
+ post-event m 'done make object! [value: :r] negate id s
74
+ ]
75
+ ]
76
+
77
+ ; A machine invoked that is done tells the one that invoked it.
78
+ returned: function [c] [
79
+ if all [c/done x: c/caller] [
80
+ c/caller: none
81
+ ev: make object! [outcome: c/outcome data: c/data]
82
+ if fourth x [ev: make ev [item: fourth x]]
83
+ post-event first x 'done ev negate second x third x
84
+ ]
85
+ ]
86
+
87
+ serial: function [m] [m/serial: m/serial + 1]
88
+
89
+ arm-ttl: function [m] [
90
+ if m/ttl [
91
+ disarm m 0
92
+ clock m 0 m/ttl 'expired
93
+ ]
94
+ ]
95
+
96
+ stop: function [m] [
97
+ foreach [key x] m/timers [if x [drop second x]]
98
+ m/timers: make map! []
99
+ clear m/deferred
100
+ ]
101
+
102
+ post: function [m name key serial] [post-event m name make object! [] key serial]
103
+
104
+ post-event: function [m name ev key serial] [
105
+ append/only m/queue reduce [name ev key serial]
106
+ unless m/busy [run m]
107
+ ]
108
+
109
+ ; A timer that went off: its timeout is taken from its own state, or from one around it.
110
+ ring: function [m name key serial] [
111
+ x: select m/timers key
112
+ unless all [x serial = first x] [exit]
113
+ ; a machine of a fan-out that ends: the state is done when all of them are
114
+ if all [name = 'done block? second x in e: m/scope/event 'item] [
115
+ poke third x e/item e/outcome
116
+ poke fourth x e/item e/data
117
+ poke x 5 (pick x 5) - 1
118
+ if 0 < pick x 5 [exit]
119
+ m/scope/event: make object! [outcomes: third x data: fourth x]
120
+ ]
121
+ put m/timers key none
122
+ case [
123
+ name = 'timeout [
124
+ if t: find-from m key 'timeout true none [go m t]
125
+ ]
126
+ name = 'expired [
127
+ unless deliver m 'expired [
128
+ m/outcome: 'expired
129
+ m/done: true
130
+ stop m
131
+ exit
132
+ ]
133
+ ]
134
+ name = 'done [
135
+ if t: find-from m negate key 'done true none [go m t]
136
+ ]
137
+ name = 'error [
138
+ ; routed as a failure of the state that ran it
139
+ m/active: negate key
140
+ do m/scope/event
141
+ ]
142
+ ]
143
+ drain m
144
+ ]
145
+
146
+ ; An event goes to every innermost state: each takes its own way out, looking no further
147
+ ; out than its parallel; when none does, the states around the parallels may.
148
+ deliver: function [m name] [
149
+ taken: false
150
+ foreach id leaves m [
151
+ if on? m id [
152
+ if t: find-from m id name true region-of m id [go m t taken: true]
153
+ ]
154
+ ]
155
+ if taken [return true]
156
+ foreach id leaves m [
157
+ if t: find-from m id name true none [go m t return true]
158
+ ]
159
+ false
160
+ ]
161
+
162
+ ; The first event held by defer that the machine can take now.
163
+ redeliver: function [m] [
164
+ pos: m/deferred
165
+ while [not tail? pos] [
166
+ e: first pos
167
+ m/scope/event: second e
168
+ if deliver m first e [
169
+ remove pos
170
+ return true
171
+ ]
172
+ pos: next pos
173
+ ]
174
+ false
175
+ ]
176
+
177
+ deferring?: function [m name] [
178
+ foreach id head insert copy m/on 1 [if find select node m id 'defer name [return true]]
179
+ false
180
+ ]
181
+
182
+ remember: function [m id] [
183
+ if p: select node m id 'parent [put m/history p id]
184
+ ]
185
+
186
+ ; Enters the states of path, the first one down to the last, and then what is inside the
187
+ ; last: its history or initial state, or every state of a parallel.
188
+ enter-down: function [m path] [
189
+ id: first path
190
+ rest: next path
191
+ enter m id
192
+ remember m id
193
+ n: node m id
194
+ case [
195
+ n/parallel [
196
+ foreach c n/children [
197
+ put m/results c none
198
+ enter-down m either all [not tail? rest c = first rest] [rest] [reduce [c]]
199
+ ]
200
+ ]
201
+ not tail? rest [enter-down m rest]
202
+ not empty? n/children [
203
+ unless n/history [put m/history id none]
204
+ enter-down m reduce [any [select m/history id n/initial]]
205
+ ]
206
+ ]
207
+ ]
208
+
209
+ ; Leaves every state on inside state id, innermost first.
210
+ exit-below: function [m id] [
211
+ foreach c reverse copy select node m id 'children [
212
+ if on? m c [
213
+ exit-below m c
214
+ leave m c
215
+ remember m c
216
+ ]
217
+ ]
218
+ ]
219
+
220
+ ; Every right side of a set, worked out before any state is left.
221
+ values: function [m sets] [
222
+ out: copy []
223
+ foreach b sets [
224
+ pos: b
225
+ while [not tail? pos] [
226
+ unless set-word? first pos [fail "set takes field: value pairs" pos]
227
+ f: to word! first pos
228
+ v: do/next next pos 'pos
229
+ append out reduce [f :v]
230
+ ]
231
+ ]
232
+ out
233
+ ]
234
+
235
+ ; The states left and entered are those below the one the source and the target share;
236
+ ; a parallel is never that one, so going between its states leaves it and enters it again.
237
+ go: function [m t] [
238
+ r: m/taken
239
+ dest: first r
240
+ sets: copy []
241
+ if t/set [append/only sets t/set]
242
+ if second r [append/only sets second r]
243
+ vals: values m sets
244
+ pivot: lca m t/source dest
245
+ while [select node m pivot 'parallel] [pivot: select node m pivot 'parent]
246
+ exit-below m pivot
247
+ m/active: pivot
248
+ foreach [f v] vals [set in m/data f :v]
249
+ path: next chain m pivot dest
250
+ unless empty? path [enter-down m path]
251
+ ]
252
+
253
+ ; A state with nowhere to go ends: a leaf gives its outcome, a state of a parallel how it ended.
254
+ sink: function [m id] [
255
+ n: node m id
256
+ if empty? n/children [
257
+ m/last: any [n/outcome n/name]
258
+ if all [n/outcome none? m/outcome none? region-of m id] [m/outcome: n/outcome]
259
+ ]
260
+ leave m id
261
+ case [
262
+ n/parent = 1 [
263
+ m/on: reduce [id]
264
+ m/done: true
265
+ stop m
266
+ ]
267
+ select node m n/parent 'parallel [put m/results id m/last]
268
+ true [put m/history n/parent none]
269
+ ]
270
+ ]
271
+
272
+ ; One move with no event, if there is one: a way out with no event, or a state that ends.
273
+ settle: function [m] [
274
+ foreach id leaves m [
275
+ if on? m id [
276
+ if t: find-from m id none false none [go m t return true]
277
+ unless find-from m id true false none [sink m id return true]
278
+ ]
279
+ ]
280
+ foreach id copy m/on [
281
+ if all [select node m id 'parallel t: find-from m id none false none] [go m t return true]
282
+ ]
283
+ false
284
+ ]
285
+
286
+ drain: function [m] [
287
+ while [not m/done] [
288
+ unless any [settle m redeliver m] [exit]
289
+ ]
290
+ ]
291
+
292
+ begin: function [m] [
293
+ arm-ttl m
294
+ enter-down m reduce [select node m 1 'initial]
295
+ drain m
296
+ ]
297
+
298
+ ; An event from send: one that no transition takes waits if a defer holds it.
299
+ event: function [m name] [
300
+ unless deliver m name [
301
+ either deferring? m name [
302
+ append/only m/deferred reduce [name m/scope/event]
303
+ ] [
304
+ foreach id leaves m [hook m id 'activity]
305
+ ]
306
+ ]
307
+ drain m
308
+ unless m/done [arm-ttl m]
309
+ ]
310
+
311
+ ; One step, the start, an event or a timer; a failure on the way in routes to on error.
312
+ step: function [m e] [
313
+ name: first e
314
+ m/scope/event: second e
315
+ r: try [
316
+ case [
317
+ none? name [begin m]
318
+ third e [ring m name third e fourth e]
319
+ true [event m name]
320
+ ]
321
+ none
322
+ ]
323
+ if error? :r [
324
+ if m/exiting [m/exiting: false do r]
325
+ m/error: r
326
+ m/scope/event: r
327
+ unless t: find-from m m/active 'error true none [do r]
328
+ go m t
329
+ drain m
330
+ ]
331
+ ]
332
+
333
+ ; A word or a path for one state on; a block of paths when there are several.
334
+ where: function [m] [
335
+ out: collect [
336
+ foreach leaf leaves m [
337
+ names: collect [foreach id next chain m 1 leaf [keep select node m id 'name]]
338
+ keep either 1 = length? names [first names] [to path! names]
339
+ ]
340
+ ]
341
+ case [
342
+ empty? out [none]
343
+ 1 = length? out [first out]
344
+ true [out]
345
+ ]
346
+ ]
347
+
348
+ run: function [m] [
349
+ m/busy: true
350
+ r: try [
351
+ while [not empty? m/queue] [
352
+ e: take m/queue
353
+ unless m/done [step m e]
354
+ ]
355
+ none
356
+ ]
357
+ m/busy: false
358
+ m/state: where m
359
+ if error? :r [clear m/queue do r]
360
+ saved m
361
+ returned m
362
+ m
363
+ ]
364
+
365
+ `;
@@ -0,0 +1 @@
1
+ export declare const RUNNING: string;
@@ -0,0 +1,180 @@
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
+ export const RUNNING = String.raw `
4
+ ; --- running ----------------------------------------------------------------
5
+
6
+ chain: function [m top leaf] [
7
+ out: copy []
8
+ id: leaf
9
+ while [true] [
10
+ insert out id
11
+ if id = top [return out]
12
+ id: select node m id 'parent
13
+ ]
14
+ ]
15
+
16
+ lca: function [m a b] [
17
+ up: chain m 1 a
18
+ while [not find up b] [b: select node m b 'parent]
19
+ b
20
+ ]
21
+
22
+ holds?: function [m guard] [
23
+ if none? guard [return true]
24
+ r: try guard
25
+ either error? :r [false] [either :r [true] [false]]
26
+ ]
27
+
28
+ ; The target and the sets of a transition, as they are now; none when a choose has nowhere to go.
29
+ ; The block of a select runs once, here: a value no branch takes is a mistake.
30
+ route: function [m t] [
31
+ unless t/choices [return reduce [t/target t/set]]
32
+ if t/select [
33
+ v: do t/select
34
+ foreach c t/choices [if :v = first first c [return reduce [second c third c]]]
35
+ if t/other [return reduce [t/other t/other-set]]
36
+ fail rejoin ["select gave " mold :v ", which no branch takes"] t/place
37
+ ]
38
+ foreach c t/choices [if holds? m first c [return reduce [second c third c]]]
39
+ if t/other [return reduce [t/other t/other-set]]
40
+ none
41
+ ]
42
+
43
+ ; want: none = a transition with no event, true = any, a word = that event.
44
+ ; The route of the one that fits is kept in m/taken, for go. A way out of a parallel
45
+ ; with no event sees in event how each of its states ended: a to waits for all of them.
46
+ fits?: function [m t want] [
47
+ case [
48
+ want = true [true]
49
+ none? want [
50
+ if t/events [return false]
51
+ if select node m t/source 'parallel [
52
+ if all [none? t/choices inside? m t/source] [return false]
53
+ m/scope/event: results m t/source
54
+ ]
55
+ to logic! m/taken: route m t
56
+ ]
57
+ true [all [t/events find t/events want holds? m t/guard m/taken: route m t]]
58
+ ]
59
+ ]
60
+
61
+ ; The transition from state id that fits: its own first, then, when outer, those of the
62
+ ; states around it, up to the state stop (none: up to the top).
63
+ find-from: function [m id want outer stop] [
64
+ while [all [id id <> 1 id <> stop]] [
65
+ n: node m id
66
+ foreach t select node m n/parent 'transitions [
67
+ if all [t/source = id fits? m t want] [return t]
68
+ ]
69
+ unless outer [return none]
70
+ id: n/parent
71
+ ]
72
+ none
73
+ ]
74
+
75
+ ; --- the states the machine is in: m/on holds each, from the top down to the leaves ---
76
+
77
+ on?: function [m id] [to logic! find m/on id]
78
+
79
+ ; Whether a state has one of its own states on.
80
+ inside?: function [m id] [
81
+ foreach c select node m id 'children [if on? m c [return true]]
82
+ false
83
+ ]
84
+
85
+ ; The innermost states on, in the order the block gives them.
86
+ leaves: function [m] [
87
+ out: copy []
88
+ foreach id m/on [unless inside? m id [append out id]]
89
+ sort out
90
+ ]
91
+
92
+ ; The nearest parallel around a state, or none.
93
+ region-of: function [m id] [
94
+ while [id: select node m id 'parent] [if select node m id 'parallel [return id]]
95
+ none
96
+ ]
97
+
98
+ ; How each state of a parallel ended: its outcome, or the name of its final state; none while on.
99
+ results: function [m id] [
100
+ make object! collect [
101
+ foreach c select node m id 'children [
102
+ keep to set-word! select node m c 'name
103
+ v: either on? m c [none] [select m/results c]
104
+ keep either v [to lit-word! v] ['none]
105
+ ]
106
+ ]
107
+ ]
108
+
109
+ hook: function [m id which] [
110
+ n: node m id
111
+ if b: select n which [
112
+ either which = 'exit [
113
+ m/exiting: true
114
+ do b
115
+ m/exiting: false
116
+ ] [do b]
117
+ ]
118
+ ]
119
+
120
+ enter: function [m id] [
121
+ m/active: id
122
+ append m/on id
123
+ arm m id
124
+ work m id
125
+ hook m id 'enter
126
+ ]
127
+
128
+ leave: function [m id] [
129
+ m/active: id
130
+ disarm m id
131
+ disarm m negate id
132
+ hook m id 'exit
133
+ remove find m/on id
134
+ ]
135
+
136
+ ; --- time: a timer per state with a timeout, and one (key 0) for the ttl ---
137
+
138
+ ; The timer sends the machine its own event; a serial tells a timer that was stopped.
139
+ clock: function [m key time name] [clock-at m key now/precise + to time! time name]
140
+
141
+ ; A timer that goes off at a moment, kept with it for snapshot; one already past goes off at once.
142
+ clock-at: function [m key at name] [
143
+ s: serial m
144
+ left: difference at now/precise
145
+ if left < 0:00 [left: 0:00]
146
+ put m/timers key reduce [s delay left compose [machine-engine/post (m) (to lit-word! name) (key) (s)] at]
147
+ ]
148
+
149
+ arm: function [m id] [
150
+ unless v: select node m id 'timeout [exit]
151
+ if path? v [
152
+ v: get in m/data second v
153
+ unless any [number? :v time? :v] [
154
+ do make error! rejoin ["machine: the timeout of " select node m id 'name " is not seconds or a time: " mold :v]
155
+ ]
156
+ ]
157
+ clock m id v 'timeout
158
+ ]
159
+
160
+ disarm: function [m key] [
161
+ if x: select m/timers key [
162
+ drop second x
163
+ put m/timers key none
164
+ ]
165
+ ]
166
+
167
+ ; Stops a timer, a task or a machine invoked; none is one that has already ended.
168
+ drop: function [x] [
169
+ case [
170
+ object? x [
171
+ stop x
172
+ x/caller: none
173
+ x/done: true
174
+ ]
175
+ block? x [foreach c x [drop c]]
176
+ x [cancel x]
177
+ ]
178
+ ]
179
+
180
+ `;
@@ -0,0 +1 @@
1
+ export declare const SHARED: string;
@@ -0,0 +1,123 @@
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
+ export const SHARED = String.raw `
4
+ ; --- shared: one machine for every tab of the app, run by the tab that owns it ---
5
+ ; The owner holds a lock named after the machine and tells the other tabs its
6
+ ; snapshot after each event; they keep a copy and send their events to it.
7
+
8
+ share-name: function [m] [rejoin ["trebol-machine-" m/shared]]
9
+
10
+ ; Makes m the owner, or a copy of the owner another tab has.
11
+ share-start: function [m saved] [
12
+ ; a host with no tabs has no share-open: the machine is only this one's
13
+ m/channel: all [value? 'share-open share-open share-name m compose [machine-engine/heard (m) message asked]]
14
+ either any [
15
+ none? m/channel
16
+ share-lock share-name m compose [machine-engine/took (m)]
17
+ ] [
18
+ either saved [resume m saved] [start m]
19
+ ] [
20
+ m/owner: false
21
+ share-ask m/channel mold/all [hello]
22
+ ]
23
+ m
24
+ ]
25
+
26
+ ; The snapshot to the other tabs, answering the asks done since the last one.
27
+ tell: function [m fresh] [
28
+ asks: m/asks
29
+ m/asks: copy []
30
+ share-post m/channel mold/all reduce ['state snapshot m] asks fresh
31
+ ]
32
+
33
+ ; What another tab says: to the owner an event, restart or hello; to a copy the owner's snapshot.
34
+ heard: function [m message asked] [
35
+ x: load message
36
+ unless m/owner [
37
+ if 'state = first x [
38
+ m/seen: second x
39
+ clean m
40
+ restore m m/seen
41
+ ]
42
+ exit
43
+ ]
44
+ if 'state = first x [exit]
45
+ if asked [append m/asks asked]
46
+ obey m x
47
+ unless m/busy [tell m false]
48
+ ]
49
+
50
+ ; An event or a restart that came from another tab.
51
+ obey: function [m x] [
52
+ switch first x [
53
+ event [
54
+ append/only m/queue reduce [second x object-of third x]
55
+ unless m/busy [run m]
56
+ ]
57
+ restart [again m]
58
+ rewind [rewind m second x]
59
+ ]
60
+ ]
61
+
62
+ ; The owner's tab has gone: this one goes on from the last snapshot it was told.
63
+ took: function [m] [
64
+ m/owner: true
65
+ clean m
66
+ either m/seen [resume m m/seen] [start m]
67
+ foreach [id message] share-asks m/channel [
68
+ append m/asks id
69
+ obey m load message
70
+ ]
71
+ tell m true
72
+ ]
73
+
74
+ ; restart, for the owner or a machine of one tab.
75
+ again: function [m] [
76
+ stop m
77
+ fresh m
78
+ m/history: make map! []
79
+ m/outcome: m/error: m/active: m/last: none
80
+ m/on: copy []
81
+ m/results: make map! []
82
+ m/done: false
83
+ clear m/queue
84
+ start m
85
+ ]
86
+
87
+ make-machine: function [spec [block!]] [
88
+ spec: copy/deep spec
89
+ m: make object! [
90
+ state: data: outcome: error: active: data-spec: ttl: taken: none
91
+ done: false
92
+ nodes: copy [] code: copy [] history: make map! [] queue: copy []
93
+ timers: make map! [] serial: 0 deferred: copy []
94
+ on: copy [] results: make map! [] last: caller: none
95
+ busy: false exiting: false on-save: saver: none
96
+ shared: channel: seen: none owner: true asks: copy []
97
+ scope: make object! [data: event: none]
98
+ ]
99
+ add-node m none none
100
+ level m node m 1 spec
101
+ ; targets once every state is known: one inside may lead to one further out
102
+ foreach n m/nodes [foreach t n/transitions [resolve m n t]]
103
+ if empty? select node m 1 'children [fail "a machine needs a state" spec]
104
+ check-timeouts m
105
+ check-data m
106
+ check-reach m
107
+ check-hang m
108
+ m
109
+ ]
110
+
111
+ ; The data as the machine starts with it.
112
+ fresh: function [m] [
113
+ m/data: make object! copy/deep any [m/data-spec []]
114
+ m/scope/data: m/data
115
+ ]
116
+
117
+ start: function [m] [
118
+ append/only m/queue reduce [none make object! []]
119
+ run m
120
+ ]
121
+ ]
122
+
123
+ `;
@@ -0,0 +1 @@
1
+ export declare const SNAPSHOT: string;