solid-js 2.0.0-beta.1 → 2.0.0-beta.11

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 (38) hide show
  1. package/CHEATSHEET.md +640 -0
  2. package/README.md +42 -188
  3. package/dist/dev.cjs +446 -291
  4. package/dist/dev.js +426 -293
  5. package/dist/server.cjs +941 -345
  6. package/dist/server.js +897 -299
  7. package/dist/solid.cjs +438 -261
  8. package/dist/solid.js +418 -263
  9. package/package.json +67 -39
  10. package/types/client/component.d.ts +64 -19
  11. package/types/client/core.d.ts +110 -34
  12. package/types/client/flow.d.ts +176 -42
  13. package/types/client/hydration.d.ts +513 -37
  14. package/types/index.d.ts +8 -13
  15. package/types/server/component.d.ts +11 -11
  16. package/types/server/core.d.ts +19 -21
  17. package/types/server/flow.d.ts +76 -21
  18. package/types/server/hydration.d.ts +34 -9
  19. package/types/server/index.d.ts +6 -8
  20. package/types/server/shared.d.ts +10 -2
  21. package/types/server/signals.d.ts +80 -19
  22. package/types/types.d.ts +15 -0
  23. package/types-cjs/client/component.d.cts +120 -0
  24. package/types-cjs/client/core.d.cts +141 -0
  25. package/types-cjs/client/flow.d.cts +234 -0
  26. package/types-cjs/client/hydration.d.cts +568 -0
  27. package/types-cjs/index.d.cts +15 -0
  28. package/types-cjs/package.json +3 -0
  29. package/types-cjs/server/component.d.cts +67 -0
  30. package/types-cjs/server/core.d.cts +42 -0
  31. package/types-cjs/server/flow.d.cts +115 -0
  32. package/types-cjs/server/hydration.d.cts +63 -0
  33. package/types-cjs/server/index.d.cts +10 -0
  34. package/types-cjs/server/shared.d.cts +54 -0
  35. package/types-cjs/server/signals.d.cts +123 -0
  36. package/types-cjs/types.d.cts +15 -0
  37. package/jsx-runtime.d.ts +0 -1
  38. package/types/jsx.d.ts +0 -4129
package/dist/server.js CHANGED
@@ -1,5 +1,5 @@
1
- import { getOwner, getContext, getNextChildId, createOwner, runWithOwner, onCleanup, NotReadyError, isWrappable, setContext, flatten, createRoot } from '@solidjs/signals';
2
- export { $PROXY, $TRACK, NotReadyError, createOwner, createRoot, flatten, getNextChildId, getOwner, isEqual, isWrappable, merge, omit, onCleanup, runWithOwner, snapshot, storePath } from '@solidjs/signals';
1
+ import { NotReadyError, $REFRESH, isWrappable, NoOwnerError, ContextNotFoundError, flatten } from '@solidjs/signals';
2
+ export { $PROXY, $REFRESH, $TRACK, NotReadyError, enableExternalSource, enforceLoadingBoundary, flatten, isEqual, isWrappable, merge, omit, snapshot, storePath } from '@solidjs/signals';
3
3
 
4
4
  const NoHydrateContext = {
5
5
  id: Symbol("NoHydrateContext"),
@@ -14,6 +14,142 @@ const sharedConfig = {
14
14
  }
15
15
  };
16
16
 
17
+ const defaultSSRContext = {};
18
+ let currentOwner = null;
19
+ const OWNER_POOL_MAX = 4096;
20
+ const ownerPool = [];
21
+ function formatChildId(prefix, id) {
22
+ const num = id.toString(36);
23
+ const len = num.length - 1;
24
+ return prefix + (len ? String.fromCharCode(64 + len) : "") + num;
25
+ }
26
+ function nextChildIdFor(owner, consume) {
27
+ let counter = owner;
28
+ while (counter._transparent && counter._parent) counter = counter._parent;
29
+ if (counter.id != null) {
30
+ return formatChildId(counter.id, counter._childCount++ );
31
+ }
32
+ throw new Error("Cannot get child id from owner without an id");
33
+ }
34
+ function getNextChildId(owner) {
35
+ return nextChildIdFor(owner);
36
+ }
37
+ function createOwner(options) {
38
+ const parent = currentOwner;
39
+ const transparent = options?.transparent ?? false;
40
+ const id = options?.id ?? (transparent ? parent?.id : parent?.id != null ? nextChildIdFor(parent) : undefined);
41
+ const ctx = parent?._context ?? defaultSSRContext;
42
+ let owner;
43
+ if (ownerPool.length) {
44
+ owner = ownerPool.pop();
45
+ owner.id = id;
46
+ owner._transparent = transparent;
47
+ owner._disposal = null;
48
+ owner._parent = parent;
49
+ owner._context = ctx;
50
+ owner._childCount = 0;
51
+ owner._firstChild = null;
52
+ owner._nextSibling = null;
53
+ owner._disposed = false;
54
+ } else {
55
+ owner = {
56
+ id,
57
+ _transparent: transparent,
58
+ _disposal: null,
59
+ _parent: parent,
60
+ _context: ctx,
61
+ _childCount: 0,
62
+ _firstChild: null,
63
+ _nextSibling: null,
64
+ _disposed: false
65
+ };
66
+ }
67
+ if (parent) {
68
+ const lastChild = parent._firstChild;
69
+ if (lastChild) owner._nextSibling = lastChild;
70
+ parent._firstChild = owner;
71
+ }
72
+ return owner;
73
+ }
74
+ function runWithOwner(owner, fn) {
75
+ const prev = currentOwner;
76
+ currentOwner = owner;
77
+ try {
78
+ return fn();
79
+ } finally {
80
+ currentOwner = prev;
81
+ }
82
+ }
83
+ function getOwner() {
84
+ return currentOwner;
85
+ }
86
+ function isDisposed(owner) {
87
+ return owner._disposed;
88
+ }
89
+ function onCleanup(fn) {
90
+ const o = currentOwner;
91
+ if (!o) return fn;
92
+ if (!o._disposal) o._disposal = fn;else if (Array.isArray(o._disposal)) o._disposal.push(fn);else o._disposal = [o._disposal, fn];
93
+ return fn;
94
+ }
95
+ function getContext(context, owner = currentOwner) {
96
+ if (!owner) throw new NoOwnerError();
97
+ const map = owner._context;
98
+ const stored = map[context.id];
99
+ const value = stored !== undefined ? stored : context.defaultValue;
100
+ if (value === undefined) throw new ContextNotFoundError();
101
+ return value;
102
+ }
103
+ function setContext(context, value, owner = currentOwner) {
104
+ if (!owner) throw new NoOwnerError();
105
+ const o = owner;
106
+ o._context = {
107
+ ...o._context,
108
+ [context.id]: value === undefined ? context.defaultValue : value
109
+ };
110
+ }
111
+ function disposeOwner(owner, self = true) {
112
+ const node = owner;
113
+ if (node._disposed) return;
114
+ if (!node._firstChild && !node._disposal) {
115
+ if (self) {
116
+ node._disposed = true;
117
+ if (ownerPool.length < OWNER_POOL_MAX) {
118
+ node._parent = null;
119
+ node._nextSibling = null;
120
+ ownerPool.push(node);
121
+ }
122
+ }
123
+ return;
124
+ }
125
+ if (self) node._disposed = true;
126
+ let child = node._firstChild;
127
+ while (child) {
128
+ const next = child._nextSibling;
129
+ disposeOwner(child, true);
130
+ child = next;
131
+ }
132
+ node._firstChild = null;
133
+ node._childCount = 0;
134
+ const d = node._disposal;
135
+ if (d) {
136
+ if (Array.isArray(d)) {
137
+ for (let i = 0, len = d.length; i < len; i++) d[i]();
138
+ } else {
139
+ d();
140
+ }
141
+ node._disposal = null;
142
+ }
143
+ if (self && ownerPool.length < OWNER_POOL_MAX) {
144
+ node._parent = null;
145
+ node._nextSibling = null;
146
+ ownerPool.push(node);
147
+ }
148
+ }
149
+ function createRoot(init, options) {
150
+ const owner = createOwner(options);
151
+ return runWithOwner(owner, () => init(() => disposeOwner(owner)));
152
+ }
17
153
  let Observer = null;
