solid-js 2.0.0-experimental.4 → 2.0.0-experimental.6

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,66 +1,15 @@
1
- import {
2
- createEffect,
3
- getContext,
4
- createMemo,
5
- flatten,
6
- setContext,
7
- untrack,
8
- createRoot,
9
- getOwner,
10
- onCleanup,
11
- createSignal,
12
- createAsync,
13
- mapArray,
14
- repeat,
15
- createErrorBoundary,
16
- createSuspense
17
- } from "@solidjs/signals";
18
- export {
19
- $PROXY,
20
- $RAW,
21
- $TRACK,
22
- catchError,
23
- createAsync,
24
- createEffect,
25
- createMemo,
26
- createProjection,
27
- createRenderEffect,
28
- createRoot,
29
- createSignal,
30
- createStore,
31
- flatten,
32
- flushSync,
33
- getObserver,
34
- getOwner,
35
- isEqual,
36
- isPending,
37
- isWrappable,
38
- latest,
39
- mapArray,
40
- merge,
41
- omit,
42
- onCleanup,
43
- reconcile,
44
- repeat,
45
- resolve,
46
- runWithObserver,
47
- runWithOwner,
48
- untrack,
49
- unwrap
50
- } from "@solidjs/signals";
1
+ import { createEffect, getContext, createMemo, flatten, createRoot, setContext, getOwner, untrack, onCleanup, createSignal, createSuspense, flush, runWithObserver, createAsync as createAsync$1, mapArray, repeat, createErrorBoundary, createBoundary } from '@solidjs/signals';
2
+ export { $PROXY, $TRACK, createEffect, createMemo, createProjection, createRenderEffect, createRoot, createSignal, createStore, deep, flatten, flush, getObserver, getOwner, isEqual, isPending, isWrappable, latest, mapArray, merge, omit, onCleanup, reconcile, repeat, resolve, runWithObserver, runWithOwner, snapshot, tryCatch, untrack } from '@solidjs/signals';
51
3
 
52
- const $DEVCOMP = Symbol("COMPONENT_DEV");
4
+ const $DEVCOMP = Symbol("COMPONENT_DEV" );
53
5
  function onMount(fn) {
54
6
  createEffect(() => null, fn);
55
7
  }
