@wardic/dom 0.0.0-stage → 1.0.0-rc.10

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 (315) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +8 -2
  3. package/THIRD_PARTY_NOTICES.md +145 -0
  4. package/dist/Appear.d.ts +30 -0
  5. package/dist/Appear.d.ts.map +1 -0
  6. package/dist/Appear.js +31 -0
  7. package/dist/Appear.js.map +1 -0
  8. package/dist/Color.d.ts +14 -0
  9. package/dist/Color.d.ts.map +1 -0
  10. package/dist/Color.js +16 -0
  11. package/dist/Color.js.map +1 -0
  12. package/dist/DomAnnouncer.d.ts +13 -0
  13. package/dist/DomAnnouncer.d.ts.map +1 -0
  14. package/dist/DomAnnouncer.js +14 -0
  15. package/dist/DomAnnouncer.js.map +1 -0
  16. package/dist/DomBatch.d.ts +20 -0
  17. package/dist/DomBatch.d.ts.map +1 -0
  18. package/dist/DomBatch.js +23 -0
  19. package/dist/DomBatch.js.map +1 -0
  20. package/dist/DomClock.d.ts +11 -0
  21. package/dist/DomClock.d.ts.map +1 -0
  22. package/dist/DomClock.js +12 -0
  23. package/dist/DomClock.js.map +1 -0
  24. package/dist/DomDefaultTransition.d.ts +12 -0
  25. package/dist/DomDefaultTransition.d.ts.map +1 -0
  26. package/dist/DomDefaultTransition.js +13 -0
  27. package/dist/DomDefaultTransition.js.map +1 -0
  28. package/dist/DomInput.d.ts +14 -0
  29. package/dist/DomInput.d.ts.map +1 -0
  30. package/dist/DomInput.js +17 -0
  31. package/dist/DomInput.js.map +1 -0
  32. package/dist/DomInterpolators.d.ts +13 -0
  33. package/dist/DomInterpolators.d.ts.map +1 -0
  34. package/dist/DomInterpolators.js +14 -0
  35. package/dist/DomInterpolators.js.map +1 -0
  36. package/dist/DomKeyRules.d.ts +13 -0
  37. package/dist/DomKeyRules.d.ts.map +1 -0
  38. package/dist/DomKeyRules.js +14 -0
  39. package/dist/DomKeyRules.js.map +1 -0
  40. package/dist/DomPositionalKeys.d.ts +13 -0
  41. package/dist/DomPositionalKeys.d.ts.map +1 -0
  42. package/dist/DomPositionalKeys.js +14 -0
  43. package/dist/DomPositionalKeys.js.map +1 -0
  44. package/dist/DomProbe.d.ts +13 -0
  45. package/dist/DomProbe.d.ts.map +1 -0
  46. package/dist/DomProbe.js +14 -0
  47. package/dist/DomProbe.js.map +1 -0
  48. package/dist/DomProjectionSystem.d.ts +28 -0
  49. package/dist/DomProjectionSystem.d.ts.map +1 -0
  50. package/dist/DomProjectionSystem.js +41 -0
  51. package/dist/DomProjectionSystem.js.map +1 -0
  52. package/dist/DomRenderer.d.ts +13 -0
  53. package/dist/DomRenderer.d.ts.map +1 -0
  54. package/dist/DomRenderer.js +15 -0
  55. package/dist/DomRenderer.js.map +1 -0
  56. package/dist/DomScrollSource.d.ts +14 -0
  57. package/dist/DomScrollSource.d.ts.map +1 -0
  58. package/dist/DomScrollSource.js +15 -0
  59. package/dist/DomScrollSource.js.map +1 -0
  60. package/dist/FlushGate.d.ts +48 -0
  61. package/dist/FlushGate.d.ts.map +1 -0
  62. package/dist/FlushGate.js +62 -0
  63. package/dist/FlushGate.js.map +1 -0
  64. package/dist/KeyboardPress.d.ts +16 -0
  65. package/dist/KeyboardPress.d.ts.map +1 -0
  66. package/dist/KeyboardPress.js +17 -0
  67. package/dist/KeyboardPress.js.map +1 -0
  68. package/dist/MeasuredPair.d.ts +14 -0
  69. package/dist/MeasuredPair.d.ts.map +1 -0
  70. package/dist/MeasuredPair.js +16 -0
  71. package/dist/MeasuredPair.js.map +1 -0
  72. package/dist/OwnValues.d.ts +14 -0
  73. package/dist/OwnValues.d.ts.map +1 -0
  74. package/dist/OwnValues.js +15 -0
  75. package/dist/OwnValues.js.map +1 -0
  76. package/dist/ProjectionSystem.d.ts +34 -0
  77. package/dist/ProjectionSystem.d.ts.map +1 -0
  78. package/dist/ProjectionSystem.js +29 -0
  79. package/dist/ProjectionSystem.js.map +1 -0
  80. package/dist/ReorderController.d.ts +61 -0
  81. package/dist/ReorderController.d.ts.map +1 -0
  82. package/dist/ReorderController.js +78 -0
  83. package/dist/ReorderController.js.map +1 -0
  84. package/dist/Report.d.ts +12 -0
  85. package/dist/Report.d.ts.map +1 -0
  86. package/dist/Report.js +13 -0
  87. package/dist/Report.js.map +1 -0
  88. package/dist/ScrollOffset.d.ts +34 -0
  89. package/dist/ScrollOffset.d.ts.map +1 -0
  90. package/dist/ScrollOffset.js +42 -0
  91. package/dist/ScrollOffset.js.map +1 -0
  92. package/dist/Style.d.ts +15 -0
  93. package/dist/Style.d.ts.map +1 -0
  94. package/dist/Style.js +17 -0
  95. package/dist/Style.js.map +1 -0
  96. package/dist/Svg.d.ts +13 -0
  97. package/dist/Svg.d.ts.map +1 -0
  98. package/dist/Svg.js +14 -0
  99. package/dist/Svg.js.map +1 -0
  100. package/dist/Variables.d.ts +13 -0
  101. package/dist/Variables.d.ts.map +1 -0
  102. package/dist/Variables.js +14 -0
  103. package/dist/Variables.js.map +1 -0
  104. package/dist/index.d.ts +83 -0
  105. package/dist/index.d.ts.map +1 -0
  106. package/dist/index.js +71 -0
  107. package/dist/index.js.map +1 -0
  108. package/dist/internal/announcer.d.ts +50 -0
  109. package/dist/internal/announcer.d.ts.map +1 -0
  110. package/dist/internal/announcer.js +115 -0
  111. package/dist/internal/announcer.js.map +1 -0
  112. package/dist/internal/appear-slot.d.ts +23 -0
  113. package/dist/internal/appear-slot.d.ts.map +1 -0
  114. package/dist/internal/appear-slot.js +29 -0
  115. package/dist/internal/appear-slot.js.map +1 -0
  116. package/dist/internal/appear.d.ts +237 -0
  117. package/dist/internal/appear.d.ts.map +1 -0
  118. package/dist/internal/appear.js +740 -0
  119. package/dist/internal/appear.js.map +1 -0
  120. package/dist/internal/auto-scroll.d.ts +24 -0
  121. package/dist/internal/auto-scroll.d.ts.map +1 -0
  122. package/dist/internal/auto-scroll.js +124 -0
  123. package/dist/internal/auto-scroll.js.map +1 -0
  124. package/dist/internal/batch-writer.d.ts +16 -0
  125. package/dist/internal/batch-writer.d.ts.map +1 -0
  126. package/dist/internal/batch-writer.js +77 -0
  127. package/dist/internal/batch-writer.js.map +1 -0
  128. package/dist/internal/clock.d.ts +19 -0
  129. package/dist/internal/clock.d.ts.map +1 -0
  130. package/dist/internal/clock.js +39 -0
  131. package/dist/internal/clock.js.map +1 -0
  132. package/dist/internal/colors.d.ts +45 -0
  133. package/dist/internal/colors.d.ts.map +1 -0
  134. package/dist/internal/colors.js +193 -0
  135. package/dist/internal/colors.js.map +1 -0
  136. package/dist/internal/complex.d.ts +24 -0
  137. package/dist/internal/complex.d.ts.map +1 -0
  138. package/dist/internal/complex.js +28 -0
  139. package/dist/internal/complex.js.map +1 -0
  140. package/dist/internal/content-watch.d.ts +20 -0
  141. package/dist/internal/content-watch.d.ts.map +1 -0
  142. package/dist/internal/content-watch.js +76 -0
  143. package/dist/internal/content-watch.js.map +1 -0
  144. package/dist/internal/default-transition.d.ts +25 -0
  145. package/dist/internal/default-transition.d.ts.map +1 -0
  146. package/dist/internal/default-transition.js +41 -0
  147. package/dist/internal/default-transition.js.map +1 -0
  148. package/dist/internal/drag-defaults.d.ts +18 -0
  149. package/dist/internal/drag-defaults.d.ts.map +1 -0
  150. package/dist/internal/drag-defaults.js +38 -0
  151. package/dist/internal/drag-defaults.js.map +1 -0
  152. package/dist/internal/gate.d.ts +188 -0
  153. package/dist/internal/gate.d.ts.map +1 -0
  154. package/dist/internal/gate.js +458 -0
  155. package/dist/internal/gate.js.map +1 -0
  156. package/dist/internal/input.d.ts +35 -0
  157. package/dist/internal/input.d.ts.map +1 -0
  158. package/dist/internal/input.js +658 -0
  159. package/dist/internal/input.js.map +1 -0
  160. package/dist/internal/interpolators.d.ts +19 -0
  161. package/dist/internal/interpolators.d.ts.map +1 -0
  162. package/dist/internal/interpolators.js +30 -0
  163. package/dist/internal/interpolators.js.map +1 -0
  164. package/dist/internal/key-rules.d.ts +11 -0
  165. package/dist/internal/key-rules.d.ts.map +1 -0
  166. package/dist/internal/key-rules.js +244 -0
  167. package/dist/internal/key-rules.js.map +1 -0
  168. package/dist/internal/keyboard-press.d.ts +7 -0
  169. package/dist/internal/keyboard-press.d.ts.map +1 -0
  170. package/dist/internal/keyboard-press.js +45 -0
  171. package/dist/internal/keyboard-press.js.map +1 -0
  172. package/dist/internal/measure.d.ts +157 -0
  173. package/dist/internal/measure.d.ts.map +1 -0
  174. package/dist/internal/measure.js +296 -0
  175. package/dist/internal/measure.js.map +1 -0
  176. package/dist/internal/measured-pairs.d.ts +38 -0
  177. package/dist/internal/measured-pairs.d.ts.map +1 -0
  178. package/dist/internal/measured-pairs.js +251 -0
  179. package/dist/internal/measured-pairs.js.map +1 -0
  180. package/dist/internal/own-values.d.ts +35 -0
  181. package/dist/internal/own-values.d.ts.map +1 -0
  182. package/dist/internal/own-values.js +117 -0
  183. package/dist/internal/own-values.js.map +1 -0
  184. package/dist/internal/pass-ends.d.ts +21 -0
  185. package/dist/internal/pass-ends.d.ts.map +1 -0
  186. package/dist/internal/pass-ends.js +14 -0
  187. package/dist/internal/pass-ends.js.map +1 -0
  188. package/dist/internal/positional-keys.d.ts +16 -0
  189. package/dist/internal/positional-keys.d.ts.map +1 -0
  190. package/dist/internal/positional-keys.js +21 -0
  191. package/dist/internal/positional-keys.js.map +1 -0
  192. package/dist/internal/probe.d.ts +10 -0
  193. package/dist/internal/probe.d.ts.map +1 -0
  194. package/dist/internal/probe.js +116 -0
  195. package/dist/internal/probe.js.map +1 -0
  196. package/dist/internal/projection-defaults.d.ts +34 -0
  197. package/dist/internal/projection-defaults.d.ts.map +1 -0
  198. package/dist/internal/projection-defaults.js +21 -0
  199. package/dist/internal/projection-defaults.js.map +1 -0
  200. package/dist/internal/projection.d.ts +537 -0
  201. package/dist/internal/projection.d.ts.map +1 -0
  202. package/dist/internal/projection.js +5228 -0
  203. package/dist/internal/projection.js.map +1 -0
  204. package/dist/internal/renderer.d.ts +104 -0
  205. package/dist/internal/renderer.d.ts.map +1 -0
  206. package/dist/internal/renderer.js +618 -0
  207. package/dist/internal/renderer.js.map +1 -0
  208. package/dist/internal/reorder.d.ts +174 -0
  209. package/dist/internal/reorder.d.ts.map +1 -0
  210. package/dist/internal/reorder.js +466 -0
  211. package/dist/internal/reorder.js.map +1 -0
  212. package/dist/internal/report.d.ts +30 -0
  213. package/dist/internal/report.d.ts.map +1 -0
  214. package/dist/internal/report.js +60 -0
  215. package/dist/internal/report.js.map +1 -0
  216. package/dist/internal/require-global.d.ts +11 -0
  217. package/dist/internal/require-global.d.ts.map +1 -0
  218. package/dist/internal/require-global.js +31 -0
  219. package/dist/internal/require-global.js.map +1 -0
  220. package/dist/internal/reveal.d.ts +20 -0
  221. package/dist/internal/reveal.d.ts.map +1 -0
  222. package/dist/internal/reveal.js +71 -0
  223. package/dist/internal/reveal.js.map +1 -0
  224. package/dist/internal/runner.d.ts +21 -0
  225. package/dist/internal/runner.d.ts.map +1 -0
  226. package/dist/internal/runner.js +48 -0
  227. package/dist/internal/runner.js.map +1 -0
  228. package/dist/internal/scale-correction.d.ts +80 -0
  229. package/dist/internal/scale-correction.d.ts.map +1 -0
  230. package/dist/internal/scale-correction.js +227 -0
  231. package/dist/internal/scale-correction.js.map +1 -0
  232. package/dist/internal/scroll.d.ts +37 -0
  233. package/dist/internal/scroll.d.ts.map +1 -0
  234. package/dist/internal/scroll.js +279 -0
  235. package/dist/internal/scroll.js.map +1 -0
  236. package/dist/internal/scrollers.d.ts +35 -0
  237. package/dist/internal/scrollers.d.ts.map +1 -0
  238. package/dist/internal/scrollers.js +38 -0
  239. package/dist/internal/scrollers.js.map +1 -0
  240. package/dist/internal/starting-style.d.ts +141 -0
  241. package/dist/internal/starting-style.d.ts.map +1 -0
  242. package/dist/internal/starting-style.js +300 -0
  243. package/dist/internal/starting-style.js.map +1 -0
  244. package/dist/internal/svg.d.ts +60 -0
  245. package/dist/internal/svg.d.ts.map +1 -0
  246. package/dist/internal/svg.js +257 -0
  247. package/dist/internal/svg.js.map +1 -0
  248. package/dist/internal/variables.d.ts +45 -0
  249. package/dist/internal/variables.d.ts.map +1 -0
  250. package/dist/internal/variables.js +92 -0
  251. package/dist/internal/variables.js.map +1 -0
  252. package/package.json +65 -4
  253. package/src/Appear.ts +54 -0
  254. package/src/Color.ts +24 -0
  255. package/src/DomAnnouncer.ts +22 -0
  256. package/src/DomBatch.ts +22 -0
  257. package/src/DomClock.ts +13 -0
  258. package/src/DomDefaultTransition.ts +16 -0
  259. package/src/DomInput.ts +23 -0
  260. package/src/DomInterpolators.ts +16 -0
  261. package/src/DomKeyRules.ts +17 -0
  262. package/src/DomPositionalKeys.ts +16 -0
  263. package/src/DomProbe.ts +15 -0
  264. package/src/DomProjectionSystem.ts +44 -0
  265. package/src/DomRenderer.ts +26 -0
  266. package/src/DomScrollSource.ts +14 -0
  267. package/src/FlushGate.ts +81 -0
  268. package/src/KeyboardPress.ts +16 -0
  269. package/src/MeasuredPair.ts +15 -0
  270. package/src/OwnValues.ts +19 -0
  271. package/src/ProjectionSystem.ts +51 -0
  272. package/src/ReorderController.ts +122 -0
  273. package/src/Report.ts +17 -0
  274. package/src/ScrollOffset.ts +54 -0
  275. package/src/Style.ts +37 -0
  276. package/src/Svg.ts +21 -0
  277. package/src/Variables.ts +17 -0
  278. package/src/index.ts +120 -0
  279. package/src/internal/announcer.ts +132 -0
  280. package/src/internal/appear-slot.ts +32 -0
  281. package/src/internal/appear.ts +1134 -0
  282. package/src/internal/auto-scroll.ts +176 -0
  283. package/src/internal/batch-writer.ts +95 -0
  284. package/src/internal/clock.ts +45 -0
  285. package/src/internal/colors.ts +258 -0
  286. package/src/internal/complex.ts +29 -0
  287. package/src/internal/content-watch.ts +84 -0
  288. package/src/internal/culori.d.ts +58 -0
  289. package/src/internal/default-transition.ts +50 -0
  290. package/src/internal/drag-defaults.ts +54 -0
  291. package/src/internal/gate.ts +598 -0
  292. package/src/internal/input.ts +763 -0
  293. package/src/internal/interpolators.ts +33 -0
  294. package/src/internal/key-rules.ts +357 -0
  295. package/src/internal/keyboard-press.ts +45 -0
  296. package/src/internal/measure.ts +373 -0
  297. package/src/internal/measured-pairs.ts +297 -0
  298. package/src/internal/own-values.ts +166 -0
  299. package/src/internal/pass-ends.ts +23 -0
  300. package/src/internal/positional-keys.ts +24 -0
  301. package/src/internal/probe.ts +175 -0
  302. package/src/internal/projection-defaults.ts +45 -0
  303. package/src/internal/projection.ts +6233 -0
  304. package/src/internal/renderer.ts +648 -0
  305. package/src/internal/reorder.ts +650 -0
  306. package/src/internal/report.ts +69 -0
  307. package/src/internal/require-global.ts +46 -0
  308. package/src/internal/reveal.ts +75 -0
  309. package/src/internal/runner.ts +64 -0
  310. package/src/internal/scale-correction.ts +310 -0
  311. package/src/internal/scroll.ts +358 -0
  312. package/src/internal/scrollers.ts +51 -0
  313. package/src/internal/starting-style.ts +396 -0
  314. package/src/internal/svg.ts +300 -0
  315. package/src/internal/variables.ts +145 -0
