solid-js 2.0.0-experimental.1 → 2.0.0-experimental.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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2016-2023 Ryan Carniato
3
+ Copyright (c) 2016-2025 Ryan Carniato
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/dev.cjs CHANGED
@@ -9,8 +9,8 @@ function onMount(fn) {
9
9
  function createContext(defaultValue, options) {
10
10
  const id = Symbol(options && options.name || "");
11
11
  function provider(props) {
12
- return signals.createMemo(() => {
13
- signals.setContext(provider, signals.untrack(() => props.value));
12
+ return signals.createRoot(() => {
13
+ signals.setContext(provider, props.value);
14
14
  return children(() => props.children);
15
15
  });
16
16
  }
@@ -53,6 +53,27 @@ function registerGraph(value) {
53
53
  value.graph = owner;
54
54
  }
55
55
 
56
+ function tryCatch(fn) {
57
+ try {
58
+ const v = fn();
59
+ if (v instanceof Promise) {
60
+ return v.then(v => [undefined, v], e => {
61
+ if (e instanceof signals.NotReadyError) throw e;
62
+ return [e];
63
+ });
64
+ }
65
+ return [undefined, v];
66
+ } catch (e) {
67
+ if (e instanceof signals.NotReadyError) throw e;
68
+ return [e];
69
+ }
70
+ }
71
+ function reducer(source, reducerFn) {
72
+ return [source[0], action => {
73
+ source[1](s => reducerFn(s, action));
74
+ }];
75
+ }
76
+
56
77
  function observable(input) {
57
78
  return {
58
79
  subscribe(observer) {
@@ -83,8 +104,8 @@ function observable(input) {
83
104
  }
84
105
  };
85
106
  }
86
- function from(producer) {
87
- const [s, set] = signals.createSignal(undefined, {
107
+ function from(producer, initialValue = undefined) {
108
+ const [s, set] = signals.createSignal(() => initialValue, initialValue, {
88
109
  equals: false
89
110
  });
90
111
  if ("subscribe" in producer) {
@@ -98,65 +119,111 @@ function from(producer) {
98
119
  }
99
120
 
100
121
  const sharedConfig = {
101
- context: undefined,
122
+ hydrating: false,
102
123
  registry: undefined,
103
124
  done: false,
104
- getContextId() {
105
- return getContextId(this.context.count);
106
- },
107
125
  getNextContextId() {
108
- return getContextId(this.context.count++);
126
+ const o = signals.getOwner();
127
+ if (!o) throw new Error(`getNextContextId cannot be used under non-hydrating context`);
128
+ return signals.getNextChildId(o);
109
129
  }
110
130
  };
111
- function getContextId(count) {
112
- const num = String(count),
113
- len = num.length - 1;
114
- return sharedConfig.context.id + (len ? String.fromCharCode(96 + len) : "") + num;
131
+ function Loading(props) {
132
+ if (!sharedConfig.hydrating) return signals.createLoadBoundary(() => props.children, () => props.fallback);
133
+ return signals.createMemo(() => {
134
+ const o = signals.getOwner();
135
+ const id = o.id;
136
+ if (sharedConfig.hydrating && sharedConfig.has(id)) {
137
+ let ref = sharedConfig.load(id);
138
+ let p;
139
+ if (ref) {
140
+ if (typeof ref !== "object" || ref.s !== "success") p = ref;else sharedConfig.gather(id);
141
+ }
142
+ if (p) {
143
+ const [s, set] = signals.createSignal(undefined, {
144
+ equals: false
145
+ });
146
+ s();
147
+ if (p !== "$$f") {
148
+ p.then(() => {
149
+ sharedConfig.gather(id);
150
+ sharedConfig.hydrating = true;
151
+ set();
152
+ signals.flush();
153
+ sharedConfig.hydrating = false;
154
+ }, err => signals.runWithOwner(o, () => {
155
+ throw err;
156
+ }));
157
+ } else queueMicrotask(set);
158
+ return props.fallback;
159
+ }
160
+ }
161
+ return signals.createLoadBoundary(() => props.children, () => props.fallback);
162
+ });
115
163
  }
116
- function setHydrateContext(context) {
117
- sharedConfig.context = context;
164
+ function createAsync(compute, value, options) {
165
+ if (!sharedConfig.hydrating) return signals.createAsync(compute, value, options);
166
+ return signals.createAsync(prev => {
167
+ if (!sharedConfig.hydrating) return compute(prev);
168
+ const o = signals.getOwner();
169
+ let initP;
170
+ if (sharedConfig.has(o.id)) initP = sharedConfig.load(o.id);
171
+ const init = initP?.value || initP;
172
+ return init ? (subFetch(compute, prev), init) : compute(prev);
173
+ }, value, options);
118
174
  }
119
- function nextHydrateContext() {
120
- return {
121
- ...sharedConfig.context,
122
- id: sharedConfig.getNextContextId(),
123
- count: 0
124
- };
175
+ class MockPromise {
176
+ static all() {
177
+ return new MockPromise();
178
+ }
179
+ static allSettled() {
180
+ return new MockPromise();
181
+ }
182
+ static any() {
183
+ return new MockPromise();
184
+ }
185
+ static race() {
186
+ return new MockPromise();
187
+ }
188
+ static reject() {
189
+ return new MockPromise();
190
+ }
191
+ static resolve() {
192
+ return new MockPromise();
193
+ }
194
+ catch() {
195
+ return new MockPromise();
196
+ }
197
+ then() {
198
+ return new MockPromise();
199
+ }
200
+ finally() {
201
+ return new MockPromise();
202
+ }
203
+ }
204
+ function subFetch(fn, prev) {
205
+ const ogFetch = fetch;
206
+ const ogPromise = Promise;
207
+ try {
208
+ window.fetch = () => new MockPromise();
209
+ Promise = MockPromise;
210
+ return fn(prev);
211
+ } finally {
212
+ window.fetch = ogFetch;
213
+ Promise = ogPromise;
214
+ }
125
215
  }
126
216
 
127
- let hydrationEnabled = false;
128
217
  function enableHydration() {
129
- hydrationEnabled = true;
130
218
  }
131
219
  function createComponent(Comp, props) {
132
- if (hydrationEnabled) {
133
- if (sharedConfig.context) {
134
- const c = sharedConfig.context;
135
- setHydrateContext(nextHydrateContext());
136
- const r = devComponent(Comp, props || {}) ;
137
- setHydrateContext(c);
138
- return r;
139
- }
140
- }
141
220
  return devComponent(Comp, props || {});
142
221
  }
143
222
  function lazy(fn) {
144
223
  let comp;
145
224
  let p;
146
225
  const wrap = props => {
147
- const ctx = sharedConfig.context;
148
- if (ctx) {
149
- const [s, set] = signals.createSignal();
150
- sharedConfig.count || (sharedConfig.count = 0);
151
- sharedConfig.count++;
152
- (p || (p = fn())).then(mod => {
153
- !sharedConfig.done && setHydrateContext(ctx);
154
- sharedConfig.count--;
155
- set(() => mod.default);
156
- setHydrateContext();
157
- });
158
- comp = s;
159
- } else if (!comp) {
226
+ if (!comp) {
160
227
  const s = signals.createAsync(() => (p || (p = fn())).then(mod => mod.default));
161
228
  comp = s;
162
229
  }
@@ -165,12 +232,7 @@ function lazy(fn) {
165
232
  Object.assign(Comp, {
166
233
  [$DEVCOMP]: true
167
234
  });
168
- if (!ctx || sharedConfig.done) return Comp(props);
169
- const c = sharedConfig.context;
170
- setHydrateContext(ctx);
171
- const r = Comp(props);
172
- setHydrateContext(c);
173
- return r;
235
+ return Comp(props);
174
236
  }) : "");
175
237
  };
176
238
  wrap.preload = () => p || ((p = fn()).then(mod => comp = () => mod.default), p);
@@ -178,8 +240,7 @@ function lazy(fn) {
178
240
  }
179
241
  let counter = 0;
180
242
  function createUniqueId() {
181
- const ctx = sharedConfig.context;
182
- return ctx ? sharedConfig.getNextContextId() : `cl-${counter++}`;
243
+ return sharedConfig.hydrating ? sharedConfig.getNextContextId() : `cl-${counter++}`;
183
244
  }
184
245
 
185
246
  const narrowedError = name => `Attempting to access a stale value from <${name}> that could possibly be undefined. This may occur because you are reading the accessor returned from the component at a time where it has already been unmounted. We recommend cleaning up any stale timers or async, or reading from the initial condition.` ;
@@ -190,22 +251,22 @@ function For(props) {
190
251
  } : {
191
252
  keyed: props.keyed
192
253
  };
193
- return signals.createMemo(signals.mapArray(() => props.each, props.children, options), undefined, {
194
- name: "value"
195
- }) ;
254
+ return signals.mapArray(() => props.each, props.children, options);
196
255
  }
197
256
  function Repeat(props) {
198
257
  const options = "fallback" in props ? {
199
258
  fallback: () => props.fallback
200
259
  } : {};
201
- return signals.createMemo(signals.repeat(() => props.count, index => typeof props.children === "function" ? props.children(index) : props.children, options), undefined, {
202
- name: "value"
203
- }) ;
260
+ options.from = () => props.from;
261
+ return signals.repeat(() => props.count, index => typeof props.children === "function" ? props.children(index) : props.children, options);
204
262
  }
205
263
  function Show(props) {
206
264
  const keyed = props.keyed;
207
- const condition = signals.createMemo(() => props.when, undefined, {
208
- equals: (a, b) => keyed ? a === b : !a === !b,
265
+ const conditionValue = signals.createMemo(() => props.when, undefined, {
266
+ name: "condition value"
267
+ } );
268
+ const condition = keyed ? conditionValue : signals.createMemo(conditionValue, undefined, {
269
+ equals: (a, b) => !a === !b,
209
270
  name: "condition"
210
271
  } );
211
272
  return signals.createMemo(() => {
@@ -215,7 +276,7 @@ function Show(props) {
215
276
  const fn = typeof child === "function" && child.length > 0;
216
277
  return fn ? signals.untrack(() => child(() => {
217
278
  if (!signals.untrack(condition)) throw narrowedError("Show");
218
- return props.when;
279
+ return conditionValue();
219
280
  })) : child;
220
281
  }
221
282
  return props.fallback;
@@ -224,51 +285,55 @@ function Show(props) {
224
285
  } );
225
286
  }
226
287
  function Switch(props) {
227
- let keyed = false;
228
- const equals = (a, b) => (keyed ? a[1] === b[1] : !a[1] === !b[1]) && a[2] === b[2];
229
- const conditions = children(() => props.children),
230
- evalConditions = signals.createMemo(() => {
231
- let conds = conditions();
232
- if (!Array.isArray(conds)) conds = [conds];
233
- for (let i = 0; i < conds.length; i++) {
234
- const c = conds[i].when;
235
- if (c) {
236
- keyed = !!conds[i].keyed;
237
- return [i, c, conds[i]];
238
- }
239
- }
240
- return [-1];
241
- }, undefined, {
242
- equals,
243
- name: "eval conditions"
244
- } );
288
+ const chs = children(() => props.children);
289
+ const switchFunc = signals.createMemo(() => {
290
+ const mps = chs.toArray();
291
+ let func = () => undefined;
292
+ for (let i = 0; i < mps.length; i++) {
293
+ const index = i;
294
+ const mp = mps[i];
295
+ const prevFunc = func;
296
+ const conditionValue = signals.createMemo(() => prevFunc() ? undefined : mp.when, undefined, {
297
+ name: "condition value"
298
+ } );
299
+ const condition = mp.keyed ? conditionValue : signals.createMemo(conditionValue, undefined, {
300
+ equals: (a, b) => !a === !b,
301
+ name: "condition"
302
+ } );
303
+ func = () => prevFunc() || (condition() ? [index, conditionValue, mp] : undefined);
304
+ }
305
+ return func;
306
+ });
245
307
  return signals.createMemo(() => {
246
- const [index, when, cond] = evalConditions();
247
- if (index < 0) return props.fallback;
248
- const c = cond.children;
249
- const fn = typeof c === "function" && c.length > 0;
250
- return fn ? signals.untrack(() => c(() => {
251
- if (signals.untrack(evalConditions)[0] !== index) throw narrowedError("Match");
252
- return cond.when;
253
- })) : c;
308
+ const sel = switchFunc()();
309
+ if (!sel) return props.fallback;
310
+ const [index, conditionValue, mp] = sel;
311
+ const child = mp.children;
312
+ const fn = typeof child === "function" && child.length > 0;
313
+ return fn ? signals.untrack(() => child(() => {
314
+ if (signals.untrack(switchFunc)()?.[0] !== index) throw narrowedError("Match");
315
+ return conditionValue();
316
+ })) : child;
254
317
  }, undefined, {
255
- name: "value"
318
+ name: "eval conditions"
256
319
  } );
257
320
  }
258
321
  function Match(props) {
259
322
  return props;
260
323
  }
261
- function ErrorBoundary(props) {
324
+ function Errored(props) {
262
325
  return signals.createErrorBoundary(() => props.children, (err, reset) => {
263
326
  const f = props.fallback;
264
327
  if ((typeof f !== "function" || f.length == 0)) console.error(err);
265
328
  return typeof f === "function" && f.length ? f(err, reset) : f;
266
329
  });
267
330
  }
268
- function Suspense(props) {
269
- return signals.createSuspense(() => props.children, () => props.fallback);
331
+ function Boundary(props) {
332
+ return signals.createBoundary(() => props.children, () => props.mode);
270
333
  }
271
334
 
335
+ function ssrHandleError() {}
336
+ function ssrRunInScope() {}
272
337
  const DevHooks = {};
273
338
  const DEV = {
274
339
  hooks: DevHooks,
@@ -282,22 +347,10 @@ Object.defineProperty(exports, "$PROXY", {
282
347
  enumerable: true,
283
348
  get: function () { return signals.$PROXY; }
284
349
  });
285
- Object.defineProperty(exports, "$RAW", {
286
- enumerable: true,
287
- get: function () { return signals.$RAW; }
288
- });
289
350
  Object.defineProperty(exports, "$TRACK", {
290
351
  enumerable: true,
291
352
  get: function () { return signals.$TRACK; }
292
353
  });
293
- Object.defineProperty(exports, "catchError", {
294
- enumerable: true,
295
- get: function () { return signals.catchError; }
296
- });
297
- Object.defineProperty(exports, "createAsync", {
298
- enumerable: true,
299
- get: function () { return signals.createAsync; }
300
- });
301
354
  Object.defineProperty(exports, "createEffect", {
302
355
  enumerable: true,
303
356
  get: function () { return signals.createEffect; }
@@ -306,6 +359,14 @@ Object.defineProperty(exports, "createMemo", {
306
359
  enumerable: true,
307
360
  get: function () { return signals.createMemo; }
308
361
  });
362
+ Object.defineProperty(exports, "createOptimistic", {
363
+ enumerable: true,
364
+ get: function () { return signals.createOptimistic; }
365
+ });
366
+ Object.defineProperty(exports, "createOptimisticStore", {
367
+ enumerable: true,
368
+ get: function () { return signals.createOptimisticStore; }
369
+ });
309
370
  Object.defineProperty(exports, "createProjection", {
310
371
  enumerable: true,
311
372
  get: function () { return signals.createProjection; }
@@ -330,13 +391,21 @@ Object.defineProperty(exports, "createStore", {
330
391
  enumerable: true,
331
392
  get: function () { return signals.createStore; }
332
393
  });
394
+ Object.defineProperty(exports, "createTrackedEffect", {
395
+ enumerable: true,
396
+ get: function () { return signals.createTrackedEffect; }
397
+ });
398
+ Object.defineProperty(exports, "deep", {
399
+ enumerable: true,
400
+ get: function () { return signals.deep; }
401
+ });
333
402
  Object.defineProperty(exports, "flatten", {
334
403
  enumerable: true,
335
404
  get: function () { return signals.flatten; }
336
405
  });
337
- Object.defineProperty(exports, "flushSync", {
406
+ Object.defineProperty(exports, "flush", {
338
407
  enumerable: true,
339
- get: function () { return signals.flushSync; }
408
+ get: function () { return signals.flush; }
340
409
  });
341
410
  Object.defineProperty(exports, "getObserver", {
342
411
  enumerable: true,
@@ -350,18 +419,14 @@ Object.defineProperty(exports, "isEqual", {
350
419
  enumerable: true,
351
420
  get: function () { return signals.isEqual; }
352
421
  });
353
- Object.defineProperty(exports, "isStale", {
422
+ Object.defineProperty(exports, "isPending", {
354
423
  enumerable: true,
355
- get: function () { return signals.isStale; }
424
+ get: function () { return signals.isPending; }
356
425
  });
357
426
  Object.defineProperty(exports, "isWrappable", {
358
427
  enumerable: true,
359
428
  get: function () { return signals.isWrappable; }
360
429
  });
361
- Object.defineProperty(exports, "latest", {
362
- enumerable: true,
363
- get: function () { return signals.latest; }
364
- });
365
430
  Object.defineProperty(exports, "mapArray", {
366
431
  enumerable: true,
367
432
  get: function () { return signals.mapArray; }
@@ -378,6 +443,14 @@ Object.defineProperty(exports, "onCleanup", {
378
443
  enumerable: true,
379
444
  get: function () { return signals.onCleanup; }
380
445
  });
446
+ Object.defineProperty(exports, "onSettled", {
447
+ enumerable: true,
448
+ get: function () { return signals.onSettled; }
449
+ });
450
+ Object.defineProperty(exports, "pending", {
451
+ enumerable: true,
452
+ get: function () { return signals.pending; }
453
+ });
381
454
  Object.defineProperty(exports, "reconcile", {
382
455
  enumerable: true,
383
456
  get: function () { return signals.reconcile; }
@@ -394,24 +467,26 @@ Object.defineProperty(exports, "runWithOwner", {
394
467
  enumerable: true,
395
468
  get: function () { return signals.runWithOwner; }
396
469
  });
397
- Object.defineProperty(exports, "untrack", {
470
+ Object.defineProperty(exports, "snapshot", {
398
471
  enumerable: true,
399
- get: function () { return signals.untrack; }
472
+ get: function () { return signals.snapshot; }
400
473
  });
401
- Object.defineProperty(exports, "unwrap", {
474
+ Object.defineProperty(exports, "untrack", {
402
475
  enumerable: true,
403
- get: function () { return signals.unwrap; }
476
+ get: function () { return signals.untrack; }
404
477
  });
405
478
  exports.$DEVCOMP = $DEVCOMP;
479
+ exports.Boundary = Boundary;
406
480
  exports.DEV = DEV;
407
- exports.ErrorBoundary = ErrorBoundary;
481
+ exports.Errored = Errored;
408
482
  exports.For = For;
483
+ exports.Loading = Loading;
409
484
  exports.Match = Match;
410
485
  exports.Repeat = Repeat;
411
486
  exports.Show = Show;
412
- exports.Suspense = Suspense;
413
487
  exports.Switch = Switch;
414
488
  exports.children = children;
489
+ exports.createAsync = createAsync;
415
490
  exports.createComponent = createComponent;
416
491
  exports.createContext = createContext;
417
492
  exports.createUniqueId = createUniqueId;
@@ -420,5 +495,9 @@ exports.from = from;
420
495
  exports.lazy = lazy;
421
496
  exports.observable = observable;
422
497
  exports.onMount = onMount;
498
+ exports.reducer = reducer;
423
499
  exports.sharedConfig = sharedConfig;
500
+ exports.ssrHandleError = ssrHandleError;
501
+ exports.ssrRunInScope = ssrRunInScope;
502
+ exports.tryCatch = tryCatch;
424
503
  exports.useContext = useContext;