56
8
  function createContext(defaultValue, options) {
57
- const id = Symbol((options && options.name) || "");
9
+ const id = Symbol(options && options.name || "");
58
10
  function provider(props) {
59
- return createMemo(() => {
60
- setContext(
61
- provider,
62
- untrack(() => props.value)
63
- );
11
+ return createRoot(() => {
12
+ setContext(provider, props.value);
64
13
  return children(() => props.children);
65
14
  });
66
15
  }
@@ -75,7 +24,7 @@ function children(fn) {
75
24
  const children = createMemo(fn);
76
25
  const memo = createMemo(() => flatten(children()), undefined, {
77
26
  name: "children"
78
- });
27
+ }) ;
79
28
  memo.toArray = () => {
80
29
  const c = memo();
81
30
  return Array.isArray(c) ? c : c != null ? [c] : [];
@@ -99,8 +48,7 @@ function devComponent(Comp, props) {
99
48
  function registerGraph(value) {
100
49
  const owner = getOwner();
101
50
  if (!owner) return;
102
- if (owner.sourceMap) owner.sourceMap.push(value);
103
- else owner.sourceMap = [value];
51
+ if (owner.sourceMap) owner.sourceMap.push(value);else owner.sourceMap = [value];
104
52
  value.graph = owner;
105
53
  }
106
54
 
@@ -110,20 +58,16 @@ function observable(input) {
110
58
  if (!(observer instanceof Object) || observer == null) {
111
59
  throw new TypeError("Expected the observer to be an object.");
112
60
  }
113
- const handler =
114
- typeof observer === "function" ? observer : observer.next && observer.next.bind(observer);
61
+ const handler = typeof observer === "function" ? observer : observer.next && observer.next.bind(observer);
115
62
  if (!handler) {
116
63
  return {
117
64
  unsubscribe() {}
118
65
  };
119
66
  }
120
67
  const dispose = createRoot(disposer => {
121
- createEffect(
122
- () => input(),
123
- v => {
124
- handler(v);
125
- }
126
- );
68
+ createEffect(() => input(), v => {
69
+ handler(v);
70
+ });
127
71
  return disposer;
128
72
  });
129
73
  if (getOwner()) onCleanup(dispose);
@@ -144,7 +88,7 @@ function from(producer, initialValue = undefined) {
144
88
  });
145
89
  if ("subscribe" in producer) {
146
90
  const unsub = producer.subscribe(v => set(() => v));
147
- onCleanup(() => ("unsubscribe" in unsub ? unsub.unsubscribe() : unsub()));
91
+ onCleanup(() => "unsubscribe" in unsub ? unsub.unsubscribe() : unsub());
148
92
  } else {
149
93
  const clean = producer(set);
150
94
  onCleanup(clean);
@@ -153,167 +97,174 @@ function from(producer, initialValue = undefined) {
153
97
  }
154
98
 
155
99
  const sharedConfig = {
156
- context: undefined,
100
+ hydrating: false,
157
101
  registry: undefined,
158
102
  done: false,
159
- getContextId() {
160
- return getContextId(this.context.count);
161
- },
162
103
  getNextContextId() {
163
- return getContextId(this.context.count++);
104
+ const o = getOwner();
105
+ if (!o) throw new Error(`getNextContextId cannot be used under non-hydrating context`);
106
+ return o.getNextChildId();
164
107
  }
165
108
  };
166
- function getContextId(count) {
167
- const num = String(count),
168
- len = num.length - 1;
169
- return sharedConfig.context.id + (len ? String.fromCharCode(96 + len) : "") + num;
109
+ function Suspense(props) {
110
+ if (!sharedConfig.hydrating) return createSuspense(() => props.children, () => props.fallback);
111
+ return createMemo(() => {
112
+ const o = getOwner();
113
+ const id = o.id;
114
+ if (sharedConfig.hydrating && sharedConfig.has(id)) {
115
+ let ref = sharedConfig.load(id);
116
+ let p;
117
+ if (ref) {
118
+ if (typeof ref !== "object" || ref.s !== "success") p = ref;else sharedConfig.gather(id);
119
+ }
120
+ if (p) {
121
+ const [s, set] = createSignal(undefined, {
122
+ equals: false
123
+ });
124
+ s();
125
+ if (p !== "$$f") {
126
+ p.then(() => {
127
+ sharedConfig.gather(id);
128
+ sharedConfig.hydrating = true;
129
+ set();
130
+ flush();
131
+ sharedConfig.hydrating = false;
132
+ }, err => runWithObserver(o, () => {
133
+ throw err;
134
+ }));
135
+ } else queueMicrotask(set);
136
+ return props.fallback;
137
+ }
138
+ }
139
+ return createSuspense(() => props.children, () => props.fallback);
140
+ });
170
141
  }
171
- function setHydrateContext(context) {
172
- sharedConfig.context = context;
142
+ function createAsync(compute, value, options) {
143
+ if (!sharedConfig.hydrating) return createAsync$1(compute, value, options);
144
+ return createAsync$1(prev => {
145
+ if (!sharedConfig.hydrating) return compute(prev);
146
+ const o = getOwner();
147
+ let initP;
148
+ if (sharedConfig.has(o.id)) initP = sharedConfig.load(o.id);
149
+ const init = initP?.value || initP;
150
+ return init ? (subFetch(compute, prev), init) : compute(prev);
151
+ }, value, options);
173
152
  }
174
- function nextHydrateContext() {
175
- return {
176
- ...sharedConfig.context,
177
- id: sharedConfig.getNextContextId(),
178
- count: 0
179
- };
153
+ class MockPromise {
154
+ static all() {
155
+ return new MockPromise();
156
+ }
157
+ static allSettled() {
158
+ return new MockPromise();
159
+ }
160
+ static any() {
161
+ return new MockPromise();
162
+ }
163
+ static race() {
164
+ return new MockPromise();
165
+ }
166
+ static reject() {
167
+ return new MockPromise();
168
+ }
169
+ static resolve() {
170
+ return new MockPromise();
171
+ }
172
+ catch() {
173
+ return new MockPromise();
174
+ }
175
+ then() {
176
+ return new MockPromise();
177
+ }
178
+ finally() {
179
+ return new MockPromise();
180
+ }
181
+ }
182
+ function subFetch(fn, prev) {
183
+ const ogFetch = fetch;
184
+ const ogPromise = Promise;
185
+ try {
186
+ window.fetch = () => new MockPromise();
187
+ Promise = MockPromise;
188
+ return fn(prev);
189
+ } finally {
190
+ window.fetch = ogFetch;
191
+ Promise = ogPromise;
192
+ }
180
193
  }
181
194
 
182
- let hydrationEnabled = false;
183
195
  function enableHydration() {
184
- hydrationEnabled = true;
185
196
  }
186
197
  function createComponent(Comp, props) {
187
- if (hydrationEnabled) {
188
- if (sharedConfig.context) {
189
- const c = sharedConfig.context;
190
- setHydrateContext(nextHydrateContext());
191
- const r = devComponent(Comp, props || {});
192
- setHydrateContext(c);
193
- return r;
194
- }
195
- }
196
198
  return devComponent(Comp, props || {});
197
199
  }
198
200
  function lazy(fn) {
199
201
  let comp;
200
202
  let p;
201
203
  const wrap = props => {
202
- const ctx = sharedConfig.context;
203
- if (ctx) {
204
- const [s, set] = createSignal();
205
- sharedConfig.count || (sharedConfig.count = 0);
206
- sharedConfig.count++;
207
- (p || (p = fn())).then(mod => {
208
- !sharedConfig.done && setHydrateContext(ctx);
209
- sharedConfig.count--;
210
- set(() => mod.default);
211
- setHydrateContext();
212
- });
213
- comp = s;
214
- } else if (!comp) {
215
- const s = createAsync(() => (p || (p = fn())).then(mod => mod.default));
204
+ if (!comp) {
205
+ const s = createAsync$1(() => (p || (p = fn())).then(mod => mod.default));
216
206
  comp = s;
217
207
  }
218
208
  let Comp;
219
- return createMemo(() =>
220
- (Comp = comp())
221
- ? untrack(() => {
222
- Object.assign(Comp, {
223
- [$DEVCOMP]: true
224
- });
225
- if (!ctx || sharedConfig.done) return Comp(props);
226
- const c = sharedConfig.context;
227
- setHydrateContext(ctx);
228
- const r = Comp(props);
229
- setHydrateContext(c);
230
- return r;
231
- })
232
- : ""
233
- );
209
+ return createMemo(() => (Comp = comp()) ? untrack(() => {
210
+ Object.assign(Comp, {
211
+ [$DEVCOMP]: true
212
+ });
213
+ return Comp(props);
214
+ }) : "");
234
215
  };
235
- wrap.preload = () => p || ((p = fn()).then(mod => (comp = () => mod.default)), p);
216
+ wrap.preload = () => p || ((p = fn()).then(mod => comp = () => mod.default), p);
236
217
  return wrap;
237
218
  }
238
219
  let counter = 0;
239
220
  function createUniqueId() {
240
- const ctx = sharedConfig.context;
241
- return ctx ? sharedConfig.getNextContextId() : `cl-${counter++}`;
221
+ return sharedConfig.hydrating ? sharedConfig.getNextContextId() : `cl-${counter++}`;
242
222
  }
243
223
 
244
- const narrowedError = name =>
245
- `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.`;
224
+ 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.` ;
246
225
  function For(props) {
247
- const options =
248
- "fallback" in props
249
- ? {
250
- keyed: props.keyed,
251
- fallback: () => props.fallback
252
- }
253
- : {
254
- keyed: props.keyed
255
- };
256
- return createMemo(
257
- mapArray(() => props.each, props.children, options),
258
- undefined,
259
- {
260
- name: "value"
261
- }
262
- );
226
+ const options = "fallback" in props ? {
227
+ keyed: props.keyed,
228
+ fallback: () => props.fallback
229
+ } : {
230
+ keyed: props.keyed
231
+ };
232
+ return createMemo(mapArray(() => props.each, props.children, options), undefined, {
233
+ name: "value"
234
+ }) ;
263
235
  }
264
236
  function Repeat(props) {
265
- const options =
266
- "fallback" in props
267
- ? {
268
- fallback: () => props.fallback
269
- }
270
- : {};
237
+ const options = "fallback" in props ? {
238
+ fallback: () => props.fallback
239
+ } : {};
271
240
  options.from = () => props.from;
272
- return createMemo(
273
- repeat(
274
- () => props.count,
275
- index => (typeof props.children === "function" ? props.children(index) : props.children),
276
- options
277
- ),
278
- undefined,
279
- {
280
- name: "value"
281
- }
282
- );
241
+ return createMemo(repeat(() => props.count, index => typeof props.children === "function" ? props.children(index) : props.children, options), undefined, {
242
+ name: "value"
243
+ }) ;
283
244
  }
284
245
  function Show(props) {
285
246
  const keyed = props.keyed;
286
247
  const conditionValue = createMemo(() => props.when, undefined, {
287
248
  name: "condition value"
288
- });
289
- const condition = keyed
290
- ? conditionValue
291
- : createMemo(conditionValue, undefined, {
292
- equals: (a, b) => !a === !b,
293
- name: "condition"
294
- });
295
- return createMemo(
296
- () => {
297
- const c = condition();
298
- if (c) {
299
- const child = props.children;
300
- const fn = typeof child === "function" && child.length > 0;
301
- return fn
302
- ? untrack(() =>
303
- child(() => {
304
- if (!untrack(condition)) throw narrowedError("Show");
305
- return conditionValue();
306
- })
307
- )
308
- : child;
309
- }
310
- return props.fallback;
311
- },
312
- undefined,
313
- {
314
- name: "value"
249
+ } );
250
+ const condition = keyed ? conditionValue : createMemo(conditionValue, undefined, {
251
+ equals: (a, b) => !a === !b,
252
+ name: "condition"
253
+ } );
254
+ return createMemo(() => {
255
+ const c = condition();
256
+ if (c) {
257
+ const child = props.children;
258
+ const fn = typeof child === "function" && child.length > 0;
259
+ return fn ? untrack(() => child(() => {
260
+ if (!untrack(condition)) throw narrowedError("Show");
261
+ return conditionValue();
262
+ })) : child;
315
263
  }
316
- );
264
+ return props.fallback;
265
+ }, undefined, {
266
+ name: "value"
267
+ } );
317
268
  }
318
269
  function Switch(props) {
319
270
  const chs = children(() => props.children);
@@ -325,93 +276,54 @@ function Switch(props) {
325
276
  const index = i;
326
277
  const mp = mps[i];
327
278
  const prevFunc = func;
328
- const conditionValue = createMemo(() => (prevFunc() ? undefined : mp.when), undefined, {
279
+ const conditionValue = createMemo(() => prevFunc() ? undefined : mp.when, undefined, {
329
280
  name: "condition value"
330
- });
331
- const condition = mp.keyed
332
- ? conditionValue
333
- : createMemo(conditionValue, undefined, {
334
- equals: (a, b) => !a === !b,
335
- name: "condition"
336
- });
281
+ } );
282
+ const condition = mp.keyed ? conditionValue : createMemo(conditionValue, undefined, {
283
+ equals: (a, b) => !a === !b,
284
+ name: "condition"
285
+ } );
337
286
  func = () => prevFunc() || (condition() ? [index, conditionValue, mp] : undefined);
338
287
  }
339
288
  return func;
340
289
  });
341
- return createMemo(
342
- () => {
343
- const sel = switchFunc()();
344
- if (!sel) return props.fallback;
345
- const [index, conditionValue, mp] = sel;
346
- const child = mp.children;
347
- const fn = typeof child === "function" && child.length > 0;
348
- return fn
349
- ? untrack(() =>
350
- child(() => {
351
- if (untrack(switchFunc)()?.[0] !== index) throw narrowedError("Match");
352
- return conditionValue();
353
- })
354
- )
355
- : child;
356
- },
357
- undefined,
358
- {
359
- name: "eval conditions"
360
- }
361
- );
290
+ return createMemo(() => {
291
+ const sel = switchFunc()();
292
+ if (!sel) return props.fallback;
293
+ const [index, conditionValue, mp] = sel;
294
+ const child = mp.children;
295
+ const fn = typeof child === "function" && child.length > 0;
296
+ return fn ? untrack(() => child(() => {
297
+ if (untrack(switchFunc)()?.[0] !== index) throw narrowedError("Match");
298
+ return conditionValue();
299
+ })) : child;
300
+ }, undefined, {
301
+ name: "eval conditions"
302
+ } );
362
303
  }
363
304
  function Match(props) {
364
305
  return props;
365
306
  }
366
307
  function ErrorBoundary(props) {
367
- return createErrorBoundary(
368
- () => props.children,
369
- (err, reset) => {
370
- const f = props.fallback;
371
- if (typeof f !== "function" || f.length == 0) console.error(err);
372
- return typeof f === "function" && f.length ? f(err, reset) : f;
373
- }
374
- );
308
+ return createErrorBoundary(() => props.children, (err, reset) => {
309
+ const f = props.fallback;
310
+ if ((typeof f !== "function" || f.length == 0)) console.error(err);
311
+ return typeof f === "function" && f.length ? f(err, reset) : f;
312
+ });
375
313
  }
376
- function Suspense(props) {
377
- return createSuspense(
378
- () => props.children,
379
- () => props.fallback
380
- );
314
+ function Boundary(props) {
315
+ return createBoundary(() => props.children, () => props.mode);
381
316
  }
382
317
 
318
+ function ssrHandleError() {}
319
+ function ssrRunInScope() {}
383
320
  const DevHooks = {};
384
321
  const DEV = {
385
322
  hooks: DevHooks,
386
323
  registerGraph
387
- };
324
+ } ;
388
325
  if (globalThis) {
389
- if (!globalThis.Solid$$) globalThis.Solid$$ = true;
390
- else
391
- console.warn(
392
- "You appear to have multiple instances of Solid. This can lead to unexpected behavior."
393
- );
326
+ if (!globalThis.Solid$$) globalThis.Solid$$ = true;else console.warn("You appear to have multiple instances of Solid. This can lead to unexpected behavior.");
394
327
  }
395
328
 
396
- export {
397
- $DEVCOMP,
398
- DEV,
399
- ErrorBoundary,
400
- For,
401
- Match,
402
- Repeat,
403
- Show,
404
- Suspense,
405
- Switch,
406
- children,
407
- createComponent,
408
- createContext,
409
- createUniqueId,
410
- enableHydration,
411
- from,
412
- lazy,
413
- observable,
414
- onMount,
415
- sharedConfig,
416
- useContext
417
- };
329
+ export { $DEVCOMP, Boundary, DEV, ErrorBoundary, For, Match, Repeat, Show, Suspense, Switch, children, createAsync, createComponent, createContext, createUniqueId, enableHydration, from, lazy, observable, onMount, sharedConfig, ssrHandleError, ssrRunInScope, useContext };