@@ -0,0 +1,188 @@
1
+ /**
2
+ * `makeFlushGate` — the deferred **Flush gate** (ADR-0006), framework-free here in `@wardic/dom`
3
+ * so every binding shares one (ADR-0031, decision 3). The binding hands in its `batch` and its
4
+ * sinks; the gate imports nothing from a framework.
5
+ *
6
+ * Per ADR-0006, per-frame writes never reach their element directly. Each source
7
+ * `write(id, partialValues)`s into a provider-owned gate that:
8
+ *
9
+ * 1. Accumulates *value-maps* (NOT already-projected Patches) per element,
10
+ * merging partials by key — so multiple sources can feed one element
11
+ * (e.g. a drag setting `x`/`y` while a spring drives `scale`, all
12
+ * collapsing into one grouped `transform`). Projecting per source would
13
+ * clobber the grouped transform.
14
+ * 2. Resolves the Renderer's PURE `project` ONCE at creation and uses it to
15
+ * turn each dirty element's merged value-map into a `Patch` exactly once
16
+ * per element per frame.
17
+ * 3. Commits each resulting Patch onto its element with the Renderer's `commit`, a plain
18
+ * call (ADR-0034) — one DOM pass per frame, shared by every element: at the end of the
19
+ * Frame loop's tick, in the Refresh frame that computed the values (ADR-0033). No
20
+ * framework runs per frame: a binding's JSX carries an element's style only until its
21
+ * first commit (ADR-0021, decision 6), and after it only the keys the gate doesn't hold.
22
+ *
23
+ * Three locked design decisions (this session's grills):
24
+ * - FLUSHED at the end of the Frame loop's tick (ADR-0033): the gate registers a hook on
25
+ * the Environment's loop (`afterTick`), so everything a tick writes is applied at the end
26
+ * of that tick, inside the Clock's callback, and paints in that Refresh frame. A write
27
+ * made outside a tick (a drag's move, a `release` from a task, an effect a sink's hook ran,
28
+ * a Tier 2 `Stream` on `Clock.ticks` subscribed after the loop) still requests a
29
+ * Refresh frame, only if one is not already pending, and the hook withdraws that request
30
+ * when it flushes first. `flush` does NOT auto-reschedule, so an idle accumulator
31
+ * requests no frames — the next `write` does. The frames come from the Environment's
32
+ * `RefreshFrameSource` (ADR-0031): on the web, `requestAnimationFrame` itself. No binding
33
+ * calls the browser's frame functions itself. A Tier 2 `Stream` that writes here paints in
34
+ * the loop's frame or the next one, by its subscription order (ADR-0033): run it on the
35
+ * loop (`Machine.run`) to paint with it.
36
+ * - ONE flush per frame: all dirty elements flush together. The one exception
37
+ * is `flushPass` (ADR-0018): a projection Measure pass's own writes are applied
38
+ * at the end of its task, so a new layout never paints without its projection.
39
+ * - ONE sink shape (ADR-0034): a sink is the element its patch is committed onto. The gate
40
+ * also removes a key its last patch had and this one doesn't (a dragged node's `z-index`
41
+ * once its raise is dropped, ADR-0016 *Amendment (Reorder)*), restoring the element's own
42
+ * inline value, saved before the gate first wrote the key. Two hooks reach the binding,
43
+ * outside the commits, each in the binding's `batch` (ADR-0034, *Amendment (#209)*): the
44
+ * Starting-style switch before an element's first commit (`beforeFirstCommit`), and the keys
45
+ * the gate holds on the element whenever they change (`onHeld`), so the binding's `style`
46
+ * leaves them to Wardic.
47
+ *
48
+ * Observability (CONTEXT.md convention): the hot path (`write`/`flush`) is the
49
+ * per-frame work, so — exactly like core's per-frame writes and the Clock's
50
+ * `tick` — it stays quiet (no span, no per-call log, no metric). A flush that throws (a
51
+ * binding's hook, a commit) is reported (`reportFailure`) and the gate
52
+ * goes on, so it can't fail the Frame loop's tick it runs in (ADR-0033).
53
+ */
54
+ import * as FrameLoop from "@wardic/core/FrameLoop";
55
+ import * as RefreshFrameSource from "@wardic/core/RefreshFrameSource";
56
+ import * as Renderer from "@wardic/core/Renderer";
57
+ import * as Atom from "effect/reactivity/Atom";
58
+ import * as AtomRegistry from "effect/reactivity/AtomRegistry";
59
+ import * as Effect from "effect/Effect";
60
+ import type * as Fiber from "effect/Fiber";
61
+ import * as Scope from "effect/Scope";
62
+ /**
63
+ * Where a flushed `Patch` is delivered: committed onto `target` with the Renderer's `commit`,
64
+ * at flush time.
65
+ */
66
+ export interface FlushSink {
67
+ /** The element the patch is committed onto. */
68
+ readonly target: unknown;
69
+ /**
70
+ * Called once, just before the element's first commit in a frame flush (not a Measure
71
+ * pass's `flushPass`), inside the binding's `batch` with every other element's switch that
72
+ * flush: a binding drops its Starting style from its `style` prop here,
73
+ * so its framework removes those keys and the commit writes the first frame in the same
74
+ * Refresh frame, with no paint in between.
75
+ */
76
+ readonly beforeFirstCommit?: (() => void) | undefined;
77
+ /**
78
+ * Called with every key the gate now holds on the element, each time that set changes: after the
79
+ * gate restores a key it dropped, and before the commit that first
80
+ * writes a key. A binding whose `style` prop reaches the element leaves these keys out of it,
81
+ * so its framework never writes over Wardic's value (`style` is a key's fallback). Called in
82
+ * the binding's `batch`, with every other element's whose keys changed that flush, so what the
83
+ * binding changes reaches the element before the commit, a framework that applies an update later
84
+ * (Solid 2) included.
85
+ */
86
+ readonly onHeld?: ((keys: ReadonlyArray<string>) => void) | undefined;
87
+ /**
88
+ * Called once per frame in which the element's values were committed, after every commit of that
89
+ * frame (Motion's `onUpdate`), with its values before
90
+ * projection (`x` as a number). The object is the element's own, reused each call: valid during
91
+ * the call, copied to keep. A Measure pass's commits are told at the frame flush after them, so
92
+ * a frame tells each element once. A write it makes is the next frame's. A throw is reported,
93
+ * and the others are still told.
94
+ */
95
+ readonly onCommit?: ((values: Readonly<Record<string, unknown>>) => void) | undefined;
96
+ }
97
+ /** A Flush gate: what a binding registers its elements with and writes their values into, one per provider. */
98
+ export interface FlushGate {
99
+ /**
100
+ * Register an element under `id` with its `sink`. Returns an unregister fn
101
+ * that removes the element from the accumulator (a subsequent `write` is a
102
+ * no-op). `values` start its value-map, unflushed: drawn with its first `write`, so they never
103
+ * commit alone.
104
+ */
105
+ readonly register: (id: string, sink: FlushSink, values?: Readonly<Record<string, unknown>>) => () => void;
106
+ /**
107
+ * Merge `partial` into the element's value-map by key, mark it dirty, and
108
+ * schedule a flush: the end of the Frame loop's tick, for a write made in one, or else the
109
+ * next Refresh frame. A `write` for an unregistered (or unregistered-since) id
110
+ * is a no-op.
111
+ */
112
+ readonly write: (id: string, partial: Record<string, unknown>) => void;
113
+ /**
114
+ * Run a projection **Measure pass** now and apply exactly what it wrote before
115
+ * returning. Only `write()`s made while
116
+ * `pass()` runs are recorded; each recorded entry is `project`ed and committed once, and
117
+ * marked clean. Everything else stays dirty for its flush. A write made by an effect a sink's
118
+ * hook runs (a Solid effect) takes the ordinary path. The Projection system calls it at the
119
+ * end of a task, so a new layout never paints without its projection.
120
+ */
121
+ readonly flushPass: (pass: () => void) => void;
122
+ /**
123
+ * Hand `keys` back to the element's own style: drop each from the element's
124
+ * value-map and schedule a flush, which writes `restore[key]` (CSS text) for a key it had
125
+ * written, or else the inline value it overwrote, and tells the sink (`onHeld`). A key the
126
+ * entry doesn't hold, or an unknown id, is a no-op. A binding releases a key back at its `style`
127
+ * value, so a later `style` change shows at rest. A later `write` brings the key back. A property in
128
+ * `keep` (`transform` for its keys) is left as it's drawn when it drops out: another writer draws it next.
129
+ */
130
+ readonly release: (id: string, keys: ReadonlyArray<string>, restore?: Readonly<Record<string, string>>, keep?: ReadonlyArray<string>) => void;
131
+ /**
132
+ * The element's value for `id`'s `key`, live: an `Atom` set as each write that
133
+ * changes it lands, so it's the value the next commit draws, before the Renderer projects it
134
+ * (`x` as a number, not a `transform`). Set at the write, not the commit, so a value derived
135
+ * from it (another key following it through a binding) is written in the same frame and
136
+ * commits with it. `undefined` before the key's first write, and once it's released or the
137
+ * element unregisters. One Atom for an id and a key, made on first ask, so per frame only the
138
+ * keys asked for cost anything.
139
+ */
140
+ readonly valueOf: (id: string, key: string) => Atom.Atom<unknown>;
141
+ /**
142
+ * Call `fn` once, after the gate's next flush: the end of the Frame loop's tick, for a call
143
+ * made in one, else the next Refresh frame's, so what was written before it is on the elements.
144
+ * It schedules that flush if none is pending. A binding tells an animation's end here, as Motion's
145
+ * `onAnimationComplete` comes with the final value set. One that throws is reported, and the rest
146
+ * still run.
147
+ */
148
+ readonly afterFlush: (fn: () => void) => void;
149
+ /** Takes the gate off the Frame loop, cancels a pending frame and clears what it held. The Layer's Scope calls it. */
150
+ readonly dispose: () => void;
151
+ }
152
+ /** What a flush gate needs from its Environment: the `Renderer`, the Frame loop it flushes at the end of each tick of, and the `RefreshFrameSource`. */
153
+ export type FlushGateServices = Renderer.Renderer | RefreshFrameSource.RefreshFrameSource | FrameLoop.FrameLoop | AtomRegistry.AtomRegistry;
154
+ /**
155
+ * How the gate runs its Effects: synchronously, and forked. `FlushGate.make` builds one from its
156
+ * Layer's context, forking into its Scope (#132, ADR-0041); a test's `ManagedRuntime` is one too.
157
+ */
158
+ export interface FlushGateRuntime {
159
+ readonly runSync: <A, E>(effect: Effect.Effect<A, E, FlushGateServices>) => A;
160
+ readonly runFork: <A, E>(effect: Effect.Effect<A, E, FlushGateServices>) => Fiber.Fiber<A, E>;
161
+ /** The Scope it was built in: its own fibers live there, ended as it closes (#618). */
162
+ readonly scope: Scope.Scope;
163
+ }
164
+ /** What a binding hands its flush gate. */
165
+ export interface FlushGateOptions {
166
+ /**
167
+ * Runs a flush's hooks together: calls `apply`, and returns once every update inside it has reached
168
+ * the elements, before the gate commits. Solid 1 passes its `batch`, Solid 2 runs `apply` then
169
+ * `flush()`, and React would pass `flushSync`, untried; a binding with nothing to batch passes
170
+ * `(apply) => apply()`. Called at most twice a flush: once for the starting-style switches
171
+ * (`beforeFirstCommit`) of the elements committing for the first time, before anything is read off the
172
+ * elements; and once for the held keys (`onHeld`) of the elements whose held keys changed.
173
+ */
174
+ readonly batch: (apply: () => void) => void;
175
+ }
176
+ /**
177
+ * Build a flush gate against the provider's `runtime`, running each frame flush's Starting-style
178
+ * switches through the binding's `batch`.
179
+ *
180
+ * Resolves both ONCE here: `project` is pure and `commit` a plain call (both called directly at
181
+ * flush, ADR-0034), `requestFrame` /
182
+ * `cancelFrame` schedule and cancel the flush of a write made outside a tick, and the Frame
183
+ * loop's `afterTick` takes the gate's hook for its lifetime. All come from whichever env the
184
+ * provider was given (`DomLive` / `TestLive`), so the gate inherits that environment's
185
+ * grouping/unit logic, its Refresh frames and its loop with zero duplication.
186
+ */
187
+ export declare const makeFlushGate: (runtime: FlushGateRuntime, options: FlushGateOptions) => FlushGate;
188
+ //# sourceMappingURL=gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gate.d.ts","sourceRoot":"","sources":["../../src/internal/gate.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,458 @@
1
+ /**
2
+ * `makeFlushGate` — the deferred **Flush gate** (ADR-0006), framework-free here in `@wardic/dom`
3
+ * so every binding shares one (ADR-0031, decision 3). The binding hands in its `batch` and its
4
+ * sinks; the gate imports nothing from a framework.
5
+ *
6
+ * Per ADR-0006, per-frame writes never reach their element directly. Each source
7
+ * `write(id, partialValues)`s into a provider-owned gate that:
8
+ *
9
+ * 1. Accumulates *value-maps* (NOT already-projected Patches) per element,
10
+ * merging partials by key — so multiple sources can feed one element
11
+ * (e.g. a drag setting `x`/`y` while a spring drives `scale`, all
12
+ * collapsing into one grouped `transform`). Projecting per source would
13
+ * clobber the grouped transform.
14
+ * 2. Resolves the Renderer's PURE `project` ONCE at creation and uses it to
15
+ * turn each dirty element's merged value-map into a `Patch` exactly once
16
+ * per element per frame.
17
+ * 3. Commits each resulting Patch onto its element with the Renderer's `commit`, a plain
18
+ * call (ADR-0034) — one DOM pass per frame, shared by every element: at the end of the
19
+ * Frame loop's tick, in the Refresh frame that computed the values (ADR-0033). No
20
+ * framework runs per frame: a binding's JSX carries an element's style only until its
21
+ * first commit (ADR-0021, decision 6), and after it only the keys the gate doesn't hold.
22
+ *
23
+ * Three locked design decisions (this session's grills):
24
+ * - FLUSHED at the end of the Frame loop's tick (ADR-0033): the gate registers a hook on
25
+ * the Environment's loop (`afterTick`), so everything a tick writes is applied at the end
26
+ * of that tick, inside the Clock's callback, and paints in that Refresh frame. A write
27
+ * made outside a tick (a drag's move, a `release` from a task, an effect a sink's hook ran,
28
+ * a Tier 2 `Stream` on `Clock.ticks` subscribed after the loop) still requests a
29
+ * Refresh frame, only if one is not already pending, and the hook withdraws that request
30
+ * when it flushes first. `flush` does NOT auto-reschedule, so an idle accumulator
31
+ * requests no frames — the next `write` does. The frames come from the Environment's
32
+ * `RefreshFrameSource` (ADR-0031): on the web, `requestAnimationFrame` itself. No binding
33
+ * calls the browser's frame functions itself. A Tier 2 `Stream` that writes here paints in
34
+ * the loop's frame or the next one, by its subscription order (ADR-0033): run it on the
35
+ * loop (`Machine.run`) to paint with it.
36
+ * - ONE flush per frame: all dirty elements flush together. The one exception
37
+ * is `flushPass` (ADR-0018): a projection Measure pass's own writes are applied
38
+ * at the end of its task, so a new layout never paints without its projection.
39
+ * - ONE sink shape (ADR-0034): a sink is the element its patch is committed onto. The gate
40
+ * also removes a key its last patch had and this one doesn't (a dragged node's `z-index`
41
+ * once its raise is dropped, ADR-0016 *Amendment (Reorder)*), restoring the element's own
42
+ * inline value, saved before the gate first wrote the key. Two hooks reach the binding,
43
+ * outside the commits, each in the binding's `batch` (ADR-0034, *Amendment (#209)*): the
44
+ * Starting-style switch before an element's first commit (`beforeFirstCommit`), and the keys
45
+ * the gate holds on the element whenever they change (`onHeld`), so the binding's `style`
46
+ * leaves them to Wardic.
47
+ *
48
+ * Observability (CONTEXT.md convention): the hot path (`write`/`flush`) is the
49
+ * per-frame work, so — exactly like core's per-frame writes and the Clock's
50
+ * `tick` — it stays quiet (no span, no per-call log, no metric). A flush that throws (a
51
+ * binding's hook, a commit) is reported (`reportFailure`) and the gate
52
+ * goes on, so it can't fail the Frame loop's tick it runs in (ADR-0033).
53
+ */
54
+ import * as FrameLoop from "@wardic/core/FrameLoop";
55
+ import * as RefreshFrameSource from "@wardic/core/RefreshFrameSource";
56
+ import * as Renderer from "@wardic/core/Renderer";
57
+ import * as Cause from "effect/Cause";
58
+ import * as Atom from "effect/reactivity/Atom";
59
+ import * as AtomRegistry from "effect/reactivity/AtomRegistry";
60
+ import * as Effect from "effect/Effect";
61
+ import * as Exit from "effect/Exit";
62
+ import * as Predicate from "effect/Predicate";
63
+ import * as UndefinedOr from "effect/UndefinedOr";
64
+ import * as Scope from "effect/Scope";
65
+ import { reportFailure } from "./report.js";
66
+ import { TEMPLATE_KEY } from "./renderer.js";
67
+ import { dashCase } from "./svg.js";
68
+ /** The element's own inline value for a patch key (`""` for none, or a target with no style). */
69
+ const inlineValueOf = (target, key) => {
70
+ if (Predicate.isNullish(target))
71
+ return "";
72
+ const style = target.style;
73
+ if (Predicate.isUndefined(style))
74
+ return "";
75
+ return style.getPropertyValue(dashCase(key));
76
+ };
77
+ /**
78
+ * Build a flush gate against the provider's `runtime`, running each frame flush's Starting-style
79
+ * switches through the binding's `batch`.
80
+ *
81
+ * Resolves both ONCE here: `project` is pure and `commit` a plain call (both called directly at
82
+ * flush, ADR-0034), `requestFrame` /
83
+ * `cancelFrame` schedule and cancel the flush of a write made outside a tick, and the Frame
84
+ * loop's `afterTick` takes the gate's hook for its lifetime. All come from whichever env the
85
+ * provider was given (`DomLive` / `TestLive`), so the gate inherits that environment's
86
+ * grouping/unit logic, its Refresh frames and its loop with zero duplication.
87
+ */
88
+ export const makeFlushGate = (runtime, options) => {
89
+ const { batch } = options;
90
+ // Resolve the Renderer's two halves and the Refresh frame source exactly once at creation.
91
+ // `requestFrame` and `cancelFrame` are called detached, never as `frames.requestFrame(cb)`: a
92
+ // browser throws "Illegal invocation" when `requestAnimationFrame` runs with `this` bound to
93
+ // anything but the window (ADR-0031, decision 7).
94
+ const { commit, project, requestFrame, cancelFrame, afterTick } = runtime.runSync(Effect.gen(function* () {
95
+ const { commit, project } = yield* Renderer.Renderer;
96
+ const { requestFrame, cancelFrame } = yield* RefreshFrameSource.RefreshFrameSource;
97
+ const { afterTick } = yield* FrameLoop.FrameLoop;
98
+ return { commit, project, requestFrame, cancelFrame, afterTick };
99
+ }));
100
+ const accumulator = new Map();
101
+ // Every live output asked for (`valueOf`), by id: an entry registered later picks its map up.
102
+ const outputsById = new Map();
103
+ const registry = runtime.runSync(AtomRegistry.AtomRegistry);
104
+ // Sinks waiting for their first commit's `beforeFirstCommit` (usually empty).
105
+ const firstCommits = new Set();
106
+ let scheduled = false;
107
+ // What `afterFlush` queued since the last flush, called at its end (#257).
108
+ let afterFlushes = [];
109
+ // The pending Refresh frame's handle, while `scheduled`.
110
+ let frame;
111
+ // The ids written while a `flushPass` pass runs; `undefined` otherwise.
112
+ let recording;
113
+ // The entries committed since the last frame flush whose sink has an `onCommit` (#233).
114
+ const committedSince = new Set();
115
+ // A frame flush's entries, in order, prepared and waiting for their commit; and those whose
116
+ // keys held changed, which the binding hears first. Reused each flush: the per-frame path.
117
+ const prepared = [];
118
+ const heldChanged = [];
119
+ /**
120
+ * Project one dirty entry's patch and restore the keys it dropped (once per element per flush),
121
+ * marking it clean: into `into`, and into `held` when the keys it holds change and its sink
122
+ * hears them. Its commit waits for the binding's `batch` of those (ADR-0034, *Amendment
123
+ * (#209)*). Plain calls only: this is the per-frame path.
124
+ */
125
+ const prepare = (entry, into, held) => {
126
+ const patch = project(entry.values);
127
+ entry.dirty = false;
128
+ entry.patch = patch;
129
+ into.push(entry);
130
+ const sink = entry.sink;
131
+ const target = sink.target;
132
+ const committed = entry.committed;
133
+ const before = committed.size;
134
+ let kept = 0;
135
+ let added = false;
136
+ for (const key in patch) {
137
+ if (committed.has(key)) {
138
+ kept++;
139
+ continue;
140
+ }
141
+ committed.set(key, inlineValueOf(target, key));
142
+ added = true;
143
+ }
144
+ // A key the last patch wrote and this one doesn't: the element gets its own value
145
+ // back. Only looked for when one dropped out (the usual frame keeps every key).
146
+ let restore;
147
+ if (kept < before) {
148
+ for (const [key, own] of committed) {
149
+ if (key in patch)
150
+ continue;
151
+ if (Predicate.isUndefined(restore))
152
+ restore = {};
153
+ restore[key] = own;
154
+ committed.delete(key);
155
+ }
156
+ }
157
+ if (Predicate.isNotUndefined(restore))
158
+ commit(target, restore);
159
+ // The keys held changed (the usual frame changes none): the binding hears before the commit.
160
+ if ((added || Predicate.isNotUndefined(restore)) && Predicate.isNotUndefined(sink.onHeld))
161
+ held.push(entry);
162
+ };
163
+ /** Commit a prepared entry's patch onto its element. */
164
+ const commitPrepared = (entry) => {
165
+ const patch = entry.patch;
166
+ if (Predicate.isUndefined(patch))
167
+ return;
168
+ entry.patch = undefined;
169
+ commit(entry.sink.target, patch);
170
+ if (Predicate.isNotUndefined(entry.sink.onCommit))
171
+ committedSince.add(entry);
172
+ };
173
+ /**
174
+ * The prepared entries, committed, after the binding has heard the keys held change, together, in
175
+ * its `batch` (#209); both lists emptied for the next flush.
176
+ */
177
+ const commitAll = (entries, held) => {
178
+ if (held.length > 0)
179
+ batch(() => tellHeld(held));
180
+ for (let i = 0; i < entries.length; i++)
181
+ commitPrepared(entries[i]);
182
+ entries.length = 0;
183
+ held.length = 0;
184
+ };
185
+ /** Each entry's `onHeld`, with every key the gate holds on its element now. */
186
+ const tellHeld = (held) => {
187
+ for (let i = 0; i < held.length; i++) {
188
+ const entry = held[i];
189
+ const onHeld = entry.sink.onHeld;
190
+ if (Predicate.isNotUndefined(onHeld))
191
+ onHeld(Array.from(entry.committed.keys()));
192
+ }
193
+ };
194
+ /**
195
+ * Tell each entry committed since the last frame flush, once, after every commit of this one
196
+ * (#233). Its values are copied into its own reused object, so its sink never holds the gate's.
197
+ */
198
+ const tellCommitted = () => {
199
+ for (const entry of committedSince) {
200
+ const onCommit = entry.sink.onCommit;
201
+ if (Predicate.isUndefined(onCommit))
202
+ continue;
203
+ let latest = entry.latest;
204
+ if (Predicate.isUndefined(latest)) {
205
+ latest = {};
206
+ entry.latest = latest;
207
+ }
208
+ for (const key in latest)
209
+ if (!Object.hasOwn(entry.values, key))
210
+ delete latest[key];
211
+ Object.assign(latest, entry.values);
212
+ // Its Transform template rides in its values (#443), and isn't one: Motion's `onUpdate` never sees it.
213
+ delete latest[TEMPLATE_KEY];
214
+ try {
215
+ onCommit(latest);
216
+ entry.failing = false;
217
+ }
218
+ catch (defect) {
219
+ if (entry.failing)
220
+ continue;
221
+ entry.failing = true;
222
+ runtime.runFork(reportFailure("wardic.gate: an onCommit failed", Cause.die(defect)));
223
+ }
224
+ }
225
+ committedSince.clear();
226
+ };
227
+ /**
228
+ * The live outputs asked for of `entry`, each set where its value changed, as a write or a
229
+ * release lands: a value derived from one (a Live value of another key, through a binding) is
230
+ * written in the same frame, and commits with it. An unchanged one skips the registry. Plain
231
+ * calls: a write is the per-frame path.
232
+ */
233
+ const setOutputs = (entry) => {
234
+ const outputs = entry.outputs;
235
+ if (Predicate.isUndefined(outputs))
236
+ return;
237
+ for (const [key, output] of outputs) {
238
+ const value = entry.values[key];
239
+ if (value === output.last)
240
+ continue;
241
+ output.last = value;
242
+ registry.set(output.atom, value);
243
+ }
244
+ };
245
+ /**
246
+ * Run `beforeFirstCommit` for each waiting entry about to commit (dirty), in the binding's
247
+ * `batch` before any commit, so the binding applies what it changes first. A wrap that never
248
+ * commits stays waiting, so this loops over the wraps still waiting, once per frame flush.
249
+ */
250
+ const runFirstCommits = () => {
251
+ for (const id of firstCommits) {
252
+ const entry = accumulator.get(id);
253
+ if (Predicate.isUndefined(entry) || !entry.dirty)
254
+ continue;
255
+ firstCommits.delete(id);
256
+ const hook = entry.sink.beforeFirstCommit;
257
+ if (Predicate.isNotUndefined(hook))
258
+ hook();
259
+ }
260
+ };
261
+ const flush = () => {
262
+ scheduled = false;
263
+ try {
264
+ // The switches first: an element's own inline values are read once its Starting style is gone.
265
+ if (firstCommits.size > 0)
266
+ batch(runFirstCommits);
267
+ // Every dirty element, once. No auto-reschedule — the next write() does. A write()
268
+ // from an effect a sink's hook runs requests the next frame (it isn't stranded).
269
+ for (const entry of accumulator.values())
270
+ if (entry.dirty)
271
+ prepare(entry, prepared, heldChanged);
272
+ commitAll(prepared, heldChanged);
273
+ if (committedSince.size > 0)
274
+ tellCommitted();
275
+ }
276
+ catch (defect) {
277
+ prepared.length = 0;
278
+ heldChanged.length = 0;
279
+ runtime.runFork(reportFailure("wardic.gate: a flush failed", Cause.die(defect)));
280
+ }
281
+ if (afterFlushes.length > 0)
282
+ runAfterFlushes();
283
+ };
284
+ /** The calls `afterFlush` queued, each once, a new queue for any they make. */
285
+ const runAfterFlushes = () => {
286
+ const due = afterFlushes;
287
+ afterFlushes = [];
288
+ for (const fn of due) {
289
+ try {
290
+ fn();
291
+ }
292
+ catch (defect) {
293
+ runtime.runFork(reportFailure("wardic.gate: an afterFlush call failed", Cause.die(defect)));
294
+ }
295
+ }
296
+ };
297
+ const afterFlush = (fn) => {
298
+ afterFlushes.push(fn);
299
+ scheduleFlush();
300
+ };
301
+ /**
302
+ * The end of the Frame loop's tick (ADR-0033): flush now, in the Refresh frame that computed
303
+ * the values, and withdraw the frame the first write since the last flush requested.
304
+ */
305
+ const flushTicked = () => {
306
+ if (!scheduled)
307
+ return undefined;
308
+ cancelFrame(frame);
309
+ flush();
310
+ return undefined;
311
+ };
312
+ const scheduleFlush = () => {
313
+ if (scheduled)
314
+ return;
315
+ scheduled = true;
316
+ frame = requestFrame(flush);
317
+ };
318
+ const register = (id, sink, values) => {
319
+ const entry = { values: UndefinedOr.match(values, { onUndefined: () => ({}), onDefined: (start) => ({ ...start }) }), sink, dirty: false, committed: new Map(), outputs: outputsById.get(id), latest: undefined, failing: false, patch: undefined };
320
+ accumulator.set(id, entry);
321
+ if (Predicate.isNotUndefined(sink.beforeFirstCommit))
322
+ firstCommits.add(id);
323
+ return () => {
324
+ accumulator.delete(id);
325
+ firstCommits.delete(id);
326
+ committedSince.delete(entry);
327
+ endOutputs(id);
328
+ };
329
+ };
330
+ /** `id`'s live outputs, each set to `undefined` and forgotten: its element is gone. */
331
+ const endOutputs = (id) => {
332
+ const outputs = outputsById.get(id);
333
+ if (Predicate.isUndefined(outputs))
334
+ return;
335
+ outputsById.delete(id);
336
+ for (const output of outputs.values()) {
337
+ registry.set(output.atom, undefined);
338
+ output.unmount();
339
+ }
340
+ };
341
+ /** `id`'s current value for `key`, or `undefined` for an unregistered id or a key it hasn't written. */
342
+ const committedValueOf = (id, key) => {
343
+ const entry = accumulator.get(id);
344
+ if (Predicate.isUndefined(entry))
345
+ return undefined;
346
+ return entry.values[key];
347
+ };
348
+ const valueOf = (id, key) => {
349
+ let outputs = outputsById.get(id);
350
+ if (Predicate.isUndefined(outputs)) {
351
+ outputs = new Map();
352
+ outputsById.set(id, outputs);
353
+ const entry = accumulator.get(id);
354
+ if (Predicate.isNotUndefined(entry))
355
+ entry.outputs = outputs;
356
+ }
357
+ const known = outputs.get(key);
358
+ if (Predicate.isNotUndefined(known))
359
+ return known.atom;
360
+ // Made `undefined` and mounted until its element unregisters, not kept alive: a kept-alive
361
+ // Atom's node never leaves the registry, and one left behind reads `undefined` afresh.
362
+ const atom = Atom.make(undefined);
363
+ const output = { atom, last: undefined, unmount: registry.mount(atom) };
364
+ outputs.set(key, output);
365
+ const value = committedValueOf(id, key);
366
+ if (Predicate.isNotUndefined(value)) {
367
+ output.last = value;
368
+ registry.set(atom, value);
369
+ }
370
+ return atom;
371
+ };
372
+ const write = (id, partial) => {
373
+ const entry = accumulator.get(id);
374
+ if (Predicate.isUndefined(entry))
375
+ return;
376
+ Object.assign(entry.values, partial);
377
+ entry.dirty = true;
378
+ if (Predicate.isNotUndefined(recording))
379
+ recording.add(id);
380
+ scheduleFlush();
381
+ setOutputs(entry);
382
+ };
383
+ const flushPass = (pass) => {
384
+ const written = new Set();
385
+ recording = written;
386
+ try {
387
+ pass();
388
+ }
389
+ finally {
390
+ recording = undefined;
391
+ }
392
+ if (written.size === 0)
393
+ return;
394
+ // No `beforeFirstCommit` here: a Measure pass may write only a node's projection, and
395
+ // dropping the Starting style then would leave its animated keys unset until the frame
396
+ // flush (a frame later for an element mounted inside a frame). The frame flush switches.
397
+ // A commit here is told at the frame flush (#233): the write that made it scheduled that flush.
398
+ // Its own lists: a pass may run from a hook the frame flush's `batch` runs.
399
+ const entries = [];
400
+ const held = [];
401
+ for (const id of written) {
402
+ const entry = accumulator.get(id);
403
+ if (Predicate.isNotUndefined(entry) && entry.dirty)
404
+ prepare(entry, entries, held);
405
+ }
406
+ commitAll(entries, held);
407
+ };
408
+ const release = (id, keys, restore, keep) => {
409
+ const entry = accumulator.get(id);
410
+ if (Predicate.isUndefined(entry))
411
+ return;
412
+ // A property another writer draws next: dropped from what's saved, so its drop-out writes nothing.
413
+ if (Predicate.isNotUndefined(keep))
414
+ for (const property of keep)
415
+ entry.committed.delete(property);
416
+ let released = false;
417
+ for (const key of keys) {
418
+ if (!Object.hasOwn(entry.values, key))
419
+ continue;
420
+ delete entry.values[key];
421
+ released = true;
422
+ // The drop-out path in `apply` writes the saved value back: make it the one handed in.
423
+ if (Predicate.isUndefined(restore))
424
+ continue;
425
+ const value = restore[key];
426
+ if (Predicate.isNotUndefined(value) && entry.committed.has(key))
427
+ entry.committed.set(key, value);
428
+ }
429
+ if (!released)
430
+ return;
431
+ entry.dirty = true;
432
+ scheduleFlush();
433
+ setOutputs(entry);
434
+ };
435
+ const dispose = () => {
436
+ // Off the loop first, so no tick flushes what follows; a failure reported, never thrown (#618).
437
+ const unhooked = Effect.runSyncExit(Scope.close(ticks, Exit.void));
438
+ if (Exit.isFailure(unhooked))
439
+ runtime.runFork(reportFailure("wardic.gate: its tick hook failed to close", unhooked.cause));
440
+ if (scheduled) {
441
+ cancelFrame(frame);
442
+ scheduled = false;
443
+ }
444
+ accumulator.clear();
445
+ firstCommits.clear();
446
+ committedSince.clear();
447
+ // The calls waiting on a flush that won't come: their provider is gone (#257).
448
+ afterFlushes = [];
449
+ for (const id of [...outputsById.keys()])
450
+ endOutputs(id);
451
+ };
452
+ // The gate's hook on the loop, for the gate's lifetime: a child of the Scope it's built in, so a gate
453
+ // its `dispose` never reached is unhooked as that Scope closes.
454
+ const ticks = Scope.forkUnsafe(runtime.scope, "sequential");
455
+ runtime.runSync(afterTick(flushTicked).pipe(Scope.provide(ticks)));
456
+ return { register, write, flushPass, release, valueOf, afterFlush, dispose };
457
+ };
458
+ //# sourceMappingURL=gate.js.map