@wardic/foldkit 1.3.1 → 1.4.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.
@@ -32,9 +32,10 @@ import { mounted as scopeMounted } from "./scope.js";
32
32
  import { layoutOf } from "./layout.js";
33
33
  import { nearestPresence } from "./presence.js";
34
34
  import { groupDelivered, reorderItemOf } from "./reorder.js";
35
- import { hydrationMounted, reducedMotionUnder } from "./services.js";
35
+ import { hydrationMounted, reducedMotionUnder, reporterOf } from "./services.js";
36
36
  import { configValueOf, dropNode, layoutGroupOf, nodeOf, startUnder } from "./tree.js";
37
37
  import { DEV } from "@wardic/foldkit/internal/dev";
38
+ import { IS_SERVER } from "@wardic/foldkit/internal/server";
38
39
  /** No config: an element the view gave nothing, which rests where it is. */
39
40
  const NO_CONFIG = {};
40
41
  /** The config delivered to an element, or none. */
@@ -45,6 +46,8 @@ export const configOf = (delivered) => {
45
46
  };
46
47
  /** What a Retarget throwing inside the patch is reported as: contained at the binding's edge (decision 13), the element left where it is. */
47
48
  const RETARGET_FAILED = "wardic.retarget: a w.* element's Retarget failed; it stays where it is";
49
+ /** What a `w.*` element's Mount or start failing is reported as: contained at the binding's edge (decision 13), the element left at its Starting style. */
50
+ export const MOUNT_FAILED = "wardic.mount: a w.* element's mount failed; it stays at its Starting style";
48
51
  // Module state (#460, Q7): an id counter, only ever compared for distinctness.
49
52
  let ids = 0;
50
53
  /**
@@ -66,7 +69,14 @@ const devDiagnostics = () => {
66
69
  * `w.Provider()` element's build gives both (#794, Q7), and its Mount writes them for an element with
67
70
  * no Provider element above.
68
71
  */
69
- export const startingStyleOf = (tag, config, registry, inherited, held = false) => startingStyleFrom(tag, config, registry, Spine.startOf(AnimationControls.uncontrolledOf(config), inherited, held, reactivity.untrack));
72
+ export const startingStyleOf = (tag, config, registry, inherited, held = false) => {
73
+ const start = Spine.startOf(AnimationControls.uncontrolledOf(config), inherited, held, reactivity.untrack);
74
+ // A server renders it once: a variant that throws is logged there, its values left out of the markup.
75
+ // A client's mount reports it instead, since each render resolves it again (#979).
76
+ if (IS_SERVER && Option.isSome(start.failure))
77
+ Spine.variantFailedOnServer(start.failure.value);
78
+ return startingStyleFrom(tag, config, registry, start);
79
+ };
70
80
  /** The element's Starting style (above) drawn from `start`, its own: what the walk draws beside its Appear entry, which reads `start` too (#794, PR 2). */
71
81
  export const startingStyleFrom = (tag, config, registry, start) => Style.starting(tag, ElementSetup.drawnAtRest({ ...liveValuesOf(config.style, registry), ...start.initialValues }, false, config.transformTemplate)).style;
72
82
  /** The user's `style` with each Live value read now (#130): what a Starting style is projected from. */
