solid-js 1.2.5 → 1.3.0-beta.10

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/README.md CHANGED
@@ -1,4 +1,6 @@
1
- ## <img src="https://raw.githubusercontent.com/solidjs/solid/main/assets/logo.png" alt="Solid" width="350"/><br>
1
+ <p>
2
+ <img width="100%" src="https://raw.githubusercontent.com/solidjs/solid/master/banner.png" alt="SolidJS">
3
+ </p>
2
4
 
3
5
  [![Build Status](https://github.com/solidjs/solid/workflows/Solid%20CI/badge.svg)](https://github.com/solidjs/solid/actions/workflows/main-ci.yml)
4
6
  [![Coverage Status](https://img.shields.io/coveralls/github/solidjs/solid.svg?style=flat)](https://coveralls.io/github/solidjs/solid?branch=main)
package/dist/dev.cjs CHANGED
@@ -149,6 +149,7 @@ const [transPending, setTransPending] = /*@__PURE__*/createSignal(false);
149
149
  var Owner = null;
150
150
  let Transition = null;
151
151
  let Scheduler = null;
152
+ let ExternalSourceFactory = null;
152
153
  let Listener = null;
153
154
  let Pending = null;
154
155
  let Updates = null;
@@ -187,12 +188,13 @@ function createSignal(value, options) {
187
188
  comparator: options.equals || undefined
188
189
  };
189
190
  if (!options.internal) s.name = registerGraph(options.name || hashValue(value), s);
190
- return [readSignal.bind(s), value => {
191
+ const setter = value => {
191
192
  if (typeof value === "function") {
192
193
  if (Transition && Transition.running && Transition.sources.has(s)) value = value(s.pending !== NOTPENDING ? s.pending : s.tValue);else value = value(s.pending !== NOTPENDING ? s.pending : s.value);
193
194
  }
194
195
  return writeSignal(s, value);
195
- }];
196
+ };
197
+ return [readSignal.bind(s), setter];
196
198
  }
197
199
  function createComputed(fn, value, options) {
198
200
  const c = createComputation(fn, value, true, STALE, options );
@@ -234,6 +236,9 @@ function createResource(source, fetcher, options) {
234
236
  fetcher = source;
235
237
  source = true;
236
238
  }
239
+ Resources || (Resources = new Set());
240
+ Resources.add(load);
241
+ onCleanup(() => Resources.delete(load));
237
242
  const contexts = new Set(),
238
243
  [s, set] = createSignal((options || {}).initialValue),
239
244
  [track, trigger] = createSignal(undefined, {
@@ -249,12 +254,7 @@ function createResource(source, fetcher, options) {
249
254
  dynamic = typeof source === "function";
250
255
  if (sharedConfig.context) {
251
256
  id = `${sharedConfig.context.id}${sharedConfig.context.count++}`;
252
- if (sharedConfig.context.loadResource) {
253
- initP = sharedConfig.context.loadResource(id);
254
- } else if (sharedConfig.resources && id && id in sharedConfig.resources) {
255
- initP = sharedConfig.resources[id];
256
- delete sharedConfig.resources[id];
257
- }
257
+ if (sharedConfig.load) initP = sharedConfig.load(id);
258
258
  }
259
259
  function loadEnd(p, v, e) {
260
260
  if (pr === p) {
@@ -300,7 +300,7 @@ function createResource(source, fetcher, options) {
300
300
  }
301
301
  return v;
302
302
  }
303
- function load() {
303
+ function load(refetching = true) {
304
304
  setError(err = undefined);
305
305
  const lookup = dynamic ? source() : source;
306
306
  loadedUnderTransition = Transition && Transition.running;
@@ -309,7 +309,10 @@ function createResource(source, fetcher, options) {
309
309
  return;
310
310
  }
311
311
  if (Transition && pr) Transition.promises.delete(pr);
312
- const p = initP || untrack(() => fetcher(lookup, s));
312
+ const p = initP || untrack(() => fetcher(lookup, {
313
+ value: s(),
314
+ refetching
315
+ }));
313
316
  initP = null;
314
317
  if (typeof p !== "object" || !("then" in p)) {
315
318
  loadEnd(pr, p);
@@ -334,12 +337,16 @@ function createResource(source, fetcher, options) {
334
337
  }
335
338
  }
336
339
  });
337
- if (dynamic) createComputed(load);else load();
340
+ if (dynamic) createComputed(() => load(false));else load(false);
338
341
  return [read, {
339
342
  refetch: load,
340
343
  mutate: set
341
344
  }];
342
345
  }
346
+ let Resources;
347
+ function refetchResources(info) {
348
+ Resources && Resources.forEach(fn => fn(info));
349
+ }
343
350
  function createDeferred(source, options) {
344
351
  let t,
345
352
  timeout = options ? options.timeoutMs : undefined;
@@ -358,7 +365,7 @@ function createSelector(source, fn = equalFn, options) {
358
365
  const subs = new Map();
359
366
  const node = createComputation(p => {
360
367
  const v = source();
361
- for (const key of subs.keys()) if (fn(key, v) || p !== undefined && fn(key, p)) {
368
+ for (const key of subs.keys()) if (fn(key, v) !== (p !== undefined && fn(key, p))) {
362
369
  const l = subs.get(key);
363
370
  for (const c of l.values()) {
364
371
  c.state = STALE;
@@ -409,7 +416,8 @@ function untrack(fn) {
409
416
  Listener = listener;
410
417
  return result;
411
418
  }
412
- function on(deps, fn, options) {
419
+ function on(deps, fn,
420
+ options) {
413
421
  const isArray = Array.isArray(deps);
414
422
  let prevInput;
415
423
  let defer = options && options.defer;
@@ -557,6 +565,24 @@ let SuspenseContext;
557
565
  function getSuspenseContext() {
558
566
  return SuspenseContext || (SuspenseContext = createContext({}));
559
567
  }
568
+ function enableExternalSource(factory) {
569
+ if (ExternalSourceFactory) {
570
+ const oldFactory = ExternalSourceFactory;
571
+ ExternalSourceFactory = (fn, trigger) => {
572
+ const oldSource = oldFactory(fn, trigger);
573
+ const source = factory(x => oldSource.track(x), trigger);
574
+ return {
575
+ track: x => source.track(x),
576
+ dispose() {
577
+ source.dispose();
578
+ oldSource.dispose();
579
+ }
580
+ };
581
+ };
582
+ } else {
583
+ ExternalSourceFactory = factory;
584
+ }
585
+ }
560
586
  function readSignal() {
561
587
  const runningTransition = Transition && Transition.running;
562
588
  if (this.sources && (!runningTransition && this.state || runningTransition && this.tState)) {
@@ -685,6 +711,19 @@ function createComputation(fn, init, pure, state = STALE, options) {
685
711
  }
686
712
  c.name = options && options.name || `${Owner.name || "c"}-${(Owner.owned || Owner.tOwned).length}`;
687
713
  }
714
+ if (ExternalSourceFactory) {
715
+ const [track, trigger] = createSignal(undefined, {
716
+ equals: false
717
+ });
718
+ const ordinary = ExternalSourceFactory(c.fn, trigger);
719
+ onCleanup(() => ordinary.dispose());
720
+ const triggerInTransition = () => startTransition(trigger, () => inTransition.dispose());
721
+ const inTransition = ExternalSourceFactory(c.fn, triggerInTransition);
722
+ c.fn = x => {
723
+ track();
724
+ return Transition && Transition.running ? inTransition.track(x) : ordinary.track(x);
725
+ };
726
+ }
688
727
  return c;
689
728
  }
690
729
  function runTop(node) {
@@ -711,7 +750,7 @@ function runTop(node) {
711
750
  } else if (!runningTransition && node.state === PENDING || runningTransition && node.tState === PENDING) {
712
751
  const updates = Updates;
713
752
  Updates = null;
714
- lookDownstream(node);
753
+ lookDownstream(node, ancestors[0]);
715
754
  Updates = updates;
716
755
  }
717
756
  }
@@ -810,13 +849,15 @@ function runUserEffects(queue) {
810
849
  for (i = 0; i < userLength; i++) runTop(queue[i]);
811
850
  for (i = resume; i < queue.length; i++) runTop(queue[i]);
812
851
  }
813
- function lookDownstream(node) {
852
+ function lookDownstream(node, ignore) {
814
853
  node.state = 0;
815
854
  const runningTransition = Transition && Transition.running;
816
855
  for (let i = 0; i < node.sources.length; i += 1) {
817
856
  const source = node.sources[i];
818
857
  if (source.sources) {
819
- if (!runningTransition && source.state === STALE || runningTransition && source.tState === STALE) runTop(source);else if (!runningTransition && source.state === PENDING || runningTransition && source.tState === PENDING) lookDownstream(source);
858
+ if (!runningTransition && source.state === STALE || runningTransition && source.tState === STALE) {
859
+ if (source !== ignore) runTop(source);
860
+ } else if (!runningTransition && source.state === PENDING || runningTransition && source.tState === PENDING) lookDownstream(source, ignore);
820
861
  }
821
862
  }
822
863
  }
@@ -1140,13 +1181,19 @@ function indexArray(list, mapFn, options = {}) {
1140
1181
  };
1141
1182
  }
1142
1183
 
1184
+ let hydrationEnabled = false;
1185
+ function enableHydration() {
1186
+ hydrationEnabled = true;
1187
+ }
1143
1188
  function createComponent(Comp, props) {
1144
- if (sharedConfig.context) {
1145
- const c = sharedConfig.context;
1146
- setHydrateContext(nextHydrateContext());
1147
- const r = devComponent(Comp, props) ;
1148
- setHydrateContext(c);
1149
- return r;
1189
+ if (hydrationEnabled) {
1190
+ if (sharedConfig.context) {
1191
+ const c = sharedConfig.context;
1192
+ setHydrateContext(nextHydrateContext());
1193
+ const r = devComponent(Comp, props) ;
1194
+ setHydrateContext(c);
1195
+ return r;
1196
+ }
1150
1197
  }
1151
1198
  return devComponent(Comp, props);
1152
1199
  }
@@ -1235,19 +1282,20 @@ function splitProps(props, ...keys) {
1235
1282
  }
1236
1283
  function lazy(fn) {
1237
1284
  let comp;
1285
+ let p;
1238
1286
  const wrap = props => {
1239
1287
  const ctx = sharedConfig.context;
1240
- if (ctx && sharedConfig.resources) {
1288
+ if (ctx) {
1241
1289
  ctx.count++;
1242
1290
  const [s, set] = createSignal();
1243
- fn().then(mod => {
1291
+ (p || (p = fn())).then(mod => {
1244
1292
  setHydrateContext(ctx);
1245
1293
  set(() => mod.default);
1246
- setHydrateContext(undefined);
1294
+ setHydrateContext();
1247
1295
  });
1248
1296
  comp = s;
1249
1297
  } else if (!comp) {
1250
- const [s] = createResource(() => fn().then(mod => mod.default));
1298
+ const [s] = createResource(() => (p || (p = fn())).then(mod => mod.default));
1251
1299
  comp = s;
1252
1300
  } else {
1253
1301
  const c = comp();
@@ -1263,7 +1311,7 @@ function lazy(fn) {
1263
1311
  return r;
1264
1312
  }));
1265
1313
  };
1266
- wrap.preload = () => comp || fn().then(mod => comp = () => mod.default);
1314
+ wrap.preload = () => p || ((p = fn()).then(mod => comp = () => mod.default), p);
1267
1315
  return wrap;
1268
1316
  }
1269
1317
  let counter = 0;
@@ -1310,7 +1358,7 @@ function Switch(props) {
1310
1358
  }
1311
1359
  return [-1];
1312
1360
  }, undefined, {
1313
- equals: (a, b) => a && a[0] === b[0] && (strictEqual ? a[1] === b[1] : !a[1] === !b[1]) && a[2] === b[2]
1361
+ equals: (a, b) => a[0] === b[0] && (strictEqual ? a[1] === b[1] : !a[1] === !b[1]) && a[2] === b[2]
1314
1362
  });
1315
1363
  return createMemo(() => {
1316
1364
  const [index, when, cond] = evalConditions();
@@ -1323,7 +1371,11 @@ function Match(props) {
1323
1371
  return props;
1324
1372
  }
1325
1373
  function ErrorBoundary(props) {
1326
- const [errored, setErrored] = createSignal();
1374
+ let err = undefined;
1375
+ if (sharedConfig.context && sharedConfig.load) {
1376
+ err = sharedConfig.load(sharedConfig.context.id + sharedConfig.context.count);
1377
+ }
1378
+ const [errored, setErrored] = createSignal(err);
1327
1379
  let e;
1328
1380
  return createMemo(() => {
1329
1381
  if ((e = errored()) != null) {
@@ -1404,7 +1456,11 @@ function SuspenseList(props) {
1404
1456
  function Suspense(props) {
1405
1457
  let counter = 0,
1406
1458
  showContent,
1407
- showFallback;
1459
+ showFallback,
1460
+ ctx,
1461
+ p,
1462
+ flicker,
1463
+ error;
1408
1464
  const [inFallback, setFallback] = createSignal(false),
1409
1465
  SuspenseContext = getSuspenseContext(),
1410
1466
  store = {
@@ -1419,6 +1475,24 @@ function Suspense(props) {
1419
1475
  resolved: false
1420
1476
  },
1421
1477
  owner = getOwner();
1478
+ if (sharedConfig.context) {
1479
+ const key = sharedConfig.context.id + sharedConfig.context.count;
1480
+ p = sharedConfig.load(key);
1481
+ if (p && typeof p === "object") {
1482
+ const [s, set] = createSignal(undefined, {
1483
+ equals: false
1484
+ });
1485
+ flicker = s;
1486
+ p.then(err => {
1487
+ if (error = err) return set();
1488
+ sharedConfig.gather(key);
1489
+ setHydrateContext(ctx);
1490
+ set();
1491
+ setHydrateContext();
1492
+ p = undefined;
1493
+ });
1494
+ }
1495
+ }
1422
1496
  const listContext = useContext(SuspenseListContext);
1423
1497
  if (listContext) [showContent, showFallback] = listContext.register(store.inFallback);
1424
1498
  let dispose;
@@ -1426,28 +1500,43 @@ function Suspense(props) {
1426
1500
  return createComponent(SuspenseContext.Provider, {
1427
1501
  value: store,
1428
1502
  get children() {
1429
- const rendered = untrack(() => props.children);
1430
1503
  return createMemo(() => {
1431
- const inFallback = store.inFallback(),
1432
- visibleContent = showContent ? showContent() : true,
1433
- visibleFallback = showFallback ? showFallback() : true;
1434
- dispose && dispose();
1435
- if (!inFallback && visibleContent) {
1436
- store.resolved = true;
1437
- resumeEffects(store.effects);
1438
- return rendered;
1504
+ if (error) throw error;
1505
+ ctx = sharedConfig.context;
1506
+ if (flicker) {
1507
+ flicker();
1508
+ return flicker = undefined;
1439
1509
  }
1440
- if (!visibleFallback) return;
1441
- return createRoot(disposer => {
1442
- dispose = disposer;
1443
- return props.fallback;
1444
- }, owner);
1510
+ if (ctx && p === undefined) setHydrateContext();
1511
+ const rendered = untrack(() => props.children);
1512
+ return createMemo(() => {
1513
+ const inFallback = store.inFallback(),
1514
+ visibleContent = showContent ? showContent() : true,
1515
+ visibleFallback = showFallback ? showFallback() : true;
1516
+ dispose && dispose();
1517
+ if ((!inFallback || p !== undefined) && visibleContent) {
1518
+ store.resolved = true;
1519
+ resumeEffects(store.effects);
1520
+ return rendered;
1521
+ }
1522
+ if (!visibleFallback) return;
1523
+ return createRoot(disposer => {
1524
+ dispose = disposer;
1525
+ if (ctx) {
1526
+ setHydrateContext({
1527
+ id: ctx.id + "f",
1528
+ count: 0
1529
+ });
1530
+ ctx = undefined;
1531
+ }
1532
+ return props.fallback;
1533
+ }, owner);
1534
+ });
1445
1535
  });
1446
1536
  }
1447
1537
  });
1448
1538
  }
1449
1539
 
1450
- function awaitSuspense() {}
1451
1540
  exports.DEV = void 0;
1452
1541
  {
1453
1542
  exports.DEV = {
@@ -1470,7 +1559,6 @@ exports.Show = Show;
1470
1559
  exports.Suspense = Suspense;
1471
1560
  exports.SuspenseList = SuspenseList;
1472
1561
  exports.Switch = Switch;
1473
- exports.awaitSuspense = awaitSuspense;
1474
1562
  exports.batch = batch;
1475
1563
  exports.cancelCallback = cancelCallback;
1476
1564
  exports.children = children;
@@ -1486,6 +1574,8 @@ exports.createRoot = createRoot;
1486
1574
  exports.createSelector = createSelector;
1487
1575
  exports.createSignal = createSignal;
1488
1576
  exports.createUniqueId = createUniqueId;
1577
+ exports.enableExternalSource = enableExternalSource;
1578
+ exports.enableHydration = enableHydration;
1489
1579
  exports.enableScheduling = enableScheduling;
1490
1580
  exports.equalFn = equalFn;
1491
1581
  exports.from = from;
@@ -1500,6 +1590,7 @@ exports.on = on;
1500
1590
  exports.onCleanup = onCleanup;
1501
1591
  exports.onError = onError;
1502
1592
  exports.onMount = onMount;
1593
+ exports.refetchResources = refetchResources;
1503
1594
  exports.requestCallback = requestCallback;
1504
1595
  exports.runWithOwner = runWithOwner;
1505
1596
  exports.sharedConfig = sharedConfig;