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/solid.cjs CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  var signals = require('@solidjs/signals');
4
4
 
5
+ const IS_DEV = false;
5
6
  const $DEVCOMP = Symbol(0);
6
7
  function createContext(defaultValue, options) {
7
8
  const id = Symbol(options && options.name || "");
@@ -132,11 +133,41 @@ function subFetch(fn, prev) {
132
133
  Promise = ogPromise;
133
134
  }
134
135
  }
135
- function consumeFirstSync(ai) {
136
- const iter = ai[Symbol.asyncIterator]();
137
- const r = iter.next();
138
- const value = !(r instanceof Promise) && !r.done ? r.value : undefined;
139
- return [value, iter];
136
+ function syncThenable(value) {
137
+ return {
138
+ then(fn) {
139
+ fn(value);
140
+ }
141
+ };
142
+ }
143
+ function normalizeIterator(it) {
144
+ let first = true;
145
+ let buffered = null;
146
+ return {
147
+ next() {
148
+ if (first) {
149
+ first = false;
150
+ const r = it.next();
151
+ return r && typeof r.then === "function" ? r : syncThenable(r);
152
+ }
153
+ if (buffered) {
154
+ const b = buffered;
155
+ buffered = null;
156
+ return b;
157
+ }
158
+ let latest = it.next();
159
+ if (latest && typeof latest.then === "function") return latest;
160
+ while (!latest.done) {
161
+ const peek = it.next();
162
+ if (peek && typeof peek.then === "function") {
163
+ buffered = peek;
164
+ break;
165
+ }
166
+ latest = peek;
167
+ }
168
+ return Promise.resolve(latest);
169
+ }
170
+ };
140
171
  }
141
172
  function applyPatches(target, patches) {
142
173
  for (const patch of patches) {
@@ -153,63 +184,163 @@ function applyPatches(target, patches) {
153
184
  }
154
185
  }
155
186
  }
156
- function scheduleIteratorConsumption(iter, apply) {
157
- const consume = () => {
158
- while (true) {
159
- const n = iter.next();
160
- if (n instanceof Promise) {
161
- n.then(r => {
162
- if (r.done) return;
163
- apply(r.value);
164
- consume();
165
- });
166
- return;
187
+ function isAsyncIterable(v) {
188
+ return v != null && typeof v[Symbol.asyncIterator] === "function";
189
+ }
190
+ function createShadowDraft(realDraft) {
191
+ const shadow = JSON.parse(JSON.stringify(realDraft));
192
+ let useShadow = true;
193
+ return {
194
+ proxy: new Proxy(shadow, {
195
+ get(_, prop) {
196
+ return useShadow ? shadow[prop] : realDraft[prop];
197
+ },
198
+ set(_, prop, value) {
199
+ if (useShadow) {
200
+ shadow[prop] = value;
201
+ return true;
202
+ }
203
+ return Reflect.set(realDraft, prop, value);
204
+ },
205
+ deleteProperty(_, prop) {
206
+ if (useShadow) {
207
+ delete shadow[prop];
208
+ return true;
209
+ }
210
+ return Reflect.deleteProperty(realDraft, prop);
211
+ },
212
+ has(_, prop) {
213
+ return prop in (useShadow ? shadow : realDraft);
214
+ },
215
+ ownKeys() {
216
+ return Reflect.ownKeys(useShadow ? shadow : realDraft);
217
+ },
218
+ getOwnPropertyDescriptor(_, prop) {
219
+ return Object.getOwnPropertyDescriptor(useShadow ? shadow : realDraft, prop);
167
220
  }
168
- if (n.done) break;
169
- apply(n.value);
221
+ }),
222
+ activate() {
223
+ useShadow = false;
170
224
  }
171
225
  };
172
- consume();
173
226
  }
174
- function isAsyncIterable(v) {
175
- return v != null && typeof v[Symbol.asyncIterator] === "function";
227
+ function wrapFirstYield(iterable, activate) {
228
+ const srcIt = iterable[Symbol.asyncIterator]();
229
+ let first = true;
230
+ return {
231
+ [Symbol.asyncIterator]() {
232
+ return {
233
+ next() {
234
+ const p = srcIt.next();
235
+ if (first) {
236
+ first = false;
237
+ return p.then(r => {
238
+ activate();
239
+ return r.done ? r : {
240
+ done: false,
241
+ value: undefined
242
+ };
243
+ });
244
+ }
245
+ return p;
246
+ }
247
+ };
248
+ }
249
+ };
176
250
  }
177
251
  function hydrateSignalFromAsyncIterable(coreFn, compute, value, options) {
178
252
  const parent = signals.getOwner();
179
253
  const expectedId = signals.peekNextChildId(parent);
180
254
  if (!sharedConfig.has(expectedId)) return null;
181
- const initP = sharedConfig.load(expectedId);
182
- if (!isAsyncIterable(initP)) return null;
183
- const [firstValue, iter] = consumeFirstSync(initP);
184
- const [get, set] = signals.createSignal(firstValue);
185
- const result = coreFn(() => get(), firstValue, options);
186
- scheduleIteratorConsumption(iter, v => {
187
- set(() => v);
188
- signals.flush();
189
- });
190
- return result;
255
+ const loaded = sharedConfig.load(expectedId);
256
+ if (!isAsyncIterable(loaded)) return null;
257
+ const it = normalizeIterator(loaded[Symbol.asyncIterator]());
258
+ const iterable = {
259
+ [Symbol.asyncIterator]() {
260
+ return it;
261
+ }
262
+ };
263
+ return coreFn(() => iterable, value, options);
191
264
  }
192
265
  function hydrateStoreFromAsyncIterable(coreFn, initialValue, options) {
193
266
  const parent = signals.getOwner();
194
267
  const expectedId = signals.peekNextChildId(parent);
195
268
  if (!sharedConfig.has(expectedId)) return null;
196
- const initP = sharedConfig.load(expectedId);
197
- if (!isAsyncIterable(initP)) return null;
198
- const [firstState, iter] = consumeFirstSync(initP);
199
- const [store, setStore] = coreFn(() => {}, firstState ?? initialValue, options);
200
- scheduleIteratorConsumption(iter, patches => {
201
- setStore(d => {
202
- applyPatches(d, patches);
203
- });
204
- });
205
- return [store, setStore];
269
+ const loaded = sharedConfig.load(expectedId);
270
+ if (!isAsyncIterable(loaded)) return null;
271
+ const srcIt = loaded[Symbol.asyncIterator]();
272
+ let isFirst = true;
273
+ let buffered = null;
274
+ return coreFn(draft => {
275
+ const process = res => {
276
+ if (res.done) return {
277
+ done: true,
278
+ value: undefined
279
+ };
280
+ if (isFirst) {
281
+ isFirst = false;
282
+ if (Array.isArray(res.value)) {
283
+ for (let i = 0; i < res.value.length; i++) draft[i] = res.value[i];
284
+ draft.length = res.value.length;
285
+ } else {
286
+ Object.assign(draft, res.value);
287
+ }
288
+ } else {
289
+ applyPatches(draft, res.value);
290
+ }
291
+ return {
292
+ done: false,
293
+ value: undefined
294
+ };
295
+ };
296
+ return {
297
+ [Symbol.asyncIterator]() {
298
+ return {
299
+ next() {
300
+ if (isFirst) {
301
+ const r = srcIt.next();
302
+ return r && typeof r.then === "function" ? {
303
+ then(fn, rej) {
304
+ r.then(v => fn(process(v)), rej);
305
+ }
306
+ } : syncThenable(process(r));
307
+ }
308
+ if (buffered) {
309
+ const b = buffered;
310
+ buffered = null;
311
+ return b.then(process);
312
+ }
313
+ let r = srcIt.next();
314
+ if (r && typeof r.then === "function") {
315
+ return r.then(process);
316
+ }
317
+ let result = process(r);
318
+ while (!r.done) {
319
+ const peek = srcIt.next();
320
+ if (peek && typeof peek.then === "function") {
321
+ buffered = peek;
322
+ break;
323
+ }
324
+ r = peek;
325
+ if (!r.done) result = process(r);
326
+ }
327
+ return Promise.resolve(result);
328
+ }
329
+ };
330
+ }
331
+ };
332
+ }, initialValue, options);
206
333
  }
207
334
  function hydratedCreateMemo(compute, value, options) {
208
- if (!sharedConfig.hydrating) return signals.createMemo(compute, value, options);
335
+ if (!sharedConfig.hydrating || options?.transparent) {
336
+ return signals.createMemo(compute, value, options);
337
+ }
209
338
  markTopLevelSnapshotScope();
210
339
  const ssrSource = options?.ssrSource;
211
340
  if (ssrSource === "client") {
212
- const [hydrated, setHydrated] = signals.createSignal(false);
341
+ const [hydrated, setHydrated] = signals.createSignal(false, {
342
+ pureWrite: true
343
+ });
213
344
  const memo = signals.createMemo(prev => {
214
345
  if (!hydrated()) return prev ?? value;
215
346
  return compute(prev);
@@ -240,7 +371,9 @@ function hydratedCreateSignal(fn, second, third) {
240
371
  markTopLevelSnapshotScope();
241
372
  const ssrSource = third?.ssrSource;
242
373
  if (ssrSource === "client") {
243
- const [hydrated, setHydrated] = signals.createSignal(false);
374
+ const [hydrated, setHydrated] = signals.createSignal(false, {
375
+ pureWrite: true
376
+ });
244
377
  const sig = signals.createSignal(prev => {
245
378
  if (!hydrated()) return prev ?? second;
246
379
  return fn(prev);
@@ -291,7 +424,9 @@ function hydratedCreateOptimistic(fn, second, third) {
291
424
  markTopLevelSnapshotScope();
292
425
  const ssrSource = third?.ssrSource;
293
426
  if (ssrSource === "client") {
294
- const [hydrated, setHydrated] = signals.createSignal(false);
427
+ const [hydrated, setHydrated] = signals.createSignal(false, {
428
+ pureWrite: true
429
+ });
295
430
  const sig = signals.createOptimistic(prev => {
296
431
  if (!hydrated()) return prev ?? second;
297
432
  return fn(prev);
@@ -317,14 +452,7 @@ function hydratedCreateOptimistic(fn, second, third) {
317
452
  return init != null ? (subFetch(fn, prev), init) : fn(prev);
318
453
  }, second, third);
319
454
  }
320
- function wrapStoreFn(fn, ssrSource) {
321
- if (ssrSource === "initial") {
322
- return draft => {
323
- if (!sharedConfig.hydrating) return fn(draft);
324
- subFetch(fn, draft);
325
- return undefined;
326
- };
327
- }
455
+ function wrapStoreFn(fn) {
328
456
  return draft => {
329
457
  const o = signals.getOwner();
330
458
  if (!sharedConfig.hydrating) return fn(draft);
@@ -334,44 +462,77 @@ function wrapStoreFn(fn, ssrSource) {
334
462
  return init != null ? (subFetch(fn, draft), init) : fn(draft);
335
463
  };
336
464
  }
465
+ function hydrateStoreLikeFn(coreFn, fn, initialValue, options, ssrSource) {
466
+ if (ssrSource === "client") {
467
+ const [hydrated, setHydrated] = signals.createSignal(false, {
468
+ pureWrite: true
469
+ });
470
+ const result = coreFn(draft => {
471
+ if (!hydrated()) return;
472
+ return fn(draft);
473
+ }, initialValue, options);
474
+ setHydrated(true);
475
+ return result;
476
+ }
477
+ if (ssrSource === "hybrid") {
478
+ const [hydrated, setHydrated] = signals.createSignal(false, {
479
+ pureWrite: true
480
+ });
481
+ const result = coreFn(draft => {
482
+ const o = signals.getOwner();
483
+ if (!hydrated()) {
484
+ if (sharedConfig.has(o.id)) {
485
+ const initP = sharedConfig.load(o.id);
486
+ const init = initP?.v ?? initP;
487
+ if (init != null) {
488
+ subFetch(fn, draft);
489
+ return init;
490
+ }
491
+ }
492
+ return fn(draft);
493
+ }
494
+ const {
495
+ proxy,
496
+ activate
497
+ } = createShadowDraft(draft);
498
+ const r = fn(proxy);
499
+ return isAsyncIterable(r) ? wrapFirstYield(r, activate) : r;
500
+ }, initialValue, options);
501
+ setHydrated(true);
502
+ return result;
503
+ }
504
+ const aiResult = hydrateStoreFromAsyncIterable(coreFn, initialValue, options);
505
+ if (aiResult !== null) return aiResult;
506
+ return coreFn(wrapStoreFn(fn), initialValue, options);
507
+ }
337
508
  function hydratedCreateStore(first, second, third) {
338
509
  if (typeof first !== "function" || !sharedConfig.hydrating) return signals.createStore(first, second, third);
339
510
  markTopLevelSnapshotScope();
340
511
  const ssrSource = third?.ssrSource;
341
- if (ssrSource === "client" || ssrSource === "initial") {
342
- return signals.createStore(second ?? {}, undefined, third);
343
- }
344
- const aiResult = hydrateStoreFromAsyncIterable(signals.createStore, second ?? {}, third);
345
- if (aiResult !== null) return aiResult;
346
- return signals.createStore(wrapStoreFn(first, ssrSource), second, third);
512
+ if (ssrSource === "initial") return signals.createStore(second ?? {}, undefined, third);
513
+ return hydrateStoreLikeFn(signals.createStore, first, second ?? {}, third, ssrSource);
347
514
  }
348
515
  function hydratedCreateOptimisticStore(first, second, third) {
349
516
  if (typeof first !== "function" || !sharedConfig.hydrating) return signals.createOptimisticStore(first, second, third);
350
517
  markTopLevelSnapshotScope();
351
518
  const ssrSource = third?.ssrSource;
352
- if (ssrSource === "client" || ssrSource === "initial") {
353
- return signals.createOptimisticStore(second ?? {}, undefined, third);
354
- }
355
- const aiResult = hydrateStoreFromAsyncIterable(signals.createOptimisticStore, second ?? {}, third);
356
- if (aiResult !== null) return aiResult;
357
- return signals.createOptimisticStore(wrapStoreFn(first, ssrSource), second, third);
519
+ if (ssrSource === "initial") return signals.createOptimisticStore(second ?? {}, undefined, third);
520
+ return hydrateStoreLikeFn(signals.createOptimisticStore, first, second ?? {}, third, ssrSource);
358
521
  }
359
522
  function hydratedCreateProjection(fn, initialValue, options) {
360
523
  if (!sharedConfig.hydrating) return signals.createProjection(fn, initialValue, options);
361
524
  markTopLevelSnapshotScope();
362
525
  const ssrSource = options?.ssrSource;
363
- if (ssrSource === "client" || ssrSource === "initial") {
364
- return signals.createProjection(draft => draft, initialValue, options);
365
- }
366
- const aiResult = hydrateStoreFromAsyncIterable(signals.createStore, initialValue, options);
367
- if (aiResult !== null) return aiResult[0];
368
- return signals.createProjection(wrapStoreFn(fn, ssrSource), initialValue, options);
526
+ if (ssrSource === "initial") return signals.createProjection(draft => draft, initialValue, options);
527
+ return hydrateStoreLikeFn(signals.createProjection, fn, initialValue, options, ssrSource);
369
528
  }
370
529
  function hydratedEffect(coreFn, compute, effectFn, value, options) {
371
530
  if (!sharedConfig.hydrating) return coreFn(compute, effectFn, value, options);
372
531
  const ssrSource = options?.ssrSource;
373
532
  if (ssrSource === "client") {
374
- const [hydrated, setHydrated] = signals.createSignal(false);
533
+ const [hydrated, setHydrated] = signals.createSignal(false, {
534
+ pureWrite: true
535
+ });
375
536
  let active = false;
376
537
  coreFn(prev => {
377
538
  if (!hydrated()) return value;
@@ -505,13 +666,16 @@ function resumeBoundaryHydration(o, id, set) {
505
666
  set();
506
667
  signals.flush();
507
668
  _snapshotRootOwner = null;
508
- _hydratingValue = false;
509
669
  signals.releaseSnapshotScope(o);
510
670
  signals.flush();
671
+ _hydratingValue = false;
511
672
  checkHydrationComplete();
512
673
  }
513
674
  function Loading(props) {
514
- if (!sharedConfig.hydrating) return signals.createLoadBoundary(() => props.children, () => props.fallback);
675
+ const onOpt = props.on ? {
676
+ on: () => props.on()
677
+ } : undefined;
678
+ if (!sharedConfig.hydrating) return signals.createLoadingBoundary(() => props.children, () => props.fallback, onOpt);
515
679
  return signals.createMemo(() => {
516
680
  const o = signals.getOwner();
517
681
  const id = o.id;
@@ -559,7 +723,8 @@ function Loading(props) {
559
723
  assetPromise.then(() => resumeBoundaryHydration(o, id, set));
560
724
  return undefined;
561
725
  }
562
- return signals.createLoadBoundary(() => props.children, () => props.fallback);
726
+ const boundary = signals.createLoadingBoundary(() => props.children, () => props.fallback, onOpt);
727
+ return boundary;
563
728
  });
564
729
  }
565
730
  function NoHydration(props) {
@@ -637,15 +802,10 @@ function Show(props) {
637
802
  if (c) {
638
803
  const child = props.children;
639
804
  const fn = typeof child === "function" && child.length > 0;
640
- return fn ? signals.untrack(() => {
641
- try {
642
- return child(() => {
643
- if (!signals.untrack(condition)) throw narrowedError("Show");
644
- return conditionValue();
645
- });
646
- } finally {
647
- }
648
- }) : child;
805
+ return fn ? signals.untrack(() => child(() => {
806
+ if (!signals.untrack(condition)) throw narrowedError("Show");
807
+ return conditionValue();
808
+ }), IS_DEV) : child;
649
809
  }
650
810
  return props.fallback;
651
811
  }, undefined, undefined);
@@ -673,15 +833,10 @@ function Switch(props) {
673
833
  const [index, conditionValue, mp] = sel;
674
834
  const child = mp.children;
675
835
  const fn = typeof child === "function" && child.length > 0;
676
- return fn ? signals.untrack(() => {
677
- try {
678
- return child(() => {
679
- if (signals.untrack(switchFunc)()?.[0] !== index) throw narrowedError("Match");
680
- return conditionValue();
681
- });
682
- } finally {
683
- }
684
- }) : child;
836
+ return fn ? signals.untrack(() => child(() => {
837
+ if (signals.untrack(switchFunc)()?.[0] !== index) throw narrowedError("Match");
838
+ return conditionValue();
839
+ }), IS_DEV) : child;
685
840
  }, undefined, undefined);
686
841
  }
687
842
  function Match(props) {
@@ -702,6 +857,10 @@ Object.defineProperty(exports, "$PROXY", {
702
857
  enumerable: true,
703
858
  get: function () { return signals.$PROXY; }
704
859
  });
860
+ Object.defineProperty(exports, "$REFRESH", {
861
+ enumerable: true,
862
+ get: function () { return signals.$REFRESH; }
863
+ });
705
864
  Object.defineProperty(exports, "$TRACK", {
706
865
  enumerable: true,
707
866
  get: function () { return signals.$TRACK; }
@@ -714,6 +873,14 @@ Object.defineProperty(exports, "action", {
714
873
  enumerable: true,
715
874
  get: function () { return signals.action; }
716
875
  });
876
+ Object.defineProperty(exports, "createErrorBoundary", {
877
+ enumerable: true,
878
+ get: function () { return signals.createErrorBoundary; }
879
+ });
880
+ Object.defineProperty(exports, "createLoadingBoundary", {
881
+ enumerable: true,
882
+ get: function () { return signals.createLoadingBoundary; }
883
+ });
717
884
  Object.defineProperty(exports, "createOwner", {
718
885
  enumerable: true,
719
886
  get: function () { return signals.createOwner; }
@@ -734,6 +901,14 @@ Object.defineProperty(exports, "deep", {
734
901
  enumerable: true,
735
902
  get: function () { return signals.deep; }
736
903
  });
904
+ Object.defineProperty(exports, "enableExternalSource", {
905
+ enumerable: true,
906
+ get: function () { return signals.enableExternalSource; }
907
+ });
908
+ Object.defineProperty(exports, "enforceLoadingBoundary", {
909
+ enumerable: true,
910
+ get: function () { return signals.enforceLoadingBoundary; }
911
+ });
737
912
  Object.defineProperty(exports, "flatten", {
738
913
  enumerable: true,
739
914
  get: function () { return signals.flatten; }