@@ -231,331 +241,382 @@ export const mountElement = (provider, state, element, paused, offer, root) => {
231
241
  const context = UndefinedOr.map(presence, (p) => p.context);
232
242
  UndefinedOr.map(context, (ctx) => UndefinedOr.map(ctx.made, (made) => made(element, config.key, Predicate.isNotUndefined(config.layoutId))));
233
243
  const begin = () => {
234
- // Its Live values in `style` (#130, ADR-0043): routed keys the spine follows through the gate.
235
- const live = LiveStyle.createLiveStyle({ registry: registryNow, onDroppedTransform: droppedTransformWarning(provider, id), reactivity, ownValues: provider.valueReads.ownValues });
236
- live.css(config.style);
237
- // Where it starts (ADR-0003), read once: the config the view rendered it with, its inherited `initial`
238
- // included (#50), at its `animate` under a presence's `initial: false` (#134).
239
- const start = Spine.startOf(view, inherited, PresenceRecord.presenceBlocksInitial(context), reactivity.untrack);
240
- const snapshot = Spine.snapshotOf(start);
241
- // Its Transform template (#443), drawn with its first write: the one at mount, then each other one.
242
- const templateAtMount = config.transformTemplate;
243
- // The keys its Starting style set, switched to the user's in its first commit. A Variant child's
244
- // inherited Starting style wasn't rendered by the view: written now, inside the patch (P2). So is a
245
- // child of a `w.Presence({ initial: false })`: the view rendered it at its `Animate` (the walk at a
246
- // Provider element's build, #794), where only the presence's first render starts it there (#134).
247
- const starting = Style.starting(tag, ElementSetup.drawnAtRest(start.initialValues, false, templateAtMount)).style;
248
- if (Bool.or(Predicate.isNotUndefined(inherited), UndefinedOr.match(presence, { onUndefined: constFalse, onDefined: (p) => p.holds() })))
249
- writeStartingStyle(element, starting);
250
- const startingKeys = Rec.keys(starting);
251
- // Its Appear animation, when it hydrates one the server rendered (ADR-0044 decision 6; #794, Q10):
252
- // found by the slot its inline `animation` names, which the hydration render kept, taken over by its
253
- // first Retarget that starts, as far in as the CSS has got, and removed in its first commit, the
254
- // Handoff. Asked at every mount, a string read: an element a client render made names no slot. One
255
- // found keeps hydration's window open until that commit, so no render in between strips the slot.
256
- const handoff = Option.getOrUndefined(AppearAnimations.handoffOf(element));
257
- const released = hydrationMounted(state, Predicate.isNotUndefined(handoff));
258
- const sink = ElementSetup.releasingAppear(Spine.withOnUpdate({
259
- target: element,
260
- beforeFirstCommit: () => {
261
- switchToUserStyle(element, tag, startingKeys, config, registryNow);
262
- released();
263
- },
264
- }, view.onUpdate, configNow), handoff, element);
265
- const unregister = provider.gate.register(id, sink, ElementSetup.registeredValuesOf(templateAtMount));
266
- // The user's style now, for its Base values (ADR-0022): the config's, as the view last delivered it.
267
- const styleNow = () => config.style;
268
- const links = ElementSetup.startLinksOf(element, styleNow, live);
269
- // Reduced motion (ADR-0029) and skipped animations (#468) under its nearest `w.Config`, read at each Retarget.
270
- const reducedMotion = reducedMotionUnder(provider, configValue);
271
- const skipped = WardicConfigValue.skipAnimationsOf(configValue);
272
- const skipAnimations = () => paused() || skipped();
273
- // Where its drag's and its pan's points are (#793, Q9): its nearest `w.Config`'s `transformPagePoint`, read at each down.
274
- const pagePoint = () => UndefinedOr.match(configValue, { onUndefined: constUndefined, onDefined: (v) => v.transformPagePoint() });
275
- // Its presence record (ADR-0007): what its presence drives its exit with after Foldkit removes it,
276
- // resolved from its `Exit` as it leaves; the presence owns the registration. And where it starts when
277
- // it takes over from a leaver under its key (#286).
278
- const exitConfig = { current: Spine.exitConfigOf(view, inherited) };
279
- const record = UndefinedOr.map(context, (ctx) => {
280
- const rec = Spine.presenceRecordOf(element, start, (custom, inheritedNames) => Spine.exitTargetOf(exitConfig.current, custom, snapshot, inheritedNames), links, Spine.presenceTreeOf(view), snapshot, reducedMotion, skipAnimations);
281
- ctx.register(rec);
282
- return rec;
283
- });
284
- const taken = PresenceRecord.presenceSeedOf(context, element);
285
- const recordFrame = Spine.recordFrameOf(record);
286
- // Its gestures (#793; decision 23): each started as a delivery wants it, its entry in the Override
287
- // stack re-resolved as it flips, and its Variant children told through the port's watches (#291).
288
- const gestures = gesturesOf({
289
- provider,
290
- element,
291
- config: configNow,
292
- dispatch,
293
- onChange: () => {
294
- retarget();
295
- recheck();
296
- },
297
- id,
298
- pagePoint,
299
- // A Reorder item's keys are its Keyboard reorder's (ADR-0037): its press starts from no key.
300
- keysTaken: () => Predicate.isNotUndefined(MutableRef.get(item)),
301
- });
302
- node.bindGestures(gestures.states);
303
- // A Reorder item (decision 28): joined to the group above it, which decides its drag's axis and hears
304
- // its samples; its keyboard lift turns `WhileDrag` on. Joined now, or as a load brings the Features (Q15).
305
- const item = MutableRef.make(reorderItemOf(provider, element, id, () => config, dispatch, gestures.lift));
306
- // Its spine's options, with what its drag writes as its track starts (ADR-0004; #793, PR 2): its
307
- // axis, its track and whether it drives a layout node, which the spine reads where it uses them.
308
- // The axis is seeded from the config at mount too, as Solid's: the spine registers its drag hook (a
309
- // Batch member's hold, #665) at make from it, so a `Drag` a later render adds registers none.
310
- const spineOptions = {
311
- runtime: provider.runtime,
312
- gate: provider.gate,
313
- id,
314
- element,
315
- tag,
316
- config: configNow,
317
- parent: inherited,
318
- canStart: node.canStart,
319
- extraDelay: node.extraDelay,
320
- onFrame: node.onFrame,
321
- byName: node.names,
322
- reducedMotion,
323
- skipAnimations,
324
- positionalKeys: provider.positionalKeys,
325
- defaultTransition: provider.defaultTransition,
326
- ownValues: provider.valueReads.ownValues,
327
- measuredPairs: provider.valueReads.measuredPairs,
328
- variableValues: provider.valueReads.variableValues,
329
- start,
330
- snapshot,
331
- reuse: Spine.makeReuse(),
332
- links,
333
- styleNow,
334
- live,
335
- builtFeatures: provider.registry.built,
336
- presence: record,
337
- projected: false,
338
- dragAxis: DragDefaults.dragAxisOf(view, { dragAxis: UndefinedOr.map(MutableRef.get(item), (joined) => joined.dragAxis) }),
339
- drag: undefined,
340
- handoff,
341
- // Motion's onAnimationStart / onAnimationComplete (decision 23), as Messages through `dispatch`.
342
- animation: Spine.animationCallbacks(provider.runtime, id),
343
- diagnostics: devDiagnostics(),
244
+ // What its release undoes, each set as it's made, in the order the release runs them (#997, ADR-0074):
245
+ // a start that throws runs them on what it had made by then, its setter gone with the rest, so it stays
246
+ // at its Starting style and no later delivery reaches it.
247
+ let unsubscribe = constVoid;
248
+ let releaseHydration = constVoid;
249
+ let unwatch = constVoid;
250
+ let unfollowText = constVoid;
251
+ let unfollowTemplate = constVoid;
252
+ let unregisterAnimatable = constVoid;
253
+ let uninstall = constVoid;
254
+ let stopRechecks = constVoid;
255
+ let releaseScope = constVoid;
256
+ let disposeStartsDrag = constVoid;
257
+ let disposeItem = constVoid;
258
+ let disposeDrag = constVoid;
259
+ let disposeGestures = constVoid;
260
+ let disposeLayoutNode = constVoid;
261
+ let unregisterLayoutValues = constVoid;
262
+ let disposeSpine = constVoid;
263
+ let unregisterGate = constVoid;
264
+ // Its presence record's, run only when its start throws: else the presence keeps it, to drive its exit.
265
+ let unregisterRecord = constVoid;
266
+ const releaseMade = () => {
267
+ unsubscribe();
268
+ releaseHydration();
269
+ unwatch();
270
+ unfollowText();
271
+ unfollowTemplate();
272
+ unregisterAnimatable();
273
+ uninstall();
274
+ stopRechecks();
275
+ releaseScope();
276
+ disposeStartsDrag();
277
+ disposeItem();
278
+ disposeDrag();
279
+ disposeGestures();
280
+ disposeLayoutNode();
281
+ unregisterLayoutValues();
282
+ disposeSpine();
283
+ unregisterGate();
344
284
  };
345
- const spine = Spine.makeElementSpine(spineOptions);
346
- UndefinedOr.map(taken, (seed) => spine.seed(seed));
347
- // The `StartsDrag` element (Q8): its downs start its controls' drags, from the first delivery that gives them.
348
- const startsDrag = startsDragOf(element, () => config);
349
- // Its Animation scope (#798, Q7 and Q9): the scope's element from now, and the animations started in it stopped at its release.
350
- const releaseScope = UndefinedOr.match(config.animationScope, { onUndefined: () => constVoid, onDefined: (scope) => scopeMounted(scope, element) });
351
- // Its layout (ADR-0009, ADR-0010; decision 30): one projection node for `Layout`, `LayoutId`,
352
- // `LayoutScroll` or `LayoutRoot`, on the provider's layout, built at the first; and, with a layout
353
- // built, its values by element, for a twin's flight to follow (ADR-0012 *Amendment (#137)*). Each
354
- // joined at mount, or as a load brings the Feature (#793, Q15).
355
- // Its config as dom's layout code reads it: the layout keys alone.
356
- const wardConfig = () => view;
357
- const builtProjection = () => Keys.projectionIn(provider.registry.built());
358
- const layoutNode = MutableRef.make(undefined);
359
- const unregisterValues = MutableRef.make(undefined);
360
- const joinLayout = () => {
361
- if (Bool.and(Predicate.isUndefined(MutableRef.get(layoutNode)), Feature.wantsLayoutNode(wardConfig()))) {
362
- MutableRef.set(layoutNode, UndefinedOr.map(layoutOf(provider, state), (made) => made.projection.register(made.nodes.registrationOf(made.nodes.optionsOf({
363
- id,
364
- element,
365
- config: wardConfig,
366
- parent: inherited,
367
- snapshot,
368
- // What its release runs on (ADR-0016, *Amendment (#140)*; Q10): the element's drag transition, read now.
369
- dragTransition: DragTransition.dragTransitionOf(view.dragElastic, view.dragTransition),
370
- // Raised while dragged as a Reorder item (ADR-0020), over the rest.
371
- raiseWhileDragged: Predicate.isNotUndefined(MutableRef.get(item)),
372
- taken,
373
- layoutGroup: layoutGroupOf(element),
374
- reducedMotion,
375
- skipAnimations,
376
- heldTransform: ElementSetup.heldTransformFor(snapshot, templateAtMount, () => view.transformTemplate),
377
- })))));
285
+ try {
286
+ // Its Live values in `style` (#130, ADR-0043): routed keys the spine follows through the gate.
287
+ const live = LiveStyle.createLiveStyle({ registry: registryNow, onDroppedTransform: droppedTransformWarning(provider, id), reactivity, ownValues: provider.valueReads.ownValues });
288
+ live.css(config.style);
289
+ // Where it starts (ADR-0003), read once: the config the view rendered it with, its inherited `initial`
290
+ // included (#50), at its `animate` under a presence's `initial: false` (#134).
291
+ const start = Spine.startOf(view, inherited, PresenceRecord.presenceBlocksInitial(context), reactivity.untrack);
292
+ // A variant whose resolver throws is this element's alone: reported once at its mount and once a run
293
+ // of throws until it resolves, the element keeping its last target, and nothing thrown into the
294
+ // render, which would crash the app (#979; ADR-0074). Each render's Starting style resolves it silently.
295
+ const variantFailures = Spine.variantFailuresOf(provider.runtime);
296
+ Spine.reportStart(variantFailures, start);
297
+ const snapshot = Spine.snapshotOf(start);
298
+ // Its Transform template (#443), drawn with its first write: the one at mount, then each other one.
299
+ const templateAtMount = config.transformTemplate;
300
+ // The keys its Starting style set, switched to the user's in its first commit. A Variant child's
301
+ // inherited Starting style wasn't rendered by the view: written now, inside the patch (P2). So is a
302
+ // child of a `w.Presence({ initial: false })`: the view rendered it at its `Animate` (the walk at a
303
+ // Provider element's build, #794), where only the presence's first render starts it there (#134).
304
+ const starting = Style.starting(tag, ElementSetup.drawnAtRest(start.initialValues, false, templateAtMount)).style;
305
+ if (Bool.or(Predicate.isNotUndefined(inherited), UndefinedOr.match(presence, { onUndefined: constFalse, onDefined: (p) => p.holds() })))
306
+ writeStartingStyle(element, starting);
307
+ const startingKeys = Rec.keys(starting);
308
+ // Its Appear animation, when it hydrates one the server rendered (ADR-0044 decision 6; #794, Q10):
309
+ // found by the slot its inline `animation` names, which the hydration render kept, taken over by its
310
+ // first Retarget that starts, as far in as the CSS has got, and removed in its first commit, the
311
+ // Handoff. Asked at every mount, a string read: an element a client render made names no slot. One
312
+ // found keeps hydration's window open until that commit, so no render in between strips the slot.
313
+ const handoff = Option.getOrUndefined(AppearAnimations.handoffOf(element));
314
+ releaseHydration = hydrationMounted(state, Predicate.isNotUndefined(handoff));
315
+ const sink = ElementSetup.releasingAppear(Spine.withOnUpdate({
316
+ target: element,
317
+ beforeFirstCommit: () => {
318
+ switchToUserStyle(element, tag, startingKeys, config, registryNow);
319
+ releaseHydration();
320
+ },
321
+ }, view.onUpdate, configNow), handoff, element);
322
+ unregisterGate = provider.gate.register(id, sink, ElementSetup.registeredValuesOf(templateAtMount));
323
+ // The user's style now, for its Base values (ADR-0022): the config's, as the view last delivered it.
324
+ const styleNow = () => config.style;
325
+ const links = ElementSetup.startLinksOf(element, styleNow, live);
326
+ // Reduced motion (ADR-0029) and skipped animations (#468) under its nearest `w.Config`, read at each Retarget.
327
+ const reducedMotion = reducedMotionUnder(provider, configValue);
328
+ const skipped = WardicConfigValue.skipAnimationsOf(configValue);
329
+ const skipAnimations = () => paused() || skipped();
330
+ // Where its drag's and its pan's points are (#793, Q9): its nearest `w.Config`'s `transformPagePoint`, read at each down.
331
+ const pagePoint = () => UndefinedOr.match(configValue, { onUndefined: constUndefined, onDefined: (v) => v.transformPagePoint() });
332
+ // Its presence record (ADR-0007): what its presence drives its exit with after Foldkit removes it,
333
+ // resolved from its `Exit` as it leaves; the presence owns the registration. And where it starts when
334
+ // it takes over from a leaver under its key (#286).
335
+ const exitConfig = { current: Spine.exitConfigOf(view, inherited) };
336
+ const record = UndefinedOr.map(context, (ctx) => {
337
+ const rec = Spine.presenceRecordOf(element, start, (custom, inheritedNames) => Spine.exitTargetOf(exitConfig.current, custom, snapshot, inheritedNames), links, Spine.presenceTreeOf(view), snapshot, reducedMotion, skipAnimations);
338
+ unregisterRecord = ctx.register(rec);
339
+ return rec;
340
+ });
341
+ const taken = PresenceRecord.presenceSeedOf(context, element);
342
+ const recordFrame = Spine.recordFrameOf(record);
343
+ // Its gestures (#793; decision 23): each started as a delivery wants it, its entry in the Override
344
+ // stack re-resolved as it flips, and its Variant children told through the port's watches (#291).
345
+ const gestures = gesturesOf({
346
+ provider,
347
+ element,
348
+ config: configNow,
349
+ dispatch,
350
+ onChange: () => {
351
+ retarget();
352
+ recheck();
353
+ },
354
+ id,
355
+ pagePoint,
356
+ // A Reorder item's keys are its Keyboard reorder's (ADR-0037): its press starts from no key.
357
+ keysTaken: () => Predicate.isNotUndefined(MutableRef.get(item)),
358
+ });
359
+ disposeGestures = gestures.dispose;
360
+ node.bindGestures(gestures.states);
361
+ node.reportsTo(variantFailures);
362
+ // A Reorder item (decision 28): joined to the group above it, which decides its drag's axis and hears
363
+ // its samples; its keyboard lift turns `WhileDrag` on. Joined now, or as a load brings the Features (Q15).
364
+ const item = MutableRef.make(reorderItemOf(provider, element, id, () => config, dispatch, gestures.lift));
365
+ disposeItem = () => UndefinedOr.map(MutableRef.get(item), (joined) => joined.dispose());
366
+ // Its spine's options, with what its drag writes as its track starts (ADR-0004; #793, PR 2): its
367
+ // axis, its track and whether it drives a layout node, which the spine reads where it uses them.
368
+ // The axis is seeded from the config at mount too, as Solid's: the spine registers its drag hook (a
369
+ // Batch member's hold, #665) at make from it, so a `Drag` a later render adds registers none.
370
+ const spineOptions = {
371
+ runtime: provider.runtime,
372
+ gate: provider.gate,
373
+ id,
374
+ subject: element,
375
+ tag,
376
+ config: configNow,
377
+ parent: inherited,
378
+ canStart: node.canStart,
379
+ extraDelay: node.extraDelay,
380
+ onFrame: node.onFrame,
381
+ byName: node.names,
382
+ reducedMotion,
383
+ skipAnimations,
384
+ positionalKeys: provider.positionalKeys,
385
+ defaultTransition: provider.defaultTransition,
386
+ ownValues: provider.valueReads.ownValues,
387
+ measuredPairs: provider.valueReads.measuredPairs,
388
+ variableValues: provider.valueReads.variableValues,
389
+ start,
390
+ snapshot,
391
+ reuse: Spine.makeReuse(),
392
+ links,
393
+ styleNow,
394
+ live,
395
+ builtFeatures: provider.registry.built,
396
+ presence: record,
397
+ projected: false,
398
+ dragAxis: DragDefaults.dragAxisOf(view, { dragAxis: UndefinedOr.map(MutableRef.get(item), (joined) => joined.dragAxis) }),
399
+ drag: undefined,
400
+ handoff,
401
+ // Motion's onAnimationStart / onAnimationComplete (decision 23), as Messages through `dispatch`.
402
+ animation: Spine.animationCallbacks(provider.runtime, id),
403
+ diagnostics: devDiagnostics(),
404
+ };
405
+ const spine = Spine.makeSubjectSpine(spineOptions);
406
+ disposeSpine = spine.dispose;
407
+ UndefinedOr.map(taken, (seed) => spine.seed(seed));
408
+ // The `StartsDrag` element (Q8): its downs start its controls' drags, from the first delivery that gives them.
409
+ const startsDrag = startsDragOf(element, () => config);
410
+ disposeStartsDrag = startsDrag.dispose;
411
+ // Its Animation scope (#798, Q7 and Q9): the scope's element from now, and the animations started in it stopped at its release.
412
+ releaseScope = UndefinedOr.match(config.animationScope, { onUndefined: () => constVoid, onDefined: (scope) => scopeMounted(scope, element) });
413
+ // Its layout (ADR-0009, ADR-0010; decision 30): one projection node for `Layout`, `LayoutId`,
414
+ // `LayoutScroll` or `LayoutRoot`, on the provider's layout, built at the first; and, with a layout
415
+ // built, its values by element, for a twin's flight to follow (ADR-0012 *Amendment (#137)*). Each
416
+ // joined at mount, or as a load brings the Feature (#793, Q15).
417
+ // Its config as dom's layout code reads it: the layout keys alone.
418
+ const wardConfig = () => view;
419
+ const builtProjection = () => Keys.projectionIn(provider.registry.built());
420
+ const layoutNode = MutableRef.make(undefined);
421
+ const unregisterValues = MutableRef.make(undefined);
422
+ disposeLayoutNode = () => UndefinedOr.map(MutableRef.get(layoutNode), (made) => made.dispose());
423
+ unregisterLayoutValues = () => UndefinedOr.map(MutableRef.get(unregisterValues), (unregisterNow) => unregisterNow());
424
+ const joinLayout = () => {
425
+ if (Bool.and(Predicate.isUndefined(MutableRef.get(layoutNode)), Feature.wantsLayoutNode(wardConfig()))) {
426
+ MutableRef.set(layoutNode, UndefinedOr.map(layoutOf(provider, state), (made) => made.projection.register(made.nodes.registrationOf(made.nodes.optionsOf({
427
+ id,
428
+ element,
429
+ config: wardConfig,
430
+ parent: inherited,
431
+ snapshot,
432
+ // What its release runs on (ADR-0016, *Amendment (#140)*; Q10): the element's drag transition, read now.
433
+ dragTransition: DragTransition.dragTransitionOf(view.dragElastic, view.dragTransition),
434
+ // Raised while dragged as a Reorder item (ADR-0020), over the rest.
435
+ raiseWhileDragged: Predicate.isNotUndefined(MutableRef.get(item)),
436
+ taken,
437
+ layoutGroup: layoutGroupOf(element),
438
+ reducedMotion,
439
+ skipAnimations,
440
+ heldTransform: ElementSetup.heldTransformFor(snapshot, templateAtMount, () => view.transformTemplate),
441
+ })))));
442
+ registerItemNode();
443
+ }
444
+ if (Predicate.isUndefined(MutableRef.get(unregisterValues)))
445
+ MutableRef.set(unregisterValues, UndefinedOr.map(builtProjection(), (projection) => projection.registerValues(element, () => snapshot.values)));
446
+ };
447
+ /** A Reorder item's node, registered with its group once both are there (ADR-0020): its box, its drag offset and its lift. */
448
+ const registerItemNode = () => {
449
+ UndefinedOr.map(MutableRef.get(layoutNode), (made) => UndefinedOr.map(MutableRef.get(item), (joined) => joined.onLayoutHandle(made)));
450
+ };
451
+ /** The item joined as a load brings the Features (Q15): its group made by it if its own Mount hasn't, its node registered. */
452
+ const joinItem = () => {
453
+ if (Predicate.isNotUndefined(MutableRef.get(item)))
454
+ return;
455
+ MutableRef.set(item, reorderItemOf(provider, element, id, () => config, dispatch, gestures.lift));
378
456
  registerItemNode();
379
- }
380
- if (Predicate.isUndefined(MutableRef.get(unregisterValues)))
381
- MutableRef.set(unregisterValues, UndefinedOr.map(builtProjection(), (projection) => projection.registerValues(element, () => snapshot.values)));
382
- };
383
- /** A Reorder item's node, registered with its group once both are there (ADR-0020): its box, its drag offset and its lift. */
384
- const registerItemNode = () => {
385
- UndefinedOr.map(MutableRef.get(layoutNode), (made) => UndefinedOr.map(MutableRef.get(item), (joined) => joined.onLayoutHandle(made)));
386
- };
387
- /** The item joined as a load brings the Features (Q15): its group made by it if its own Mount hasn't, its node registered. */
388
- const joinItem = () => {
389
- if (Predicate.isNotUndefined(MutableRef.get(item)))
390
- return;
391
- MutableRef.set(item, reorderItemOf(provider, element, id, () => config, dispatch, gestures.lift));
392
- registerItemNode();
393
- };
394
- joinLayout();
395
- // Its drag (ADR-0004; #793, PR 2): dom's track, started as a delivery wants it, driving the layout
396
- // node it has then; its samples and its hold on `x`/`y` to the gestures, its release's end too.
397
- const drag = dragOf({
398
- provider,
399
- element,
400
- id,
401
- config: configNow,
402
- snapshot,
403
- recordFrame,
404
- live,
405
- spine: spineOptions,
406
- layoutNode: () => MutableRef.get(layoutNode),
407
- // A Reorder item's axis at each gesture's start, and its samples to its group before the callbacks (ADR-0020).
408
- itemAxis: () => UndefinedOr.map(MutableRef.get(item), (joined) => joined.dragAxis),
409
- pagePoint,
410
- skipAnimations,
411
- onDragging: gestures.dragHeld,
412
- onSample: (sample) => {
413
- UndefinedOr.map(MutableRef.get(item), (joined) => joined.onDragSample(sample));
414
- gestures.dragged(sample);
415
- },
416
- onReleaseEnd: gestures.released,
417
- });
418
- // A load's Features arriving (Q15): what the element wants of them starts then; an item joins its
419
- // group first, and a `layout` from the same load makes its node before the drag's track drives it (#582).
420
- const stopRechecks = provider.onFeatures(() => {
421
- joinItem();
422
- joinLayout();
423
- drag.check();
424
- gestures.check();
425
- });
426
- // Its `LayoutDependency`, compared per delivery as Motion's `prevProps.layoutDependency !==` is: a
427
- // change asks for the node's measure, or, on an element with no node (a group's), the whole tree's.
428
- let dependency = config.layoutDependency;
429
- const dependencyChanged = () => {
430
- const next = config.layoutDependency;
431
- if (Equal.equals(dependency, next))
432
- return;
433
- dependency = next;
434
- const ask = () => {
435
- UndefinedOr.match(MutableRef.get(layoutNode), {
436
- onDefined: (node) => node.notify(),
437
- onUndefined: () => UndefinedOr.map(builtProjection(), (projection) => projection.notify()),
438
- });
439
457
  };
440
- // A View Transition's render (decision 33): the pass it asks for is an Instant change of the layout.
441
- if (state.transitioning) {
442
- state.transitioning = false;
443
- UndefinedOr.match(builtProjection(), { onUndefined: ask, onDefined: (projection) => projection.instant(ask) });
444
- return;
445
- }
446
- ask();
447
- };
448
- // The Override stack resolved (ADR-0007), compared by value (#470): a delivery that resolves the same
449
- // targets tells nothing, as a `transition` change alone doesn't in Motion; a gesture's flip
450
- // re-resolves it with the entries on now.
451
- let last;
452
- const retarget = () => {
453
- const next = Spine.resolutionOf(view, inherited, start.initialLink, gestures.active(), snapshot, node.change());
454
- if (Predicate.isNotUndefined(last) && Spine.sameResolution(last, next))
455
- return;
456
- last = next;
457
- spine.retarget(next);
458
- };
459
- // Its Delivery (decision 20): each render's fresh config, inside the patch, Retargets at once (#791, Q14),
460
- // after the binding's hint (decision 32), so the Retarget's legs take their first step in this frame.
461
- // A defect there is Foldkit's patch's otherwise, which would stop the whole app (decision 13): reported,
462
- // and the element stays where it is.
463
- const uninstall = installSetter(element, (delivered) => {
464
- config = configOf(delivered);
465
- if (DEV)
466
- warnChangedControls(provider, id, controls, config);
467
- view = viewOf();
468
- // A Reorder group's values that changed (decision 28): its controller told, before the pass that lays the items out in them.
469
- groupDelivered(element);
470
- exitConfig.current = Spine.exitConfigOf(view, inherited);
471
- patchTouched();
472
- hintFrameUnless(provider.loop, paused);
473
- try {
474
- live.css(config.style);
475
- dependencyChanged();
476
- // A gesture this render wants starts now (ADR-0047 decision 5): a callback added later starts its recognizer, a `Drag` its track.
458
+ joinLayout();
459
+ // Its drag (ADR-0004; #793, PR 2): dom's track, started as a delivery wants it, driving the layout
460
+ // node it has then; its samples and its hold on `x`/`y` to the gestures, its release's end too.
461
+ const drag = dragOf({
462
+ provider,
463
+ element,
464
+ id,
465
+ config: configNow,
466
+ snapshot,
467
+ recordFrame,
468
+ live,
469
+ spine: spineOptions,
470
+ layoutNode: () => MutableRef.get(layoutNode),
471
+ // A Reorder item's axis at each gesture's start, and its samples to its group before the callbacks (ADR-0020).
472
+ itemAxis: () => UndefinedOr.map(MutableRef.get(item), (joined) => joined.dragAxis),
473
+ pagePoint,
474
+ skipAnimations,
475
+ onDragging: gestures.dragHeld,
476
+ onSample: (sample) => {
477
+ UndefinedOr.map(MutableRef.get(item), (joined) => joined.onDragSample(sample));
478
+ gestures.dragged(sample);
479
+ },
480
+ onReleaseEnd: gestures.released,
481
+ });
482
+ disposeDrag = drag.dispose;
483
+ // A load's Features arriving (Q15): what the element wants of them starts then; an item joins its
484
+ // group first, and a `layout` from the same load makes its node before the drag's track drives it (#582).
485
+ stopRechecks = provider.onFeatures(() => {
486
+ joinItem();
487
+ joinLayout();
477
488
  drag.check();
478
489
  gestures.check();
479
- startsDrag.check();
480
- retarget();
481
- }
482
- catch (error) {
483
- provider.runtime.runFork(Report.failure(RETARGET_FAILED, Cause.die(error)));
484
- }
485
- });
486
- // Its Live values followed from its first routed key on (#130), its text (decision 16), and its
487
- // Transform template after the one at mount (#443), which its presence record draws its exit through.
488
- spine.follow({ resolved: () => UndefinedOr.match(last, { onUndefined: () => Spine.NO_VALUES, onDefined: (resolution) => resolution.resolved }), dragging: gestures.dragging });
489
- const unfollowText = followText(element, config.text, registry);
490
- const unfollowTemplate = UndefinedOr.match(templateAtMount, {
491
- onUndefined: () => constVoid,
492
- onDefined: (template) => {
493
- recordFrame(ElementSetup.registeredValuesOf(template));
494
- return ElementSetup.followTemplate(template, () => view.transformTemplate, reactivity.watch, (next) => {
495
- provider.gate.write(id, { ...snapshot.values, [DomRenderer.TEMPLATE_KEY]: next });
496
- recordFrame({ [DomRenderer.TEMPLATE_KEY]: next });
497
- });
498
- },
499
- });
500
- // Its Imperative animations (#433) go through its spine while it's mounted.
501
- const unregisterAnimatable = Imperative.registerAnimatable(element, spine);
502
- // What a Variant child hears without a delivery of its own (P17): the name its ancestor hands down and
503
- // the gestures it hands on (#291), as one trigger key, and its `when` gate opening, re-checked once a
504
- // patch, at each settle that changes a gate (decision 17) and at each gesture flip above it.
505
- let unwatch = constVoid;
506
- if (Predicate.isNotUndefined(node.names)) {
507
- const unwatchName = UndefinedOr.match(inherited, {
490
+ });
491
+ // Its `LayoutDependency`, compared per delivery as Motion's `prevProps.layoutDependency !==` is: a
492
+ // change asks for the node's measure, or, on an element with no node (a group's), the whole tree's.
493
+ let dependency = config.layoutDependency;
494
+ const dependencyChanged = () => {
495
+ const next = config.layoutDependency;
496
+ if (Equal.equals(dependency, next))
497
+ return;
498
+ dependency = next;
499
+ const ask = () => {
500
+ UndefinedOr.match(MutableRef.get(layoutNode), {
501
+ onDefined: (node) => node.notify(),
502
+ onUndefined: () => UndefinedOr.map(builtProjection(), (projection) => projection.notify()),
503
+ });
504
+ };
505
+ // A View Transition's render (decision 33): the pass it asks for is an Instant change of the layout.
506
+ if (state.transitioning) {
507
+ state.transitioning = false;
508
+ UndefinedOr.match(builtProjection(), { onUndefined: ask, onDefined: (projection) => projection.instant(ask) });
509
+ return;
510
+ }
511
+ ask();
512
+ };
513
+ // The Override stack resolved (ADR-0007), compared by value (#470): a delivery that resolves the same
514
+ // targets tells nothing, as a `transition` change alone doesn't in Motion; a gesture's flip
515
+ // re-resolves it with the entries on now.
516
+ let last;
517
+ const retarget = () => {
518
+ const next = Spine.keptOf(variantFailures, Spine.resolutionOf(view, inherited, start.initialLink, gestures.active(), snapshot, node.change()));
519
+ if (Predicate.isNotUndefined(last) && Spine.sameResolution(last, next))
520
+ return;
521
+ last = next;
522
+ spine.retarget(next);
523
+ };
524
+ // Its Delivery (decision 20): each render's fresh config, inside the patch, Retargets at once (#791, Q14),
525
+ // after the binding's hint (decision 32), so the Retarget's legs take their first step in this frame.
526
+ // A defect there is Foldkit's patch's otherwise, which would stop the whole app (decision 13): reported,
527
+ // and the element stays where it is.
528
+ uninstall = installSetter(element, (delivered) => {
529
+ config = configOf(delivered);
530
+ if (DEV)
531
+ warnChangedControls(provider, id, controls, config);
532
+ view = viewOf();
533
+ // A Reorder group's values that changed (decision 28): its controller told, before the pass that lays the items out in them.
534
+ groupDelivered(element);
535
+ exitConfig.current = Spine.exitConfigOf(view, inherited);
536
+ patchTouched();
537
+ hintFrameUnless(provider.loop, paused);
538
+ try {
539
+ live.css(config.style);
540
+ dependencyChanged();
541
+ // A gesture this render wants starts now (ADR-0047 decision 5): a callback added later starts its recognizer, a `Drag` its track.
542
+ drag.check();
543
+ gestures.check();
544
+ startsDrag.check();
545
+ retarget();
546
+ }
547
+ catch (error) {
548
+ provider.runtime.runFork(Report.failure(RETARGET_FAILED, Cause.die(error)));
549
+ }
550
+ });
551
+ // Its Live values followed from its first routed key on (#130), its text (decision 16), and its
552
+ // Transform template after the one at mount (#443), which its presence record draws its exit through.
553
+ spine.follow({ resolved: () => UndefinedOr.match(last, { onUndefined: () => Spine.NO_VALUES, onDefined: (resolution) => resolution.resolved }), dragging: gestures.dragging });
554
+ unfollowText = followText(element, config.text, registry);
555
+ unfollowTemplate = UndefinedOr.match(templateAtMount, {
508
556
  onUndefined: () => constVoid,
509
- // Its ancestor's Controls change is a trigger too (#433), even to the same name.
510
- onDefined: (parentContext) => {
511
- const inheritedChange = VariantTree.controlsChangeOf(undefined, parentContext);
512
- return reactivity.watch(() => VariantTree.triggerKeyOf(parentContext.variant(), VariantTree.handDownFrom(parentContext), inheritedChange()), retarget);
557
+ onDefined: (template) => {
558
+ recordFrame(ElementSetup.registeredValuesOf(template));
559
+ return ElementSetup.followTemplate(template, () => view.transformTemplate, reactivity.watch, (next) => {
560
+ provider.gate.write(id, { ...snapshot.values, [DomRenderer.TEMPLATE_KEY]: next });
561
+ recordFrame({ [DomRenderer.TEMPLATE_KEY]: next });
562
+ });
513
563
  },
514
564
  });
515
- const unwatchGate = reactivity.watch(node.canStart, (open) => {
516
- if (Bool.and(open, Predicate.isNotUndefined(last)))
517
- spine.retarget(UndefinedOr.getOrThrow(last));
565
+ // Its Imperative animations (#433) go through its spine while it's mounted.
566
+ unregisterAnimatable = Imperative.registerAnimatable(element, spine);
567
+ // What a Variant child hears without a delivery of its own (P17): the name its ancestor hands down and
568
+ // the gestures it hands on (#291), as one trigger key, and its `when` gate opening, re-checked once a
569
+ // patch, at each settle that changes a gate (decision 17) and at each gesture flip above it.
570
+ if (Predicate.isNotUndefined(node.names)) {
571
+ const unwatchName = UndefinedOr.match(inherited, {
572
+ onUndefined: () => constVoid,
573
+ // Its ancestor's Controls change is a trigger too (#433), even to the same name.
574
+ onDefined: (parentContext) => {
575
+ const inheritedChange = VariantTree.controlsChangeOf(undefined, parentContext);
576
+ return reactivity.watch(() => VariantTree.triggerKeyOf(parentContext.variant(), VariantTree.handDownFrom(parentContext), inheritedChange()), retarget);
577
+ },
578
+ });
579
+ unwatch = unwatchName;
580
+ const unwatchGate = reactivity.watch(node.canStart, (open) => {
581
+ if (Bool.and(open, Predicate.isNotUndefined(last)))
582
+ spine.retarget(UndefinedOr.getOrThrow(last));
583
+ });
584
+ unwatch = () => {
585
+ unwatchName();
586
+ unwatchGate();
587
+ };
588
+ }
589
+ // Its Animation controls reach it from its mount to its release (#798, Q4): each change sets its
590
+ // entry, re-resolves it on the change's override and tells the watches, its Variant children's
591
+ // triggers among them; their stop ends its legs where they are.
592
+ unsubscribe = UndefinedOr.match(controls, {
593
+ onUndefined: () => constVoid,
594
+ onDefined: (c) => AnimationControls.subscribe(c, AnimationControls.controlledSubject((change) => {
595
+ node.setChange(change);
596
+ view = viewOf();
597
+ retarget();
598
+ recheck();
599
+ }, { custom: () => config.custom, snapshot }, spine.stop)),
518
600
  });
519
- unwatch = () => {
520
- unwatchName();
521
- unwatchGate();
522
- };
601
+ // Its drag and its gestures wanted at mount, then its first Retarget: from `initial` to `animate`.
602
+ drag.check();
603
+ gestures.check();
604
+ startsDrag.check();
605
+ retarget();
606
+ }
607
+ catch (defect) {
608
+ // Its start's failure is its own (#997, ADR-0074), wherever it runs (its Mount's, its parent's, its
609
+ // presence's delivery): what it made released, its presence record too, reported once with its id,
610
+ // and never thrown on. Its node reports it settled, as its spine never will, so a parent waiting
611
+ // `afterChildren` goes on. Its release then drops what remains, its node in the tree.
612
+ releaseMade();
613
+ unregisterRecord();
614
+ node.onFrame(true);
615
+ reporterOf(provider, id)(MOUNT_FAILED, Cause.die(defect));
616
+ return () => dropNode(element);
523
617
  }
524
- // Its Animation controls reach it from its mount to its release (#798, Q4): each change sets its
525
- // entry, re-resolves it on the change's override and tells the watches, its Variant children's
526
- // triggers among them; their stop ends its legs where they are.
527
- const unsubscribe = UndefinedOr.match(controls, {
528
- onUndefined: () => constVoid,
529
- onDefined: (c) => AnimationControls.subscribe(c, AnimationControls.controlledElement((change) => {
530
- node.setChange(change);
531
- view = viewOf();
532
- retarget();
533
- recheck();
534
- }, { custom: () => config.custom, snapshot }, spine.stop)),
535
- });
536
- // Its drag and its gestures wanted at mount, then its first Retarget: from `initial` to `animate`.
537
- drag.check();
538
- gestures.check();
539
- startsDrag.check();
540
- retarget();
541
618
  return () => {
542
- unsubscribe();
543
- released();
544
- unwatch();
545
- unfollowText();
546
- unfollowTemplate();
547
- unregisterAnimatable();
548
- uninstall();
549
- stopRechecks();
550
- releaseScope();
551
- startsDrag.dispose();
552
- UndefinedOr.map(MutableRef.get(item), (joined) => joined.dispose());
553
- drag.dispose();
554
- gestures.dispose();
555
- UndefinedOr.map(MutableRef.get(layoutNode), (node) => node.dispose());
556
- UndefinedOr.map(MutableRef.get(unregisterValues), (unregister) => unregister());
557
- spine.dispose();
558
- unregister();
619
+ releaseMade();
559
620
  dropNode(element);
560
621
  };
561
622
  };