@wardic/solid 2.0.1 → 2.1.1

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 (222) hide show
  1. package/dist/browser/batch-reorder-group.js +3 -2
  2. package/dist/browser/batch-reorder-group.js.map +1 -1
  3. package/dist/browser/complete.js +3 -8
  4. package/dist/browser/complete.js.map +1 -1
  5. package/dist/browser/create-batch.js +13 -3
  6. package/dist/browser/create-batch.js.map +1 -1
  7. package/dist/browser/drag-sensor.js +44 -6
  8. package/dist/browser/drag-sensor.js.map +1 -1
  9. package/dist/browser/element.js +3 -1
  10. package/dist/browser/element.js.map +1 -1
  11. package/dist/browser/focus-sensor.js +21 -4
  12. package/dist/browser/focus-sensor.js.map +1 -1
  13. package/dist/browser/frame-effect.js +108 -0
  14. package/dist/browser/frame-effect.js.map +1 -0
  15. package/dist/browser/hover-sensor.js +21 -7
  16. package/dist/browser/hover-sensor.js.map +1 -1
  17. package/dist/browser/index.js +7 -0
  18. package/dist/browser/index.js.map +1 -1
  19. package/dist/browser/lifecycle.js +12 -0
  20. package/dist/browser/lifecycle.js.map +1 -1
  21. package/dist/browser/presence-hooks.js +5 -3
  22. package/dist/browser/presence-hooks.js.map +1 -1
  23. package/dist/browser/press-sensor.js +21 -7
  24. package/dist/browser/press-sensor.js.map +1 -1
  25. package/dist/browser/provider.js +7 -4
  26. package/dist/browser/provider.js.map +1 -1
  27. package/dist/browser/sensors.js +54 -3
  28. package/dist/browser/sensors.js.map +1 -1
  29. package/dist/browser/solid-reactivity.js +7 -0
  30. package/dist/browser/solid-reactivity.js.map +1 -1
  31. package/dist/browser/stage-batch.js +85 -0
  32. package/dist/browser/stage-batch.js.map +1 -0
  33. package/dist/browser/stage-graph.js +264 -0
  34. package/dist/browser/stage-graph.js.map +1 -0
  35. package/dist/browser/stage-hooks.js +61 -0
  36. package/dist/browser/stage-hooks.js.map +1 -0
  37. package/dist/browser/stage-ward.js +687 -0
  38. package/dist/browser/stage-ward.js.map +1 -0
  39. package/dist/browser/stage.js +8 -0
  40. package/dist/browser/stage.js.map +1 -0
  41. package/dist/browser/unstable/v2/index.js +9 -0
  42. package/dist/browser/unstable/v2/index.js.map +1 -1
  43. package/dist/browser/v1/index.js +1 -1
  44. package/dist/browser/w.js +3 -3
  45. package/dist/browser/w.js.map +1 -1
  46. package/dist/browser/ward-first.js +45 -0
  47. package/dist/browser/ward-first.js.map +1 -0
  48. package/dist/browser/ward.js +35 -6
  49. package/dist/browser/ward.js.map +1 -1
  50. package/dist/server/batch-reorder-group.js +3 -2
  51. package/dist/server/batch-reorder-group.js.map +1 -1
  52. package/dist/server/complete.js +3 -8
  53. package/dist/server/complete.js.map +1 -1
  54. package/dist/server/create-batch.js +13 -3
  55. package/dist/server/create-batch.js.map +1 -1
  56. package/dist/server/drag-sensor.js +44 -6
  57. package/dist/server/drag-sensor.js.map +1 -1
  58. package/dist/server/element.js +3 -1
  59. package/dist/server/element.js.map +1 -1
  60. package/dist/server/focus-sensor.js +21 -4
  61. package/dist/server/focus-sensor.js.map +1 -1
  62. package/dist/server/frame-effect.js +108 -0
  63. package/dist/server/frame-effect.js.map +1 -0
  64. package/dist/server/hover-sensor.js +21 -7
  65. package/dist/server/hover-sensor.js.map +1 -1
  66. package/dist/server/index.js +7 -0
  67. package/dist/server/index.js.map +1 -1
  68. package/dist/server/lifecycle.js +12 -0
  69. package/dist/server/lifecycle.js.map +1 -1
  70. package/dist/server/presence-hooks.js +5 -3
  71. package/dist/server/presence-hooks.js.map +1 -1
  72. package/dist/server/press-sensor.js +21 -7
  73. package/dist/server/press-sensor.js.map +1 -1
  74. package/dist/server/provider.js +7 -4
  75. package/dist/server/provider.js.map +1 -1
  76. package/dist/server/sensors.js +54 -3
  77. package/dist/server/sensors.js.map +1 -1
  78. package/dist/server/solid-reactivity.js +7 -0
  79. package/dist/server/solid-reactivity.js.map +1 -1
  80. package/dist/server/stage-batch.js +85 -0
  81. package/dist/server/stage-batch.js.map +1 -0
  82. package/dist/server/stage-graph.js +264 -0
  83. package/dist/server/stage-graph.js.map +1 -0
  84. package/dist/server/stage-hooks.js +61 -0
  85. package/dist/server/stage-hooks.js.map +1 -0
  86. package/dist/server/stage-ward.js +687 -0
  87. package/dist/server/stage-ward.js.map +1 -0
  88. package/dist/server/stage.js +8 -0
  89. package/dist/server/stage.js.map +1 -0
  90. package/dist/server/unstable/v2/index.js +9 -0
  91. package/dist/server/unstable/v2/index.js.map +1 -1
  92. package/dist/server/v1/index.js +1 -1
  93. package/dist/server/w.js +3 -3
  94. package/dist/server/w.js.map +1 -1
  95. package/dist/server/ward-first.js +45 -0
  96. package/dist/server/ward-first.js.map +1 -0
  97. package/dist/server/ward.js +35 -6
  98. package/dist/server/ward.js.map +1 -1
  99. package/dist/solid/batch-reorder-group.d.ts.map +1 -1
  100. package/dist/solid/batch-reorder-group.jsx +3 -2
  101. package/dist/solid/batch-reorder-group.jsx.map +1 -1
  102. package/dist/solid/complete.d.ts +8 -1
  103. package/dist/solid/complete.d.ts.map +1 -1
  104. package/dist/solid/complete.jsx +3 -9
  105. package/dist/solid/complete.jsx.map +1 -1
  106. package/dist/solid/create-batch.d.ts +5 -0
  107. package/dist/solid/create-batch.d.ts.map +1 -1
  108. package/dist/solid/create-batch.jsx +14 -3
  109. package/dist/solid/create-batch.jsx.map +1 -1
  110. package/dist/solid/drag-sensor.d.ts +18 -2
  111. package/dist/solid/drag-sensor.d.ts.map +1 -1
  112. package/dist/solid/drag-sensor.jsx +41 -6
  113. package/dist/solid/drag-sensor.jsx.map +1 -1
  114. package/dist/solid/element.d.ts.map +1 -1
  115. package/dist/solid/element.jsx +3 -1
  116. package/dist/solid/element.jsx.map +1 -1
  117. package/dist/solid/focus-sensor.d.ts +10 -3
  118. package/dist/solid/focus-sensor.d.ts.map +1 -1
  119. package/dist/solid/focus-sensor.jsx +25 -5
  120. package/dist/solid/focus-sensor.jsx.map +1 -1
  121. package/dist/solid/frame-effect.d.ts +3 -0
  122. package/dist/solid/frame-effect.d.ts.map +1 -0
  123. package/dist/solid/frame-effect.jsx +112 -0
  124. package/dist/solid/frame-effect.jsx.map +1 -0
  125. package/dist/solid/hover-sensor.d.ts +14 -3
  126. package/dist/solid/hover-sensor.d.ts.map +1 -1
  127. package/dist/solid/hover-sensor.jsx +20 -6
  128. package/dist/solid/hover-sensor.jsx.map +1 -1
  129. package/dist/solid/index.d.ts +8 -0
  130. package/dist/solid/index.d.ts.map +1 -1
  131. package/dist/solid/index.jsx +7 -0
  132. package/dist/solid/index.jsx.map +1 -1
  133. package/dist/solid/lifecycle.d.ts +3 -0
  134. package/dist/solid/lifecycle.d.ts.map +1 -1
  135. package/dist/solid/lifecycle.js +13 -0
  136. package/dist/solid/lifecycle.js.map +1 -1
  137. package/dist/solid/presence-hooks.d.ts +5 -3
  138. package/dist/solid/presence-hooks.d.ts.map +1 -1
  139. package/dist/solid/presence-hooks.jsx +5 -3
  140. package/dist/solid/presence-hooks.jsx.map +1 -1
  141. package/dist/solid/press-sensor.d.ts +11 -3
  142. package/dist/solid/press-sensor.d.ts.map +1 -1
  143. package/dist/solid/press-sensor.jsx +24 -6
  144. package/dist/solid/press-sensor.jsx.map +1 -1
  145. package/dist/solid/provider.d.ts.map +1 -1
  146. package/dist/solid/provider.jsx +7 -4
  147. package/dist/solid/provider.jsx.map +1 -1
  148. package/dist/solid/sensors.d.ts +25 -1
  149. package/dist/solid/sensors.d.ts.map +1 -1
  150. package/dist/solid/sensors.jsx +53 -3
  151. package/dist/solid/sensors.jsx.map +1 -1
  152. package/dist/solid/solid-reactivity.d.ts +7 -0
  153. package/dist/solid/solid-reactivity.d.ts.map +1 -1
  154. package/dist/solid/solid-reactivity.js +7 -0
  155. package/dist/solid/solid-reactivity.js.map +1 -1
  156. package/dist/solid/stage-batch.d.ts +6 -0
  157. package/dist/solid/stage-batch.d.ts.map +1 -0
  158. package/dist/solid/stage-batch.js +86 -0
  159. package/dist/solid/stage-batch.js.map +1 -0
  160. package/dist/solid/stage-graph.d.ts +79 -0
  161. package/dist/solid/stage-graph.d.ts.map +1 -0
  162. package/dist/solid/stage-graph.js +224 -0
  163. package/dist/solid/stage-graph.js.map +1 -0
  164. package/dist/solid/stage-hooks.d.ts +52 -0
  165. package/dist/solid/stage-hooks.d.ts.map +1 -0
  166. package/dist/solid/stage-hooks.js +44 -0
  167. package/dist/solid/stage-hooks.js.map +1 -0
  168. package/dist/solid/stage-ward.d.ts +52 -0
  169. package/dist/solid/stage-ward.d.ts.map +1 -0
  170. package/dist/solid/stage-ward.jsx +609 -0
  171. package/dist/solid/stage-ward.jsx.map +1 -0
  172. package/dist/solid/stage.d.ts +309 -0
  173. package/dist/solid/stage.d.ts.map +1 -0
  174. package/dist/solid/stage.js +8 -0
  175. package/dist/solid/stage.js.map +1 -0
  176. package/dist/solid/unstable/v2/index.d.ts +96 -0
  177. package/dist/solid/unstable/v2/index.d.ts.map +1 -1
  178. package/dist/solid/unstable/v2/index.jsx +18 -0
  179. package/dist/solid/unstable/v2/index.jsx.map +1 -1
  180. package/dist/solid/v1/index.d.ts +1 -1
  181. package/dist/solid/v1/index.jsx +1 -1
  182. package/dist/solid/w.d.ts +11 -0
  183. package/dist/solid/w.d.ts.map +1 -1
  184. package/dist/solid/w.jsx +3 -3
  185. package/dist/solid/w.jsx.map +1 -1
  186. package/dist/solid/ward-first.d.ts +21 -0
  187. package/dist/solid/ward-first.d.ts.map +1 -0
  188. package/dist/solid/ward-first.js +50 -0
  189. package/dist/solid/ward-first.js.map +1 -0
  190. package/dist/solid/ward-keys.d.ts +13 -12
  191. package/dist/solid/ward-keys.d.ts.map +1 -1
  192. package/dist/solid/ward.d.ts +49 -0
  193. package/dist/solid/ward.d.ts.map +1 -1
  194. package/dist/solid/ward.jsx +31 -6
  195. package/dist/solid/ward.jsx.map +1 -1
  196. package/package.json +5 -4
  197. package/src/batch-reorder-group.tsx +3 -2
  198. package/src/complete.tsx +16 -9
  199. package/src/create-batch.tsx +19 -3
  200. package/src/drag-sensor.tsx +54 -6
  201. package/src/element.tsx +3 -1
  202. package/src/focus-sensor.tsx +33 -6
  203. package/src/frame-effect.tsx +122 -0
  204. package/src/hover-sensor.tsx +25 -6
  205. package/src/index.tsx +46 -0
  206. package/src/lifecycle.ts +13 -0
  207. package/src/presence-hooks.tsx +5 -3
  208. package/src/press-sensor.tsx +30 -6
  209. package/src/provider.tsx +7 -4
  210. package/src/sensors.tsx +67 -2
  211. package/src/solid-reactivity.ts +8 -0
  212. package/src/stage-batch.ts +89 -0
  213. package/src/stage-graph.ts +264 -0
  214. package/src/stage-hooks.ts +100 -0
  215. package/src/stage-ward.tsx +795 -0
  216. package/src/stage.ts +337 -0
  217. package/src/unstable/v2/index.tsx +96 -0
  218. package/src/v1/index.tsx +1 -1
  219. package/src/w.tsx +22 -5
  220. package/src/ward-first.ts +67 -0
  221. package/src/ward-keys.ts +14 -13
  222. package/src/ward.tsx +42 -6
