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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/dev.js CHANGED
@@ -1,5 +1,5 @@
1
- import { getContext, createMemo as createMemo$1, flatten, getOwner, createRoot, setContext, untrack, setStrictRead, createLoadBoundary, onCleanup, isDisposed, runWithOwner, createOwner, createEffect as createEffect$1, createOptimistic as createOptimistic$1, createOptimisticStore as createOptimisticStore$1, createProjection as createProjection$1, createRenderEffect as createRenderEffect$1, createSignal as createSignal$1, createStore as createStore$1, setSnapshotCapture, releaseSnapshotScope, getNextChildId, createErrorBoundary as createErrorBoundary$1, markSnapshotScope, flush, clearSnapshots, peekNextChildId, mapArray, repeat } from '@solidjs/signals';
2
- export { $PROXY, $TRACK, NotReadyError, action, createOwner, createReaction, createRoot, createTrackedEffect, deep, flatten, flush, getNextChildId, getObserver, getOwner, isEqual, isPending, isRefreshing, isWrappable, latest, mapArray, merge, omit, onCleanup, onSettled, reconcile, refresh, repeat, resolve, runWithOwner, snapshot, storePath, untrack } from '@solidjs/signals';
1
+ import { getContext, createMemo as createMemo$1, flatten, getOwner, createRoot, setContext, untrack, createLoadingBoundary, onCleanup, isDisposed, runWithOwner, createOwner, createEffect as createEffect$1, createOptimistic as createOptimistic$1, createOptimisticStore as createOptimisticStore$1, createProjection as createProjection$1, createRenderEffect as createRenderEffect$1, createSignal as createSignal$1, createStore as createStore$1, setSnapshotCapture, releaseSnapshotScope, getNextChildId, createErrorBoundary as createErrorBoundary$1, markSnapshotScope, flush, clearSnapshots, peekNextChildId, mapArray, repeat } from '@solidjs/signals';
2
+ export { $PROXY, $REFRESH, $TRACK, NotReadyError, action, createErrorBoundary, createLoadingBoundary, createOwner, createReaction, createRoot, createTrackedEffect, deep, enableExternalSource, enforceLoadingBoundary, flatten, flush, getNextChildId, getObserver, getOwner, isEqual, isPending, isRefreshing, isWrappable, latest, mapArray, merge, omit, onCleanup, onSettled, reconcile, refresh, repeat, resolve, runWithOwner, snapshot, storePath, untrack } from '@solidjs/signals';
3
3
 
4
4
  const $DEVCOMP = Symbol("COMPONENT_DEV" );
