solid-js 2.0.0-experimental.0 → 2.0.0-experimental.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/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 o.getNextChildId();
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 Suspense(props) {
132
+ if (!sharedConfig.hydrating) return signals.createSuspense(() => 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.createSuspense(() => 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.` ;
@@ -198,14 +259,18 @@ function Repeat(props) {
198
259
  const options = "fallback" in props ? {
199
260
  fallback: () => props.fallback
200
261
  } : {};
262
+ options.from = () => props.from;
201
263
  return signals.createMemo(signals.repeat(() => props.count, index => typeof props.children === "function" ? props.children(index) : props.children, options), undefined, {
202
264
  name: "value"
203
265
  }) ;
204
266
  }
205
267
  function Show(props) {
206
268
  const keyed = props.keyed;
207
- const condition = signals.createMemo(() => props.when, undefined, {
208
- equals: (a, b) => keyed ? a === b : !a === !b,
269
+ const conditionValue = signals.createMemo(() => props.when, undefined, {
270
+ name: "condition value"
271
+ } );
272
+ const condition = keyed ? conditionValue : signals.createMemo(conditionValue, undefined, {
273
+ equals: (a, b) => !a === !b,
209
274
  name: "condition"
210
275
  } );
211
276
  return signals.createMemo(() => {
@@ -215,7 +280,7 @@ function Show(props) {
215
280
  const fn = typeof child === "function" && child.length > 0;
216
281
  return fn ? signals.untrack(() => child(() => {
217
282
  if (!signals.untrack(condition)) throw narrowedError("Show");
218
- return props.when;
283
+ return conditionValue();
219
284
  })) : child;
220
285
  }
221
286
  return props.fallback;
@@ -224,35 +289,38 @@ function Show(props) {
224
289
  } );
225
290
  }
226
291
  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
- } );
292
+ const chs = children(() => props.children);
293
+ const switchFunc = signals.createMemo(() => {
294
+ const ch = chs();
295
+ const mps = Array.isArray(ch) ? ch : [ch];
296
+ let func = () => undefined;
297
+ for (let i = 0; i < mps.length; i++) {
298
+ const index = i;
299
+ const mp = mps[i];
300
+ const prevFunc = func;
301
+ const conditionValue = signals.createMemo(() => prevFunc() ? undefined : mp.when, undefined, {
302
+ name: "condition value"
303
+ } );
304
+ const condition = mp.keyed ? conditionValue : signals.createMemo(conditionValue, undefined, {
305
+ equals: (a, b) => !a === !b,
306
+ name: "condition"
307
+ } );
308
+ func = () => prevFunc() || (condition() ? [index, conditionValue, mp] : undefined);
309
+ }
310
+ return func;
311
+ });
245
312
  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;
313
+ const sel = switchFunc()();
314
+ if (!sel) return props.fallback;
315
+ const [index, conditionValue, mp] = sel;
316
+ const child = mp.children;
317
+ const fn = typeof child === "function" && child.length > 0;
318
+ return fn ? signals.untrack(() => child(() => {
319
+ if (signals.untrack(switchFunc)()?.[0] !== index) throw narrowedError("Match");
320
+ return conditionValue();
321
+ })) : child;
254
322
  }, undefined, {
255
- name: "value"
323
+ name: "eval conditions"
256
324
  } );
257
325
  }
258
326
  function Match(props) {
@@ -265,10 +333,12 @@ function ErrorBoundary(props) {
265
333
  return typeof f === "function" && f.length ? f(err, reset) : f;
266
334
  });
267
335
  }
268
- function Suspense(props) {
269
- return signals.createSuspense(() => props.children, () => props.fallback);
336
+ function Boundary(props) {
337
+ return signals.createBoundary(() => props.children, () => props.mode);
270
338
  }
271
339
 
340
+ function ssrHandleError() {}
341
+ function ssrRunInScope() {}
272
342
  const DevHooks = {};
273
343
  const DEV = {
274
344
  hooks: DevHooks,
@@ -282,22 +352,10 @@ Object.defineProperty(exports, "$PROXY", {
282
352
  enumerable: true,
283
353
  get: function () { return signals.$PROXY; }
284
354
  });
285
- Object.defineProperty(exports, "$RAW", {
286
- enumerable: true,
287
- get: function () { return signals.$RAW; }
288
- });
289
355
  Object.defineProperty(exports, "$TRACK", {
290
356
  enumerable: true,
291
357
  get: function () { return signals.$TRACK; }
292
358
  });
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
359
  Object.defineProperty(exports, "createEffect", {
302
360
  enumerable: true,
303
361
  get: function () { return signals.createEffect; }
@@ -306,6 +364,14 @@ Object.defineProperty(exports, "createMemo", {
306
364
  enumerable: true,
307
365
  get: function () { return signals.createMemo; }
308
366
  });
367
+ Object.defineProperty(exports, "createOptimistic", {
368
+ enumerable: true,
369
+ get: function () { return signals.createOptimistic; }
370
+ });
371
+ Object.defineProperty(exports, "createOptimisticStore", {
372
+ enumerable: true,
373
+ get: function () { return signals.createOptimisticStore; }
374
+ });
309
375
  Object.defineProperty(exports, "createProjection", {
310
376
  enumerable: true,
311
377
  get: function () { return signals.createProjection; }
@@ -330,13 +396,21 @@ Object.defineProperty(exports, "createStore", {
330
396
  enumerable: true,
331
397
  get: function () { return signals.createStore; }
332
398
  });
399
+ Object.defineProperty(exports, "createTrackedEffect", {
400
+ enumerable: true,
401
+ get: function () { return signals.createTrackedEffect; }
402
+ });
403
+ Object.defineProperty(exports, "deep", {
404
+ enumerable: true,
405
+ get: function () { return signals.deep; }
406
+ });
333
407
  Object.defineProperty(exports, "flatten", {
334
408
  enumerable: true,
335
409
  get: function () { return signals.flatten; }
336
410
  });
337
- Object.defineProperty(exports, "flushSync", {
411
+ Object.defineProperty(exports, "flush", {
338
412
  enumerable: true,
339
- get: function () { return signals.flushSync; }
413
+ get: function () { return signals.flush; }
340
414
  });
341
415
  Object.defineProperty(exports, "getObserver", {
342
416
  enumerable: true,
@@ -350,9 +424,9 @@ Object.defineProperty(exports, "isEqual", {
350
424
  enumerable: true,
351
425
  get: function () { return signals.isEqual; }
352
426
  });
353
- Object.defineProperty(exports, "isStale", {
427
+ Object.defineProperty(exports, "isPending", {
354
428
  enumerable: true,
355
- get: function () { return signals.isStale; }
429
+ get: function () { return signals.isPending; }
356
430
  });
357
431
  Object.defineProperty(exports, "isWrappable", {
358
432
  enumerable: true,
@@ -394,15 +468,24 @@ Object.defineProperty(exports, "runWithOwner", {
394
468
  enumerable: true,
395
469
  get: function () { return signals.runWithOwner; }
396
470
  });
471
+ Object.defineProperty(exports, "snapshot", {
472
+ enumerable: true,
473
+ get: function () { return signals.snapshot; }
474
+ });
475
+ Object.defineProperty(exports, "transition", {
476
+ enumerable: true,
477
+ get: function () { return signals.transition; }
478
+ });
397
479
  Object.defineProperty(exports, "untrack", {
398
480
  enumerable: true,
399
481
  get: function () { return signals.untrack; }
400
482
  });
401
- Object.defineProperty(exports, "unwrap", {
483
+ Object.defineProperty(exports, "useTransition", {
402
484
  enumerable: true,
403
- get: function () { return signals.unwrap; }
485
+ get: function () { return signals.useTransition; }
404
486
  });
405
487
  exports.$DEVCOMP = $DEVCOMP;
488
+ exports.Boundary = Boundary;
406
489
  exports.DEV = DEV;
407
490
  exports.ErrorBoundary = ErrorBoundary;
408
491
  exports.For = For;
@@ -412,6 +495,7 @@ exports.Show = Show;
412
495
  exports.Suspense = Suspense;
413
496
  exports.Switch = Switch;
414
497
  exports.children = children;
498
+ exports.createAsync = createAsync;
415
499
  exports.createComponent = createComponent;
416
500
  exports.createContext = createContext;
417
501
  exports.createUniqueId = createUniqueId;
@@ -420,5 +504,9 @@ exports.from = from;
420
504
  exports.lazy = lazy;
421
505
  exports.observable = observable;
422
506
  exports.onMount = onMount;
507
+ exports.reducer = reducer;
423
508
  exports.sharedConfig = sharedConfig;
509
+ exports.ssrHandleError = ssrHandleError;
510
+ exports.ssrRunInScope = ssrRunInScope;
511
+ exports.tryCatch = tryCatch;
424
512
  exports.useContext = useContext;