@@ -0,0 +1,795 @@
1
+ /**
2
+ * A ward on a Stage's Subject: what `ward(mesh, config)` makes under a `<WardicStage>`, a Stage's own
3
+ * `ward`. `stageWard(environment)` binds one to a Stage's Environment, for the Stage to hand out.
4
+ *
5
+ * It moves its Subject as a ward moves an element: `initial`, `animate` (an async one held with
6
+ * `held`), `transition`, its own `variants`, `whileHover` and `whileTap` through the Stage's input, its
7
+ * events, `read` and `atom`. Given an accessor, it follows each Subject it gives: a new one takes the
8
+ * ward over, starting as the ward did, with no exit.
9
+ *
10
+ * A Stage over `@wardic/object`'s runtime gives it the runtime's `stageEnvironment` and its scene's input:
11
+ * `stageWard({ ...runtime.stageEnvironment, input })`.
12
+ *
13
+ * `stageSceneObject(environment)` makes the same ward with a **membership** (#885, ADR-0073 decisions 10,
14
+ * 14 and 15): an Environment's `createSceneObject` gives it what joins its Subject to the scene and what
15
+ * takes it out. It joins once the ward can start, and leaves once its `exit` lands, run on its own spine
16
+ * past its owner, as it ends or as an accessor gives another Subject, or none.
17
+ *
18
+ * @module
19
+ */
20
+ // #884's PR 4 (ADR-0073 decisions 8, 10 and 20; *Amendment (#884)*, Q2, Q3 and Q5): a second, smaller ward
21
+ // on core's spine and the DOM ward's config view, variant node and lifecycle. The DOM's own parts (its
22
+ // Starting style, server render, hydration, layout, drag, presence, `style`'s Live values) stay `wardWith`'s.
23
+ import type { JSX } from "@solidjs/web"
24
+ import * as BaseValue from "@wardic/core/BaseValue"
25
+ import * as InputSource from "@wardic/core/InputSource"
26
+ import * as Recognizer from "@wardic/core/Recognizer"
27
+ import * as AnimationControls from "@wardic/core/unstable/AnimationControls"
28
+ import type * as Config from "@wardic/core/unstable/Config"
29
+ import * as Imperative from "@wardic/core/unstable/Imperative"
30
+ import type * as OwnValues from "@wardic/core/unstable/OwnValues"
31
+ import * as Report from "@wardic/core/unstable/Report"
32
+ import * as Spine from "@wardic/core/unstable/Spine"
33
+ import * as VariantTree from "@wardic/core/unstable/VariantTree"
34
+ import * as OwnerScope from "@wardic/dom/unstable/OwnerScope"
35
+ import * as Arr from "effect/Array"
36
+ import * as Bool from "effect/Boolean"
37
+ import type * as Cause from "effect/Cause"
38
+ import * as Effect from "effect/Effect"
39
+ import { constUndefined, constVoid, identity } from "effect/Function"
40
+ import * as Predicate from "effect/Predicate"
41
+ import * as Option from "effect/Option"
42
+ import * as Atom from "effect/reactivity/Atom"
43
+ import * as Rec from "effect/Record"
44
+ import * as Scope from "effect/Scope"
45
+ import * as UndefinedOr from "effect/UndefinedOr"
46
+ import { type Accessor, createEffect, createMemo, createSignal, DEV, getOwner, onCleanup, runWithOwner, untrack } from "solid-js"
47
+ import { followAtom } from "./bridge.tsx"
48
+ import { Lifecycle, type WardElement } from "./lifecycle.ts"
49
+ import { CLIENT_ONLY, CLIENT_ONLY_SYNC, CLIENT_ONLY_SYNC_DEFERRED } from "./solid-reactivity.ts"
50
+ import type { SceneObjectEvents, SceneWard, Stage, StageDrag, StageDragConfig, StageWard, StageWardConfig } from "./stage.ts"
51
+ import { GraphParent, provide, type StageGraph } from "./stage-graph.ts"
52
+ import { createVariantNode } from "./variant-node.ts"
53
+ import { configAtSetup, configuredView, gateOf, sameTarget, STARTS_AT_PENDING, WARD_VIEW, type WardConfigValues } from "./ward.tsx"
54
+ import { eachOf, Waiters, whenOf } from "./waiters.ts"
55
+ import { useWardicConfig } from "./wardic-config.tsx"
56
+
57
+ /**
58
+ * What a Stage ward runs on: what `animate` runs on (its runtime, gate, Subjects' values, Positional keys,
59
+ * Default transitions, reduced motion and `skipAnimations`), where a Subject starts, and the scene's
60
+ * pointer input. `@wardic/object`'s `stageEnvironment`, with the Stage's input, is one.
61
+ */
62
+ export interface StageWardEnvironment extends Imperative.AnimateEnv {
63
+ /** Where each key of a Subject starts: its Environment's Base-value links. */
64
+ readonly startLinksOf: (subject: object) => Spine.StartLinks
65
+ /** The scene's pointer input, which its hovers and presses are heard through. */
66
+ readonly input: InputSource.InputSource["Service"]
67
+ /**
68
+ * The scene's graph, which variants pass down (#885, decision 17): a Variant child follows the nearest
69
+ * ward above its Subject. None: each ward has only its own variants.
70
+ */
71
+ readonly graph?: StageGraph | undefined
72
+ /** What drags its Subjects (#885's PR 4): a `drag` with none is an error in development. */
73
+ readonly drag?: StageDrag | undefined
74
+ }
75
+
76
+ /**
77
+ * A Scene object's membership of its scene (#885; ADR-0073 decisions 10 and 14): what an Environment's
78
+ * `createSceneObject` gives `stageSceneObject`'s ward.
79
+ */
80
+ export interface StageMembership {
81
+ /** Its Subject joins the scene: the ward can start, its `initial` written for the next frame drawn. */
82
+ readonly join: (subject: object) => void
83
+ /** Its Subject leaves the scene: its exit has landed, or it had none. */
84
+ readonly leave: (subject: object) => void
85
+ /** The parent its Subject will join, before it has: where a Variant child looks for the ward it follows, at its setup. */
86
+ readonly parentOf?: ((subject: object) => object | undefined) | undefined
87
+ /**
88
+ * An exit in flight: `land` ends it now, the Subject taken out, as the Stage ends before it lands
89
+ * (*Amendment (#885)* Q5). The call returned says it has landed.
90
+ */
91
+ readonly exiting: (land: () => void) => () => void
92
+ }
93
+
94
+ /** The keys a ward on a Stage's Subject takes: any other is the page's, or a Scene object's (decision 5). */
95
+ const STAGE_KEYS: ReadonlySet<string> = new Set([
96
+ "initial",
97
+ "animate",
98
+ "transition",
99
+ "variants",
100
+ "whileHover",
101
+ "whileTap",
102
+ "custom",
103
+ "inherit",
104
+ // A scene's drag (#885's PR 4): its Stage's `drag` reads them.
105
+ "whileDrag",
106
+ "drag",
107
+ "dragPlane",
108
+ "dragConstraints",
109
+ "dragElastic",
110
+ "dragMomentum",
111
+ "dragTransition",
112
+ "dragSnapToOrigin",
113
+ ])
114
+
115
+ /** A scene's drag keys, read from its config as given, at each gesture's start. */
116
+ const DRAG_KEYS = ["drag", "dragPlane", "dragConstraints", "dragElastic", "dragMomentum", "dragTransition", "dragSnapToOrigin"] as const
117
+
118
+ /** A `drag` on a Stage with nothing to drag with. */
119
+ const NO_DRAG =
120
+ "ward(subject, { drag }): the Stage above it drags nothing, as it has no `drag`. With `@wardic/three/solid`, give `createStage` the feature: `createStage({ …, features: [stageDrag] })`."
121
+
122
+ /** `config`'s drag keys now, each an accessor's value as it's read. */
123
+ const dragConfigOf = (config: StageWardConfig): StageDragConfig => {
124
+ const given = config as Readonly<Record<string, unknown>>
125
+ const out: Record<string, unknown> = {}
126
+ Arr.forEach(DRAG_KEYS, (key) => {
127
+ const value = given[key]
128
+ out[key] = Bool.match(Predicate.isFunction(value), { onTrue: () => (value as () => unknown)(), onFalse: () => value })
129
+ })
130
+ return out as StageDragConfig
131
+ }
132
+
133
+ /** A Scene object's own key: its ward's, plus its `exit`. */
134
+ const SCENE_KEYS: ReadonlySet<string> = new Set([...STAGE_KEYS, "exit"])
135
+
136
+ /** A key of the page's alone: a scene has no focus or viewport. */
137
+ const PAGE_ONLY: ReadonlySet<string> = new Set(["whileFocus", "whileInView", "viewport"])
138
+
139
+ /** Animation controls given as its `animate`, which a ward on a Stage's Subject doesn't follow. */
140
+ const NO_CONTROLS = "ward(subject, config): a ward on a Stage's Subject doesn't follow Animation controls. Give its `animate` a target, or call the Stage's `animate`."
141
+
142
+ /** In development: a key a ward on a Stage's Subject doesn't take, named; `exit` is a Scene object's. */
143
+ const checkKeys = (config: StageWardConfig, scene: boolean): void =>
144
+ Arr.forEach(Rec.keys(config as Readonly<Record<string, unknown>>), (key) => {
145
+ if (Bool.match(scene, { onTrue: () => SCENE_KEYS.has(key), onFalse: () => STAGE_KEYS.has(key) })) return
146
+ if (PAGE_ONLY.has(key)) throw new Error(`ward(subject, config): \`${key}\` is the page's: a scene has no focus or viewport. Give it to a ward on an element.`)
147
+ if (key === "exit") throw new Error("ward(subject, config): `exit` is a Scene object's: `createSceneObject(mesh, { exit })` plays it as the object leaves the scene.")
148
+ throw new Error(
149
+ `ward(subject, config): a ward on a Stage's Subject takes initial, animate, transition, variants, whileHover, whileTap, whileDrag, custom and the drag keys; \`${key}\` is a ward on an element's.`,
150
+ )
151
+ })
152
+
153
+ /** No gesture on: what a Scene object's exit resolves with. */
154
+ const NOTHING_ACTIVE: Spine.Active = { inView: false, focused: false, hovered: false, pressed: false, dragged: false, dragging: false }
155
+
156
+ /** A Scene object's own events: its exit's. */
157
+ type ExitEvents = Pick<SceneObjectEvents, "exitStart" | "exitComplete">
158
+
159
+ /** What a Scene object's exit waits reject with once it's gone and its last exit has landed. */
160
+ const EXITS_GONE = "The Scene object was gone before its next exit."
161
+
162
+ /** Whether `name` is a Scene object's exit event, its own waits', not its ward's. */
163
+ const isExitEvent = (name: string): name is "exitStart" | "exitComplete" => Bool.or(name === "exitStart", name === "exitComplete")
164
+
165
+ /** Its hover, press and drag, on the Subject they were heard on: a state recorded on another reads as none. */
166
+ interface Gestures {
167
+ readonly subject: object | undefined
168
+ readonly hovered: boolean
169
+ readonly pressed: boolean
170
+ readonly dragged: boolean
171
+ }
172
+ const NO_GESTURES: Gestures = { subject: undefined, hovered: false, pressed: false, dragged: false }
173
+
174
+ /** The Subject it's on: its spine, its gate registration, and which of its recognizers run. */
175
+ interface Mounted {
176
+ readonly subject: object
177
+ readonly spine: Spine.ElementSpine
178
+ /** Its spine's record of what it drew: a Scene object's own each Subject, as the last exits beside the next. */
179
+ readonly snapshot: Spine.Snapshot
180
+ hover: boolean
181
+ press: boolean
182
+ drag: boolean
183
+ /** A Scene object's exit starts: `land` lands it once its spine settles, a failed exit too; none, it's taken back. */
184
+ readonly exitWith: (land: (() => void) | undefined) => void
185
+ readonly watch: (watch: Effect.Effect<void, never, Scope.Scope | InputSource.InputSource>) => void
186
+ /** Its recognizers stop, and the Stage's `animate` no longer goes through it: an exit's start. */
187
+ readonly quiet: () => void
188
+ /** What `quiet` stopped goes again: a Scene object's Subject taken back while it exits. */
189
+ readonly wake: () => void
190
+ readonly dispose: () => void
191
+ }
192
+
193
+ /**
194
+ * `links` with each key's own value read once, before the ward first moves it: what a key a gesture moved
195
+ * returns to once no entry sets it (ADR-0022's Own value), as Motion's base value is. An object's
196
+ * property read again is where the ward left it, so a hover would have nothing to return to.
197
+ */
198
+ const baseHeld = (links: Spine.StartLinks, ownValues: OwnValues.Shape): Spine.StartLinks => {
199
+ const first = new Map<string, Option.Option<number | string>>()
200
+ const ownValue = (key: string): Option.Option<number | string> => {
201
+ const known = first.get(key)
202
+ if (Predicate.isNotUndefined(known)) return known
203
+ const read = links.ownValue(key)
204
+ first.set(key, read)
205
+ return read
206
+ }
207
+ const linksFor = (like: number | string): ReturnType<Spine.StartLinks["linksFor"]> => ({
208
+ style: links.linksFor(like).style,
209
+ own: (key) => Option.flatMap(ownValue(key), (raw) => ownValues.valueLike(raw, like)),
210
+ })
211
+ return { ownValue, linksFor, startLinkOf: (key, like) => BaseValue.start(key, linksFor(like)) }
212
+ }
213
+
214
+ /**
215
+ * A Subject's own value for `key` now, as its Environment reads where a key starts (#939): none for a key
216
+ * with no value of its own to start from, which the Environment's reading refuses with an error.
217
+ */
218
+ const ownValueOf: (links: Spine.StartLinks, key: string) => Option.Option<Option.Option<number | string>> = /*#__PURE__*/ Option.liftThrowable(
219
+ (links: Spine.StartLinks, key: string) => links.ownValue(key),
220
+ )
221
+
222
+ /** The Stage wards' gate ids, counted: unique across every gate. */
223
+ // Module state (#460, Q7): an id counter. Not `createUniqueId`, so a scene leaves the page's hydration keys alone.
224
+ let stageIds = 0
225
+
226
+ /** A Subject as the lifecycle's events carry it: its `element`, typed as the page's (`StageEvents` says otherwise). */
227
+ const asElement = (subject: object): WardElement => subject as WardElement
228
+
229
+ /** `given`, a Subject or an accessor of one, as an accessor. */
230
+ const subjectAccessorOf = (given: unknown): Accessor<object | undefined> => {
231
+ if (Predicate.isFunction(given)) return given as Accessor<object | undefined>
232
+ return () => given as object
233
+ }
234
+
235
+ /** A mount's own gate id, its spine's snapshot, and its transitions' reuse. */
236
+ interface Own {
237
+ readonly id: string
238
+ readonly snapshot: Spine.Snapshot
239
+ readonly reuse: ReturnType<typeof Spine.makeReuse>
240
+ }
241
+
242
+ /** What a Scene object's side reads of the ward it's part of: its exit resolves on them. */
243
+ interface WardParts {
244
+ readonly id: string
245
+ readonly cfg: () => WardConfigValues
246
+ readonly inherited: VariantTree.WardicVariantContextValue | undefined
247
+ readonly start: Spine.Start
248
+ readonly lifecycle: Lifecycle
249
+ readonly gate: StageWardEnvironment["gate"]
250
+ readonly registry: ReturnType<typeof Spine.registryOf>
251
+ }
252
+
253
+ /**
254
+ * A Scene object's own part of its ward (#885): made only by {@link stageSceneObject}, so a Stage ward
255
+ * that isn't one carries none of it.
256
+ */
257
+ interface SceneSide {
258
+ /** A mount's own gate id, snapshot and reuse: the last Subject may still be exiting beside the next. */
259
+ readonly own: (begun: Spine.Start) => Own
260
+ /** Its spine's hooks: the ward's lifecycle while it's on its Subject, its exit's landing once it's leaving. */
261
+ readonly hooks: (landingOf: () => (() => void) | undefined) => Spine.AnimationHooks
262
+ /** Its Subject has joined the scene, on gate id `id`. */
263
+ readonly joined: (subject: object, id: string) => void
264
+ /** `m` leaves, through its exit. */
265
+ readonly leave: (m: Mounted) => void
266
+ /**
267
+ * `subject`'s mount, taken back while its exit is in flight: its exit stops where it is, and it stays in
268
+ * the scene. None when it isn't exiting.
269
+ */
270
+ readonly reclaim: (subject: object) => Mounted | undefined
271
+ /** Its owner has ended: its exit waits end once its last exit has landed. */
272
+ readonly ended: () => void
273
+ /** Its value for `key`, on the Subject it's on now. */
274
+ readonly atom: (key: string) => Atom.Atom<unknown>
275
+ /** Its exit events' waits, which outlive its owner until its last exit lands. */
276
+ readonly exits: Waiters<ExitEvents>
277
+ }
278
+
279
+ /**
280
+ * A Scene object's side of its ward, joining and leaving through `membership` (Q5, Q6). Each Subject
281
+ * mounts on a gate id of its own, as one exits beside the next: the last mounted is the one `read` and
282
+ * `atom` follow.
283
+ */
284
+ const sceneSideOf = (parts: WardParts, membership: StageMembership): SceneSide => {
285
+ const { id, cfg, lifecycle, gate, registry } = parts
286
+ let mounts = 0
287
+ // Kept alive: it's set before anything reads it, and a registry drops an Atom no one holds.
288
+ const currentId = Atom.keepAlive(Atom.make(id))
289
+ const exits = new Waiters<ExitEvents>(EXITS_GONE)
290
+ let leaving = 0
291
+ let gone = false
292
+ const endExits = (): void => {
293
+ if (Bool.and(gone, leaving === 0)) exits.dispose()
294
+ }
295
+ // Each Subject whose exit is in flight, with what stops it there: given again, it's taken back.
296
+ const inFlight = new Map<object, { readonly m: Mounted; readonly id: string; readonly stop: () => void }>()
297
+ // Each Subject's gate id, as it joined.
298
+ const ids = new WeakMap<object, string>()
299
+
300
+ /** Its `exit`, as a Retarget's target on `m`'s spine: none when it has no `exit`. */
301
+ const exitOf = (m: Mounted): Spine.Resolution | undefined =>
302
+ untrack(() => {
303
+ const values = cfg()
304
+ const exit = values.exit
305
+ if (Predicate.isUndefined(exit)) return undefined
306
+ const exitConfig = { animate: exit, variants: values.variants, custom: values.custom, transition: values.transition } as Config.SpineConfig
307
+ return Spine.resolutionOf(exitConfig, parts.inherited, parts.start.initialLink, NOTHING_ACTIVE, m.snapshot)
308
+ })
309
+
310
+ /**
311
+ * `m` leaves (Q5, a): its gestures stop, its `exit` runs on its own spine from where it is, at the
312
+ * speed it has, past its owner, then its Subject leaves the scene. The Stage's end lands it at once.
313
+ */
314
+ const leave = (m: Mounted): void => {
315
+ m.quiet()
316
+ leaving++
317
+ exits.tell("exitStart", () => ({ element: m.subject }))
318
+ let landed = false
319
+ let untracked: () => void = constVoid
320
+ const land = (): void => {
321
+ if (landed) return
322
+ landed = true
323
+ inFlight.delete(m.subject)
324
+ untracked()
325
+ m.dispose()
326
+ membership.leave(m.subject)
327
+ exits.tell("exitComplete", () => ({ element: m.subject }))
328
+ leaving--
329
+ endExits()
330
+ }
331
+ const exit = exitOf(m)
332
+ if (Predicate.isUndefined(exit)) return land()
333
+ m.exitWith(land)
334
+ untracked = membership.exiting(land)
335
+ inFlight.set(m.subject, {
336
+ m,
337
+ id: ids.get(m.subject) as string,
338
+ stop: () => {
339
+ landed = true
340
+ untracked()
341
+ leaving--
342
+ },
343
+ })
344
+ untrack(() => m.spine.retarget(exit))
345
+ }
346
+
347
+ // Taken back: its exit stops, and the ward's next Retarget runs from where the exit left it.
348
+ const reclaim = (subject: object): Mounted | undefined =>
349
+ UndefinedOr.map(inFlight.get(subject), (known) => {
350
+ inFlight.delete(subject)
351
+ known.stop()
352
+ known.m.exitWith(undefined)
353
+ known.m.wake()
354
+ registry().set(currentId, known.id)
355
+ return known.m
356
+ })
357
+
358
+ return {
359
+ own: (begun) => {
360
+ mounts++
361
+ return { id: `${id}.${mounts}`, snapshot: Spine.snapshotOf(begun), reuse: Spine.makeReuse() }
362
+ },
363
+ // The ward's lifecycle while it's on its Subject; once it's leaving, its exit's landing, a failed one
364
+ // too (the spine reports the failure).
365
+ hooks: (landingOf) => ({
366
+ started: (resolution) => {
367
+ if (Predicate.isUndefined(landingOf())) lifecycle.started(resolution)
368
+ },
369
+ settled: (resolution, previous) =>
370
+ UndefinedOr.match(landingOf(), {
371
+ onUndefined: () => lifecycle.settled(resolution, previous),
372
+ onDefined: (land) => land(),
373
+ }),
374
+ failed: (resolution, error) =>
375
+ UndefinedOr.match(landingOf(), {
376
+ onUndefined: () => lifecycle.failed(resolution, error),
377
+ onDefined: (land) => land(),
378
+ }),
379
+ }),
380
+ joined: (subject, mountId) => {
381
+ ids.set(subject, mountId)
382
+ registry().set(currentId, mountId)
383
+ // It joins its scene now, the ward able to start: its `initial` is the frame's to write.
384
+ membership.join(subject)
385
+ },
386
+ leave,
387
+ reclaim,
388
+ ended: () => {
389
+ gone = true
390
+ endExits()
391
+ },
392
+ atom: (key) => Atom.make((get) => get(gate.valueOf(get(currentId), key))),
393
+ exits,
394
+ }
395
+ }
396
+
397
+ /**
398
+ * A ward on `given` on `environment`: a Stage's, or, with `sideOf`, a Scene object's, which joins its
399
+ * Subject once it can start and plays its `exit` before its Subject leaves.
400
+ */
401
+ const wardOn = (
402
+ environment: StageWardEnvironment,
403
+ given: unknown,
404
+ config: StageWardConfig,
405
+ sideOf: ((parts: WardParts) => SceneSide) | undefined,
406
+ joinsUnder: ((subject: object) => object | undefined) | undefined,
407
+ ): SceneWard => {
408
+ const scene = Predicate.isNotUndefined(sideOf)
409
+ if (DEV) checkKeys(config, scene)
410
+ const { runtime, gate } = environment
411
+ const owner = getOwner()
412
+ const registry = Spine.registryOf(runtime)
413
+ const id = `wardic-stage-${stageIds++}`
414
+ const subjectOf = subjectAccessorOf(given)
415
+ // Its config per key (#259), as a ward's: a `WardicConfig`'s transition its own when it gives none (#434).
416
+ const view = configuredView(WARD_VIEW.of(config, undefined), useWardicConfig())
417
+ const cfg = (): WardConfigValues => view
418
+ const setup = WARD_VIEW.setupOf(view)
419
+ if (DEV && Predicate.isNotUndefined(AnimationControls.controlsOf(setup.config.animate))) throw new Error(NO_CONTROLS)
420
+ const initialConfig = configAtSetup(setup)
421
+ // Variants down a scene's graph (decision 17): a Variant child follows the nearest ward above its
422
+ // Subject. A value Subject finds it now, from the parent it will join, so it registers for its stagger
423
+ // slot before any Retarget reads one, as a ward on the page registers in its component's body.
424
+ const graph = environment.graph
425
+ const follower = UndefinedOr.match(graph, {
426
+ onUndefined: (): GraphParent | undefined => undefined,
427
+ onDefined: (on) => Bool.match(VariantTree.isVariantChild(initialConfig), { onTrue: (): GraphParent | undefined => new GraphParent(on, joinsUnder), onFalse: constUndefined }),
428
+ })
429
+ const node = createVariantNode(cfg, initialConfig, follower)
430
+ UndefinedOr.map(follower, (f) => {
431
+ if (Bool.not(Predicate.isFunction(given))) f.early(given as object)
432
+ })
433
+ // A ward with variants of its own provides them to the wards below its Subject, a value's from now.
434
+ const provides = Bool.and(Predicate.isNotUndefined(graph), VariantTree.hasOwnVariants(initialConfig))
435
+ let providing: { readonly subject: object; readonly stop: () => void } | undefined
436
+ const stopProviding = (): void => {
437
+ UndefinedOr.map(providing, (p) => p.stop())
438
+ providing = undefined
439
+ }
440
+ const provideOn = (subject: object): void => {
441
+ if (Bool.not(provides)) return
442
+ if (UndefinedOr.match(providing, { onUndefined: () => false, onDefined: (p) => p.subject === subject })) return
443
+ stopProviding()
444
+ providing = { subject, stop: provide(graph as StageGraph, subject, node.provided as VariantTree.WardicVariantContextValue) }
445
+ }
446
+ if (Bool.not(Predicate.isFunction(given))) provideOn(given as object)
447
+ if (Bool.and(Arr.contains(setup.pending, "animate"), initialConfig.initial === false)) throw new Error(STARTS_AT_PENDING)
448
+ const start = Spine.startOf(initialConfig, node.inherited, false, untrack)
449
+ const snapshot = Spine.snapshotOf(start)
450
+ const reuse = Spine.makeReuse()
451
+ // Where its Subject starts: the ward's start, or a fresh one for the next Subject an accessor gives.
452
+ // A Scene object's next Subject gets a snapshot of its own, since the last may still be exiting.
453
+ let begun = start
454
+ const beginFresh = (): void => {
455
+ begun = Spine.startOf(WARD_VIEW.setupOf(view).config, node.inherited, false, untrack)
456
+ if (scene) return
457
+ snapshot.values = { ...begun.initialValues }
458
+ snapshot.velocities = {}
459
+ snapshot.drawnAt = {}
460
+ }
461
+
462
+ const [gestureState, setGestureState] = createSignal<Gestures>(NO_GESTURES)
463
+ const setGesture = (subject: object, layer: "hovered" | "pressed" | "dragged", on: boolean): void => {
464
+ setGestureState((g) => {
465
+ if (g.subject !== subject) return Bool.match(on, { onTrue: (): Gestures => ({ ...NO_GESTURES, subject, [layer]: true }), onFalse: () => g })
466
+ if (g[layer] === on) return g
467
+ return { ...g, [layer]: on }
468
+ })
469
+ }
470
+ const gestures = (): Gestures => {
471
+ const g = gestureState()
472
+ if (g === NO_GESTURES) return g
473
+ if (g.subject === subjectOf()) return g
474
+ return NO_GESTURES
475
+ }
476
+ const wantsHover = (): boolean => Predicate.isNotUndefined(cfg().whileHover)
477
+ const wantsPress = (): boolean => Predicate.isNotUndefined(cfg().whileTap)
478
+ const wantsDrag = (): boolean => {
479
+ const drag = cfg().drag
480
+ return Bool.and(Predicate.isNotUndefined(drag), drag !== false)
481
+ }
482
+ if (DEV && Bool.and(Predicate.isUndefined(environment.drag), untrack(wantsDrag))) throw new Error(NO_DRAG)
483
+
484
+ const target = createMemo<Spine.Resolution>(
485
+ () => {
486
+ const g = gestures()
487
+ // `dragging` stays off: a scene's drag holds its keys as the Stage's `animate` does (#433), `z` among them.
488
+ return Spine.resolutionOf(cfg(), node.inherited, start.initialLink, { inView: false, focused: false, hovered: g.hovered, pressed: g.pressed, dragged: g.dragged, dragging: false }, snapshot, undefined)
489
+ },
490
+ { equals: sameTarget, transparent: true },
491
+ )
492
+
493
+ let mounted: Mounted | undefined
494
+ const lifecycle = new Lifecycle(owner, () => {
495
+ UndefinedOr.map(mounted, startRecognizers)
496
+ })
497
+ const side = UndefinedOr.map(sideOf, (of) => of({ id, cfg, inherited: node.inherited, start, lifecycle, gate, registry }))
498
+ /** A handler's throw, reported as a fiber's failure is (ADR-0030, decision 4); the watch goes on. */
499
+ const onDefect = (cause: Cause.Cause<never>): void => {
500
+ runtime.runFork(Report.failure("wardic.ward: a gesture handler failed", cause))
501
+ }
502
+ /** Its hover and press recognizers, each once a Subject, as its config or a `next` wants them. */
503
+ const startRecognizers = (m: Mounted): void => {
504
+ if (Bool.and(Bool.or(untrack(wantsHover), lifecycle.wantsHover), Bool.not(m.hover))) {
505
+ m.hover = true
506
+ m.watch(
507
+ Recognizer.watchHover(m.subject, {
508
+ on: (edge) => {
509
+ setGesture(m.subject, "hovered", edge.active)
510
+ lifecycle.hovered(edge, asElement(m.subject))
511
+ },
512
+ onMove: (event) => lifecycle.hoverMoved(event, asElement(m.subject)),
513
+ onDefect,
514
+ }),
515
+ )
516
+ }
517
+ if (Bool.and(Bool.or(untrack(wantsPress), lifecycle.wantsPress), Bool.not(m.press))) {
518
+ m.press = true
519
+ m.watch(
520
+ Recognizer.watchPress(
521
+ m.subject,
522
+ {
523
+ on: (press) => {
524
+ setGesture(m.subject, "pressed", press.phase === "start")
525
+ lifecycle.pressed(press, asElement(m.subject))
526
+ },
527
+ onDefect,
528
+ },
529
+ // A scene's object takes no keys: it has no focus.
530
+ { keys: false },
531
+ ),
532
+ )
533
+ }
534
+ // Its drag, its Stage's (#885's PR 4): each sample told as a ward's, `whileDrag` on from its start to its end.
535
+ if (Bool.and(untrack(wantsDrag), Bool.not(m.drag))) {
536
+ UndefinedOr.map(environment.drag, (drags) => {
537
+ m.drag = true
538
+ m.watch(
539
+ drags.watch(m.subject, () => untrack(() => dragConfigOf(config)), {
540
+ sample: (sample) => {
541
+ if (sample.phase === "start") setGesture(m.subject, "dragged", true)
542
+ lifecycle.dragged(sample, false, asElement(m.subject))
543
+ if (sample.phase === "end") setGesture(m.subject, "dragged", false)
544
+ },
545
+ released: () => lifecycle.released(),
546
+ onDefect,
547
+ }),
548
+ )
549
+ })
550
+ }
551
+ }
552
+
553
+ const mount = (subject: object): Mounted => {
554
+ const own = UndefinedOr.match(side, { onUndefined: (): Own => ({ id, snapshot, reuse }), onDefined: (s) => s.own(begun) })
555
+ const mountId = own.id
556
+ const unregister = gate.register(mountId, { target: subject })
557
+ // Its `initial`, written before its first frame is drawn (ADR-0070 decision 5).
558
+ if (Bool.not(Rec.isEmptyRecord(begun.initialValues))) gate.write(mountId, begun.initialValues)
559
+ let exiting: (() => void) | undefined
560
+ const spine = Spine.makeElementSpine({
561
+ runtime,
562
+ gate,
563
+ id: mountId,
564
+ element: subject,
565
+ tag: undefined,
566
+ config: cfg,
567
+ parent: node.inherited,
568
+ canStart: node.canStart,
569
+ extraDelay: node.extraDelay,
570
+ byName: node.names,
571
+ onFrame: node.onFrame,
572
+ reducedMotion: environment.reducedMotion,
573
+ skipAnimations: environment.skipAnimations,
574
+ positionalKeys: environment.positionalKeys,
575
+ defaultTransition: environment.defaultTransition,
576
+ ownValues: environment.ownValues,
577
+ measuredPairs: environment.measuredPairs,
578
+ variableValues: environment.variableValues,
579
+ start: begun,
580
+ snapshot: own.snapshot,
581
+ reuse: own.reuse,
582
+ links: baseHeld(environment.startLinksOf(subject), environment.ownValues),
583
+ styleNow: undefined,
584
+ live: undefined,
585
+ presence: undefined,
586
+ projected: false,
587
+ dragAxis: undefined,
588
+ handoff: undefined,
589
+ animation: UndefinedOr.match(side, { onUndefined: (): Spine.AnimationHooks => lifecycle, onDefined: (s) => s.hooks(() => exiting) }),
590
+ })
591
+ // The Stage's `animate` on its Subject goes through its spine while it's on it (#433).
592
+ let unregisterAnimatable: () => void = Imperative.registerAnimatable(subject, spine)
593
+ // Its recognizers' Scope, made at the first: a ward with no gesture makes none.
594
+ let watchScope: Scope.Closeable | undefined
595
+ const watch = (made: Effect.Effect<void, never, Scope.Scope | InputSource.InputSource>): void => {
596
+ if (Predicate.isUndefined(watchScope)) watchScope = OwnerScope.open(runtime)
597
+ runtime.runSync(made.pipe(Effect.provideService(InputSource.InputSource, environment.input), Scope.provide(watchScope)))
598
+ }
599
+ const quiet = (): void => {
600
+ unregisterAnimatable()
601
+ unregisterAnimatable = constVoid
602
+ UndefinedOr.map(watchScope, (scope) => OwnerScope.close(runtime, scope, "wardic.ward: a finalizer failed"))
603
+ watchScope = undefined
604
+ }
605
+ const m: Mounted = {
606
+ subject,
607
+ spine,
608
+ snapshot: own.snapshot,
609
+ hover: false,
610
+ press: false,
611
+ drag: false,
612
+ exitWith: (land) => {
613
+ exiting = land
614
+ },
615
+ watch,
616
+ quiet,
617
+ wake: () => {
618
+ unregisterAnimatable = Imperative.registerAnimatable(subject, spine)
619
+ m.hover = false
620
+ m.press = false
621
+ m.drag = false
622
+ },
623
+ dispose: () => {
624
+ quiet()
625
+ spine.dispose()
626
+ unregister()
627
+ },
628
+ }
629
+ startRecognizers(m)
630
+ UndefinedOr.map(side, (s) => s.joined(subject, mountId))
631
+ return m
632
+ }
633
+
634
+ /** Off its Subject: a Stage ward's ends now, a Scene object's leaves through its exit. */
635
+ const off = (): void => {
636
+ const m = mounted
637
+ mounted = undefined
638
+ if (Predicate.isUndefined(m)) return
639
+ UndefinedOr.match(side, {
640
+ onUndefined: () => m.dispose(),
641
+ onDefined: (s) => s.leave(m),
642
+ })
643
+ }
644
+ onCleanup(() => {
645
+ off()
646
+ lifecycle.dispose()
647
+ UndefinedOr.map(side, (s) => s.ended())
648
+ UndefinedOr.map(follower, (f) => f.release())
649
+ stopProviding()
650
+ })
651
+
652
+ // A `whileHover` or `whileTap` set later starts its recognizer then (#87).
653
+ createEffect(
654
+ () => Bool.some([wantsHover(), wantsPress(), wantsDrag()]),
655
+ () => {
656
+ UndefinedOr.map(mounted, startRecognizers)
657
+ },
658
+ CLIENT_ONLY_SYNC_DEFERRED,
659
+ )
660
+
661
+ // Its keys still loading at setup, and its Subject, read where `held` renders: its component's
662
+ // `<Loading>` holds until they land, as a ward's style does (#257 item 1).
663
+ const loaded = Bool.match(setup.pending.length === 0, {
664
+ onTrue: () => constVoid,
665
+ onFalse: () => {
666
+ const config = cfg() as unknown as Readonly<Record<string, unknown>>
667
+ const memo = createMemo((): true => {
668
+ Arr.forEach(setup.pending, (key) => void config[key])
669
+ target()
670
+ return true
671
+ }, CLIENT_ONLY)
672
+ return (): void => {
673
+ memo()
674
+ }
675
+ },
676
+ })
677
+
678
+ // Mount on its Subject, then Retarget on each change of its target or its `when` gate; a new Subject
679
+ // takes the ward over, starting as the ward did, and the last keeps what it shows (decision 10).
680
+ // A Scene object joins once every key still loading at setup has landed, not only its target's
681
+ // (*Amendment (#881)*, item 2): read in the compute, so the effect waits for them.
682
+ const open = gateOf(node)
683
+ let mountedOnce = false
684
+ createEffect(
685
+ () => {
686
+ const subject = subjectOf()
687
+ if (Predicate.isUndefined(subject)) return undefined
688
+ if (scene) loaded()
689
+ return { subject, resolution: target(), open: open() }
690
+ },
691
+ (mounting) => {
692
+ if (Predicate.isUndefined(mounting)) {
693
+ off()
694
+ return
695
+ }
696
+ const { subject, resolution } = mounting
697
+ if (Predicate.isUndefined(mounted) || mounted.subject !== subject) {
698
+ // A Stage ward's next Subject starts as the ward did; a Scene object's each enters afresh.
699
+ const fresh = Bool.match(scene, { onTrue: () => mountedOnce, onFalse: () => Predicate.isNotUndefined(mounted) })
700
+ off()
701
+ // On the graph: it follows the ward above this Subject, and provides for the ones below it. A
702
+ // follower starts from the `initial` that ward hands down.
703
+ UndefinedOr.map(follower, (f) => f.attach(subject))
704
+ provideOn(subject)
705
+ // A Scene object's Subject still exiting is taken back, from where it is; any other mounts.
706
+ mounted = UndefinedOr.match(
707
+ UndefinedOr.map(side, (s) => s.reclaim(subject)),
708
+ {
709
+ onDefined: (taken) => {
710
+ startRecognizers(taken)
711
+ return taken
712
+ },
713
+ onUndefined: () => {
714
+ if (Bool.or(fresh, Predicate.isNotUndefined(follower))) beginFresh()
715
+ return untrack(() => mount(subject))
716
+ },
717
+ },
718
+ )
719
+ mountedOnce = true
720
+ }
721
+ const spine = mounted.spine
722
+ untrack(() => spine.retarget(resolution))
723
+ },
724
+ CLIENT_ONLY_SYNC,
725
+ )
726
+
727
+ const held = (() => {
728
+ subjectOf()
729
+ loaded()
730
+ return undefined
731
+ }) as unknown as JSX.Element
732
+
733
+ // Its value per key: the gate's output while it holds the key, else its start, else its Subject's own
734
+ // value now (#939), so a key the ward hasn't moved reads what the Subject holds. One accessor a key.
735
+ const reads = new Map<string, Accessor<unknown>>()
736
+ // Its Subject's own values now (#939): one reading of each Subject it's on, for the keys it hasn't moved.
737
+ let reading: { readonly subject: object; readonly links: Spine.StartLinks } | undefined
738
+ const ownNow = (key: string): number | string | undefined =>
739
+ UndefinedOr.match(subjectOf(), {
740
+ onUndefined: constUndefined,
741
+ onDefined: (subject) => {
742
+ if (Predicate.isUndefined(reading) || reading.subject !== subject) reading = { subject, links: environment.startLinksOf(subject) }
743
+ return Option.getOrUndefined(Option.flatten(ownValueOf(reading.links, key)))
744
+ },
745
+ })
746
+ // A Scene object's follows the gate id of the Subject it's on now.
747
+ const atom = (key: string): Atom.Atom<unknown> => UndefinedOr.match(side, { onUndefined: () => gate.valueOf(id, key), onDefined: (s) => s.atom(key) })
748
+ const read = (key: string): Accessor<unknown> => {
749
+ const known = reads.get(key)
750
+ if (Predicate.isNotUndefined(known)) return known
751
+ const output = atom(key)
752
+ const started = Atom.make((get) => UndefinedOr.match(get(output), { onUndefined: () => start.initialValues[key], onDefined: identity }))
753
+ const followed = runWithOwner(owner, () => followAtom(registry(), started))
754
+ const made = (): unknown => UndefinedOr.match(followed(), { onDefined: identity, onUndefined: () => ownNow(key) })
755
+ reads.set(key, made)
756
+ return made
757
+ }
758
+
759
+ // A Scene object's exit events are its own waits', which outlive its owner until its last exit lands.
760
+ const exitsFor = (name: string): Waiters<ExitEvents> | undefined => {
761
+ if (Bool.not(isExitEvent(name))) return undefined
762
+ return UndefinedOr.map(side, (s) => s.exits)
763
+ }
764
+ return {
765
+ held,
766
+ settled: () => lifecycle.read(),
767
+ tapped: () => lifecycle.tapped(),
768
+ next: (name) => UndefinedOr.match(exitsFor(name), { onUndefined: () => lifecycle.wait(name as never), onDefined: (exits) => exits.wait(name as "exitStart") }),
769
+ each: (name) =>
770
+ UndefinedOr.match(exitsFor(name), { onUndefined: () => lifecycle.each(name as never), onDefined: (exits) => eachOf<ExitEvents, "exitStart">(exits, name as "exitStart") }),
771
+ when: (name) =>
772
+ UndefinedOr.match(exitsFor(name), { onUndefined: () => lifecycle.when(name as never), onDefined: (exits) => whenOf<ExitEvents, "exitStart">(exits, name as "exitStart") }),
773
+ read: read as SceneWard["read"],
774
+ atom,
775
+ } as SceneWard
776
+ }
777
+
778
+ /**
779
+ * The `ward` a Stage on `environment` hands out: `ward(subject, config, options?)` under a `<WardicStage>`
780
+ * comes here, in the component that called it.
781
+ */
782
+ export const stageWard =
783
+ (environment: StageWardEnvironment): Stage["ward"] =>
784
+ (given, config): StageWard =>
785
+ wardOn(environment, given, config, undefined, undefined) as unknown as StageWard
786
+
787
+ /**
788
+ * A Scene object's ward on `environment` (#885): `stageWard`'s, joining its Subject through `membership`
789
+ * once it can start and playing its `exit` before its Subject leaves. An Environment's
790
+ * `createSceneObject` calls it, with its scene's own membership.
791
+ */
792
+ export const stageSceneObject =
793
+ (environment: StageWardEnvironment) =>
794
+ (given: unknown, config: StageWardConfig, membership: StageMembership): SceneWard =>
795
+ wardOn(environment, given, config, (parts) => sceneSideOf(parts, membership), membership.parentOf)