5
5
  function createContext(defaultValue, options) {
@@ -37,17 +37,10 @@ function devComponent(Comp, props) {
37
37
  owner._props = props;
38
38
  owner._name = Comp.name;
39
39
  owner._component = Comp;
40
- return untrack(() => {
41
- Object.assign(Comp, {
42
- [$DEVCOMP]: true
43
- });
44
- setStrictRead(`<${Comp.name || "Anonymous"}>`);
45
- try {
46
- return Comp(props);
47
- } finally {
48
- setStrictRead(false);
49
- }
40
+ Object.assign(Comp, {
41
+ [$DEVCOMP]: true
50
42
  });
43
+ return untrack(() => Comp(props), `<${Comp.name || "Anonymous"}>`);
51
44
  }, {
52
45
  transparent: true
53
46
  });
@@ -160,11 +153,41 @@ function subFetch(fn, prev) {
160
153
  Promise = ogPromise;
161
154
  }
162
155
  }
163
- function consumeFirstSync(ai) {
164
- const iter = ai[Symbol.asyncIterator]();
165
- const r = iter.next();
166
- const value = !(r instanceof Promise) && !r.done ? r.value : undefined;
167
- return [value, iter];
156
+ function syncThenable(value) {
157
+ return {
158
+ then(fn) {
159
+ fn(value);
160
+ }
161
+ };
162
+ }
163
+ function normalizeIterator(it) {
164
+ let first = true;
165
+ let buffered = null;
166
+ return {
167
+ next() {
168
+ if (first) {
169
+ first = false;
170
+ const r = it.next();
171
+ return r && typeof r.then === "function" ? r : syncThenable(r);
172
+ }
173
+ if (buffered) {
174
+ const b = buffered;
175
+ buffered = null;
176
+ return b;
177
+ }
178
+ let latest = it.next();
179
+ if (latest && typeof latest.then === "function") return latest;
180
+ while (!latest.done) {
181
+ const peek = it.next();
182
+ if (peek && typeof peek.then === "function") {
183
+ buffered = peek;
184
+ break;
185
+ }
186
+ latest = peek;
187
+ }
188
+ return Promise.resolve(latest);
189
+ }
190
+ };
168
191
  }
169
192
  function applyPatches(target, patches) {
170
193
  for (const patch of patches) {
@@ -181,63 +204,163 @@ function applyPatches(target, patches) {
181
204
  }
182
205
  }
183
206
  }
184
- function scheduleIteratorConsumption(iter, apply) {
185
- const consume = () => {
186
- while (true) {
187
- const n = iter.next();
188
- if (n instanceof Promise) {
189
- n.then(r => {
190
- if (r.done) return;
191
- apply(r.value);
192
- consume();
193
- });
194
- return;
207
+ function isAsyncIterable(v) {
208
+ return v != null && typeof v[Symbol.asyncIterator] === "function";
209
+ }
210
+ function createShadowDraft(realDraft) {
211
+ const shadow = JSON.parse(JSON.stringify(realDraft));
212
+ let useShadow = true;
213
+ return {
214
+ proxy: new Proxy(shadow, {
215
+ get(_, prop) {
216
+ return useShadow ? shadow[prop] : realDraft[prop];
217
+ },
218
+ set(_, prop, value) {
219
+ if (useShadow) {
220
+ shadow[prop] = value;
221
+ return true;
222
+ }
223
+ return Reflect.set(realDraft, prop, value);
224
+ },
225
+ deleteProperty(_, prop) {
226
+ if (useShadow) {
227
+ delete shadow[prop];
228
+ return true;
229
+ }
230
+ return Reflect.deleteProperty(realDraft, prop);
231
+ },
232
+ has(_, prop) {
233
+ return prop in (useShadow ? shadow : realDraft);
234
+ },
235
+ ownKeys() {
236
+ return Reflect.ownKeys(useShadow ? shadow : realDraft);
237
+ },
238
+ getOwnPropertyDescriptor(_, prop) {
239
+ return Object.getOwnPropertyDescriptor(useShadow ? shadow : realDraft, prop);
195
240
  }
196
- if (n.done) break;
197
- apply(n.value);
241
+ }),
242
+ activate() {
243
+ useShadow = false;
198
244
  }
199
245
  };
200
- consume();
201
246
  }
202
- function isAsyncIterable(v) {
203
- return v != null && typeof v[Symbol.asyncIterator] === "function";
247
+ function wrapFirstYield(iterable, activate) {
248
+ const srcIt = iterable[Symbol.asyncIterator]();
249
+ let first = true;
250
+ return {
251
+ [Symbol.asyncIterator]() {
252
+ return {
253
+ next() {
254
+ const p = srcIt.next();
255
+ if (first) {
256
+ first = false;
257
+ return p.then(r => {
258
+ activate();
259
+ return r.done ? r : {
260
+ done: false,
261
+ value: undefined
262
+ };
263
+ });
264
+ }
265
+ return p;
266
+ }
267
+ };
268
+ }
269
+ };
204
270
  }
205
271
  function hydrateSignalFromAsyncIterable(coreFn, compute, value, options) {
206
272
  const parent = getOwner();
207
273
  const expectedId = peekNextChildId(parent);
208
274
  if (!sharedConfig.has(expectedId)) return null;
209
- const initP = sharedConfig.load(expectedId);
210
- if (!isAsyncIterable(initP)) return null;
211
- const [firstValue, iter] = consumeFirstSync(initP);
212
- const [get, set] = createSignal$1(firstValue);
213
- const result = coreFn(() => get(), firstValue, options);
214
- scheduleIteratorConsumption(iter, v => {
215
- set(() => v);
216
- flush();
217
- });
218
- return result;
275
+ const loaded = sharedConfig.load(expectedId);
276
+ if (!isAsyncIterable(loaded)) return null;
277
+ const it = normalizeIterator(loaded[Symbol.asyncIterator]());
278
+ const iterable = {
279
+ [Symbol.asyncIterator]() {
280
+ return it;
281
+ }
282
+ };
283
+ return coreFn(() => iterable, value, options);
219
284
  }
220
285
  function hydrateStoreFromAsyncIterable(coreFn, initialValue, options) {
221
286
  const parent = getOwner();
222
287
  const expectedId = peekNextChildId(parent);
223
288
  if (!sharedConfig.has(expectedId)) return null;
224
- const initP = sharedConfig.load(expectedId);
225
- if (!isAsyncIterable(initP)) return null;
226
- const [firstState, iter] = consumeFirstSync(initP);
227
- const [store, setStore] = coreFn(() => {}, firstState ?? initialValue, options);
228
- scheduleIteratorConsumption(iter, patches => {
229
- setStore(d => {
230
- applyPatches(d, patches);
231
- });
232
- });
233
- return [store, setStore];
289
+ const loaded = sharedConfig.load(expectedId);
290
+ if (!isAsyncIterable(loaded)) return null;
291
+ const srcIt = loaded[Symbol.asyncIterator]();
292
+ let isFirst = true;
293
+ let buffered = null;
294
+ return coreFn(draft => {
295
+ const process = res => {
296
+ if (res.done) return {
297
+ done: true,
298
+ value: undefined
299
+ };
300
+ if (isFirst) {
301
+ isFirst = false;
302
+ if (Array.isArray(res.value)) {
303
+ for (let i = 0; i < res.value.length; i++) draft[i] = res.value[i];
304
+ draft.length = res.value.length;
305
+ } else {
306
+ Object.assign(draft, res.value);
307
+ }
308
+ } else {
309
+ applyPatches(draft, res.value);
310
+ }
311
+ return {
312
+ done: false,
313
+ value: undefined
314
+ };
315
+ };
316
+ return {
317
+ [Symbol.asyncIterator]() {
318
+ return {
319
+ next() {
320
+ if (isFirst) {
321
+ const r = srcIt.next();
322
+ return r && typeof r.then === "function" ? {
323
+ then(fn, rej) {
324
+ r.then(v => fn(process(v)), rej);
325
+ }
326
+ } : syncThenable(process(r));
327
+ }
328
+ if (buffered) {
329
+ const b = buffered;
330
+ buffered = null;
331
+ return b.then(process);
332
+ }
333
+ let r = srcIt.next();
334
+ if (r && typeof r.then === "function") {
335
+ return r.then(process);
336
+ }
337
+ let result = process(r);
338
+ while (!r.done) {
339
+ const peek = srcIt.next();
340
+ if (peek && typeof peek.then === "function") {
341
+ buffered = peek;
342
+ break;
343
+ }
344
+ r = peek;
345
+ if (!r.done) result = process(r);
346
+ }
347
+ return Promise.resolve(result);
348
+ }
349
+ };
350
+ }
351
+ };
352
+ }, initialValue, options);
234
353
  }
235
354
  function hydratedCreateMemo(compute, value, options) {
236
- if (!sharedConfig.hydrating) return createMemo$1(compute, value, options);
355
+ if (!sharedConfig.hydrating || options?.transparent) {
356
+ return createMemo$1(compute, value, options);
357
+ }
237
358
  markTopLevelSnapshotScope();
238
359
  const ssrSource = options?.ssrSource;
239
360
  if (ssrSource === "client") {
240
- const [hydrated, setHydrated] = createSignal$1(false);
361
+ const [hydrated, setHydrated] = createSignal$1(false, {
362
+ pureWrite: true
363
+ });
241
364
  const memo = createMemo$1(prev => {
242
365
  if (!hydrated()) return prev ?? value;
243
366
  return compute(prev);
@@ -268,7 +391,9 @@ function hydratedCreateSignal(fn, second, third) {
268
391
  markTopLevelSnapshotScope();
269
392
  const ssrSource = third?.ssrSource;
270
393
  if (ssrSource === "client") {
271
- const [hydrated, setHydrated] = createSignal$1(false);
394
+ const [hydrated, setHydrated] = createSignal$1(false, {
395
+ pureWrite: true
396
+ });
272
397
  const sig = createSignal$1(prev => {
273
398
  if (!hydrated()) return prev ?? second;
274
399
  return fn(prev);
@@ -319,7 +444,9 @@ function hydratedCreateOptimistic(fn, second, third) {
319
444
  markTopLevelSnapshotScope();
320
445
  const ssrSource = third?.ssrSource;
321
446
  if (ssrSource === "client") {
322
- const [hydrated, setHydrated] = createSignal$1(false);
447
+ const [hydrated, setHydrated] = createSignal$1(false, {
448
+ pureWrite: true
449
+ });
323
450
  const sig = createOptimistic$1(prev => {
324
451
  if (!hydrated()) return prev ?? second;
325
452
  return fn(prev);
@@ -345,14 +472,7 @@ function hydratedCreateOptimistic(fn, second, third) {
345
472
  return init != null ? (subFetch(fn, prev), init) : fn(prev);
346
473
  }, second, third);
347
474
  }
348
- function wrapStoreFn(fn, ssrSource) {
349
- if (ssrSource === "initial") {
350
- return draft => {
351
- if (!sharedConfig.hydrating) return fn(draft);
352
- subFetch(fn, draft);
353
- return undefined;
354
- };
355
- }
475
+ function wrapStoreFn(fn) {
356
476
  return draft => {
357
477
  const o = getOwner();
358
478
  if (!sharedConfig.hydrating) return fn(draft);
@@ -362,44 +482,77 @@ function wrapStoreFn(fn, ssrSource) {
362
482
  return init != null ? (subFetch(fn, draft), init) : fn(draft);
363
483
  };
364
484
  }
485
+ function hydrateStoreLikeFn(coreFn, fn, initialValue, options, ssrSource) {
486
+ if (ssrSource === "client") {
487
+ const [hydrated, setHydrated] = createSignal$1(false, {
488
+ pureWrite: true
489
+ });
490
+ const result = coreFn(draft => {
491
+ if (!hydrated()) return;
492
+ return fn(draft);
493
+ }, initialValue, options);
494
+ setHydrated(true);
495
+ return result;
496
+ }
497
+ if (ssrSource === "hybrid") {
498
+ const [hydrated, setHydrated] = createSignal$1(false, {
499
+ pureWrite: true
500
+ });
501
+ const result = coreFn(draft => {
502
+ const o = getOwner();
503
+ if (!hydrated()) {
504
+ if (sharedConfig.has(o.id)) {
505
+ const initP = sharedConfig.load(o.id);
506
+ const init = initP?.v ?? initP;
507
+ if (init != null) {
508
+ subFetch(fn, draft);
509
+ return init;
510
+ }
511
+ }
512
+ return fn(draft);
513
+ }
514
+ const {
515
+ proxy,
516
+ activate
517
+ } = createShadowDraft(draft);
518
+ const r = fn(proxy);
519
+ return isAsyncIterable(r) ? wrapFirstYield(r, activate) : r;
520
+ }, initialValue, options);
521
+ setHydrated(true);
522
+ return result;
523
+ }
524
+ const aiResult = hydrateStoreFromAsyncIterable(coreFn, initialValue, options);
525
+ if (aiResult !== null) return aiResult;
526
+ return coreFn(wrapStoreFn(fn), initialValue, options);
527
+ }
365
528
  function hydratedCreateStore(first, second, third) {
366
529
  if (typeof first !== "function" || !sharedConfig.hydrating) return createStore$1(first, second, third);
367
530
  markTopLevelSnapshotScope();
368
531
  const ssrSource = third?.ssrSource;
369
- if (ssrSource === "client" || ssrSource === "initial") {
370
- return createStore$1(second ?? {}, undefined, third);
371
- }
372
- const aiResult = hydrateStoreFromAsyncIterable(createStore$1, second ?? {}, third);
373
- if (aiResult !== null) return aiResult;
374
- return createStore$1(wrapStoreFn(first, ssrSource), second, third);
532
+ if (ssrSource === "initial") return createStore$1(second ?? {}, undefined, third);
533
+ return hydrateStoreLikeFn(createStore$1, first, second ?? {}, third, ssrSource);
375
534
  }
376
535
  function hydratedCreateOptimisticStore(first, second, third) {
377
536
  if (typeof first !== "function" || !sharedConfig.hydrating) return createOptimisticStore$1(first, second, third);
378
537
  markTopLevelSnapshotScope();
379
538
  const ssrSource = third?.ssrSource;
380
- if (ssrSource === "client" || ssrSource === "initial") {
381
- return createOptimisticStore$1(second ?? {}, undefined, third);
382
- }
383
- const aiResult = hydrateStoreFromAsyncIterable(createOptimisticStore$1, second ?? {}, third);
384
- if (aiResult !== null) return aiResult;
385
- return createOptimisticStore$1(wrapStoreFn(first, ssrSource), second, third);
539
+ if (ssrSource === "initial") return createOptimisticStore$1(second ?? {}, undefined, third);
540
+ return hydrateStoreLikeFn(createOptimisticStore$1, first, second ?? {}, third, ssrSource);
386
541
  }
387
542
  function hydratedCreateProjection(fn, initialValue, options) {
388
543
  if (!sharedConfig.hydrating) return createProjection$1(fn, initialValue, options);
389
544
  markTopLevelSnapshotScope();
390
545
  const ssrSource = options?.ssrSource;
391
- if (ssrSource === "client" || ssrSource === "initial") {
392
- return createProjection$1(draft => draft, initialValue, options);
393
- }
394
- const aiResult = hydrateStoreFromAsyncIterable(createStore$1, initialValue, options);
395
- if (aiResult !== null) return aiResult[0];
396
- return createProjection$1(wrapStoreFn(fn, ssrSource), initialValue, options);
546
+ if (ssrSource === "initial") return createProjection$1(draft => draft, initialValue, options);
547
+ return hydrateStoreLikeFn(createProjection$1, fn, initialValue, options, ssrSource);
397
548
  }
398
549
  function hydratedEffect(coreFn, compute, effectFn, value, options) {
399
550
  if (!sharedConfig.hydrating) return coreFn(compute, effectFn, value, options);
400
551
  const ssrSource = options?.ssrSource;
401
552
  if (ssrSource === "client") {
402
- const [hydrated, setHydrated] = createSignal$1(false);
553
+ const [hydrated, setHydrated] = createSignal$1(false, {
554
+ pureWrite: true
555
+ });
403
556
  let active = false;
404
557
  coreFn(prev => {
405
558
  if (!hydrated()) return value;
@@ -533,13 +686,16 @@ function resumeBoundaryHydration(o, id, set) {
533
686
  set();
534
687
  flush();
535
688
  _snapshotRootOwner = null;
536
- _hydratingValue = false;
537
689
  releaseSnapshotScope(o);
538
690
  flush();
691
+ _hydratingValue = false;
539
692
  checkHydrationComplete();
540
693
  }
541
694
  function Loading(props) {
542
- if (!sharedConfig.hydrating) return createLoadBoundary(() => props.children, () => props.fallback);
695
+ const onOpt = props.on ? {
696
+ on: () => props.on()
697
+ } : undefined;
698
+ if (!sharedConfig.hydrating) return createLoadingBoundary(() => props.children, () => props.fallback, onOpt);
543
699
  return createMemo$1(() => {
544
700
  const o = getOwner();
545
701
  const id = o.id;
@@ -587,7 +743,8 @@ function Loading(props) {
587
743
  assetPromise.then(() => resumeBoundaryHydration(o, id, set));
588
744
  return undefined;
589
745
  }
590
- return createLoadBoundary(() => props.children, () => props.fallback);
746
+ const boundary = createLoadingBoundary(() => props.children, () => props.fallback, onOpt);
747
+ return boundary;
591
748
  });
592
749
  }
593
750
  function NoHydration(props) {
@@ -673,17 +830,10 @@ function Show(props) {
673
830
  if (c) {
674
831
  const child = props.children;
675
832
  const fn = typeof child === "function" && child.length > 0;
676
- return fn ? untrack(() => {
677
- setStrictRead("<Show>");
678
- try {
679
- return child(() => {
680
- if (!untrack(condition)) throw narrowedError("Show");
681
- return conditionValue();
682
- });
683
- } finally {
684
- setStrictRead(false);
685
- }
686
- }) : child;
833
+ return fn ? untrack(() => child(() => {
834
+ if (!untrack(condition)) throw narrowedError("Show");
835
+ return conditionValue();
836
+ }), "<Show>") : child;
687
837
  }
688
838
  return props.fallback;
689
839
  }, undefined, {
@@ -716,17 +866,10 @@ function Switch(props) {
716
866
  const [index, conditionValue, mp] = sel;
717
867
  const child = mp.children;
718
868
  const fn = typeof child === "function" && child.length > 0;
719
- return fn ? untrack(() => {
720
- setStrictRead("<Match>");
721
- try {
722
- return child(() => {
723
- if (untrack(switchFunc)()?.[0] !== index) throw narrowedError("Match");
724
- return conditionValue();
725
- });
726
- } finally {
727
- setStrictRead(false);
728
- }
729
- }) : child;
869
+ return fn ? untrack(() => child(() => {
870
+ if (untrack(switchFunc)()?.[0] !== index) throw narrowedError("Match");
871
+ return conditionValue();
872
+ }), "<Match>") : child;
730
873
  }, undefined, {
731
874
  name: "eval conditions"
732
875
  } );