@wardic/foldkit 1.3.0 → 1.3.2

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.
@@ -14,6 +14,7 @@ import * as Arr from "effect/Array"
14
14
  import * as Cause from "effect/Cause"
15
15
  import * as Channel from "effect/Channel"
16
16
  import * as Effect from "effect/Effect"
17
+ import { constVoid } from "effect/Function"
17
18
  import * as Predicate from "effect/Predicate"
18
19
  import type * as Scheduler from "effect/Scheduler"
19
20
  import * as Scope from "effect/Scope"
@@ -21,11 +22,11 @@ import * as Stream from "effect/Stream"
21
22
  import type { MountAction } from "foldkit/mount"
22
23
  import type * as Ward from "../Ward.ts"
23
24
  import { patchTouched } from "./delivery.ts"
24
- import { mountElement } from "./element.ts"
25
+ import { MOUNT_FAILED, mountElement } from "./element.ts"
25
26
  import { mountPresence } from "./presence.ts"
26
27
  import { mountReorderGroup } from "./reorder.ts"
27
28
  import { flushPresences, joined, type Provider, providerFor, type State } from "./services.ts"
28
- import { markMounted } from "./tree.ts"
29
+ import { dropNode, markMounted } from "./tree.ts"
29
30
 
30
31
  /** The names a Scene test resolves Wardic's Mounts by (decision 36). */
31
32
  export const PROVIDER_NAME = "Wardic.Provider"
@@ -36,9 +37,6 @@ export const REORDER_NAME = "Wardic.Reorder"
36
37
  /** A Mount's `f`: what runs on the element. */
37
38
  type MountFn<Message> = MountAction<Message>["f"]
38
39
 
39
- /** What a Mount's setup failing is reported as: contained at the binding's edge (decision 13), the element left at its Starting style. */
40
- const FAILED = "wardic.mount: a w.* element's mount failed; it stays at its Starting style"
41
-
42
40
  /** What a Mount whose setup failed pulls: nothing, ever, so its Stream stays open, sending nothing. */
43
41
  const OPEN: Effect.Effect<never> = Effect.never
44
42
 
@@ -90,40 +88,67 @@ const mailbox = <Message>(): Mailbox<Message> => {
90
88
  return { offer, pull }
91
89
  }
92
90
 
