@solidjs/signals 2.0.0-beta.17 → 2.0.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +4 -3
  2. package/dist/dev.js +2956 -646
  3. package/dist/node.cjs +6396 -3974
  4. package/dist/prod/affects.js +222 -0
  5. package/dist/prod/boundaries.js +568 -0
  6. package/dist/prod/core/action.js +83 -0
  7. package/dist/prod/core/async.js +394 -0
  8. package/dist/prod/core/constants.js +78 -0
  9. package/dist/prod/core/context.js +67 -0
  10. package/dist/prod/core/core.js +772 -0
  11. package/dist/prod/core/dev.js +3 -0
  12. package/dist/prod/core/effect.js +145 -0
  13. package/dist/prod/core/error.js +59 -0
  14. package/dist/prod/core/external.js +98 -0
  15. package/dist/prod/core/graph.js +91 -0
  16. package/dist/prod/core/heap.js +132 -0
  17. package/dist/prod/core/invariants.js +42 -0
  18. package/dist/prod/core/lanes.js +130 -0
  19. package/dist/prod/core/optimistic.js +265 -0
  20. package/dist/prod/core/owner.js +293 -0
  21. package/dist/prod/core/scheduler.js +689 -0
  22. package/dist/prod/core/verdict.js +293 -0
  23. package/dist/prod/index.js +45 -0
  24. package/dist/prod/map.js +264 -0
  25. package/dist/prod/signals.js +389 -0
  26. package/dist/prod/store/optimistic.js +149 -0
  27. package/dist/prod/store/projection.js +184 -0
  28. package/dist/prod/store/reconcile.js +392 -0
  29. package/dist/prod/store/store.js +739 -0
  30. package/dist/prod/store/storePath.js +103 -0
  31. package/dist/prod/store/utils.js +297 -0
  32. package/dist/types/affects.d.ts +47 -0
  33. package/dist/types/boundaries.d.ts +8 -8
  34. package/dist/types/core/async.d.ts +5 -2
  35. package/dist/types/core/constants.d.ts +8 -0
  36. package/dist/types/core/core.d.ts +12 -69
  37. package/dist/types/core/dev.d.ts +1 -1
  38. package/dist/types/core/external.d.ts +0 -30
  39. package/dist/types/core/heap.d.ts +8 -0
  40. package/dist/types/core/index.d.ts +3 -2
  41. package/dist/types/core/invariants.d.ts +10 -0
  42. package/dist/types/core/optimistic.d.ts +6 -0
  43. package/dist/types/core/owner.d.ts +8 -0
  44. package/dist/types/core/scheduler.d.ts +53 -5
  45. package/dist/types/core/types.d.ts +22 -5
  46. package/dist/types/core/verdict.d.ts +2 -0
  47. package/dist/types/index.d.ts +1 -0
  48. package/dist/types/store/projection.d.ts +0 -1
  49. package/dist/types/store/store.d.ts +32 -14
  50. package/dist/types-cjs/affects.d.cts +47 -0
  51. package/dist/types-cjs/boundaries.d.cts +8 -8
  52. package/dist/types-cjs/core/async.d.cts +5 -2
  53. package/dist/types-cjs/core/constants.d.cts +8 -0
  54. package/dist/types-cjs/core/core.d.cts +12 -69
  55. package/dist/types-cjs/core/dev.d.cts +1 -1
  56. package/dist/types-cjs/core/external.d.cts +0 -30
  57. package/dist/types-cjs/core/heap.d.cts +8 -0
  58. package/dist/types-cjs/core/index.d.cts +3 -2
  59. package/dist/types-cjs/core/invariants.d.cts +10 -0
  60. package/dist/types-cjs/core/optimistic.d.cts +6 -0
  61. package/dist/types-cjs/core/owner.d.cts +8 -0
  62. package/dist/types-cjs/core/scheduler.d.cts +53 -5
  63. package/dist/types-cjs/core/types.d.cts +22 -5
  64. package/dist/types-cjs/core/verdict.d.cts +2 -0
  65. package/dist/types-cjs/index.d.cts +1 -0
  66. package/dist/types-cjs/store/projection.d.cts +0 -1
  67. package/dist/types-cjs/store/store.d.cts +32 -14
  68. package/package.json +8 -6
  69. package/dist/prod.js +0 -4406
