@wardic/foldkit 0.0.0-stage → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +59 -2
- package/THIRD_PARTY_NOTICES.md +145 -0
- package/dist/AnimationControls.d.ts +37 -0
- package/dist/AnimationControls.d.ts.map +1 -0
- package/dist/AnimationControls.js +37 -0
- package/dist/AnimationControls.js.map +1 -0
- package/dist/AnimationScope.d.ts +20 -0
- package/dist/AnimationScope.d.ts.map +1 -0
- package/dist/AnimationScope.js +20 -0
- package/dist/AnimationScope.js.map +1 -0
- package/dist/DragControls.d.ts +25 -0
- package/dist/DragControls.d.ts.map +1 -0
- package/dist/DragControls.js +21 -0
- package/dist/DragControls.js.map +1 -0
- package/dist/Features.d.ts +36 -0
- package/dist/Features.d.ts.map +1 -0
- package/dist/Features.js +44 -0
- package/dist/Features.js.map +1 -0
- package/dist/Ward.d.ts +301 -0
- package/dist/Ward.d.ts.map +1 -0
- package/dist/Ward.js +6 -0
- package/dist/Ward.js.map +1 -0
- package/dist/Wardic.d.ts +363 -0
- package/dist/Wardic.d.ts.map +1 -0
- package/dist/Wardic.js +85 -0
- package/dist/Wardic.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/builder.d.ts +8 -0
- package/dist/internal/builder.d.ts.map +1 -0
- package/dist/internal/builder.js +242 -0
- package/dist/internal/builder.js.map +1 -0
- package/dist/internal/commands.d.ts +143 -0
- package/dist/internal/commands.d.ts.map +1 -0
- package/dist/internal/commands.js +242 -0
- package/dist/internal/commands.js.map +1 -0
- package/dist/internal/delivery.d.ts +44 -0
- package/dist/internal/delivery.d.ts.map +1 -0
- package/dist/internal/delivery.js +85 -0
- package/dist/internal/delivery.js.map +1 -0
- package/dist/internal/dev.d.ts +10 -0
- package/dist/internal/dev.d.ts.map +1 -0
- package/dist/internal/dev.js +10 -0
- package/dist/internal/dev.js.map +1 -0
- package/dist/internal/dev.production.d.ts +8 -0
- package/dist/internal/dev.production.d.ts.map +1 -0
- package/dist/internal/dev.production.js +8 -0
- package/dist/internal/dev.production.js.map +1 -0
- package/dist/internal/drag.d.ts +66 -0
- package/dist/internal/drag.d.ts.map +1 -0
- package/dist/internal/drag.js +71 -0
- package/dist/internal/drag.js.map +1 -0
- package/dist/internal/element.d.ts +59 -0
- package/dist/internal/element.d.ts.map +1 -0
- package/dist/internal/element.js +582 -0
- package/dist/internal/element.js.map +1 -0
- package/dist/internal/gestures.d.ts +62 -0
- package/dist/internal/gestures.d.ts.map +1 -0
- package/dist/internal/gestures.js +109 -0
- package/dist/internal/gestures.js.map +1 -0
- package/dist/internal/hydrate.d.ts +5 -0
- package/dist/internal/hydrate.d.ts.map +1 -0
- package/dist/internal/hydrate.js +27 -0
- package/dist/internal/hydrate.js.map +1 -0
- package/dist/internal/keys.d.ts +21 -0
- package/dist/internal/keys.d.ts.map +1 -0
- package/dist/internal/keys.js +12 -0
- package/dist/internal/keys.js.map +1 -0
- package/dist/internal/layout.d.ts +29 -0
- package/dist/internal/layout.d.ts.map +1 -0
- package/dist/internal/layout.js +88 -0
- package/dist/internal/layout.js.map +1 -0
- package/dist/internal/mount.d.ts +22 -0
- package/dist/internal/mount.d.ts.map +1 -0
- package/dist/internal/mount.js +152 -0
- package/dist/internal/mount.js.map +1 -0
- package/dist/internal/presence.d.ts +33 -0
- package/dist/internal/presence.d.ts.map +1 -0
- package/dist/internal/presence.js +338 -0
- package/dist/internal/presence.js.map +1 -0
- package/dist/internal/reactivity.d.ts +15 -0
- package/dist/internal/reactivity.d.ts.map +1 -0
- package/dist/internal/reactivity.js +26 -0
- package/dist/internal/reactivity.js.map +1 -0
- package/dist/internal/reorder.d.ts +47 -0
- package/dist/internal/reorder.d.ts.map +1 -0
- package/dist/internal/reorder.js +250 -0
- package/dist/internal/reorder.js.map +1 -0
- package/dist/internal/scope.d.ts +28 -0
- package/dist/internal/scope.d.ts.map +1 -0
- package/dist/internal/scope.js +44 -0
- package/dist/internal/scope.js.map +1 -0
- package/dist/internal/server.d.ts +13 -0
- package/dist/internal/server.d.ts.map +1 -0
- package/dist/internal/server.js +13 -0
- package/dist/internal/server.js.map +1 -0
- package/dist/internal/server.node.d.ts +8 -0
- package/dist/internal/server.node.d.ts.map +1 -0
- package/dist/internal/server.node.js +8 -0
- package/dist/internal/server.node.js.map +1 -0
- package/dist/internal/services.d.ts +231 -0
- package/dist/internal/services.d.ts.map +1 -0
- package/dist/internal/services.js +350 -0
- package/dist/internal/services.js.map +1 -0
- package/dist/internal/tree.d.ts +71 -0
- package/dist/internal/tree.d.ts.map +1 -0
- package/dist/internal/tree.js +207 -0
- package/dist/internal/tree.js.map +1 -0
- package/dist/internal/variant-node.d.ts +62 -0
- package/dist/internal/variant-node.d.ts.map +1 -0
- package/dist/internal/variant-node.js +279 -0
- package/dist/internal/variant-node.js.map +1 -0
- package/dist/internal/view-transition.d.ts +10 -0
- package/dist/internal/view-transition.d.ts.map +1 -0
- package/dist/internal/view-transition.js +36 -0
- package/dist/internal/view-transition.js.map +1 -0
- package/dist/internal/walk.d.ts +26 -0
- package/dist/internal/walk.d.ts.map +1 -0
- package/dist/internal/walk.js +162 -0
- package/dist/internal/walk.js.map +1 -0
- package/package.json +99 -4
- package/src/AnimationControls.ts +46 -0
- package/src/AnimationScope.ts +19 -0
- package/src/DragControls.ts +27 -0
- package/src/Features.ts +45 -0
- package/src/Ward.ts +434 -0
- package/src/Wardic.ts +410 -0
- package/src/index.ts +20 -0
- package/src/internal/builder.ts +262 -0
- package/src/internal/commands.ts +359 -0
- package/src/internal/delivery.ts +116 -0
- package/src/internal/dev.production.ts +7 -0
- package/src/internal/dev.ts +9 -0
- package/src/internal/drag.ts +136 -0
- package/src/internal/element.ts +668 -0
- package/src/internal/gestures.ts +184 -0
- package/src/internal/hydrate.ts +29 -0
- package/src/internal/keys.ts +19 -0
- package/src/internal/layout.ts +109 -0
- package/src/internal/mount.ts +181 -0
- package/src/internal/presence.ts +362 -0
- package/src/internal/reactivity.ts +45 -0
- package/src/internal/reorder.ts +313 -0
- package/src/internal/scope.ts +60 -0
- package/src/internal/server.node.ts +7 -0
- package/src/internal/server.ts +12 -0
- package/src/internal/services.ts +508 -0
- package/src/internal/tree.ts +230 -0
- package/src/internal/variant-node.ts +352 -0
- package/src/internal/view-transition.ts +43 -0
- package/src/internal/walk.ts +211 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Presence on Foldkit (ADR-0071 decision 29; *Amendment (#791)* at slice 2): `w.Presence(options)` on
|
|
3
|
+
* the leavers' parent. Its element's Mount runs dom's presence controller over one `MutationObserver`
|
|
4
|
+
* on the element's child list. Each delivery, a microtask after Foldkit's patch (P8), or in the patch's
|
|
5
|
+
* task when a `w.*` element's Mount flushes it (#840), puts back each known child the patch took out,
|
|
6
|
+
* where it was; asks the controller what goes on the page (`shown`: under `mode="wait"` an entering
|
|
7
|
+
* child is held, hidden with `display: none`, until the last leaver is gone, P23); tells the controller
|
|
8
|
+
* what's present, which retains each leaver at Motion's place, a `popLayout` one pinned where the
|
|
9
|
+
* binding's snapshot measured it (#840), and plays its exit (#675's placement is P19's); and then starts
|
|
10
|
+
* the children mounted since the last delivery. A child under a presence starts in that delivery, not
|
|
11
|
+
* in its own Mount: take-overs (#286) and holds are decided in `shown`, after Foldkit has mounted the
|
|
12
|
+
* child and before anything of it animates.
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
import * as PresenceRecord from "@wardic/core/unstable/PresenceRecord"
|
|
17
|
+
import type * as PresenceController from "@wardic/dom/unstable/PresenceController"
|
|
18
|
+
import * as Keys from "@wardic/dom/unstable/features/Keys"
|
|
19
|
+
import * as Arr from "effect/Array"
|
|
20
|
+
import * as Bool from "effect/Boolean"
|
|
21
|
+
import { constFalse, constTrue, constVoid, identity } from "effect/Function"
|
|
22
|
+
import * as Iter from "effect/Iterable"
|
|
23
|
+
import * as MutableRef from "effect/MutableRef"
|
|
24
|
+
import * as Option from "effect/Option"
|
|
25
|
+
import * as Predicate from "effect/Predicate"
|
|
26
|
+
import * as UndefinedOr from "effect/UndefinedOr"
|
|
27
|
+
import type * as Ward from "../Ward.ts"
|
|
28
|
+
import { deliveredTo, hintFrameUnless } from "./delivery.ts"
|
|
29
|
+
import { configOf, dispatcherOf } from "./element.ts"
|
|
30
|
+
import { PresenceMaker, type PresenceMakerShape } from "./keys.ts"
|
|
31
|
+
import { quietLayoutIn } from "./layout.ts"
|
|
32
|
+
import { featureIn, type LivePresence, type Provider, snapshotSpent, type State } from "./services.ts"
|
|
33
|
+
import { isMounted, nearestPresenceElement, releaseWaiting, type Waiting } from "./tree.ts"
|
|
34
|
+
import { DEV } from "@wardic/foldkit/internal/dev"
|
|
35
|
+
|
|
36
|
+
/** A child's start, deferred to its presence's next delivery: begun there, or once shown under `wait`; released by its Mount's destroy (a `Waiting` start, `tree.ts`, with its hold). */
|
|
37
|
+
interface Pending extends Waiting {
|
|
38
|
+
/** Cancels its hold, while `wait` holds it. */
|
|
39
|
+
cancel: (() => void) | undefined
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A `w.Presence` element's presence: what its children register with, and what its own Mount runs. */
|
|
43
|
+
export interface Presence {
|
|
44
|
+
/** The controller's context: what a child's spine registers its record with. */
|
|
45
|
+
readonly context: PresenceController.PresenceContextValue
|
|
46
|
+
/** A child's start, enrolled for the next delivery; returns the child's release. */
|
|
47
|
+
readonly enroll: (element: Element, begin: () => () => void) => () => void
|
|
48
|
+
/** The presence element's own Mount runs it: the observer, the first delivery, the Messages; returns the release. */
|
|
49
|
+
readonly mount: (paused: () => boolean, offer: (message: unknown) => void) => () => void
|
|
50
|
+
/** Whether it is a `w.Presence({ initial: false })` ({@link holdsInitial}), as its options stand now. */
|
|
51
|
+
readonly holds: () => boolean
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Module state (#460, Q7): each presence element's presence, held weakly by the element.
|
|
55
|
+
const presences = new WeakMap<Element, Presence>()
|
|
56
|
+
|
|
57
|
+
/** Development's word on `w.Presence` without its Feature (#792, Q9). */
|
|
58
|
+
const MISSING = 'wardic: w.Presence needs the `presence` Feature, which this Handle doesn\'t have. Add it: `import { Features } from "@wardic/foldkit"`, then `Wardic.make({ env, features: [Features.presence] })`.'
|
|
59
|
+
|
|
60
|
+
/** Development's word on `w.Presence` given to an element after its first render: its Mount is made once, so the presence never runs. */
|
|
61
|
+
const LATE = "wardic: w.Presence was added to an element already mounted, whose Mount runs without it: its children animate with no presence. Give w.Presence on the element's first render, or key the element so it's made again."
|
|
62
|
+
|
|
63
|
+
/** No options: `w.Presence()`. */
|
|
64
|
+
const NO_OPTIONS: Ward.PresenceOptions<unknown> = {}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether a `w.Presence` with `options` holds the children of its first render at their `Animate`
|
|
68
|
+
* (`initial: false`, #134): what the walk at a provider element's build writes for them (#794), and what
|
|
69
|
+
* their Mounts start them at while that render is the presence's first (dom's `blocksInitial`).
|
|
70
|
+
*/
|
|
71
|
+
export const holdsInitial = (options: Ward.PresenceOptions<unknown> | undefined): boolean => UndefinedOr.match(options, { onUndefined: constFalse, onDefined: (given) => given.initial === false })
|
|
72
|
+
|
|
73
|
+
/** Whether a node is an element: what a presence's children and leavers are (text is never one). */
|
|
74
|
+
const isElement = (node: unknown): node is Element => node instanceof Element
|
|
75
|
+
|
|
76
|
+
/** An element with an inline style: HTML, SVG and MathML elements, which `wait` hides through it. */
|
|
77
|
+
type Styled = Element & ElementCSSInlineStyle
|
|
78
|
+
|
|
79
|
+
/** Whether an element has an inline style to hide it through. */
|
|
80
|
+
const isStyled = (node: unknown): node is Styled => Bool.and(isElement(node), Predicate.hasProperty(node, "style"))
|
|
81
|
+
|
|
82
|
+
/** The elements of what the controller's `shown` returns: a node, a flat list, or none. */
|
|
83
|
+
const elementsOf = (shown: unknown): ReadonlyArray<Element> => Arr.filter(Arr.ensure(shown), isElement)
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The presence of the nearest `w.Presence` above `element`, made at the first ask (a child's Mount, or
|
|
87
|
+
* the presence element's own); none with no presence above, or when the Handle lacks the Feature.
|
|
88
|
+
*/
|
|
89
|
+
export const nearestPresence = (provider: Provider, state: State, element: Element): Presence | undefined =>
|
|
90
|
+
UndefinedOr.match(nearestPresenceElement(element), { onUndefined: () => undefined, onDefined: (owner) => presenceOf(provider, state, owner, false) })
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* `element`'s presence, it carrying `w.Presence`: the known one, else made now, by its own Mount (`own`)
|
|
94
|
+
* or by a child's first. None without the Feature, warned once; and none when a child asks for one on an
|
|
95
|
+
* element whose Mount has already run without it (a later render's `w.Presence`), warned once in
|
|
96
|
+
* development: the child then starts as it would with no presence above.
|
|
97
|
+
*/
|
|
98
|
+
const presenceOf = (provider: Provider, state: State, element: Element, own: boolean): Presence | undefined => {
|
|
99
|
+
const known = presences.get(element)
|
|
100
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
101
|
+
// Its Feature may be on its way (#793, Q15): nothing said, and the element's own Mount makes it as it arrives.
|
|
102
|
+
const pending = provider.features.pending()
|
|
103
|
+
if (Bool.and(Bool.not(own), isMounted(element))) {
|
|
104
|
+
if (DEV && Bool.not(pending)) provider.registry.warnOnce("presence-late", LATE)
|
|
105
|
+
return undefined
|
|
106
|
+
}
|
|
107
|
+
const maker = featureIn(provider, PresenceMaker)
|
|
108
|
+
if (Predicate.isUndefined(maker)) {
|
|
109
|
+
if (Bool.not(pending)) provider.registry.warnOnce("presence", MISSING)
|
|
110
|
+
return undefined
|
|
111
|
+
}
|
|
112
|
+
const made = makePresence(provider, state, maker, element)
|
|
113
|
+
presences.set(element, made)
|
|
114
|
+
return made
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Run the presence of `element` when it carries `w.Presence` (its own Mount, after `mountElement`):
|
|
119
|
+
* nothing for an element without one. With its Feature on its way in a load (#793, Q15), made and run
|
|
120
|
+
* as it arrives: the children mounted before then started with no presence, those after enroll.
|
|
121
|
+
* Returns the release.
|
|
122
|
+
*/
|
|
123
|
+
export const mountPresence = (provider: Provider, state: State, element: Element, paused: () => boolean, offer: (message: unknown) => void): (() => void) => {
|
|
124
|
+
if (Predicate.isUndefined(configOf(deliveredTo(element)).presence)) return constVoid
|
|
125
|
+
const made = presenceOf(provider, state, element, true)
|
|
126
|
+
if (Predicate.isNotUndefined(made)) return made.mount(paused, offer)
|
|
127
|
+
if (Bool.not(provider.features.pending())) return constVoid
|
|
128
|
+
const release = MutableRef.make<() => void>(constVoid)
|
|
129
|
+
const stopRechecks = provider.onFeatures(() => {
|
|
130
|
+
UndefinedOr.map(presenceOf(provider, state, element, true), (presence) => {
|
|
131
|
+
MutableRef.set(release, presence.mount(paused, offer))
|
|
132
|
+
stopRechecks()
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
return () => {
|
|
136
|
+
stopRechecks()
|
|
137
|
+
MutableRef.get(release)()
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Put `node` back where Foldkit took it from (P8): before the node after it, if still a child of `parent`; else after the one before; else at the end. */
|
|
142
|
+
const placeBack = (parent: Element, node: Element, record: MutationRecord): void => {
|
|
143
|
+
const next = record.nextSibling
|
|
144
|
+
if (Predicate.isNotNull(next) && next.parentNode === parent) {
|
|
145
|
+
parent.insertBefore(node, next)
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
const previous = record.previousSibling
|
|
149
|
+
if (Predicate.isNotNull(previous) && previous.parentNode === parent) {
|
|
150
|
+
parent.insertBefore(node, previous.nextSibling)
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
parent.appendChild(node)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** What a delivery's records say Foldkit took out: the change's leavers, put back, and the held children dropped. */
|
|
157
|
+
interface TakenOut {
|
|
158
|
+
readonly leavers: ReadonlySet<Element>
|
|
159
|
+
readonly dropped: number
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The presence of one `w.Presence` element (above). */
|
|
163
|
+
const makePresence = (provider: Provider, state: State, maker: PresenceMakerShape, element: Element): Presence => {
|
|
164
|
+
const optionsNow = (): Ward.PresenceOptions<unknown> => UndefinedOr.match(configOf(deliveredTo(element)).presence, { onUndefined: () => NO_OPTIONS, onDefined: identity })
|
|
165
|
+
// What its Mount gives it when it runs, after its children's Mounts: until then, no Message leaves.
|
|
166
|
+
let paused: () => boolean = constTrue
|
|
167
|
+
let offer: (message: unknown) => void = constVoid
|
|
168
|
+
const pending: Array<Pending> = []
|
|
169
|
+
// The children `wait` holds, each with the `display` it had.
|
|
170
|
+
const hidden = new Map<Styled, string>()
|
|
171
|
+
// Foldkit's children as of the last delivery: a removed one of them is a leaver to put back.
|
|
172
|
+
let known = new Set<Element>()
|
|
173
|
+
let observer: MutationObserver | undefined
|
|
174
|
+
let torn = false
|
|
175
|
+
const dispatch = dispatcherOf((message) => offer(message))
|
|
176
|
+
/** Whether its children's going and coming moves the `layout` elements around them (Motion's `presenceAffectsLayout`), `true` unless told otherwise. */
|
|
177
|
+
const affectsLayout = (): boolean => optionsNow().presenceAffectsLayout !== false
|
|
178
|
+
// `onExitComplete` as a Message (decision 23), read when the last leaver has gone; none under replay (decision 34).
|
|
179
|
+
const exitCompleteOf = (): (() => void) | undefined => {
|
|
180
|
+
if (paused()) return undefined
|
|
181
|
+
return UndefinedOr.map(optionsNow().onExitComplete, (handler) => () => dispatch(handler)(undefined))
|
|
182
|
+
}
|
|
183
|
+
const controller = maker.make({
|
|
184
|
+
runtime: provider.runtime,
|
|
185
|
+
renderer: provider.renderer,
|
|
186
|
+
projection: () => Keys.projectionIn(provider.registry.built()),
|
|
187
|
+
reducedMotion: provider.reducedMotion,
|
|
188
|
+
positionalKeys: provider.positionalKeys,
|
|
189
|
+
defaultTransition: provider.defaultTransition,
|
|
190
|
+
initial: () => optionsNow().initial,
|
|
191
|
+
events: maker.events(provider.runtime, exitCompleteOf),
|
|
192
|
+
mode: () => optionsNow().mode,
|
|
193
|
+
custom: () => optionsNow().custom,
|
|
194
|
+
anchorX: () => optionsNow().anchorX,
|
|
195
|
+
anchorY: () => optionsNow().anchorY,
|
|
196
|
+
presenceAffectsLayout: () => optionsNow().presenceAffectsLayout,
|
|
197
|
+
// The last leaver has gone: the children `wait` held go on the page, in a delivery of their own. A
|
|
198
|
+
// microtask later, not now: a take-over ends the last exit inside `shown` or `present` (dom's
|
|
199
|
+
// `leaveEnds`), where a delivery can't run; Solid's release is a signal, read later, for the same reason.
|
|
200
|
+
release: () => {
|
|
201
|
+
queueMicrotask(() => deliver([], true))
|
|
202
|
+
},
|
|
203
|
+
})
|
|
204
|
+
// `propagate` (Motion's): under a presence above, read once, as Solid's.
|
|
205
|
+
const tearDown = maker.propagateTo(UndefinedOr.map(nearestPresence(provider, state, element), (above) => above.context), optionsNow().propagate === true, controller)
|
|
206
|
+
|
|
207
|
+
const hide = (child: Element): void => {
|
|
208
|
+
Option.map(Option.liftPredicate(child, isStyled), (styled) => {
|
|
209
|
+
if (hidden.has(styled)) return
|
|
210
|
+
hidden.set(styled, styled.style.display)
|
|
211
|
+
styled.style.display = "none"
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
const show = (child: Element): void => {
|
|
215
|
+
Option.map(Option.liftPredicate(child, isStyled), (styled) => {
|
|
216
|
+
UndefinedOr.map(hidden.get(styled), (was) => {
|
|
217
|
+
hidden.delete(styled)
|
|
218
|
+
styled.style.display = was
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The known children `records` say Foldkit took out, each put back where it was: the change's
|
|
224
|
+
* leavers. A held child it took out never showed: dropped, with no exit (P23's W3), not put back.
|
|
225
|
+
*/
|
|
226
|
+
const takenOut = (records: ReadonlyArray<MutationRecord>): TakenOut => {
|
|
227
|
+
const leavers = new Set<Element>()
|
|
228
|
+
let dropped = 0
|
|
229
|
+
Iter.forEach(records, (record) => {
|
|
230
|
+
Iter.forEach(record.removedNodes, (removed) => {
|
|
231
|
+
Option.map(Option.liftPredicate(removed, isElement), (node) => {
|
|
232
|
+
if (node.isConnected) return
|
|
233
|
+
if (isStyled(node) && hidden.has(node)) {
|
|
234
|
+
hidden.delete(node)
|
|
235
|
+
known.delete(node)
|
|
236
|
+
dropped++
|
|
237
|
+
return
|
|
238
|
+
}
|
|
239
|
+
if (Bool.not(known.has(node))) return
|
|
240
|
+
if (leavers.has(node)) return
|
|
241
|
+
leavers.add(node)
|
|
242
|
+
placeBack(element, node, record)
|
|
243
|
+
})
|
|
244
|
+
})
|
|
245
|
+
})
|
|
246
|
+
return { leavers, dropped }
|
|
247
|
+
}
|
|
248
|
+
/** The children `records` say Foldkit put in, still on the page: the change's enterers (a moved child among them). */
|
|
249
|
+
const putIn = (records: ReadonlyArray<MutationRecord>): Set<Element> => {
|
|
250
|
+
const enterers = new Set<Element>()
|
|
251
|
+
Iter.forEach(records, (record) => {
|
|
252
|
+
Iter.forEach(record.addedNodes, (added) => {
|
|
253
|
+
Option.map(Option.liftPredicate(added, isElement), (node) => {
|
|
254
|
+
if (node.isConnected) enterers.add(node)
|
|
255
|
+
})
|
|
256
|
+
})
|
|
257
|
+
})
|
|
258
|
+
return enterers
|
|
259
|
+
}
|
|
260
|
+
/** A child's start, now, or once `wait` shows it. */
|
|
261
|
+
const start = (child: Pending): void => {
|
|
262
|
+
if (child.gone) return
|
|
263
|
+
const begun = (): void => {
|
|
264
|
+
child.cancel = undefined
|
|
265
|
+
child.release = child.begin()
|
|
266
|
+
}
|
|
267
|
+
child.cancel = PresenceRecord.presenceHold(controller.context, child.element, begun)
|
|
268
|
+
if (Predicate.isUndefined(child.cancel)) begun()
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* What the Handle calls on it (#840): its children measured where they are, ahead of a patch, which
|
|
272
|
+
* `popLayout` pins the next change's leavers at; and its delivery run now, from the records its observer
|
|
273
|
+
* holds, ahead of a Mount, since a presence inside an entering child starts that child's elements at its
|
|
274
|
+
* own mount, inside the patch's task, before this presence's observer is told, and a `layoutId` twin
|
|
275
|
+
* starting then measures the one it takes over from, in a leaver this presence hasn't pinned yet.
|
|
276
|
+
* Foldkit runs a Mount in its vnode's `insert` hook, once the patch has committed, so the records a
|
|
277
|
+
* flush takes are the whole change's.
|
|
278
|
+
*/
|
|
279
|
+
const live: LivePresence = {
|
|
280
|
+
snapshot: () => {
|
|
281
|
+
if (optionsNow().mode !== "popLayout") return
|
|
282
|
+
// A change not yet delivered (an earlier patch's in this task) first, its leavers pinned where they were; then the children as they stand.
|
|
283
|
+
live.flush()
|
|
284
|
+
controller.snapshot(Arr.filter(Arr.fromIterable(element.children), (child) => known.has(child)))
|
|
285
|
+
// Measuring an outermost `<svg>` child puts dom's filler in and out: no change of Foldkit's.
|
|
286
|
+
UndefinedOr.map(observer, (watching) => watching.takeRecords())
|
|
287
|
+
},
|
|
288
|
+
flush: () => {
|
|
289
|
+
UndefinedOr.map(observer, (watching) => {
|
|
290
|
+
const records = watching.takeRecords()
|
|
291
|
+
if (records.length === 0) return
|
|
292
|
+
deliver(records, true)
|
|
293
|
+
// Taking the records silences the observer's own callback for them, which would have started the
|
|
294
|
+
// children enrolled after this delivery: those of the patch's Mounts that come after the one
|
|
295
|
+
// flushing. A delivery a microtask on starts them, where the observer's would have.
|
|
296
|
+
queueMicrotask(() => {
|
|
297
|
+
if (pending.length > 0) deliver([], true)
|
|
298
|
+
})
|
|
299
|
+
})
|
|
300
|
+
},
|
|
301
|
+
}
|
|
302
|
+
/** A delivery (above): after a patch, at the mount, at a flush, or as the last leaver goes. */
|
|
303
|
+
const deliver = (records: ReadonlyArray<MutationRecord>, hint: boolean): void => {
|
|
304
|
+
if (torn) return
|
|
305
|
+
const { leavers, dropped } = takenOut(records)
|
|
306
|
+
const enterers = putIn(records)
|
|
307
|
+
// Records of the controller's own removals alone (an exit's end, after its delivery): nothing of Foldkit's changed.
|
|
308
|
+
if (Bool.and(records.length > 0, Bool.and(leavers.size === 0, Bool.and(enterers.size === 0, dropped === 0)))) return
|
|
309
|
+
// The exits and the starts take their first step in this frame (decision 32; #792, Q7); not at the mount, nor under replay.
|
|
310
|
+
if (hint) hintFrameUnless(provider.loop, paused)
|
|
311
|
+
// Foldkit's children now: the known ones it kept, and the ones it put in; not a leaver the controller retains.
|
|
312
|
+
const current = Arr.filter(Arr.fromIterable(element.children), (child) => Bool.or(Bool.and(known.has(child), Bool.not(leavers.has(child))), enterers.has(child)))
|
|
313
|
+
// `shown` pins a `popLayout` leaver at the binding's snapshot and spends it (#840): the next render takes a new one.
|
|
314
|
+
snapshotSpent(state)
|
|
315
|
+
const onPage = new Set(elementsOf(controller.shown(current)))
|
|
316
|
+
Arr.forEach(current, (child) => Bool.match(onPage.has(child), { onTrue: () => show(child), onFalse: () => hide(child) }))
|
|
317
|
+
// Told not to affect layout (Motion's `presenceAffectsLayout: false`): its child list's changes make no pass either (#792, Q21).
|
|
318
|
+
quietLayoutIn(element, Bool.not(affectsLayout()))
|
|
319
|
+
controller.present(Arr.filter(current, (child) => onPage.has(child)))
|
|
320
|
+
// The children start once the leavers are retained and a `popLayout` one is pinned (#840): a child
|
|
321
|
+
// taking over a `layoutId` twin inside a leaver measures the twin as it starts, where the leaver is
|
|
322
|
+
// then, which before the pin is where the patch pushed it. Solid's children start after its presence
|
|
323
|
+
// has placed its leavers too.
|
|
324
|
+
Arr.forEach(pending.splice(0), start)
|
|
325
|
+
known = new Set(current)
|
|
326
|
+
// Its own moves, and the controller's, make records too: dropped, so they deliver nothing (P19).
|
|
327
|
+
UndefinedOr.map(observer, (watching) => watching.takeRecords())
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return {
|
|
331
|
+
context: controller.context,
|
|
332
|
+
holds: () => holdsInitial(optionsNow()),
|
|
333
|
+
enroll: (child, begin) => {
|
|
334
|
+
const entry: Pending = { element: child, begin, release: undefined, cancel: undefined, gone: false }
|
|
335
|
+
pending.push(entry)
|
|
336
|
+
return () => {
|
|
337
|
+
UndefinedOr.map(entry.cancel, (cancel) => cancel())
|
|
338
|
+
releaseWaiting(entry)
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
mount: (isPaused, offerMessage) => {
|
|
342
|
+
paused = isPaused
|
|
343
|
+
offer = offerMessage
|
|
344
|
+
observer = new MutationObserver((records) => deliver(records, true))
|
|
345
|
+
observer.observe(element, { childList: true })
|
|
346
|
+
state.presences.add(live)
|
|
347
|
+
// The first delivery, inside the patch: every child is Foldkit's; those mounted with the presence start, and `initial: false` covers them.
|
|
348
|
+
known = new Set(Arr.fromIterable(element.children))
|
|
349
|
+
deliver([], false)
|
|
350
|
+
controller.mounted()
|
|
351
|
+
return () => {
|
|
352
|
+
torn = true
|
|
353
|
+
state.presences.delete(live)
|
|
354
|
+
UndefinedOr.map(observer, (watching) => watching.disconnect())
|
|
355
|
+
observer = undefined
|
|
356
|
+
tearDown()
|
|
357
|
+
quietLayoutIn(element, false)
|
|
358
|
+
presences.delete(element)
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
}
|
|
362
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Foldkit's `Reactivity` port (ADR-0071, decision 17): a host with no reactive graph. Accessors are
|
|
3
|
+
* getters over the latest config each element received; `watch` keeps its last value and re-reads
|
|
4
|
+
* once per patch, in the patch's microtask (`delivery.ts`), firing on `!Equal.equals`; `untrack` is a
|
|
5
|
+
* plain call. One registry for every watch (P17: a child whose config is the same reference gets no
|
|
6
|
+
* delivery, so it hears its parent only through a global re-check).
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type * as Reactivity from "@wardic/core/unstable/Reactivity"
|
|
11
|
+
import * as Equal from "effect/Equal"
|
|
12
|
+
import { constVoid } from "effect/Function"
|
|
13
|
+
import * as Iter from "effect/Iterable"
|
|
14
|
+
|
|
15
|
+
/** A watch: its accessor, its last value, and what a change calls. */
|
|
16
|
+
interface Watch {
|
|
17
|
+
readonly accessor: () => unknown
|
|
18
|
+
readonly onChange: (value: unknown) => void
|
|
19
|
+
last: unknown
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Module state (#460, Q7): the page's watches across providers, re-read once a patch.
|
|
23
|
+
const watches = new Set<Watch>()
|
|
24
|
+
|
|
25
|
+
/** Re-read every watch, firing each whose value changed by `Equal.equals` (P17). */
|
|
26
|
+
export const recheck = (): void =>
|
|
27
|
+
Iter.forEach(watches, (watch) => {
|
|
28
|
+
const now = watch.accessor()
|
|
29
|
+
if (Equal.equals(watch.last, now)) return
|
|
30
|
+
watch.last = now
|
|
31
|
+
watch.onChange(now)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
/** The port. */
|
|
35
|
+
export const reactivity: Reactivity.Reactivity = {
|
|
36
|
+
untrack: (fn) => fn(),
|
|
37
|
+
trigger: () => ({ track: constVoid, notify: recheck }),
|
|
38
|
+
watch: (accessor, onChange) => {
|
|
39
|
+
const watch: Watch = { accessor, onChange: onChange as (value: unknown) => void, last: accessor() }
|
|
40
|
+
watches.add(watch)
|
|
41
|
+
return () => {
|
|
42
|
+
watches.delete(watch)
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
}
|