93
- /** `element` mounted on `on`, its release a finalizer of `scope`: the pull of its Messages. */
91
+ /**
92
+ * `element` mounted on `on`, its release a finalizer of `scope`: the pull of its Messages. A part that
93
+ * throws is reported, what was made before it released at once (#997, ADR-0074); its count out of the
94
+ * Provider still runs when it leaves.
95
+ */
94
96
  const mountOn = <Message>(state: State, on: Provider, element: Element, scope: Scope.Scope): Effect.Effect<Effect.Effect<Arr.NonEmptyReadonlyArray<Message>>> => {
95
97
  // The element's callbacks (PR 2) leave as Messages through its own Stream (P7).
96
98
  const box = mailbox<Message>()
97
99
  const offer = box.offer
100
+ // Counted in, and out once it leaves, whatever its parts make: the Provider's last member's leave disposes it.
98
101
  const left = joined(state, on, element)
99
102
  const isPaused = on.paused
103
+ // Its parts' releases, each set as it's made (#997): one that throws runs those made before it at once.
104
+ let release: () => void = constVoid
105
+ let releasePresence: () => void = constVoid
106
+ let releaseReorder: () => void = constVoid
107
+ const releaseParts = (): void => {
108
+ releaseReorder()
109
+ releasePresence()
110
+ release()
111
+ }
112
+ // One finalizer, added before any part is made, so `left` runs at its leave whatever they made.
113
+ const released = Effect.sync(() => {
114
+ releaseParts()
115
+ left()
116
+ })
100
117
  // The live presences deliver before this element registers anything (#840): Foldkit runs a Mount
101
118
  // in the patch's task, once the patch has committed and before the observers are told, and a
102
119
  // presence this element carries starts its children at its own mount, a `layoutId` twin among
103
120
  // them, which measures the one it takes over from where it is then: in a `popLayout` leaver an
104
121
  // outer presence hasn't pinned yet. Delivered before this element's key is registered, a key
105
122
  // re-entering during its exit is matched at the delivery its enrollment brings, a microtask on.
106
- flushPresences(state)
107
- const release = mountElement(on, state, element as HTMLElement, isPaused, offer, state.rootConfig)
108
- markMounted(element)
109
- // Its presence, when it carries `w.Presence` (decision 29): its children's exits, run by this Mount.
110
- const releasePresence = mountPresence(on, state, element, isPaused, offer)
111
- // Its Reorder group, when it carries `w.ReorderGroup` (decision 28): made by its first item's Mount or here, released here.
112
- const releaseReorder = mountReorderGroup(on, element as HTMLElement, isPaused, offer)
113
- const released = Effect.sync(() => {
114
- releaseReorder()
115
- releasePresence()
116
- release()
117
- left()
118
- })
119
- return Effect.map(Scope.addFinalizer(scope, released), () => {
123
+ const parts = Effect.sync(() => {
124
+ flushPresences(state)
125
+ release = mountElement(on, state, element as HTMLElement, isPaused, offer, state.rootConfig)
126
+ markMounted(element)
127
+ // Its presence, when it carries `w.Presence` (decision 29): its children's exits, run by this Mount.
128
+ releasePresence = mountPresence(on, state, element, isPaused, offer)
129
+ // Its Reorder group, when it carries `w.ReorderGroup` (decision 28): made by its first item's Mount or here, released here.
130
+ releaseReorder = mountReorderGroup(on, element as HTMLElement, isPaused, offer)
120
131
  patchTouched()
121
132
  return box.pull
122
133
  })
134
+ // A part that threw: those made before it released at once, and its node in the tree dropped, made
135
+ // before its start, when what threw came first. Reported on the Provider's runtime, the app's
136
+ // reporters, as its start's failure is (#997): this fiber runs on Foldkit's.
137
+ const unmade = (defect: unknown): Effect.Effect<Effect.Effect<never>> =>
138
+ Effect.sync(() => {
139
+ releaseParts()
140
+ dropNode(element)
141
+ release = constVoid
142
+ releasePresence = constVoid
143
+ releaseReorder = constVoid
144
+ on.runtime.runFork(Report.failure(MOUNT_FAILED, Cause.die(defect)))
145
+ return OPEN
146
+ })
147
+ return Scope.addFinalizer(scope, released).pipe(Effect.andThen(parts), Effect.catchDefect(unmade))
123
148
  }
124
149
 
125
- /** A Mount's setup failing: reported (decision 13), and its Stream left open. */
126
- const failed = (defect: unknown): Effect.Effect<Effect.Effect<never>> => Effect.as(Report.failure(FAILED, Cause.die(defect)), OPEN)
150
+ /** A Mount's setup failing before it has a Provider: reported (decision 13), and its Stream left open. */
151
+ const failed = (defect: unknown): Effect.Effect<Effect.Effect<never>> => Effect.as(Report.failure(MOUNT_FAILED, Cause.die(defect)), OPEN)
127
152
 
128
153
  /**
129
154
  * Wardic's part of a `w.*` element's Mount: gets the Handle's Provider, building it when it's the first
@@ -196,7 +196,8 @@ const makePresence = (provider: Provider, state: State, maker: PresenceMakerShap
196
196
  presenceAffectsLayout: () => optionsNow().presenceAffectsLayout,
197
197
  // The last leaver has gone: the children `wait` held go on the page, in a delivery of their own. A
198
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.
199
+ // `leaveEnds`), where a delivery can't run. Solid 1's release is a signal, read later, for the same
200
+ // reason; Solid 2's is a microtask later too when it's made inside `present` (#987).
200
201
  release: () => {
201
202
  queueMicrotask(() => deliver([], true))
202
203
  },
@@ -13,7 +13,7 @@ import type * as VariantTree from "@wardic/core/unstable/VariantTree"
13
13
  import * as LayoutGroup from "@wardic/dom/unstable/LayoutGroup"
14
14
  import * as Arr from "effect/Array"
15
15
  import * as Bool from "effect/Boolean"
16
- import { constUndefined, constVoid, identity } from "effect/Function"
16
+ import { constUndefined, identity } from "effect/Function"
17
17
  import * as Option from "effect/Option"
18
18
  import * as Predicate from "effect/Predicate"
19
19
  import * as UndefinedOr from "effect/UndefinedOr"
@@ -96,7 +96,7 @@ const waiting = new WeakMap<Element, Array<Waiting>>()
96
96
  /** A start waiting its turn: a child's for its nearest `w.*` ancestor's here, or for its presence's delivery (`presence.ts`). */
97
97
  export interface Waiting {
98
98
  readonly element: Element
99
- /** Starts the element; returns its release. */
99
+ /** Starts the element, never throwing: a start that fails is reported by `mountElement` (#997). Returns its release. */
100
100
  readonly begin: () => () => void
101
101
  /** The start's release, once it ran. */
102
102
  release: (() => void) | undefined
@@ -116,30 +116,21 @@ export const releaseWaiting = (entry: Waiting): void => {
116
116
 
117
117
  /**
118
118
  * `element`'s own start, `begin`, run now, then the starts of the children that waited for it, in their
119
- * Mounts' order. A start that fails is the one element's (decision 13): the parent's failing still starts
120
- * the children, and a child's failing starts its siblings, its defect thrown on a microtask for the
121
- * page's error handling, as nothing of the parent's Mount is to blame. Returns the release.
119
+ * Mounts' order. Returns the release.
122
120
  */
121
+ // A start doesn't throw: one that fails is its own element's, reported and released by `mountElement`'s
122
+ // guard (#997, ADR-0074; decision 13), so a parent's failing still starts its children, and a child's its
123
+ // siblings. Before #997 a child's defect was thrown on a microtask, unreported, half of it still mounted.
123
124
  const startNow = (element: Element, begin: () => () => void): (() => void) => {
124
- let release: () => void = constVoid
125
- try {
126
- release = begin()
127
- } finally {
128
- started.add(element)
129
- UndefinedOr.map(waiting.get(element), (children) => {
130
- waiting.delete(element)
131
- Arr.forEach(children, (child) => {
132
- if (child.gone) return
133
- try {
134
- child.release = startNow(child.element, child.begin)
135
- } catch (defect) {
136
- queueMicrotask(() => {
137
- throw defect
138
- })
139
- }
140
- })
125
+ const release = begin()
126
+ started.add(element)
127
+ UndefinedOr.map(waiting.get(element), (children) => {
128
+ waiting.delete(element)
129
+ Arr.forEach(children, (child) => {
130
+ if (child.gone) return
131
+ child.release = startNow(child.element, child.begin)
141
132
  })
142
- }
133
+ })
143
134
  return () => {
144
135
  started.delete(element)
145
136
  release()
@@ -12,6 +12,7 @@
12
12
  */
13
13
  import * as AnimationControls from "@wardic/core/unstable/AnimationControls"
14
14
  import type * as WardConfig from "@wardic/dom/unstable/WardConfig"
15
+ import * as Spine from "@wardic/core/unstable/Spine"
15
16
  import * as VariantTarget from "@wardic/core/unstable/VariantTarget"
16
17
  import * as VariantTree from "@wardic/core/unstable/VariantTree"
17
18
  import * as Arr from "effect/Array"
@@ -42,6 +43,11 @@ export interface VariantNode {
42
43
  * hands down what it takes.
43
44
  */
44
45
  readonly bindGestures: (states: () => VariantTree.GestureStates) => void
46
+ /**
47
+ * Where what its orchestration resolves is told, a variant that throws among it: its element's
48
+ * failures, handed over by its Mount, as the node is made at the first ask, maybe before it.
49
+ */
50
+ readonly reportsTo: (failures: Spine.VariantFailures) => void
45
51
  /** Its registration with the ancestor it joined, given up: at its element's release. */
46
52
  readonly dispose: () => void
47
53
  /**
@@ -74,6 +80,7 @@ interface OwnOrchestration {
74
80
  readonly canStartFor: (name: string) => boolean
75
81
  readonly settledFor: (names: ReadonlyArray<string>, done: boolean) => void
76
82
  readonly bindGestures: (states: () => VariantTree.GestureStates) => void
83
+ readonly reportsTo: (failures: Spine.VariantFailures) => void
77
84
  }
78
85
 
79
86
  /** No gesture on: what an element hands down before its Mount binds its gestures. */
@@ -102,6 +109,11 @@ const ownOrchestration = (
102
109
  const cfg = read.config
103
110
  const change = read.change
104
111
  const config = cfg()
112
+ // Where what it resolves is told (#979): its element's failures, once its Mount hands them over.
113
+ let reportTo: VariantTarget.Orchestrated | undefined
114
+ const orchestrated: VariantTarget.Orchestrated = (entry, failure) => {
115
+ if (Predicate.isNotUndefined(reportTo)) reportTo(entry, failure)
116
+ }
105
117
  // Children in mount order (a Set keeps insertion order); the slots are rewritten at each trigger.
106
118
  const records = new Set<ChildRecord>()
107
119
  let unsettled = 0
@@ -197,8 +209,8 @@ const ownOrchestration = (
197
209
  return VariantTree.orchestrationAt(UndefinedOr.getOrThrow(at), () => {
198
210
  const c = cfg()
199
211
  const own = UndefinedOr.match(VariantTree.topGestureName(VariantTree.activeNamesOf(gestures())), {
200
- onDefined: (name) => VariantTarget.ownTransitionOf(undefined, name, c.variants as WardConfig.WardicVariants | undefined, c.custom),
201
- onUndefined: () => VariantTarget.ownTransitionOf(c.animate as VariantTarget.Entry, variant(), c.variants as WardConfig.WardicVariants | undefined, c.custom),
212
+ onDefined: (name) => VariantTarget.ownTransitionOf(undefined, name, c.variants as WardConfig.WardicVariants | undefined, c.custom, orchestrated),
213
+ onUndefined: () => VariantTarget.ownTransitionOf(c.animate as VariantTarget.Entry, variant(), c.variants as WardConfig.WardicVariants | undefined, c.custom, orchestrated),
202
214
  })
203
215
  return VariantTarget.transitionOfTarget(own, c.transition)
204
216
  })
@@ -226,7 +238,7 @@ const ownOrchestration = (
226
238
  }
227
239
  const kept = byName.get(name)
228
240
  if (Predicate.isNotUndefined(kept)) return kept
229
- const made = VariantTree.nameConfigOf(VariantTarget.transitionOfTarget(VariantTarget.ownTransitionOf(undefined, name, c.variants as WardConfig.WardicVariants | undefined, c.custom), c.transition))
241
+ const made = VariantTree.nameConfigOf(VariantTarget.transitionOfTarget(VariantTarget.ownTransitionOf(undefined, name, c.variants as WardConfig.WardicVariants | undefined, c.custom, orchestrated), c.transition))
230
242
  byName.set(name, made)
231
243
  return made
232
244
  }
@@ -272,9 +284,36 @@ const ownOrchestration = (
272
284
  bindGestures: (states) => {
273
285
  MutableRef.set(ownStates, states)
274
286
  },
287
+ reportsTo: (failures) => {
288
+ reportTo = Spine.orchestrationOf(failures)
289
+ },
275
290
  }
276
291
  }
277
292
 
293
+ /**
294
+ * A Variant child's place with the ancestor it joined: its context, its registration, and where its
295
+ * stagger reports its parent's `delayChildren` throwing (#979), its element's failures once its Mount
296
+ * hands them over.
297
+ */
298
+ interface Joined {
299
+ readonly context: VariantTree.WardicVariantContextValue
300
+ readonly registration: VariantTree.WardicChildRegistration
301
+ delayFailures: VariantTree.DelayFailures | undefined
302
+ }
303
+
304
+ /**
305
+ * Where a node hands its element's failures: its orchestration's resolutions, and, for a Variant child, its
306
+ * stagger. An element that's neither keeps nothing, as most elements, at mount, have nothing to keep.
307
+ */
308
+ const reportsToOf = (joined: Joined | undefined, orchestration: { readonly reportsTo: (failures: Spine.VariantFailures) => void } | undefined): ((failures: Spine.VariantFailures) => void) =>
309
+ UndefinedOr.match(joined, {
310
+ onUndefined: () => UndefinedOr.match(orchestration, { onUndefined: () => constVoid, onDefined: (o) => o.reportsTo }),
311
+ onDefined: (child) => (failures: Spine.VariantFailures) => {
312
+ child.delayFailures = failures
313
+ if (Predicate.isNotUndefined(orchestration)) orchestration.reportsTo(failures)
314
+ },
315
+ })
316
+
278
317
  /**
279
318
  * An element's place in variant orchestration on the client, from its delivered config (read on each
280
319
  * ask) and the context the nearest Wardic ancestor provides. `notify` re-checks the port's watches.
@@ -296,7 +335,7 @@ export const variantNodeOf = (delivered: () => Delivered, parent: VariantTree.Wa
296
335
  const change = VariantTree.controlsChangeOf(Bool.match(controlled, { onTrue: () => ownChange, onFalse: constUndefined }), inherited)
297
336
  const read: Read = { config: () => configured(delivered()), change }
298
337
  // A Variant child registers with the ancestor it joins, for its stagger slot and its `when`.
299
- const joined = UndefinedOr.map(inherited, (context) => ({ context, registration: context.register() }))
338
+ const joined = UndefinedOr.map(inherited, (context): Joined => ({ context, registration: context.register(), delayFailures: undefined }))
300
339
  const orchestration = Bool.match(VariantTree.hasOwnVariants(config), {
301
340
  onFalse: constUndefined,
302
341
  onTrue: () => ownOrchestration(read, parent, inherited, notify),
@@ -307,10 +346,10 @@ export const variantNodeOf = (delivered: () => Delivered, parent: VariantTree.Wa
307
346
  return {
308
347
  provided: UndefinedOr.match(orchestration, { onDefined: (o) => o.context, onUndefined: () => VariantTree.passedThrough(config, parent) }),
309
348
  inherited,
310
- extraDelay: UndefinedOr.map(joined, ({ context, registration }) => (name: string | undefined) => {
311
- const s = VariantTree.staggerOfName(context, name)
312
- const n = registration.total()
313
- return VariantTree.childDelayOf(s, registration.index(), n)
349
+ extraDelay: UndefinedOr.map(joined, (child) => (name: string | undefined) => {
350
+ const s = VariantTree.staggerOfName(child.context, name)
351
+ const n = child.registration.total()
352
+ return VariantTree.childDelayOf(s, child.registration.index(), n, child.delayFailures)
314
353
  }),
315
354
  canStart: () =>
316
355
  UndefinedOr.match(inherited, { onUndefined: constTrue, onDefined: (context) => context.childrenMayStart() }) &&
@@ -321,6 +360,7 @@ export const variantNodeOf = (delivered: () => Delivered, parent: VariantTree.Wa
321
360
  },
322
361
  names: namesOf(joined, orchestration),
323
362
  bindGestures: UndefinedOr.match(orchestration, { onUndefined: () => constVoid, onDefined: (o) => o.bindGestures }),
363
+ reportsTo: reportsToOf(joined, orchestration),
324
364
  dispose: UndefinedOr.match(joined, { onUndefined: () => constVoid, onDefined: ({ registration }) => registration.dispose }),
325
365
  configured,
326
366
  change,