18
154
  function runWithObserver(comp, fn) {
19
155
  const prev = Observer;
@@ -27,38 +163,85 @@ function runWithObserver(comp, fn) {
27
163
  function getObserver() {
28
164
  return Observer;
29
165
  }
30
- function createSignal(first, second, third) {
166
+ function createDeferredPromise() {
167
+ let settled = false;
168
+ let resolvePromise;
169
+ let rejectPromise;
170
+ const promise = new Promise((resolve, reject) => {
171
+ resolvePromise = resolve;
172
+ rejectPromise = reject;
173
+ });
174
+ return {
175
+ promise,
176
+ resolve(value) {
177
+ if (settled) return;
178
+ settled = true;
179
+ promise.s = 1;
180
+ promise.v = value;
181
+ resolvePromise(value);
182
+ },
183
+ reject(error) {
184
+ if (settled) return;
185
+ settled = true;
186
+ promise.s = 2;
187
+ promise.v = error;
188
+ rejectPromise(error);
189
+ }
190
+ };
191
+ }
192
+ function subscribePendingRetry(error, retry) {
193
+ if (!(error instanceof NotReadyError)) return false;
194
+ error.source?.then(() => retry(), () => retry());
195
+ return true;
196
+ }
197
+ function settleServerAsync(initial, rerun, deferred, onSuccess, onError, isDisposed) {
198
+ let first = true;
199
+ const attempt = () => {
200
+ if (isDisposed()) return;
201
+ let current;
202
+ try {
203
+ current = first ? initial : rerun();
204
+ first = false;
205
+ } catch (error) {
206
+ if (subscribePendingRetry(error, attempt)) return;
207
+ onError(error);
208
+ deferred.reject(error);
209
+ return;
210
+ }
211
+ Promise.resolve(current).then(value => {
212
+ if (isDisposed()) return;
213
+ deferred.resolve(onSuccess(value));
214
+ }, error => {
215
+ if (isDisposed()) return;
216
+ if (subscribePendingRetry(error, attempt)) return;
217
+ onError(error);
218
+ deferred.reject(error);
219
+ });
220
+ };
221
+ attempt();
222
+ }
223
+ function createSignal(first, second) {
31
224
  if (typeof first === "function") {
32
- const ssrSource = third?.ssrSource;
33
- if (ssrSource === "initial" || ssrSource === "client") {
34
- createOwner();
35
- let value = second;
36
- return [() => value, v => {
37
- return value = typeof v === "function" ? v(value) : v;
38
- }];
39
- }
40
- const memoOpts = third?.deferStream || ssrSource ? {
41
- deferStream: third?.deferStream,
42
- ssrSource
225
+ const opts = second?.deferStream || second?.ssrSource ? {
226
+ deferStream: second?.deferStream,
227
+ ssrSource: second?.ssrSource
43
228
  } : undefined;
44
- const memo = createMemo(p => {
45
- let value = first(p ? p[0]() : second);
46
- return [() => value, v => {
47
- return value = typeof v === "function" ? v(value) : v;
48
- }];
49
- }, undefined, memoOpts);
50
- return [() => memo()[0](), v => memo()[1](v)];
229
+ const memo = createMemo(prev => first(prev), opts);
230
+ return [memo, () => undefined];
51
231
  }
52
232
  return [() => first, v => {
53
233
  return first = typeof v === "function" ? v(first) : v;
54
234
  }];
55
235
  }
56
- function createMemo(compute, value, options) {
236
+ function createMemo(compute, options) {
237
+ if (options?.sync) {
238
+ return createSyncMemo(compute, options);
239
+ }
57
240
  const ctx = sharedConfig.context;
58
241
  const owner = createOwner();
59
242
  const comp = {
60
243
  owner,
61
- value: value,
244
+ value: undefined,
62
245
  compute: compute,
63
246
  error: undefined,
64
247
  computed: false,
@@ -69,26 +252,27 @@ function createMemo(compute, value, options) {
69
252
  }));
70
253
  function update() {
71
254
  if (comp.disposed) return;
255
+ const run = () => runWithOwner(owner, () => runWithObserver(comp, () => comp.compute(comp.value)));
72
256
  try {
73
257
  comp.error = undefined;
74
- const result = runWithOwner(owner, () => runWithObserver(comp, () => comp.compute(comp.value)));
258
+ const result = run();
75
259
  comp.computed = true;
76
- processResult(comp, result, owner, ctx, options?.deferStream, options?.ssrSource);
260
+ processResult(comp, result, owner, ctx, options?.deferStream, options?.ssrSource, run);
77
261
  } catch (err) {
78
262
  if (err instanceof NotReadyError) {
79
- err.source?.then(() => update());
263
+ subscribePendingRetry(err, update);
80
264
  }
81
265
  comp.error = err;
82
266
  comp.computed = true;
83
267
  }
84
268
  }
85
269
  const ssrSource = options?.ssrSource;
86
- if (ssrSource === "initial" || ssrSource === "client") {
270
+ if (ssrSource === "client") {
87
271
  comp.computed = true;
88
272
  } else if (!options?.lazy) {
89
273
  update();
90
274
  }
91
- return () => {
275
+ const read = () => {
92
276
  if (!comp.computed) {
93
277
  update();
94
278
  }
@@ -97,6 +281,44 @@ function createMemo(compute, value, options) {
97
281
  }
98
282
  return comp.value;
99
283
  };
284
+ read[$REFRESH] = comp;
285
+ return read;
286
+ }
287
+ function createSyncMemo(compute, options) {
288
+ const owner = createOwner();
289
+ let value;
290
+ let error;
291
+ let cached = false;
292
+ function pull() {
293
+ const prev = currentOwner;
294
+ currentOwner = owner;
295
+ try {
296
+ value = compute(value);
297
+ error = undefined;
298
+ cached = true;
299
+ return value;
300
+ } catch (err) {
301
+ if (err instanceof NotReadyError) throw err;
302
+ error = err;
303
+ cached = true;
304
+ throw err;
305
+ } finally {
306
+ currentOwner = prev;
307
+ }
308
+ }
309
+ if (!options?.lazy) {
310
+ try {
311
+ pull();
312
+ } catch {
313
+ }
314
+ }
315
+ return () => {
316
+ if (cached) {
317
+ if (error !== undefined) throw error;
318
+ return value;
319
+ }
320
+ return pull();
321
+ };
100
322
  }
101
323
  function createDeepProxy(target, patches, basePath = []) {
102
324
  const childProxies = new Map();
@@ -158,76 +380,128 @@ function createDeepProxy(target, patches, basePath = []) {
158
380
  };
159
381
  return new Proxy(target, handler);
160
382
  }
161
- function processResult(comp, result, owner, ctx, deferStream, ssrSource) {
383
+ function processResult(comp, result, owner, ctx, deferStream, ssrSource, rerun) {
162
384
  if (comp.disposed) return;
163
385
  const id = owner.id;
164
- const uninitialized = comp.value === undefined;
165
386
  const noHydrate = getContext(NoHydrateContext, owner);
166
387
  if (result instanceof Promise) {
167
- result.then(v => {
168
- result.s = 1;
169
- result.v = v;
170
- if (comp.disposed) return;
171
- comp.value = v;
388
+ if (result.s === 1) {
389
+ comp.value = result.v;
172
390
  comp.error = undefined;
173
- }, () => {});
174
- if (ctx?.async && ctx.serialize && id && !noHydrate) ctx.serialize(id, result, deferStream);
175
- if (uninitialized) {
176
- comp.error = new NotReadyError(result);
391
+ return;
392
+ }
393
+ if (result.s === 2) {
394
+ comp.error = result.v;
395
+ return;
177
396
  }
397
+ const deferred = createDeferredPromise();
398
+ if (ctx?.async && ctx.serialize && id && !noHydrate) ctx.serialize(id, deferred.promise, deferStream);
399
+ settleServerAsync(result, () => rerun ? rerun() : result, deferred, value => {
400
+ result.s = 1;
401
+ result.v = value;
402
+ comp.value = value;
403
+ comp.error = undefined;
404
+ return value;
405
+ }, error => {
406
+ result.s = 2;
407
+ result.v = error;
408
+ comp.error = error;
409
+ }, () => comp.disposed);
410
+ comp.error = new NotReadyError(deferred.promise);
178
411
  return;
179
412
  }
180
413
  const iterator = result?.[Symbol.asyncIterator];
181
414
  if (typeof iterator === "function") {
182
- const iter = iterator.call(result);
183
415
  if (ssrSource === "hybrid") {
184
- const promise = iter.next().then(v => {
185
- promise.s = 1;
186
- promise.v = v.value;
187
- if (comp.disposed) return;
188
- comp.value = v.value;
416
+ let currentResult = result;
417
+ let iter;
418
+ const deferred = createDeferredPromise();
419
+ const runFirst = () => {
420
+ const source = currentResult ?? (rerun ? rerun() : result);
421
+ currentResult = undefined;
422
+ const nextIterator = source?.[Symbol.asyncIterator];
423
+ if (typeof nextIterator !== "function") {
424
+ throw new Error("Expected async iterator while retrying server createMemo");
425
+ }
426
+ iter = nextIterator.call(source);
427
+ return iter.next().then(value => {
428
+ if (!value.done) closeAsyncIterator(iter);
429
+ return value.value;
430
+ });
431
+ };
432
+ settleServerAsync(runFirst(), runFirst, deferred, value => {
433
+ comp.value = value;
189
434
  comp.error = undefined;
190
- }, () => {});
191
- if (ctx?.async && ctx.serialize && id && !noHydrate) ctx.serialize(id, promise, deferStream);
192
- if (uninitialized) {
193
- comp.error = new NotReadyError(promise);
194
- }
435
+ return value;
436
+ }, error => {
437
+ comp.error = error;
438
+ }, () => comp.disposed);
439
+ if (ctx?.async && ctx.serialize && id && !noHydrate) ctx.serialize(id, deferred.promise, deferStream);
440
+ comp.error = new NotReadyError(deferred.promise);
195
441
  } else {
196
- const firstNext = iter.next();
197
- const firstReady = firstNext.then(r => {
198
- if (comp.disposed) return;
199
- if (!r.done) {
200
- comp.value = r.value;
201
- comp.error = undefined;
442
+ let currentResult = result;
443
+ let iter;
444
+ let firstResult;
445
+ const deferred = createDeferredPromise();
446
+ const runFirst = () => {
447
+ const source = currentResult ?? (rerun ? rerun() : result);
448
+ currentResult = undefined;
449
+ const nextIterator = source?.[Symbol.asyncIterator];
450
+ if (typeof nextIterator !== "function") {
451
+ throw new Error("Expected async iterator while retrying server createMemo");
202
452
  }
203
- }, () => {});
204
- let servedFirst = false;
205
- const tapped = {
206
- [Symbol.asyncIterator]: () => ({
207
- next() {
208
- if (!servedFirst) {
209
- servedFirst = true;
210
- return firstNext.then(r => {
211
- if (!r.done && !comp.disposed) comp.value = r.value;
212
- return r;
213
- });
214
- }
215
- return iter.next().then(r => r);
216
- }
217
- })
453
+ iter = nextIterator.call(source);
454
+ return iter.next().then(value => {
455
+ firstResult = value;
456
+ return Promise.resolve();
457
+ });
218
458
  };
219
- if (ctx?.async && ctx.serialize && id && !noHydrate) ctx.serialize(id, tapped, deferStream);
220
- if (uninitialized) {
221
- comp.error = new NotReadyError(firstReady);
459
+ settleServerAsync(runFirst(), runFirst, deferred, () => {
460
+ const resolved = firstResult;
461
+ if (resolved && !resolved.done) {
462
+ comp.value = resolved.value;
463
+ }
464
+ comp.error = undefined;
465
+ return undefined;
466
+ }, error => {
467
+ comp.error = error;
468
+ }, () => comp.disposed);
469
+ if (ctx?.async && ctx.serialize && id && !noHydrate) {
470
+ let tappedFirst = true;
471
+ const tapped = {
472
+ [Symbol.asyncIterator]: () => ({
473
+ next() {
474
+ if (tappedFirst) {
475
+ tappedFirst = false;
476
+ return deferred.promise.then(() => firstResult?.done ? {
477
+ done: true,
478
+ value: undefined
479
+ } : firstResult);
480
+ }
481
+ return iter.next().then(r => r);
482
+ },
483
+ return(value) {
484
+ return iter.return?.(value);
485
+ }
486
+ })
487
+ };
488
+ ctx.serialize(id, tapped, deferStream);
222
489
  }
490
+ comp.error = new NotReadyError(deferred.promise);
223
491
  }
224
492
  return;
225
493
  }
226
494
  comp.value = result;
227
495
  }
228
- function serverEffect(compute, effectFn, value, options) {
496
+ function closeAsyncIterator(iter, value) {
497
+ const returned = iter.return?.(value);
498
+ if (returned && typeof returned.then === "function") {
499
+ returned.then(undefined, () => {});
500
+ }
501
+ }
502
+ function serverEffect(compute, effectFn, options) {
229
503
  const ssrSource = options?.ssrSource;
230
- if (ssrSource === "client" || ssrSource === "initial") {
504
+ if (ssrSource === "client") {
231
505
  createOwner();
232
506
  return;
233
507
  }
@@ -235,7 +509,7 @@ function serverEffect(compute, effectFn, value, options) {
235
509
  const owner = createOwner();
236
510
  const comp = {
237
511
  owner,
238
- value: value,
512
+ value: undefined,
239
513
  compute: compute,
240
514
  error: undefined,
241
515
  computed: true,
@@ -247,19 +521,19 @@ function serverEffect(compute, effectFn, value, options) {
247
521
  }));
248
522
  }
249
523
  try {
250
- const result = runWithOwner(owner, () => runWithObserver(comp, () => compute(value)));
524
+ const result = runWithOwner(owner, () => runWithObserver(comp, () => compute(undefined)));
251
525
  if (ssrSource) {
252
526
  processResult(comp, result, owner, ctx, options?.deferStream, ssrSource);
253
527
  }
254
- effectFn?.(ssrSource ? comp.value ?? result : result, value);
528
+ effectFn?.(ssrSource ? comp.value ?? result : result, undefined);
255
529
  } catch (err) {
256
530
  }
257
531
  }
258
- function createEffect(compute, effect, value, options) {
259
- serverEffect(compute, undefined, value, options);
532
+ function createEffect(compute, effect, options) {
533
+ serverEffect(compute, undefined, options);
260
534
  }
261
- function createRenderEffect(compute, effectFn, value, options) {
262
- serverEffect(compute, effectFn, value, options);
535
+ function createRenderEffect(compute, effectFn, options) {
536
+ serverEffect(compute, effectFn, options);
263
537
  }
264
538
  function createTrackedEffect(compute, options) {
265
539
  const o = getOwner();
@@ -270,8 +544,8 @@ function createReaction(effectFn, options) {
270
544
  tracking();
271
545
  };
272
546
  }
273
- function createOptimistic(first, second, third) {
274
- return createSignal(first, second, third);
547
+ function createOptimistic(first, second) {
548
+ return createSignal(first, second);
275
549
  }
276
550
  function setProperty(state, property, value) {
277
551
  if (state[property] === value) return;
@@ -281,7 +555,7 @@ function setProperty(state, property, value) {
281
555
  }
282
556
  function createStore(first, second) {
283
557
  if (typeof first === "function") {
284
- const store = createProjection(first, second ?? {});
558
+ const store = createProjection(first, second);
285
559
  return [store, fn => fn(store)];
286
560
  }
287
561
  const state = first;
@@ -304,11 +578,11 @@ function createPendingProxy(state, source) {
304
578
  pending = false;
305
579
  }];
306
580
  }
307
- function createProjection(fn, initialValue = {}, options) {
581
+ function createProjection(fn, initialValue, options) {
308
582
  const ctx = sharedConfig.context;
309
583
  const owner = createOwner();
310
584
  const [state] = createStore(initialValue);
311
- if (options?.ssrSource === "initial" || options?.ssrSource === "client") {
585
+ if (options?.ssrSource === "client") {
312
586
  return state;
313
587
  }
314
588
  let disposed = false;
@@ -319,100 +593,131 @@ function createProjection(fn, initialValue = {}, options) {
319
593
  const useProxy = ssrSource !== "hybrid";
320
594
  const patches = [];
321
595
  const draft = useProxy ? createDeepProxy(state, patches) : state;
322
- const result = runWithOwner(owner, () => fn(draft));
596
+ const runProjection = () => runWithOwner(owner, () => fn(draft));
597
+ const result = runProjection();
323
598
  const iteratorFn = result?.[Symbol.asyncIterator];
324
599
  if (typeof iteratorFn === "function") {
325
- const iter = iteratorFn.call(result);
326
600
  if (ssrSource === "hybrid") {
327
- const promise = iter.next().then(r => {
328
- promise.s = 1;
329
- if (disposed) {
330
- promise.v = state;
331
- return;
601
+ let currentResult = result;
602
+ let iter;
603
+ const deferred = createDeferredPromise();
604
+ const [pending, markReady] = createPendingProxy(state, deferred.promise);
605
+ const runFirst = () => {
606
+ const source = currentResult ?? runProjection();
607
+ currentResult = undefined;
608
+ const nextIterator = source?.[Symbol.asyncIterator];
609
+ if (typeof nextIterator !== "function") {
610
+ throw new Error("Expected async iterator while retrying server createProjection");
332
611
  }
333
- if (r.value !== undefined && r.value !== state) {
334
- Object.assign(state, r.value);
612
+ iter = nextIterator.call(source);
613
+ return iter.next().then(r => {
614
+ if (!r.done) closeAsyncIterator(iter);
615
+ return r.value;
616
+ });
617
+ };
618
+ settleServerAsync(runFirst(), runFirst, deferred, value => {
619
+ if (value !== undefined && value !== state) {
620
+ Object.assign(state, value);
335
621
  }
336
- promise.v = state;
337
622
  markReady();
338
- }, () => {});
339
- if (ctx?.async && !getContext(NoHydrateContext) && owner.id) ctx.serialize(owner.id, promise, options?.deferStream);
340
- const [pending, markReady] = createPendingProxy(state, promise);
623
+ return state;
624
+ }, error => {
625
+ markReady();
626
+ }, () => disposed);
627
+ if (ctx?.async && !getContext(NoHydrateContext) && owner.id) ctx.serialize(owner.id, deferred.promise, options?.deferStream);
341
628
  return pending;
342
629
  } else {
343
- const firstNext = iter.next();
344
- const firstReady = firstNext.then(r => {
345
- if (disposed) return;
630
+ let currentResult = result;
631
+ let iter;
632
+ let firstResult;
633
+ const deferred = createDeferredPromise();
634
+ const [pending, markReady] = createPendingProxy(state, deferred.promise);
635
+ const runFirst = () => {
636
+ const source = currentResult ?? runProjection();
637
+ currentResult = undefined;
638
+ const nextIterator = source?.[Symbol.asyncIterator];
639
+ if (typeof nextIterator !== "function") {
640
+ throw new Error("Expected async iterator while retrying server createProjection");
641
+ }
642
+ iter = nextIterator.call(source);
643
+ return iter.next().then(value => {
644
+ firstResult = value;
645
+ return Promise.resolve();
646
+ });
647
+ };
648
+ settleServerAsync(runFirst(), runFirst, deferred, () => {
346
649
  patches.length = 0;
347
- if (!r.done) {
348
- if (r.value !== undefined && r.value !== draft) {
349
- Object.assign(state, r.value);
350
- }
650
+ const resolved = firstResult;
651
+ if (resolved && !resolved.done && resolved.value !== undefined && resolved.value !== draft) {
652
+ Object.assign(state, resolved.value);
351
653
  }
352
654
  markReady(JSON.parse(JSON.stringify(state)));
353
- }, () => {
655
+ return undefined;
656
+ }, error => {
354
657
  markReady();
355
- });
356
- let servedFirst = false;
357
- const tapped = {
358
- [Symbol.asyncIterator]: () => ({
359
- next() {
360
- if (!servedFirst) {
361
- servedFirst = true;
362
- return firstNext.then(r => {
363
- if (!r.done && !disposed) return {
364
- done: false,
365
- value: state
658
+ }, () => disposed);
659
+ if (ctx?.async && !getContext(NoHydrateContext) && owner.id) {
660
+ let tappedFirst = true;
661
+ const tapped = {
662
+ [Symbol.asyncIterator]: () => ({
663
+ next() {
664
+ if (tappedFirst) {
665
+ tappedFirst = false;
666
+ return deferred.promise.then(() => {
667
+ if (firstResult?.done) return {
668
+ done: true,
669
+ value: undefined
670
+ };
671
+ return {
672
+ done: false,
673
+ value: JSON.parse(JSON.stringify(state))
674
+ };
675
+ });
676
+ }
677
+ return iter.next().then(r => {
678
+ if (disposed) return {
679
+ done: true,
680
+ value: undefined
366
681
  };
682
+ const flushed = patches.splice(0);
683
+ if (!r.done) {
684
+ if (r.value !== undefined && r.value !== draft) {
685
+ Object.assign(state, r.value);
686
+ }
687
+ return {
688
+ done: false,
689
+ value: flushed
690
+ };
691
+ }
367
692
  return {
368
- done: r.done,
693
+ done: true,
369
694
  value: undefined
370
695
  };
371
696
  });
697
+ },
698
+ return(value) {
699
+ return iter.return?.(value);
372
700
  }
373
- return iter.next().then(r => {
374
- if (disposed) return {
375
- done: true,
376
- value: undefined
377
- };
378
- const flushed = patches.splice(0);
379
- if (!r.done) {
380
- if (r.value !== undefined && r.value !== draft) {
381
- Object.assign(state, r.value);
382
- }
383
- return {
384
- done: false,
385
- value: flushed
386
- };
387
- }
388
- return {
389
- done: true,
390
- value: undefined
391
- };
392
- });
393
- }
394
- })
395
- };
396
- if (ctx?.async && !getContext(NoHydrateContext) && owner.id) ctx.serialize(owner.id, tapped, options?.deferStream);
397
- const [pending, markReady] = createPendingProxy(state, firstReady);
701
+ })
702
+ };
703
+ ctx.serialize(owner.id, tapped, options?.deferStream);
704
+ }
398
705
  return pending;
399
706
  }
400
707
  }
401
708
  if (result instanceof Promise) {
402
- const promise = result.then(v => {
403
- promise.s = 1;
404
- if (disposed) {
405
- promise.v = state;
406
- return;
407
- }
408
- if (v !== undefined && v !== state) {
409
- Object.assign(state, v);
709
+ const deferred = createDeferredPromise();
710
+ const [pending, markReady] = createPendingProxy(state, deferred.promise);
711
+ settleServerAsync(result, () => runProjection(), deferred, value => {
712
+ if (value !== undefined && value !== state) {
713
+ Object.assign(state, value);
410
714
  }
411
- promise.v = state;
412
715
  markReady();
413
- }, () => {});
414
- if (ctx?.async && !getContext(NoHydrateContext) && owner.id) ctx.serialize(owner.id, promise, options?.deferStream);
415
- const [pending, markReady] = createPendingProxy(state, promise);
716
+ return state;
717
+ }, error => {
718
+ markReady();
719
+ }, () => disposed);
720
+ if (ctx?.async && !getContext(NoHydrateContext) && owner.id) ctx.serialize(owner.id, deferred.promise, options?.deferStream);
416
721
  return pending;
417
722
  }
418
723
  if (result !== undefined && result !== state && result !== draft) {
@@ -439,72 +744,129 @@ function deep(store) {
439
744
  return store;
440
745
  }
441
746
  function mapArray(list, mapFn, options = {}) {
442
- const parent = createOwner();
443
- return () => {
747
+ return createMemo(() => {
444
748
  const items = list();
445
- let s = [];
749
+ const s = [];
446
750
  if (items && items.length) {
447
- runWithOwner(parent, () => {
751
+ const parent = currentOwner;
752
+ const origId = parent.id;
753
+ const origChildCount = parent._childCount;
754
+ try {
448
755
  for (let i = 0, len = items.length; i < len; i++) {
449
- const o = createOwner();
450
- s.push(runWithOwner(o, () => mapFn(() => items[i], () => i)));
756
+ if (origId !== undefined) {
757
+ parent.id = formatChildId(origId, origChildCount + i);
758
+ }
759
+ parent._childCount = 0;
760
+ s.push(mapFn(() => items[i], () => i));
451
761
  }
452
- });
762
+ } finally {
763
+ parent.id = origId;
764
+ parent._childCount = origChildCount + items.length;
765
+ }
453
766
  } else if (options.fallback) {
454
- s = [runWithOwner(parent, () => {
455
- const fo = createOwner();
456
- return runWithOwner(fo, () => options.fallback());
457
- })];
767
+ const fo = createOwner();
768
+ s.push(runWithOwner(fo, () => options.fallback()));
458
769
  }
459
770
  return s;
460
- };
771
+ }, {
772
+ sync: true
773
+ });
461
774
  }
462
775
  function repeat(count, mapFn, options = {}) {
463
- const owner = createOwner();
464
- const len = count();
465
- const offset = options.from?.() || 0;
466
- let s = [];
467
- if (len) {
468
- runWithOwner(owner, () => {
776
+ return createMemo(() => {
777
+ const len = count();
778
+ const offset = options.from?.() || 0;
779
+ if (!len) {
780
+ if (!options.fallback) return [];
781
+ const fo = createOwner();
782
+ return [runWithOwner(fo, () => options.fallback())];
783
+ }
784
+ const out = new Array(len);
785
+ const parent = currentOwner;
786
+ const origId = parent.id;
787
+ const origChildCount = parent._childCount;
788
+ try {
469
789
  for (let i = 0; i < len; i++) {
470
- const itemOwner = createOwner();
471
- s.push(runWithOwner(itemOwner, () => mapFn(i + offset)));
790
+ if (origId !== undefined) {
791
+ parent.id = formatChildId(origId, origChildCount + i);
792
+ }
793
+ parent._childCount = 0;
794
+ out[i] = mapFn(i + offset);
472
795
  }
473
- });
474
- } else if (options.fallback) {
475
- s = [runWithOwner(owner, () => {
476
- const fo = createOwner();
477
- return runWithOwner(fo, () => options.fallback());
478
- })];
479
- }
480
- return () => s;
796
+ } finally {
797
+ parent.id = origId;
798
+ parent._childCount = origChildCount + len;
799
+ }
800
+ return out;
801
+ }, {
802
+ sync: true
803
+ });
481
804
  }
482
805
  const ErrorContext = {
483
806
  id: Symbol("ErrorContext"),
484
807
  defaultValue: null
485
808
  };
809
+ function runWithBoundaryErrorContext(owner, render, onError, context, boundaryId) {
810
+ const prevCtx = sharedConfig.context;
811
+ const prevBoundary = context?._currentBoundaryId;
812
+ if (context) {
813
+ sharedConfig.context = context;
814
+ if (boundaryId !== undefined) context._currentBoundaryId = boundaryId;
815
+ }
816
+ try {
817
+ return runWithOwner(owner, () => {
818
+ const parentHandler = getContext(ErrorContext);
819
+ setContext(ErrorContext, err => onError(err, parentHandler));
820
+ return render();
821
+ });
822
+ } finally {
823
+ if (context) {
824
+ if (boundaryId !== undefined) context._currentBoundaryId = prevBoundary;
825
+ sharedConfig.context = prevCtx;
826
+ }
827
+ }
828
+ }
486
829
  function createErrorBoundary(fn, fallback) {
487
830
  const ctx = sharedConfig.context;
488
- const owner = createOwner();
489
831
  const parent = getOwner();
490
- if (parent?.id != null) getNextChildId(parent);
491
- owner.id = owner.id + "0";
492
- return runWithOwner(owner, () => {
832
+ const owner = createOwner();
833
+ const resolve = () => {
834
+ const resolved = ctx.resolve(runWithOwner(createOwner(), fn));
835
+ if (resolved?.p?.length) throw new NotReadyError(Promise.all(resolved.p));
836
+ return resolved;
837
+ };
838
+ const renderFallback = err => ctx ? runWithOwner(parent, () => {
839
+ const fallbackOwner = createOwner();
840
+ return runWithOwner(fallbackOwner, () => fallback(err, () => {}));
841
+ }) : fallback(err, () => {});
842
+ const serializeError = err => {
843
+ if (ctx && owner.id && !runWithOwner(owner, () => getContext(NoHydrateContext))) {
844
+ ctx.serialize(owner.id, err);
845
+ }
846
+ };
847
+ const handleError = err => {
848
+ serializeError(err);
849
+ return renderFallback(err);
850
+ };
851
+ return () => {
493
852
  let result;
494
- setContext(ErrorContext, err => {
495
- if (ctx && !getContext(NoHydrateContext) && owner.id) ctx.serialize(owner.id, err);
496
- result = fallback(err, () => {});
497
- });
853
+ let handled = false;
854
+ if (ctx) disposeOwner(owner, false);
498
855
  try {
499
- result = fn();
856
+ result = ctx ? runWithBoundaryErrorContext(owner, resolve, err => {
857
+ if (err instanceof NotReadyError) throw err;
858
+ handled = true;
859
+ result = handleError(err);
860
+ throw err;
861
+ }) : runWithOwner(owner, fn);
500
862
  } catch (err) {
501
- if (ctx && !getContext(NoHydrateContext) && owner.id) ctx.serialize(owner.id, err);
502
- result = fallback(err, () => {});
863
+ if (err instanceof NotReadyError) throw err;
864
+ result = handled ? result : handleError(err);
503
865
  }
504
- return () => result;
505
- });
866
+ return result;
867
+ };
506
868
  }
507
- function createLoadBoundary(fn, fallback) {
869
+ function createLoadingBoundary$1(fn, fallback, options) {
508
870
  try {
509
871
  const result = fn();
510
872
  return () => result;
@@ -515,6 +877,10 @@ function createLoadBoundary(fn, fallback) {
515
877
  throw err;
516
878
  }
517
879
  }
880
+ function createRevealOrder(fn, _options) {
881
+ const o = createOwner();
882
+ return runWithOwner(o, fn);
883
+ }
518
884
  function untrack(fn) {
519
885
  return fn();
520
886
  }
@@ -539,8 +905,8 @@ function latest(fn) {
539
905
  function isRefreshing() {
540
906
  return false;
541
907
  }
542
- function refresh(fn) {
543
- return fn();
908
+ function refresh(_target) {
909
+ return undefined;
544
910
  }
545
911
  function action(fn) {
546
912
  return fn;
@@ -567,11 +933,12 @@ function useContext(context) {
567
933
  return getContext(context);
568
934
  }
569
935
  function children(fn) {
570
- const c = createMemo(fn, undefined, {
936
+ const c = createMemo(fn, {
571
937
  lazy: true
572
938
  });
573
- const memo = createMemo(() => flatten(c()), undefined, {
574
- lazy: true
939
+ const memo = createMemo(() => flatten(c()), {
940
+ lazy: true,
941
+ sync: true
575
942
  });
576
943
  memo.toArray = () => {
577
944
  const v = memo();
@@ -579,9 +946,6 @@ function children(fn) {
579
946
  };
580
947
  return memo;
581
948
  }
582
- function ssrRunInScope(fn) {
583
- return fn;
584
- }
585
949
 
586
950
  function enableHydration() {}
587
951
  function createComponent(Comp, props) {
@@ -625,6 +989,8 @@ function lazy(fn, moduleUrl) {
625
989
  return createMemo(() => {
626
990
  if (!p.v) throw new NotReadyError(p);
627
991
  return p.v(props);
992
+ }, {
993
+ sync: true
628
994
  });
629
995
  };
630
996
  wrap.preload = load;
@@ -637,6 +1003,160 @@ function createUniqueId() {
637
1003
  return getNextChildId(o);
638
1004
  }
639
1005
 
1006
+ const RevealGroupContext = {
1007
+ id: Symbol("RevealGroupContext"),
1008
+ defaultValue: null
1009
+ };
1010
+ function ssrHandleError(err) {
1011
+ if (err instanceof NotReadyError) {
1012
+ return err.source;
1013
+ }
1014
+ const handler = getContext(ErrorContext);
1015
+ if (handler) {
1016
+ handler(err);
1017
+ return;
1018
+ }
1019
+ throw err;
1020
+ }
1021
+ function createLoadingBoundary(fn, fallback, options) {
1022
+ const currentCtx = sharedConfig.context;
1023
+ if (!currentCtx) {
1024
+ return createLoadingBoundary$1(fn, fallback);
1025
+ }
1026
+ const ctx = currentCtx;
1027
+ const parent = getOwner();
1028
+ const parentHandler = parent && runWithOwner(parent, () => getContext(ErrorContext));
1029
+ const revealGroup = parent && runWithOwner(parent, () => getContext(RevealGroupContext));
1030
+ const o = createOwner();
1031
+ const id = o.id;
1032
+ o.id = id + "00";
1033
+ let done;
1034
+ let handledRenderError;
1035
+ let retryPromise;
1036
+ let serializeBuffer = [];
1037
+ const bufferedCtx = Object.create(ctx);
1038
+ bufferedCtx.serialize = (id, value, deferStream) => {
1039
+ serializeBuffer.push([id, value, deferStream]);
1040
+ };
1041
+ bufferedCtx._currentBoundaryId = id;
1042
+ function flushSerializeBuffer() {
1043
+ for (const args of serializeBuffer) ctx.serialize(args[0], args[1], args[2]);
1044
+ serializeBuffer = [];
1045
+ }
1046
+ function commitBoundaryState() {
1047
+ flushSerializeBuffer();
1048
+ const modules = ctx.getBoundaryModules?.(id);
1049
+ if (modules) ctx.serialize(id + "_assets", modules);
1050
+ }
1051
+ function runLoadingPhase(render) {
1052
+ handledRenderError = undefined;
1053
+ return runWithBoundaryErrorContext(o, render, (err, parentHandler) => {
1054
+ handledRenderError = err;
1055
+ if (done?.(undefined, err)) throw err;
1056
+ if (parentHandler) {
1057
+ parentHandler(err);
1058
+ return;
1059
+ }
1060
+ throw err;
1061
+ }, bufferedCtx, id);
1062
+ }
1063
+ function finalizeError(err) {
1064
+ if (handledRenderError === err) {
1065
+ handledRenderError = undefined;
1066
+ return;
1067
+ }
1068
+ if (done?.(undefined, err)) return;
1069
+ if (!parentHandler) throw err;
1070
+ try {
1071
+ runWithOwner(parent, () => parentHandler(err));
1072
+ } catch (caught) {
1073
+ if (caught !== err) throw caught;
1074
+ }
1075
+ }
1076
+ function runDiscovery() {
1077
+ disposeOwner(o, false);
1078
+ serializeBuffer = [];
1079
+ retryPromise = undefined;
1080
+ return runLoadingPhase(() => {
1081
+ try {
1082
+ return ctx.resolve(fn());
1083
+ } catch (err) {
1084
+ if (err instanceof NotReadyError) {
1085
+ retryPromise = err.source;
1086
+ return undefined;
1087
+ }
1088
+ throw err;
1089
+ }
1090
+ });
1091
+ }
1092
+ let ret = runDiscovery();
1093
+ if (!retryPromise && !ret?.p?.length) {
1094
+ commitBoundaryState();
1095
+ return () => ret;
1096
+ }
1097
+ const regResult = revealGroup ? revealGroup.register(id) : null;
1098
+ const collapseFallback = regResult?.collapseFallback ?? false;
1099
+ if (collapseFallback && !ctx.async) {
1100
+ commitBoundaryState();
1101
+ ctx.serialize(id, "$$f");
1102
+ return () => undefined;
1103
+ }
1104
+ const fallbackOwner = createOwner({
1105
+ id
1106
+ });
1107
+ const fallbackResult = runWithOwner(fallbackOwner, () => {
1108
+ if (!ctx.async) return fallback();
1109
+ const tpl = collapseFallback ? [`<template id="pl-${id}">`, `</template><!--pl-${id}-->`] : [`<template id="pl-${id}"></template>`, `<!--pl-${id}-->`];
1110
+ return ctx.ssr(tpl, ctx.escape(fallback()));
1111
+ });
1112
+ if (ctx.async) {
1113
+ const regOpts = revealGroup ? {
1114
+ revealGroup: revealGroup.id
1115
+ } : undefined;
1116
+ done = ctx.registerFragment(id, regOpts);
1117
+ (async () => {
1118
+ try {
1119
+ while (retryPromise) {
1120
+ await retryPromise.catch(() => {});
1121
+ ret = runDiscovery();
1122
+ }
1123
+ commitBoundaryState();
1124
+ while (ret && ret.p && ret.p.length) {
1125
+ const pending = ret;
1126
+ await Promise.all(pending.p).catch(() => {});
1127
+ ret = runLoadingPhase(() => ctx.ssr(pending.t, ...pending.h));
1128
+ }
1129
+ flushSerializeBuffer();
1130
+ done(ret && Array.isArray(ret.t) ? ret.t[0] : ret && ret.t);
1131
+ if (revealGroup) revealGroup.onResolved(id);
1132
+ } catch (err) {
1133
+ finalizeError(err);
1134
+ }
1135
+ })();
1136
+ return () => fallbackResult;
1137
+ }
1138
+ commitBoundaryState();
1139
+ ctx.serialize(id, "$$f");
1140
+ return () => fallbackResult;
1141
+ }
1142
+ function NoHydration(props) {
1143
+ const o = createOwner();
1144
+ return runWithOwner(o, () => {
1145
+ setContext(NoHydrateContext, true);
1146
+ return props.children;
1147
+ });
1148
+ }
1149
+ function Hydration(props) {
1150
+ if (!getContext(NoHydrateContext)) return props.children;
1151
+ const o = createOwner({
1152
+ id: props.id ?? ""
1153
+ });
1154
+ return runWithOwner(o, () => {
1155
+ setContext(NoHydrateContext, false);
1156
+ return props.children;
1157
+ });
1158
+ }
1159
+
640
1160
  function For(props) {
641
1161
  const options = "fallback" in props ? {
642
1162
  keyed: props.keyed,
@@ -644,7 +1164,7 @@ function For(props) {
644
1164
  } : {
645
1165
  keyed: props.keyed
646
1166
  };
647
- return createMemo(mapArray(() => props.each, props.children, options));
1167
+ return mapArray(() => props.each, props.children, options);
648
1168
  }
649
1169
  function Repeat(props) {
650
1170
  const options = "fallback" in props ? {
@@ -669,6 +1189,8 @@ function Show(props) {
669
1189
  return child;
670
1190
  }
671
1191
  return props.fallback;
1192
+ }, {
1193
+ sync: true
672
1194
  });
673
1195
  }
674
1196
  function Switch(props) {
@@ -686,6 +1208,8 @@ function Switch(props) {
686
1208
  }
687
1209
  }
688
1210
  return props.fallback;
1211
+ }, {
1212
+ sync: true
689
1213
  });
690
1214
  }
691
1215
  function Match(props) {
@@ -697,108 +1221,182 @@ function Errored(props) {
697
1221
  return typeof f === "function" && f.length ? f(err, reset) : f;
698
1222
  });
699
1223
  }
700
-
701
- function ssrHandleError(err) {
702
- if (err instanceof NotReadyError) {
703
- return err.source;
704
- }
705
- const handler = getContext(ErrorContext);
706
- if (handler) {
707
- handler(err);
708
- return;
709
- }
710
- throw err;
711
- }
712
1224
  function Loading(props) {
713
- const ctx = sharedConfig.context;
714
- if (!ctx) {
715
- return createLoadBoundary(() => props.children, () => props.fallback);
716
- }
1225
+ return createLoadingBoundary(() => props.children, () => props.fallback);
1226
+ }
1227
+ function Reveal(props) {
717
1228
  const o = createOwner();
718
1229
  const id = o.id;
719
- o.id = id + "00";
720
- let runPromise;
721
- let serializeBuffer = [];
722
- const origSerialize = ctx.serialize;
723
- function runInitially() {
724
- o.dispose(false);
725
- serializeBuffer = [];
726
- ctx.serialize = (id, p, deferStream) => {
727
- serializeBuffer.push([id, p, deferStream]);
728
- };
729
- const prevBoundary = ctx._currentBoundaryId;
730
- ctx._currentBoundaryId = id;
731
- const result = runWithOwner(o, () => {
732
- try {
733
- return ctx.resolve(props.children);
734
- } catch (err) {
735
- runPromise = ssrHandleError(err);
1230
+ const order = props.order ?? "sequential";
1231
+ const collapsed = order === "sequential" && !!props.collapsed;
1232
+ if (!sharedConfig.context?.async) {
1233
+ const parent = getOwner();
1234
+ const parentGroup = parent ? runWithOwner(parent, () => getContext(RevealGroupContext)) : null;
1235
+ let collapsedByParent = false;
1236
+ if (parentGroup) {
1237
+ const reg = parentGroup.register(id);
1238
+ collapsedByParent = reg.collapseFallback;
1239
+ if (order === "together" || collapsed) console.warn("Nested <Reveal> with collapsed/together won't coordinate correctly with renderToString. Use renderToStream for full support.");
1240
+ }
1241
+ let count = 0;
1242
+ return runWithOwner(o, () => {
1243
+ setContext(RevealGroupContext, {
1244
+ id,
1245
+ register(_key) {
1246
+ count++;
1247
+ const collapseFallback = collapsedByParent || order === "sequential" && collapsed && count > 1;
1248
+ return {
1249
+ collapseFallback,
1250
+ held: false
1251
+ };
1252
+ },
1253
+ onResolved() {}
1254
+ });
1255
+ return props.children;
1256
+ });
1257
+ }
1258
+ const ctx = sharedConfig.context;
1259
+ const keys = [];
1260
+ const resolved = new Set();
1261
+ const minimallyResolved = new Set();
1262
+ const composites = new Map();
1263
+ const activated = new Set();
1264
+ const stash = [];
1265
+ const collapsedLeafKeys = [];
1266
+ let frontier = 0;
1267
+ let heldByParent = false;
1268
+ let collapsedByParent = false;
1269
+ let selfMinimallyResolved = false;
1270
+ let notifiedParentDone = false;
1271
+ const parent = getOwner();
1272
+ const parentGroup = parent ? runWithOwner(parent, () => getContext(RevealGroupContext)) : null;
1273
+ if (parentGroup) {
1274
+ const reg = parentGroup.register(id, {
1275
+ onActivate: () => {
1276
+ if (!heldByParent) return;
1277
+ heldByParent = false;
1278
+ if (collapsedByParent) {
1279
+ collapsedByParent = false;
1280
+ if (collapsedLeafKeys.length) {
1281
+ ctx.revealFallbacks?.([...collapsedLeafKeys]);
1282
+ collapsedLeafKeys.length = 0;
1283
+ }
1284
+ }
1285
+ if (order === "sequential") advanceFrontier();else if (order === "together") checkTogetherRelease();else naturalRelease();
736
1286
  }
737
1287
  });
738
- ctx._currentBoundaryId = prevBoundary;
739
- ctx.serialize = origSerialize;
740
- return result;
1288
+ collapsedByParent = reg.collapseFallback;
1289
+ heldByParent = reg.held;
741
1290
  }
742
- let ret = runInitially();
743
- if (!(runPromise || ret?.p?.length)) {
744
- for (const args of serializeBuffer) origSerialize(args[0], args[1], args[2]);
745
- serializeBuffer = [];
746
- const modules = ctx.getBoundaryModules?.(id);
747
- if (modules) ctx.serialize(id + "_assets", modules);
748
- return ret;
1291
+ function notifyParentIfDone() {
1292
+ if (notifiedParentDone) return;
1293
+ if (parentGroup && resolved.size === keys.length) {
1294
+ notifiedParentDone = true;
1295
+ parentGroup.onResolved(id);
1296
+ }
749
1297
  }
750
- const fallbackOwner = createOwner({
751
- id
752
- });
753
- getNextChildId(fallbackOwner);
754
- if (ctx.async) {
755
- const done = ctx.registerFragment(id);
756
- (async () => {
757
- try {
758
- while (runPromise) {
759
- o.dispose(false);
760
- await runPromise;
761
- runPromise = undefined;
762
- ret = runInitially();
1298
+ function activateComposite(key) {
1299
+ if (activated.has(key)) return;
1300
+ activated.add(key);
1301
+ composites.get(key)();
1302
+ }
1303
+ function updateSelfMinimallyResolved() {
1304
+ if (selfMinimallyResolved) return;
1305
+ if (keys.length === 0) selfMinimallyResolved = true;else if (order === "together") selfMinimallyResolved = minimallyResolved.size === keys.length;else if (order === "sequential") selfMinimallyResolved = minimallyResolved.has(keys[0]);
1306
+ else selfMinimallyResolved = resolved.size > 0;
1307
+ if (selfMinimallyResolved) parentGroup?.onMinimallyResolved?.(id);
1308
+ }
1309
+ function advanceFrontier() {
1310
+ if (heldByParent) return;
1311
+ while (frontier < keys.length && resolved.has(keys[frontier])) {
1312
+ const k = keys[frontier];
1313
+ if (composites.has(k)) activateComposite(k);else ctx.revealFragments?.([k]);
1314
+ frontier++;
1315
+ }
1316
+ if (frontier < keys.length) {
1317
+ const k = keys[frontier];
1318
+ if (composites.has(k)) activateComposite(k);else if (order === "sequential" && collapsed) ctx.revealFallbacks?.([k]);
1319
+ }
1320
+ notifyParentIfDone();
1321
+ }
1322
+ function checkTogetherRelease() {
1323
+ if (order !== "together" || heldByParent) return;
1324
+ if (minimallyResolved.size < keys.length) return;
1325
+ if (stash.length) {
1326
+ ctx.revealFragments?.([...stash]);
1327
+ stash.length = 0;
1328
+ }
1329
+ composites.forEach((_, key) => activateComposite(key));
1330
+ notifyParentIfDone();
1331
+ }
1332
+ function naturalRelease() {
1333
+ if (stash.length) {
1334
+ ctx.revealFragments?.([...stash]);
1335
+ stash.length = 0;
1336
+ }
1337
+ composites.forEach((_, key) => activateComposite(key));
1338
+ notifyParentIfDone();
1339
+ }
1340
+ return runWithOwner(o, () => {
1341
+ setContext(RevealGroupContext, {
1342
+ id,
1343
+ register(key, options) {
1344
+ keys.push(key);
1345
+ const isComposite = !!options?.onActivate;
1346
+ if (isComposite) composites.set(key, options.onActivate);
1347
+ const selfCollapse = order === "sequential" && collapsed && keys.length > 1;
1348
+ const collapseFallback = collapsedByParent || selfCollapse;
1349
+ if (collapseFallback && !isComposite) collapsedLeafKeys.push(key);
1350
+ let held = heldByParent;
1351
+ if (!held) {
1352
+ if (order === "together") held = true;else if (order === "sequential" && keys.length > 1) held = true;
763
1353
  }
764
- for (const args of serializeBuffer) origSerialize(args[0], args[1], args[2]);
765
- serializeBuffer = [];
766
- while (ret.p.length) {
767
- await Promise.all(ret.p);
768
- ret = runWithOwner(o, () => ctx.ssr(ret.t, ...ret.h));
1354
+ return {
1355
+ collapseFallback,
1356
+ held
1357
+ };
1358
+ },
1359
+ onResolved(key) {
1360
+ resolved.add(key);
1361
+ const isLeaf = !composites.has(key);
1362
+ if (isLeaf) {
1363
+ if (order === "together") {
1364
+ stash.push(key);
1365
+ } else if (order === "natural" && heldByParent) {
1366
+ stash.push(key);
1367
+ } else if (order === "natural") {
1368
+ ctx.revealFragments?.([key]);
1369
+ }
1370
+ markMinimallyResolved(key);
1371
+ if (order === "sequential" && !heldByParent) advanceFrontier();
1372
+ if (order === "natural") updateSelfMinimallyResolved();
1373
+ } else {
1374
+ if (!heldByParent) {
1375
+ if (order === "sequential") advanceFrontier();else if (order === "natural") activateComposite(key);
1376
+ }
1377
+ if (order === "together") checkTogetherRelease();
1378
+ if (order === "natural") updateSelfMinimallyResolved();
769
1379
  }
770
- done(ret.t[0]);
771
- } catch (err) {
772
- done(undefined, err);
1380
+ notifyParentIfDone();
1381
+ },
1382
+ onMinimallyResolved(key) {
1383
+ markMinimallyResolved(key);
773
1384
  }
774
- })();
775
- return runWithOwner(fallbackOwner, () => ctx.ssr([`<template id="pl-${id}"></template>`, `<!--pl-${id}-->`], ctx.escape(props.fallback)));
776
- }
777
- for (const args of serializeBuffer) origSerialize(args[0], args[1], args[2]);
778
- serializeBuffer = [];
779
- const modules = ctx.getBoundaryModules?.(id);
780
- if (modules) ctx.serialize(id + "_assets", modules);
781
- ctx.serialize(id, "$$f");
782
- return runWithOwner(fallbackOwner, () => props.fallback);
783
- }
784
- function NoHydration(props) {
785
- const o = createOwner();
786
- return runWithOwner(o, () => {
787
- setContext(NoHydrateContext, true);
788
- return props.children;
789
- });
790
- }
791
- function Hydration(props) {
792
- if (!getContext(NoHydrateContext)) return props.children;
793
- const o = createOwner({
794
- id: props.id ?? ""
795
- });
796
- return runWithOwner(o, () => {
797
- setContext(NoHydrateContext, false);
798
- return props.children;
1385
+ });
1386
+ const result = props.children;
1387
+ if (parentGroup && keys.length === 0) {
1388
+ parentGroup.onResolved(id);
1389
+ }
1390
+ return result;
799
1391
  });
1392
+ function markMinimallyResolved(key) {
1393
+ if (minimallyResolved.has(key)) return;
1394
+ minimallyResolved.add(key);
1395
+ updateSelfMinimallyResolved();
1396
+ if (order === "together") checkTogetherRelease();
1397
+ }
800
1398
  }
801
1399
 
802
1400
  const DEV = undefined;
803
1401
 
804
- export { $DEVCOMP, DEV, Errored, For, Hydration, Loading, Match, NoHydrateContext, NoHydration, Repeat, Show, Switch, action, children, createComponent, createContext, createDeepProxy, createEffect, createMemo, createOptimistic, createOptimisticStore, createProjection, createReaction, createRenderEffect, createSignal, createStore, createTrackedEffect, createUniqueId, deep, enableHydration, flush, getObserver, isPending, isRefreshing, latest, lazy, mapArray, onSettled, reconcile, refresh, repeat, resolve, sharedConfig, ssrHandleError, ssrRunInScope, untrack, useContext };
1402
+ export { $DEVCOMP, DEV, Errored, For, Hydration, Loading, Match, NoHydrateContext, NoHydration, Repeat, Reveal, Show, Switch, action, children, createComponent, createContext, createDeepProxy, createEffect, createErrorBoundary, createLoadingBoundary, createMemo, createOptimistic, createOptimisticStore, createOwner, createProjection, createReaction, createRenderEffect, createRevealOrder, createRoot, createSignal, createStore, createTrackedEffect, createUniqueId, deep, enableHydration, flush, getNextChildId, getObserver, getOwner, isDisposed, isPending, isRefreshing, latest, lazy, mapArray, onCleanup, onSettled, reconcile, refresh, repeat, resolve, runWithOwner, sharedConfig, ssrHandleError, untrack, useContext };