|
1 | 1 | /** |
2 | 2 | * This is mock of the eventual Solid 2.0 primitive. It is not fully featured. |
3 | 3 | */ |
4 | | -import { type Accessor, createResource, sharedConfig } from "solid-js"; |
| 4 | +import { type Accessor, createResource, sharedConfig, type Setter, untrack } from "solid-js"; |
| 5 | +import { createStore, reconcile, type ReconcileOptions, unwrap } from "solid-js/store"; |
5 | 6 | import { isServer } from "solid-js/web"; |
6 | | -export function createAsync<T>(fn: () => Promise<T>, options: { |
7 | | - name?: string; |
8 | | - initialValue: T; |
9 | | - deferStream?: boolean; |
10 | | -}): Accessor<T> |
11 | | -export function createAsync<T>(fn: () => Promise<T>, options?: { |
12 | | - name?: string; |
13 | | - initialValue?: T; |
14 | | - deferStream?: boolean; |
15 | | -}): Accessor<T | undefined> |
16 | | -export function createAsync<T>(fn: () => Promise<T>, options?: { |
17 | | - name?: string; |
18 | | - initialValue?: T; |
19 | | - deferStream?: boolean; |
20 | | -}): Accessor<T | undefined> { |
21 | | - const [resource] = createResource( |
22 | | - () => subFetch(fn), |
| 7 | + |
| 8 | +export function createAsync<T>( |
| 9 | + fn: (prev: T) => Promise<T>, |
| 10 | + options: { |
| 11 | + name?: string; |
| 12 | + initialValue: T; |
| 13 | + deferStream?: boolean; |
| 14 | + } |
| 15 | +): Accessor<T>; |
| 16 | +export function createAsync<T>( |
| 17 | + fn: (prev: T | undefined) => Promise<T>, |
| 18 | + options?: { |
| 19 | + name?: string; |
| 20 | + initialValue?: T; |
| 21 | + deferStream?: boolean; |
| 22 | + } |
| 23 | +): Accessor<T | undefined>; |
| 24 | +export function createAsync<T>( |
| 25 | + fn: (prev: T | undefined) => Promise<T>, |
| 26 | + options?: { |
| 27 | + name?: string; |
| 28 | + initialValue?: T; |
| 29 | + deferStream?: boolean; |
| 30 | + } |
| 31 | +): Accessor<T | undefined> { |
| 32 | + let resource: () => T; |
| 33 | + let prev = () => !resource || (resource as any).state === "unresolved" ? undefined : (resource as any).latest; |
| 34 | + [resource] = createResource( |
| 35 | + () => subFetch(fn, untrack(prev)), |
23 | 36 | v => v, |
24 | | - options |
| 37 | + options as any |
25 | 38 | ); |
26 | 39 | return () => resource(); |
27 | 40 | } |
28 | 41 |
|
| 42 | +export function createAsyncStore<T>( |
| 43 | + fn: (prev: T) => Promise<T>, |
| 44 | + options: { |
| 45 | + name?: string; |
| 46 | + initialValue: T; |
| 47 | + deferStream?: boolean; |
| 48 | + reconcile?: ReconcileOptions; |
| 49 | + } |
| 50 | +): Accessor<T>; |
| 51 | +export function createAsyncStore<T>( |
| 52 | + fn: (prev: T | undefined) => Promise<T>, |
| 53 | + options?: { |
| 54 | + name?: string; |
| 55 | + initialValue?: T; |
| 56 | + deferStream?: boolean; |
| 57 | + reconcile?: ReconcileOptions; |
| 58 | + } |
| 59 | +): Accessor<T | undefined>; |
| 60 | +export function createAsyncStore<T>( |
| 61 | + fn: (prev: T | undefined) => Promise<T>, |
| 62 | + options: { |
| 63 | + name?: string; |
| 64 | + initialValue?: T; |
| 65 | + deferStream?: boolean; |
| 66 | + reconcile?: ReconcileOptions; |
| 67 | + } = {} |
| 68 | +): Accessor<T | undefined> { |
| 69 | + let resource: () => T; |
| 70 | + let prev = () => !resource || (resource as any).state === "unresolved" ? undefined : unwrap((resource as any).latest); |
| 71 | + [resource] = createResource( |
| 72 | + () => subFetch(fn, untrack(prev)), |
| 73 | + v => v, |
| 74 | + { |
| 75 | + ...options, |
| 76 | + storage: (init: T | undefined) => createDeepSignal(init, options.reconcile) |
| 77 | + } as any |
| 78 | + ); |
| 79 | + return () => resource(); |
| 80 | +} |
| 81 | + |
| 82 | +function createDeepSignal<T>(value: T | undefined, options?: ReconcileOptions) { |
| 83 | + const [store, setStore] = createStore({ |
| 84 | + value |
| 85 | + }); |
| 86 | + return [ |
| 87 | + () => store.value, |
| 88 | + (v: T) => { |
| 89 | + typeof v === "function" && (v = v()); |
| 90 | + setStore("value", reconcile(v, options)); |
| 91 | + return store.value; |
| 92 | + } |
| 93 | + ] as [Accessor<T | null>, Setter<T | null>]; |
| 94 | +} |
| 95 | + |
29 | 96 | // mock promise while hydrating to prevent fetching |
30 | 97 | class MockPromise { |
31 | 98 | static all() { |
@@ -57,14 +124,14 @@ class MockPromise { |
57 | 124 | } |
58 | 125 | } |
59 | 126 |
|
60 | | -function subFetch<T>(fn: () => Promise<T>) { |
61 | | - if (isServer || !sharedConfig.context) return fn(); |
| 127 | +function subFetch<T>(fn: (prev: T | undefined) => Promise<T>, prev: T | undefined) { |
| 128 | + if (isServer || !sharedConfig.context) return fn(prev); |
62 | 129 | const ogFetch = fetch; |
63 | 130 | const ogPromise = Promise; |
64 | 131 | try { |
65 | 132 | window.fetch = () => new MockPromise() as any; |
66 | 133 | Promise = MockPromise as any; |
67 | | - return fn(); |
| 134 | + return fn(prev); |
68 | 135 | } finally { |
69 | 136 | window.fetch = ogFetch; |
70 | 137 | Promise = ogPromise; |
|
0 commit comments