@@ -0,0 +1,568 @@
1
+ import { runWithOwner, signal, untrack, read, computed, setSignal, recompute } from "./core/core.js";
2
+
3
+ import { NotReadyError, unwrapStatusError } from "./core/error.js";
4
+
5
+ import { createOwner, cleanup } from "./core/owner.js";
6
+
7
+ import { Queue, haltReactivity, schedule } from "./core/scheduler.js";
8
+
9
+ import { getContext, setContext, createContext } from "./core/context.js";
10
+
11
+ import { STATUS_PENDING, STATUS_ERROR, REACTIVE_DISPOSED, CONFIG_AUTO_DISPOSE } from "./core/constants.js";
12
+
13
+ import "./core/invariants.js";
14
+
15
+ import "./core/verdict.js";
16
+
17
+ import "./core/effect.js";
18
+
19
+ import { accessor } from "./signals.js";
20
+
21
+ function boundaryComputed(e, t) {
22
+ const r = computed(e, {
23
+ lazy: true
24
+ });
25
+ r.C = (e, t) => {
26
+ // Use passed values if provided, otherwise read from node
27
+ const n = e !== undefined ? e : r.i;
28
+ const s = t !== undefined ? t : r.k;
29
+ // Notify both status dimensions like a render effect does; the queue chain
30
+ // consumes this boundary's own type and forwards the remainder upward until
31
+ // a boundary that handles it is found.
32
+ r.i &= ~r.O;
33
+ const i = r.v.notify(r, STATUS_PENDING | STATUS_ERROR, n, s);
34
+ // The queue is the only propagation channel: a foreign status must not stay
35
+ // reader-visible on the tree, or reads re-throw it across the boundary and
36
+ // link unrelated ambient contexts (the #2809 nested-boundary loop). Deps are
37
+ // untouched, so the tree still recomputes when the foreign source settles.
38
+ const o = n & ~r.O & (STATUS_PENDING | STATUS_ERROR);
39
+ if (o) {
40
+ r.i &= ~o;
41
+ if (r.k === s && !(r.i & (STATUS_PENDING | STATUS_ERROR))) r.k = undefined;
42
+ }
43
+ // An ERROR the chain could not deliver to any boundary is uncaught. The
44
+ // scrub above already removed it from reader-visible state, so without
45
+ // escalation here it would vanish entirely (#2884) — halt-and-throw,
46
+ // exactly like an unhandled effect error.
47
+ if (!i && n & STATUS_ERROR) {
48
+ haltReactivity(unwrapStatusError(s));
49
+ throw s;
50
+ }
51
+ };
52
+ r.O = t;
53
+ r.U &= ~CONFIG_AUTO_DISPOSE;
54
+ recompute(r, true);
55
+ return r;
56
+ }
57
+
58
+ function createBoundChildren(e, t, r, n) {
59
+ const s = e.v;
60
+ s.addChild(e.v = r);
61
+ cleanup(() => s.removeChild(e.v));
62
+ return runWithOwner(e, () => {
63
+ const e = computed(t);
64
+ return boundaryComputed(() => flatten(read(e)), n);
65
+ });
66
+ }
67
+
68
+ const ON_INIT = Symbol();
69
+
70
+ const RevealControllerContext = /* @__PURE__ */ createContext(null);
71
+
72
+ let _revealUsed = false;
73
+
74
+ const FALSE_ACCESSOR = () => false;
75
+
76
+ const SEQUENTIAL_ACCESSOR = () => "sequential";
77
+
78
+ function isRevealController(e) {
79
+ return e instanceof RevealController;
80
+ }
81
+
82
+ function isSlotReady(e) {
83
+ return isRevealController(e) ? e.B() : e.W.size === 0 && !e.L;
84
+ }
85
+
86
+ function isSlotMinimallyReady(e) {
87
+ return isRevealController(e) ? e.q() : isSlotReady(e);
88
+ }
89
+
90
+ function setSlotState(e, t, r, n) {
91
+ setSignal(e.V, r);
92
+ setSignal(e.H, n);
93
+ if (isRevealController(e)) {
94
+ if (!r && e.J === t) e.J = undefined;
95
+ return e.K(r, n);
96
+ }
97
+ if (!r && e.X === t && e.Y) e.X = undefined;
98
+ }
99
+
100
+ class RevealController {
101
+ Z;
102
+ ee;
103
+ te=[];
104
+ J;
105
+ V=signal(false, {
106
+ ownedWrite: true,
107
+ re: true
108
+ });
109
+ H=signal(false, {
110
+ ownedWrite: true,
111
+ re: true
112
+ });
113
+ ne=true;
114
+ se=true;
115
+ ie=false;
116
+ constructor(e, t) {
117
+ this.Z = e;
118
+ this.ee = t;
119
+ }
120
+ oe(e) {
121
+ for (let t = 0; t < this.te.length; t++) {
122
+ const r = this.te[t];
123
+ if ((isRevealController(r) ? r.J : r.X) !== this) continue;
124
+ if (e(r) === false) return false;
125
+ }
126
+ return true;
127
+ }
128
+ B() {
129
+ return this.oe(isSlotReady);
130
+ }
131
+ /**
132
+ * "Minimally ready" = this group has something visible to show under its own policy.
133
+ * Used by an enclosing `together` group to decide when it can release.
134
+ * - `together`: every direct slot is minimally ready.
135
+ * - `sequential`: the first owned slot is minimally ready (frontier can advance).
136
+ * - `natural`: any owned slot is minimally ready.
137
+ */ q() {
138
+ const e = untrack(this.Z);
139
+ if (e === "together") return this.oe(isSlotMinimallyReady);
140
+ if (e === "natural") {
141
+ let e = false;
142
+ let t = false;
143
+ this.oe(r => {
144
+ e = true;
145
+ if (isSlotMinimallyReady(r)) {
146
+ t = true;
147
+ return false;
148
+ }
149
+ });
150
+ return !e || t;
151
+ }
152
+ // sequential: only the first owned slot matters.
153
+ let t = true;
154
+ this.oe(e => {
155
+ t = isSlotMinimallyReady(e);
156
+ return false;
157
+ });
158
+ return t;
159
+ }
160
+ le(e) {
161
+ if (this.te.includes(e)) return;
162
+ this.te.push(e);
163
+ const t = untrack(this.Z);
164
+ setSignal(e.V, true), setSignal(e.H, t === "sequential" ? !!untrack(this.ee) : false);
165
+ untrack(() => this.K());
166
+ }
167
+ ae(e) {
168
+ const t = this.te.indexOf(e);
169
+ if (t >= 0) this.te.splice(t, 1);
170
+ untrack(() => this.K());
171
+ }
172
+ K(e, t) {
173
+ if (this.ie) return;
174
+ this.ie = true;
175
+ const r = this.ne;
176
+ const n = this.se;
177
+ try {
178
+ const r = e ?? read(this.V), n = untrack(this.Z), s = n === "sequential" && !!untrack(this.ee), i = t ?? s;
179
+ if (r) {
180
+ // Held by an outer group. Propagate the hold (and whatever collapsed policy
181
+ // the outer asked for) down the whole subtree. Inner order is ignored while
182
+ // held; it resumes once the outer releases us.
183
+ this.oe(e => setSlotState(e, this, true, i));
184
+ } else if (n === "natural") {
185
+ // Each child reveals based on its own readiness. A nested controller slot
186
+ // is released to run its own order locally — we bypass setSlotState for it
187
+ // so the parent backpointer survives for upward readiness notifications.
188
+ this.oe(e => {
189
+ if (isRevealController(e)) {
190
+ setSignal(e.H, false);
191
+ setSignal(e.V, false);
192
+ e.K(false, false);
193
+ } else {
194
+ setSlotState(e, this, !isSlotReady(e), false);
195
+ }
196
+ });
197
+ } else if (n === "together") {
198
+ // Release when every direct slot is minimally ready (has something to show
199
+ // under its own order). A fully-ready inner together is minimally ready;
200
+ // sequential's first slot being ready is minimally ready; natural having any
201
+ // ready child is minimally ready. This lets `together` guarantee a single
202
+ // cohesive reveal without waiting for every grandchild.
203
+ const e = this.oe(isSlotMinimallyReady);
204
+ this.oe(t => setSlotState(t, this, !e, false));
205
+ } else {
206
+ let e = false;
207
+ this.oe(t => {
208
+ if (e) return setSlotState(t, this, true, s);
209
+ if (isSlotReady(t)) return setSlotState(t, this, false, false);
210
+ e = true;
211
+ // Frontier slot. For a leaf, holding `_disabled=true` is what keeps its
212
+ // fallback visible. For a composite, we instead release it so it runs
213
+ // its own order locally — its leaves will each show their own fallback
214
+ // until their data lands. Outer still waits on full readiness before
215
+ // advancing past this slot, and we bypass setSlotState so the parent
216
+ // backpointer survives for upward readiness notifications.
217
+ if (isRevealController(t)) {
218
+ setSignal(t.H, false);
219
+ setSignal(t.V, false);
220
+ t.K(false, false);
221
+ } else {
222
+ setSlotState(t, this, true, false);
223
+ }
224
+ });
225
+ }
226
+ } finally {
227
+ this.ne = this.B();
228
+ this.se = this.q();
229
+ this.ie = false;
230
+ }
231
+ if (this.J && (r !== this.ne || n !== this.se)) this.J.K();
232
+ }
233
+ }
234
+
235
+ class CollectionQueue extends Queue {
236
+ ue;
237
+ W=new Set;
238
+ ce;
239
+ L=true;
240
+ V=signal(false, {
241
+ ownedWrite: true,
242
+ re: true
243
+ });
244
+ k;
245
+ H=signal(false, {
246
+ ownedWrite: true,
247
+ re: true
248
+ });
249
+ X;
250
+ Y=false;
251
+ fe;
252
+ he=ON_INIT;
253
+ constructor(e) {
254
+ super();
255
+ this.ue = e;
256
+ }
257
+ run(e) {
258
+ if (!e || read(this.V) && (!_revealUsed || read(this.H))) return;
259
+ return super.run(e);
260
+ }
261
+ notify(e, t, r, n) {
262
+ if (!(t & this.ue)) return super.notify(e, t, r, n);
263
+ if (this.Y && this.fe) {
264
+ const e = untrack(() => {
265
+ try {
266
+ return this.fe();
267
+ } catch {
268
+ return ON_INIT;
269
+ }
270
+ });
271
+ if (e !== this.he) {
272
+ this.he = e;
273
+ this.Y = false;
274
+ this.W.clear();
275
+ }
276
+ }
277
+ // Routing is dimension-independent: each boundary consumes only its own
278
+ // status dimension from the mask (`type &= ~collectionType` below) and
279
+ // forwards the remainder up the queue chain. An error inside a `Loading`
280
+ // needs no special rule — the ERROR dimension survives consumption here and
281
+ // reaches the `Errored` that catches it natively, and `flags & collectionType`
282
+ // keeps this boundary from collecting a node that isn't actually pending.
283
+ // Symmetrically, a pending inside an `Errored` forwards on the PENDING
284
+ // dimension, while a status already caught by an inner boundary arrives with
285
+ // its dimension consumed from the mask and is correctly not re-routed
286
+ // (the Loading > Errored > content composition escape, #2856).
287
+ if (this.ue & STATUS_PENDING && this.Y) return super.notify(e, t, r, n);
288
+ if (r & this.ue) {
289
+ this.L = true;
290
+ const t = n?.source || e.k?.source;
291
+ if (t) {
292
+ const e = this.W.size === 0;
293
+ this.W.add(t);
294
+ if (e) setSignal(this.V, true);
295
+ if (this.ue & STATUS_ERROR) {
296
+ setSignal(this.k, unwrapStatusError(t.k));
297
+ }
298
+ }
299
+ }
300
+ t &= ~this.ue;
301
+ return t ? super.notify(e, t, r, n) : true;
302
+ }
303
+ Se() {
304
+ for (const e of this.W) {
305
+ if (e.u & REACTIVE_DISPOSED || !(e.i & this.ue) && !(this.ue & STATUS_ERROR && e.i & STATUS_PENDING)) this.W.delete(e);
306
+ }
307
+ if (!this.W.size) {
308
+ if (this.ue & STATUS_PENDING && this.L && !this.Y && this.ce) {
309
+ this.L = !!(this.ce.i & this.ue);
310
+ } else {
311
+ this.L = false;
312
+ }
313
+ if (!this.L) {
314
+ setSignal(this.V, false);
315
+ if (this.fe) {
316
+ try {
317
+ this.he = untrack(() => this.fe());
318
+ } catch {
319
+ /* value not yet committed — _prevOn stays stale, next notify will reset */}
320
+ }
321
+ }
322
+ }
323
+ if (_revealUsed) this.X?.K();
324
+ }
325
+ }
326
+
327
+ function createCollectionBoundary(e, t, r, n) {
328
+ const s = createOwner();
329
+ if (_revealUsed) setContext(RevealControllerContext, null, s);
330
+ const i = new CollectionQueue(e);
331
+ if (e === STATUS_ERROR) i.k = signal(undefined, {
332
+ ownedWrite: true,
333
+ re: true
334
+ });
335
+ if (n) i.fe = n;
336
+ const o = i.ce = createBoundChildren(s, t, i, e);
337
+ // Prime source tracking so reveal registration sees pending sources.
338
+ untrack(() => {
339
+ let t = false;
340
+ try {
341
+ read(o);
342
+ } catch (e) {
343
+ if (e instanceof NotReadyError) t = true; else throw e;
344
+ }
345
+ i.L = t || !!(o.i & e) || o.k instanceof NotReadyError;
346
+ });
347
+ const l = _revealUsed && e === STATUS_PENDING ? getContext(RevealControllerContext) : null;
348
+ if (l) {
349
+ i.X = l;
350
+ l.le(i);
351
+ cleanup(() => l.ae(i));
352
+ }
353
+ return accessor(computed(() => {
354
+ if (!read(i.V)) {
355
+ const e = read(o);
356
+ if (!untrack(() => read(i.V))) return i.Y = true, e;
357
+ }
358
+ // Collapsed reveal slots suppress their own output entirely; the
359
+ // renderer treats the hole as empty, so the cast never leaks to users
360
+ // outside a `createRevealOrder` scope.
361
+ if (_revealUsed && read(i.H)) return undefined;
362
+ return r(i);
363
+ },
364
+ // Boundary structure, not a user source: its value is fallback-or-content and
365
+ // legitimately swaps mid-hydration (reveal/resume), so it must never be frozen
366
+ // by snapshot capture. The tree no longer carries foreign status flags, so
367
+ // capture can't rely on PENDING to skip this node the way it used to.
368
+ {
369
+ re: true
370
+ }));
371
+ }
372
+
373
+ /**
374
+ * Lower-level primitive that backs the `<Loading>` flow control. Catches
375
+ * pending async reads inside `fn` and renders `fallback` until they settle.
376
+ *
377
+ * App code should use `<Loading fallback={...}>` instead — reach for this only
378
+ * when authoring custom boundary components.
379
+ *
380
+ * @param fn the tracked subtree
381
+ * @param fallback the fallback shown while async reads in `fn` are unresolved
382
+ * @param options `on` — accessor whose value scopes the boundary; when set,
383
+ * transitions caused by writes to other reactive sources are *not* caught
384
+ *
385
+ * @example
386
+ * ```tsx
387
+ * // Custom boundary component built on top of the primitive.
388
+ * function MyLoading(props: { fallback: JSX.Element; children: JSX.Element }) {
389
+ * return createLoadingBoundary(
390
+ * () => props.children,
391
+ * () => props.fallback
392
+ * ) as unknown as JSX.Element;
393
+ * }
394
+ * ```
395
+ */ function createLoadingBoundary(e, t, r) {
396
+ return createCollectionBoundary(STATUS_PENDING, e, () => t(), r?.on);
397
+ }
398
+
399
+ /**
400
+ * Lower-level primitive that backs the `<Errored>` flow control. Catches
401
+ * thrown errors inside `fn` and invokes `fallback(error, reset)` instead.
402
+ * `error` is an accessor for the latest captured error; `reset()` recomputes
403
+ * the failing sources so the boundary can attempt to recover.
404
+ *
405
+ * App code should use `<Errored fallback={...}>` instead — reach for this only
406
+ * when authoring custom boundary components.
407
+ *
408
+ * @example
409
+ * ```tsx
410
+ * // Custom boundary that wraps the primitive and adds telemetry.
411
+ * function TracedErrored(props: { fallback: (e: () => unknown) => JSX.Element; children: JSX.Element }) {
412
+ * return createErrorBoundary(
413
+ * () => props.children,
414
+ * (err, reset) => {
415
+ * reportError(err());
416
+ * return props.fallback(err);
417
+ * }
418
+ * ) as unknown as JSX.Element;
419
+ * }
420
+ * ```
421
+ */ function createErrorBoundary(e, t) {
422
+ return createCollectionBoundary(STATUS_ERROR, e, e => t(accessor(e.k), () => {
423
+ for (const t of e.W) recompute(t);
424
+ schedule();
425
+ }));
426
+ }
427
+
428
+ /**
429
+ * Coordinate the reveal timing of sibling loading boundaries.
430
+ *
431
+ * Accepts reactive accessors:
432
+ * - `order`: `"sequential"` (default) | `"together"` | `"natural"`.
433
+ * - `"sequential"` — classic frontier reveal: siblings reveal in registration order
434
+ * as each resolves; later siblings stay hidden until earlier ones complete.
435
+ * - `"together"` — every direct slot stays on its fallback until the whole group
436
+ * is "minimally ready" (each direct slot has produced its own first visible
437
+ * content under its own order), then the whole group releases at once.
438
+ * - `"natural"` — children reveal independently (as each resolves). At the top
439
+ * level this is a no-op compared to not using `createRevealOrder`; the mode
440
+ * exists for nesting, where the group registers as a single composite slot to
441
+ * any enclosing `createRevealOrder`.
442
+ * - `collapsed`: only meaningful when `order === "sequential"`. When set, tail siblings
443
+ * past the frontier suppress their own fallback output. Ignored under `"together"`
444
+ * and `"natural"` — those orders have no frontier.
445
+ *
446
+ * Nested `createRevealOrder` groups compose: the inner controller registers as a
447
+ * single slot in the outer controller and is held on its fallbacks until the outer
448
+ * releases that slot. Once released, the inner controller runs its own order locally
449
+ * over anything still pending. There is no opt-out from an outer hold.
450
+ *
451
+ * "Minimally ready" is what an order considers its first visible content:
452
+ * - `sequential` — frontier-0 is minimally ready (leaf: on resolve; nested: via its
453
+ * own minimal signal).
454
+ * - `together` — every direct slot is minimally ready.
455
+ * - `natural` — any direct slot has visible content (leaves on resolve; nested
456
+ * composites via their own minimal signal).
457
+ *
458
+ * @example
459
+ * ```ts
460
+ * // Primitive form of `<Reveal>` — coordinate sibling loading boundaries
461
+ * // programmatically. App code uses the JSX `<Reveal>` component instead.
462
+ * // Both options are accessors so they can react to state changes.
463
+ * createRevealOrder(
464
+ * () => renderSiblings(),
465
+ * { order: () => mode(), collapsed: () => true }
466
+ * );
467
+ * ```
468
+ */ function createRevealOrder(e, t) {
469
+ _revealUsed = true;
470
+ const r = createOwner();
471
+ const n = getContext(RevealControllerContext);
472
+ const s = t?.order || SEQUENTIAL_ACCESSOR, i = t?.collapsed || FALSE_ACCESSOR;
473
+ const o = new RevealController(s, i);
474
+ setContext(RevealControllerContext, o, r);
475
+ return runWithOwner(r, () => {
476
+ const t = e();
477
+ computed(() => {
478
+ s();
479
+ i();
480
+ o.K();
481
+ });
482
+ if (n) {
483
+ o.J = n;
484
+ n.le(o);
485
+ cleanup(() => n.ae(o));
486
+ }
487
+ return t;
488
+ });
489
+ }
490
+
491
+ /**
492
+ * Resolves a children value to its renderable form: unwraps zero-arg functions
493
+ * (accessors), recursively flattens arrays, and optionally skips
494
+ * non-rendering values (`null`, `undefined`, `true`, `false`, `""`).
495
+ *
496
+ * Used internally by flow components and by the renderer to walk a children
497
+ * tree. App code rarely needs this directly — see `children()` in `solid-js`
498
+ * for the user-facing helper that memoizes the result.
499
+ *
500
+ * @param children value or array of values to flatten
501
+ * @param options
502
+ * - `skipNonRendered` — drop values that won't render
503
+ * - `doNotUnwrap` — leave function children as-is (caller will resolve)
504
+ *
505
+ * @example
506
+ * ```ts
507
+ * // Custom renderer walking a children tree manually. Most authors should
508
+ * // use `children()` from solid-js, which memoizes the resolved value.
509
+ * function renderChildren(value: unknown): unknown {
510
+ * return flatten(value, { skipNonRendered: true });
511
+ * }
512
+ * ```
513
+ */ function flatten(e, t) {
514
+ if (typeof e === "function" && !e.length) {
515
+ if (t?.doNotUnwrap) return e;
516
+ do {
517
+ e = e();
518
+ } while (typeof e === "function" && !e.length);
519
+ }
520
+ if (t?.skipNonRendered && (e == null || e === true || e === false || e === "")) return;
521
+ if (Array.isArray(e)) {
522
+ let r = [];
523
+ if (flattenArray(e, r, t)) {
524
+ return () => {
525
+ let e = [];
526
+ flattenArray(r, e, {
527
+ ...t,
528
+ doNotUnwrap: false
529
+ });
530
+ return e;
531
+ };
532
+ }
533
+ return r;
534
+ }
535
+ return e;
536
+ }
537
+
538
+ function flattenArray(e, t = [], r) {
539
+ let n = null;
540
+ let s = false;
541
+ for (let i = 0; i < e.length; i++) {
542
+ try {
543
+ let n = e[i];
544
+ if (typeof n === "function" && !n.length) {
545
+ if (r?.doNotUnwrap) {
546
+ t.push(n);
547
+ s = true;
548
+ continue;
549
+ }
550
+ do {
551
+ n = n();
552
+ } while (typeof n === "function" && !n.length);
553
+ }
554
+ if (Array.isArray(n)) {
555
+ s = flattenArray(n, t, r);
556
+ } else if (r?.skipNonRendered && (n == null || n === true || n === false || n === "")) {
557
+ // skip
558
+ } else t.push(n);
559
+ } catch (e) {
560
+ if (!(e instanceof NotReadyError)) throw e;
561
+ n = e;
562
+ }
563
+ }
564
+ if (n) throw n;
565
+ return s;
566
+ }
567
+
568
+ export { CollectionQueue, RevealController, createErrorBoundary, createLoadingBoundary, createRevealOrder, flatten };
@@ -0,0 +1,83 @@
1
+ import { globalQueue, activeTransition, currentTransition, setActiveTransition, schedule, flush } from "./scheduler.js";
2
+
3
+ import { isThenable } from "./async.js";
4
+
5
+ import "./core.js";
6
+
7
+ function restoreTransition(e, n) {
8
+ globalQueue.initTransition(e);
9
+ const t = n();
10
+ flush();
11
+ return t;
12
+ }
13
+
14
+ /**
15
+ * Wraps a generator function so each invocation runs as a single transaction
16
+ * (a "transition") that batches every signal/store write between yields. The
17
+ * surrounding UI sees one atomic update per yielded step; nothing is committed
18
+ * until the action either completes or the next `yield` resolves.
19
+ *
20
+ * Yield promises (or any awaitable) inside the generator — the action waits
21
+ * for each before continuing, but the writes you made beforehand are already
22
+ * visible (or held by `<Loading>` if optimistic). Yield bare values for
23
+ * synchronous batched steps.
24
+ *
25
+ * Each call returns a `Promise` that resolves with the generator's return
26
+ * value, or rejects if it throws. Pair with `createOptimistic` /
27
+ * `createOptimisticStore` to apply tentative writes that auto-revert if the
28
+ * action fails.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const [todos, setTodos] = createOptimisticStore<Todo[]>([]);
33
+ *
34
+ * const addTodo = action(function* (text: string) {
35
+ * const tempId = crypto.randomUUID();
36
+ * setTodos(t => { t.push({ id: tempId, text, pending: true }); }); // optimistic
37
+ * const saved = yield api.createTodo(text); // network round-trip
38
+ * setTodos(t => {
39
+ * const i = t.findIndex(x => x.id === tempId);
40
+ * if (i >= 0) t[i] = saved;
41
+ * });
42
+ * return saved;
43
+ * });
44
+ *
45
+ * await addTodo("buy milk");
46
+ * ```
47
+ */ function action(e) {
48
+ return (...n) => new Promise((t, r) => {
49
+ const i = e(...n);
50
+ globalQueue.initTransition();
51
+ let o = activeTransition;
52
+ o.Te.push(i);
53
+ const done = (e, n, s = false) => {
54
+ o = currentTransition(o);
55
+ const u = o.Te.indexOf(i);
56
+ if (u >= 0) o.Te.splice(u, 1);
57
+ setActiveTransition(o);
58
+ schedule();
59
+ s ? r(n) : t(e);
60
+ };
61
+ const step = (e, n) => {
62
+ let t;
63
+ try {
64
+ t = n ? i.throw(e) : i.next(e);
65
+ } catch (e) {
66
+ return done(undefined, e, true);
67
+ }
68
+ // A rejected iterator result (async generators) means the error already
69
+ // escaped the generator body — it is completed, and throwing back in
70
+ // would just reject again forever. Settle instead.
71
+ if (isThenable(t)) return void t.then(run, e => done(undefined, e, true));
72
+ run(t);
73
+ };
74
+ const run = e => {
75
+ if (e.done) return done(e.value);
76
+ if (isThenable(e.value)) return void e.value.then(e => restoreTransition(o, () => step(e)), e => restoreTransition(o, () => step(e, true)));
77
+ restoreTransition(o, () => step(e.value));
78
+ };
79
+ step();
80
+ });
81
+ }
82
+
83
+ export { action };