@effect/atom-solid 4.0.0-beta.6 → 4.0.0-beta.62
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/Hooks.d.ts +19 -12
- package/dist/Hooks.d.ts.map +1 -1
- package/dist/Hooks.js +45 -13
- package/dist/Hooks.js.map +1 -1
- package/dist/RegistryContext.js +1 -1
- package/dist/RegistryContext.js.map +1 -1
- package/package.json +5 -5
- package/src/Hooks.ts +68 -29
- package/src/RegistryContext.ts +1 -1
package/dist/Hooks.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as Exit from "effect/Exit";
|
|
2
|
-
import
|
|
2
|
+
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
|
3
3
|
import * as Atom from "effect/unstable/reactivity/Atom";
|
|
4
4
|
import type * as AtomRef from "effect/unstable/reactivity/AtomRef";
|
|
5
|
-
import type { Accessor } from "solid-js";
|
|
5
|
+
import type { Accessor, ResourceOptions, ResourceReturn } from "solid-js";
|
|
6
6
|
/**
|
|
7
7
|
* @since 1.0.0
|
|
8
8
|
* @category hooks
|
|
@@ -17,57 +17,64 @@ export declare const useAtomValue: {
|
|
|
17
17
|
* @since 1.0.0
|
|
18
18
|
* @category hooks
|
|
19
19
|
*/
|
|
20
|
-
<A>(atom: Atom.Atom<A>): Accessor<A>;
|
|
20
|
+
<A>(atom: () => Atom.Atom<A>): Accessor<A>;
|
|
21
21
|
/**
|
|
22
22
|
* @since 1.0.0
|
|
23
23
|
* @category hooks
|
|
24
24
|
*/
|
|
25
|
-
<A, B>(atom: Atom.Atom<A>, f: (_: A) => B): Accessor<B>;
|
|
25
|
+
<A, B>(atom: () => Atom.Atom<A>, f: (_: A) => B): Accessor<B>;
|
|
26
26
|
};
|
|
27
27
|
/**
|
|
28
28
|
* @since 1.0.0
|
|
29
29
|
* @category hooks
|
|
30
30
|
*/
|
|
31
|
-
export declare const useAtomMount: <A>(atom: Atom.Atom<A>) => void;
|
|
31
|
+
export declare const useAtomMount: <A>(atom: () => Atom.Atom<A>) => void;
|
|
32
32
|
/**
|
|
33
33
|
* @since 1.0.0
|
|
34
34
|
* @category hooks
|
|
35
35
|
*/
|
|
36
|
-
export declare const useAtomSet: <R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: Atom.Writable<R, W>, options?: {
|
|
36
|
+
export declare const useAtomSet: <R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: () => Atom.Writable<R, W>, options?: {
|
|
37
37
|
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined;
|
|
38
38
|
}) => "promise" extends Mode ? ((value: W) => Promise<AsyncResult.AsyncResult.Success<R>>) : "promiseExit" extends Mode ? ((value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>) : ((value: W | ((value: R) => W)) => void);
|
|
39
39
|
/**
|
|
40
40
|
* @since 1.0.0
|
|
41
41
|
* @category hooks
|
|
42
42
|
*/
|
|
43
|
-
export declare const useAtomRefresh: <A>(atom: Atom.Atom<A>) => () => void;
|
|
43
|
+
export declare const useAtomRefresh: <A>(atom: () => Atom.Atom<A>) => () => void;
|
|
44
44
|
/**
|
|
45
45
|
* @since 1.0.0
|
|
46
46
|
* @category hooks
|
|
47
47
|
*/
|
|
48
|
-
export declare const useAtom: <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(atom: Atom.Writable<R, W>, options?: {
|
|
48
|
+
export declare const useAtom: <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(atom: () => Atom.Writable<R, W>, options?: {
|
|
49
49
|
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined;
|
|
50
50
|
}) => readonly [value: Accessor<R>, write: "promise" extends Mode ? ((value: W) => Promise<AsyncResult.AsyncResult.Success<R>>) : "promiseExit" extends Mode ? ((value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>) : ((value: W | ((value: R) => W)) => void)];
|
|
51
51
|
/**
|
|
52
52
|
* @since 1.0.0
|
|
53
53
|
* @category hooks
|
|
54
54
|
*/
|
|
55
|
-
export declare const useAtomSubscribe: <A>(atom: Atom.Atom<A>, f: (_: A) => void, options?: {
|
|
55
|
+
export declare const useAtomSubscribe: <A>(atom: () => Atom.Atom<A>, f: (_: A) => void, options?: {
|
|
56
56
|
readonly immediate?: boolean;
|
|
57
57
|
}) => void;
|
|
58
58
|
/**
|
|
59
59
|
* @since 1.0.0
|
|
60
60
|
* @category hooks
|
|
61
61
|
*/
|
|
62
|
-
export declare const
|
|
62
|
+
export declare const useAtomResource: <A, E>(atom: () => Atom.Atom<AsyncResult.AsyncResult<A, E>>, options?: ResourceOptions<A> & {
|
|
63
|
+
readonly suspendOnWaiting?: boolean | undefined;
|
|
64
|
+
}) => ResourceReturn<A, void>;
|
|
63
65
|
/**
|
|
64
66
|
* @since 1.0.0
|
|
65
67
|
* @category hooks
|
|
66
68
|
*/
|
|
67
|
-
export declare const
|
|
69
|
+
export declare const useAtomRef: <A>(ref: () => AtomRef.ReadonlyRef<A>) => Accessor<A>;
|
|
68
70
|
/**
|
|
69
71
|
* @since 1.0.0
|
|
70
72
|
* @category hooks
|
|
71
73
|
*/
|
|
72
|
-
export declare const
|
|
74
|
+
export declare const useAtomRefProp: <A, K extends keyof A>(ref: () => AtomRef.AtomRef<A>, prop: K) => Accessor<AtomRef.AtomRef<A[K]>>;
|
|
75
|
+
/**
|
|
76
|
+
* @since 1.0.0
|
|
77
|
+
* @category hooks
|
|
78
|
+
*/
|
|
79
|
+
export declare const useAtomRefPropValue: <A, K extends keyof A>(ref: () => AtomRef.AtomRef<A>, prop: K) => Accessor<A[K]>;
|
|
73
80
|
//# sourceMappingURL=Hooks.d.ts.map
|
package/dist/Hooks.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Hooks.d.ts","sourceRo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,CAAC,CAAC,CAQ3E,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EACjD,KAAK,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAC7B,MAAM,CAAC,KACN,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAuC,CAAA;AAExE;;;GAGG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CACzE,CAAA"}
|
package/dist/Hooks.js
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
import * as Cause from "effect/Cause";
|
|
5
5
|
import * as Effect from "effect/Effect";
|
|
6
6
|
import * as Exit from "effect/Exit";
|
|
7
|
+
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
|
7
8
|
import * as Atom from "effect/unstable/reactivity/Atom";
|
|
8
9
|
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
|
|
9
|
-
import { createSignal, onCleanup, useContext } from "solid-js";
|
|
10
|
+
import { createComputed, createEffect, createMemo, createResource, createSignal, onCleanup, useContext } from "solid-js";
|
|
10
11
|
import { RegistryContext } from "./RegistryContext.js";
|
|
11
12
|
const initialValuesSet = /*#__PURE__*/new WeakMap();
|
|
12
13
|
/**
|
|
@@ -33,28 +34,36 @@ export const useAtomInitialValues = initialValues => {
|
|
|
33
34
|
*/
|
|
34
35
|
export const useAtomValue = (atom, f) => {
|
|
35
36
|
const registry = useContext(RegistryContext);
|
|
36
|
-
return createAtomAccessor(registry, f ? Atom.map(atom, f) : atom);
|
|
37
|
+
return createAtomAccessor(registry, f ? () => Atom.map(atom(), f) : atom);
|
|
37
38
|
};
|
|
38
39
|
function createAtomAccessor(registry, atom) {
|
|
39
|
-
const [value, setValue] = createSignal(
|
|
40
|
-
|
|
40
|
+
const [value, setValue] = createSignal(null);
|
|
41
|
+
createComputed(() => {
|
|
42
|
+
onCleanup(registry.subscribe(atom(), setValue, constImmediate));
|
|
43
|
+
});
|
|
41
44
|
return value;
|
|
42
45
|
}
|
|
46
|
+
const constImmediate = {
|
|
47
|
+
immediate: true
|
|
48
|
+
};
|
|
43
49
|
function mountAtom(registry, atom) {
|
|
44
|
-
|
|
50
|
+
createComputed(() => {
|
|
51
|
+
onCleanup(registry.mount(atom()));
|
|
52
|
+
});
|
|
45
53
|
}
|
|
46
54
|
function setAtom(registry, atom, options) {
|
|
55
|
+
const memo = createMemo(atom);
|
|
47
56
|
if (options?.mode === "promise" || options?.mode === "promiseExit") {
|
|
48
57
|
return value => {
|
|
49
|
-
registry.set(
|
|
50
|
-
const promise = Effect.runPromiseExit(AtomRegistry.getResult(registry,
|
|
58
|
+
registry.set(memo(), value);
|
|
59
|
+
const promise = Effect.runPromiseExit(AtomRegistry.getResult(registry, memo(), {
|
|
51
60
|
suspendOnWaiting: true
|
|
52
61
|
}));
|
|
53
62
|
return options.mode === "promise" ? promise.then(flattenExit) : promise;
|
|
54
63
|
};
|
|
55
64
|
}
|
|
56
65
|
return value => {
|
|
57
|
-
registry.set(
|
|
66
|
+
registry.set(memo(), typeof value === "function" ? value(registry.get(memo())) : value);
|
|
58
67
|
};
|
|
59
68
|
}
|
|
60
69
|
const flattenExit = exit => {
|
|
@@ -85,7 +94,8 @@ export const useAtomSet = (atom, options) => {
|
|
|
85
94
|
export const useAtomRefresh = atom => {
|
|
86
95
|
const registry = useContext(RegistryContext);
|
|
87
96
|
mountAtom(registry, atom);
|
|
88
|
-
|
|
97
|
+
const memo = createMemo(atom);
|
|
98
|
+
return () => registry.refresh(memo());
|
|
89
99
|
};
|
|
90
100
|
/**
|
|
91
101
|
* @since 1.0.0
|
|
@@ -101,22 +111,44 @@ export const useAtom = (atom, options) => {
|
|
|
101
111
|
*/
|
|
102
112
|
export const useAtomSubscribe = (atom, f, options) => {
|
|
103
113
|
const registry = useContext(RegistryContext);
|
|
104
|
-
|
|
114
|
+
createEffect(() => {
|
|
115
|
+
onCleanup(registry.subscribe(atom(), f, options));
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* @since 1.0.0
|
|
120
|
+
* @category hooks
|
|
121
|
+
*/
|
|
122
|
+
export const useAtomResource = (atom, options) => {
|
|
123
|
+
const result = useAtomValue(atom);
|
|
124
|
+
return createResource(result, result => {
|
|
125
|
+
if (AsyncResult.isInitial(result) || options?.suspendOnWaiting && result.waiting) {
|
|
126
|
+
return constUnresolvedPromise;
|
|
127
|
+
} else if (AsyncResult.isSuccess(result)) {
|
|
128
|
+
return Promise.resolve(result.value);
|
|
129
|
+
}
|
|
130
|
+
return Promise.reject(Cause.squash(result.cause));
|
|
131
|
+
});
|
|
105
132
|
};
|
|
133
|
+
const constUnresolvedPromise = /*#__PURE__*/new Promise(() => {});
|
|
106
134
|
/**
|
|
107
135
|
* @since 1.0.0
|
|
108
136
|
* @category hooks
|
|
109
137
|
*/
|
|
110
138
|
export const useAtomRef = ref => {
|
|
111
|
-
const [value, setValue] = createSignal(
|
|
112
|
-
|
|
139
|
+
const [value, setValue] = createSignal(null);
|
|
140
|
+
createComputed(() => {
|
|
141
|
+
const r = ref();
|
|
142
|
+
setValue(r.value);
|
|
143
|
+
onCleanup(r.subscribe(setValue));
|
|
144
|
+
});
|
|
113
145
|
return value;
|
|
114
146
|
};
|
|
115
147
|
/**
|
|
116
148
|
* @since 1.0.0
|
|
117
149
|
* @category hooks
|
|
118
150
|
*/
|
|
119
|
-
export const useAtomRefProp = (ref, prop) => ref.prop(prop);
|
|
151
|
+
export const useAtomRefProp = (ref, prop) => createMemo(() => ref().prop(prop));
|
|
120
152
|
/**
|
|
121
153
|
* @since 1.0.0
|
|
122
154
|
* @category hooks
|
package/dist/Hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Hooks.js","names":["Cause","Effect","Exit","Atom","AtomRegistry","createSignal","onCleanup","useContext","RegistryContext","initialValuesSet","WeakMap","useAtomInitialValues","initialValues","registry","set","get","undefined","WeakSet","atom","value","has","add","ensureNode","setValue","useAtomValue","f","createAtomAccessor","map","subscribe","mountAtom","mount","setAtom","options","mode","promise","runPromiseExit","getResult","suspendOnWaiting","then","flattenExit","exit","isSuccess","squash","cause","useAtomMount","useAtomSet","useAtomRefresh","refresh","useAtom","useAtomSubscribe","useAtomRef","ref","useAtomRefProp","prop","useAtomRefPropValue"],"sources":["../src/Hooks.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,IAAI,MAAM,aAAa;
|
|
1
|
+
{"version":3,"file":"Hooks.js","names":["Cause","Effect","Exit","AsyncResult","Atom","AtomRegistry","createComputed","createEffect","createMemo","createResource","createSignal","onCleanup","useContext","RegistryContext","initialValuesSet","WeakMap","useAtomInitialValues","initialValues","registry","set","get","undefined","WeakSet","atom","value","has","add","ensureNode","setValue","useAtomValue","f","createAtomAccessor","map","subscribe","constImmediate","immediate","mountAtom","mount","setAtom","options","memo","mode","promise","runPromiseExit","getResult","suspendOnWaiting","then","flattenExit","exit","isSuccess","squash","cause","useAtomMount","useAtomSet","useAtomRefresh","refresh","useAtom","useAtomSubscribe","useAtomResource","result","isInitial","waiting","constUnresolvedPromise","Promise","resolve","reject","useAtomRef","ref","r","useAtomRefProp","prop","useAtomRefPropValue"],"sources":["../src/Hooks.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,IAAI,MAAM,aAAa;AACnC,OAAO,KAAKC,WAAW,MAAM,wCAAwC;AACrE,OAA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CAAC;EACF,OAAOJ,KAAK;AACd,CAAC;AAED;;;;AAIA,OAAO,MAAM6C,cAAc,GAAGA,CAC5BF,GAA6B,EAC7BG,IAAO,KAC6B9D,UAAU,CAAC,MAAM2D,GAAG,EAAE,CAACG,IAAI,CAACA,IAAI,CAAC,CAAC;AAExE;;;;AAIA,OAAO,MAAMC,mBAAmB,GAAGA,CAAuBJ,GAA6B,EAAEG,IAAO,KAC9FJ,UAAU,CAACG,cAAc,CAACF,GAAG,EAAEG,IAAI,CAAC,CAAC","ignoreList":[]}
|
package/dist/RegistryContext.js
CHANGED
|
@@ -14,7 +14,7 @@ export const RegistryProvider = options => {
|
|
|
14
14
|
scheduleTask: options.scheduleTask,
|
|
15
15
|
initialValues: options.initialValues,
|
|
16
16
|
timeoutResolution: options.timeoutResolution,
|
|
17
|
-
defaultIdleTTL: options.defaultIdleTTL
|
|
17
|
+
defaultIdleTTL: options.defaultIdleTTL ?? 400
|
|
18
18
|
});
|
|
19
19
|
onCleanup(() => registry.dispose());
|
|
20
20
|
return createComponent(RegistryContext.Provider, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RegistryContext.js","names":["AtomRegistry","createComponent","createContext","onCleanup","RegistryContext","make","RegistryProvider","options","registry","scheduleTask","initialValues","timeoutResolution","defaultIdleTTL","dispose","Provider","value","children"],"sources":["../src/RegistryContext.ts"],"sourcesContent":[null],"mappings":"AAIA,OAAO,KAAKA,YAAY,MAAM,yCAAyC;AAEvE,SAASC,eAAe,EAAEC,aAAa,EAAEC,SAAS,QAAQ,UAAU;AAEpE;;;;AAIA,OAAO,MAAMC,eAAe,gBAAGF,aAAa,cAA4BF,YAAY,CAACK,IAAI,EAAE,CAAC;AAE5F;;;;AAIA,OAAO,MAAMC,gBAAgB,GAAIC,OAMhC,IAAI;EACH,MAAMC,QAAQ,GAAGR,YAAY,CAACK,IAAI,CAAC;IACjCI,YAAY,EAAEF,OAAO,CAACE,YAAY;IAClCC,aAAa,EAAEH,OAAO,CAACG,aAAa;IACpCC,iBAAiB,EAAEJ,OAAO,CAACI,iBAAiB;IAC5CC,cAAc,EAAEL,OAAO,CAACK;
|
|
1
|
+
{"version":3,"file":"RegistryContext.js","names":["AtomRegistry","createComponent","createContext","onCleanup","RegistryContext","make","RegistryProvider","options","registry","scheduleTask","initialValues","timeoutResolution","defaultIdleTTL","dispose","Provider","value","children"],"sources":["../src/RegistryContext.ts"],"sourcesContent":[null],"mappings":"AAIA,OAAO,KAAKA,YAAY,MAAM,yCAAyC;AAEvE,SAASC,eAAe,EAAEC,aAAa,EAAEC,SAAS,QAAQ,UAAU;AAEpE;;;;AAIA,OAAO,MAAMC,eAAe,gBAAGF,aAAa,cAA4BF,YAAY,CAACK,IAAI,EAAE,CAAC;AAE5F;;;;AAIA,OAAO,MAAMC,gBAAgB,GAAIC,OAMhC,IAAI;EACH,MAAMC,QAAQ,GAAGR,YAAY,CAACK,IAAI,CAAC;IACjCI,YAAY,EAAEF,OAAO,CAACE,YAAY;IAClCC,aAAa,EAAEH,OAAO,CAACG,aAAa;IACpCC,iBAAiB,EAAEJ,OAAO,CAACI,iBAAiB;IAC5CC,cAAc,EAAEL,OAAO,CAACK,cAAc,IAAI;GAC3C,CAAC;EACFT,SAAS,CAAC,MAAMK,QAAQ,CAACK,OAAO,EAAE,CAAC;EACnC,OAAOZ,eAAe,CAACG,eAAe,CAACU,QAAQ,EAAE;IAC/CC,KAAK,EAAEP,QAAQ;IACf,IAAIQ,QAAQA,CAAA;MACV,OAAOT,OAAO,CAACS,QAAQ;IACzB;GACD,CAAC;AACJ,CAAC","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/atom-solid",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.62",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "SolidJS bindings for the Effect Atom modules",
|
|
@@ -44,15 +44,15 @@
|
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"solid-js": ">=1 <2",
|
|
47
|
-
"effect": "^4.0.0-beta.
|
|
47
|
+
"effect": "^4.0.0-beta.62"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@solidjs/testing-library": "^0.8.0",
|
|
51
51
|
"@testing-library/dom": "^10.4.1",
|
|
52
52
|
"@testing-library/jest-dom": "^6.9.1",
|
|
53
|
-
"jsdom": "^
|
|
54
|
-
"solid-js": "^1.9.
|
|
55
|
-
"effect": "^4.0.0-beta.
|
|
53
|
+
"jsdom": "^29.0.2",
|
|
54
|
+
"solid-js": "^1.9.12",
|
|
55
|
+
"effect": "^4.0.0-beta.62"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "tsc -b tsconfig.json && pnpm babel",
|
package/src/Hooks.ts
CHANGED
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
import * as Cause from "effect/Cause"
|
|
5
5
|
import * as Effect from "effect/Effect"
|
|
6
6
|
import * as Exit from "effect/Exit"
|
|
7
|
-
import
|
|
7
|
+
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"
|
|
8
8
|
import * as Atom from "effect/unstable/reactivity/Atom"
|
|
9
9
|
import type * as AtomRef from "effect/unstable/reactivity/AtomRef"
|
|
10
10
|
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry"
|
|
11
|
-
import type { Accessor } from "solid-js"
|
|
12
|
-
import { createSignal, onCleanup, useContext } from "solid-js"
|
|
11
|
+
import type { Accessor, ResourceOptions, ResourceReturn } from "solid-js"
|
|
12
|
+
import { createComputed, createEffect, createMemo, createResource, createSignal, onCleanup, useContext } from "solid-js"
|
|
13
13
|
import { RegistryContext } from "./RegistryContext.ts"
|
|
14
14
|
|
|
15
15
|
const initialValuesSet = new WeakMap<AtomRegistry.AtomRegistry, WeakSet<Atom.Atom<any>>>()
|
|
@@ -42,30 +42,36 @@ export const useAtomValue: {
|
|
|
42
42
|
* @since 1.0.0
|
|
43
43
|
* @category hooks
|
|
44
44
|
*/
|
|
45
|
-
<A>(atom: Atom.Atom<A>): Accessor<A>
|
|
45
|
+
<A>(atom: () => Atom.Atom<A>): Accessor<A>
|
|
46
46
|
/**
|
|
47
47
|
* @since 1.0.0
|
|
48
48
|
* @category hooks
|
|
49
49
|
*/
|
|
50
|
-
<A, B>(atom: Atom.Atom<A>, f: (_: A) => B): Accessor<B>
|
|
51
|
-
} = <A>(atom: Atom.Atom<A>, f?: (_: A) => A): Accessor<A> => {
|
|
50
|
+
<A, B>(atom: () => Atom.Atom<A>, f: (_: A) => B): Accessor<B>
|
|
51
|
+
} = <A>(atom: () => Atom.Atom<A>, f?: (_: A) => A): Accessor<A> => {
|
|
52
52
|
const registry = useContext(RegistryContext)
|
|
53
|
-
return createAtomAccessor(registry, f ? Atom.map(atom, f) : atom)
|
|
53
|
+
return createAtomAccessor(registry, f ? () => Atom.map(atom(), f) : atom)
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
function createAtomAccessor<A>(registry: AtomRegistry.AtomRegistry, atom: Atom.Atom<A>): Accessor<A> {
|
|
57
|
-
const [value, setValue] = createSignal<A>(
|
|
58
|
-
|
|
56
|
+
function createAtomAccessor<A>(registry: AtomRegistry.AtomRegistry, atom: () => Atom.Atom<A>): Accessor<A> {
|
|
57
|
+
const [value, setValue] = createSignal<A>(null as any)
|
|
58
|
+
createComputed(() => {
|
|
59
|
+
onCleanup(registry.subscribe(atom(), setValue as any, constImmediate))
|
|
60
|
+
})
|
|
59
61
|
return value
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
const constImmediate = { immediate: true }
|
|
65
|
+
|
|
66
|
+
function mountAtom<A>(registry: AtomRegistry.AtomRegistry, atom: () => Atom.Atom<A>): void {
|
|
67
|
+
createComputed(() => {
|
|
68
|
+
onCleanup(registry.mount(atom()))
|
|
69
|
+
})
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>(
|
|
67
73
|
registry: AtomRegistry.AtomRegistry,
|
|
68
|
-
atom: Atom.Writable<R, W>,
|
|
74
|
+
atom: () => Atom.Writable<R, W>,
|
|
69
75
|
options?: {
|
|
70
76
|
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
71
77
|
}
|
|
@@ -77,11 +83,12 @@ function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>
|
|
|
77
83
|
) :
|
|
78
84
|
((value: W | ((value: R) => W)) => void)
|
|
79
85
|
{
|
|
86
|
+
const memo = createMemo(atom)
|
|
80
87
|
if (options?.mode === "promise" || options?.mode === "promiseExit") {
|
|
81
88
|
return ((value: W) => {
|
|
82
|
-
registry.set(
|
|
89
|
+
registry.set(memo(), value)
|
|
83
90
|
const promise = Effect.runPromiseExit(
|
|
84
|
-
AtomRegistry.getResult(registry,
|
|
91
|
+
AtomRegistry.getResult(registry, memo() as Atom.Atom<AsyncResult.AsyncResult<any, any>>, {
|
|
85
92
|
suspendOnWaiting: true
|
|
86
93
|
})
|
|
87
94
|
)
|
|
@@ -89,7 +96,7 @@ function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>
|
|
|
89
96
|
}) as any
|
|
90
97
|
}
|
|
91
98
|
return ((value: W | ((value: R) => W)) => {
|
|
92
|
-
registry.set(
|
|
99
|
+
registry.set(memo(), typeof value === "function" ? (value as any)(registry.get(memo())) : value)
|
|
93
100
|
}) as any
|
|
94
101
|
}
|
|
95
102
|
|
|
@@ -102,7 +109,7 @@ const flattenExit = <A, E>(exit: Exit.Exit<A, E>): A => {
|
|
|
102
109
|
* @since 1.0.0
|
|
103
110
|
* @category hooks
|
|
104
111
|
*/
|
|
105
|
-
export const useAtomMount = <A>(atom: Atom.Atom<A>): void => {
|
|
112
|
+
export const useAtomMount = <A>(atom: () => Atom.Atom<A>): void => {
|
|
106
113
|
const registry = useContext(RegistryContext)
|
|
107
114
|
mountAtom(registry, atom)
|
|
108
115
|
}
|
|
@@ -116,7 +123,7 @@ export const useAtomSet = <
|
|
|
116
123
|
W,
|
|
117
124
|
Mode extends "value" | "promise" | "promiseExit" = never
|
|
118
125
|
>(
|
|
119
|
-
atom: Atom.Writable<R, W>,
|
|
126
|
+
atom: () => Atom.Writable<R, W>,
|
|
120
127
|
options?: {
|
|
121
128
|
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
122
129
|
}
|
|
@@ -137,10 +144,11 @@ export const useAtomSet = <
|
|
|
137
144
|
* @since 1.0.0
|
|
138
145
|
* @category hooks
|
|
139
146
|
*/
|
|
140
|
-
export const useAtomRefresh = <A>(atom: Atom.Atom<A>): () => void => {
|
|
147
|
+
export const useAtomRefresh = <A>(atom: () => Atom.Atom<A>): () => void => {
|
|
141
148
|
const registry = useContext(RegistryContext)
|
|
142
149
|
mountAtom(registry, atom)
|
|
143
|
-
|
|
150
|
+
const memo = createMemo(atom)
|
|
151
|
+
return () => registry.refresh(memo())
|
|
144
152
|
}
|
|
145
153
|
|
|
146
154
|
/**
|
|
@@ -148,7 +156,7 @@ export const useAtomRefresh = <A>(atom: Atom.Atom<A>): () => void => {
|
|
|
148
156
|
* @category hooks
|
|
149
157
|
*/
|
|
150
158
|
export const useAtom = <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(
|
|
151
|
-
atom: Atom.Writable<R, W>,
|
|
159
|
+
atom: () => Atom.Writable<R, W>,
|
|
152
160
|
options?: {
|
|
153
161
|
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
154
162
|
}
|
|
@@ -174,21 +182,50 @@ export const useAtom = <R, W, const Mode extends "value" | "promise" | "promiseE
|
|
|
174
182
|
* @category hooks
|
|
175
183
|
*/
|
|
176
184
|
export const useAtomSubscribe = <A>(
|
|
177
|
-
atom: Atom.Atom<A>,
|
|
185
|
+
atom: () => Atom.Atom<A>,
|
|
178
186
|
f: (_: A) => void,
|
|
179
187
|
options?: { readonly immediate?: boolean }
|
|
180
188
|
): void => {
|
|
181
189
|
const registry = useContext(RegistryContext)
|
|
182
|
-
|
|
190
|
+
createEffect(() => {
|
|
191
|
+
onCleanup(registry.subscribe(atom(), f, options))
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @since 1.0.0
|
|
197
|
+
* @category hooks
|
|
198
|
+
*/
|
|
199
|
+
export const useAtomResource = <A, E>(
|
|
200
|
+
atom: () => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
|
|
201
|
+
options?: ResourceOptions<A> & {
|
|
202
|
+
readonly suspendOnWaiting?: boolean | undefined
|
|
203
|
+
}
|
|
204
|
+
): ResourceReturn<A, void> => {
|
|
205
|
+
const result = useAtomValue(atom)
|
|
206
|
+
return createResource(result, (result) => {
|
|
207
|
+
if (AsyncResult.isInitial(result) || (options?.suspendOnWaiting && result.waiting)) {
|
|
208
|
+
return constUnresolvedPromise
|
|
209
|
+
} else if (AsyncResult.isSuccess(result)) {
|
|
210
|
+
return Promise.resolve(result.value)
|
|
211
|
+
}
|
|
212
|
+
return Promise.reject(Cause.squash(result.cause))
|
|
213
|
+
})
|
|
183
214
|
}
|
|
184
215
|
|
|
216
|
+
const constUnresolvedPromise = new Promise<never>(() => {})
|
|
217
|
+
|
|
185
218
|
/**
|
|
186
219
|
* @since 1.0.0
|
|
187
220
|
* @category hooks
|
|
188
221
|
*/
|
|
189
|
-
export const useAtomRef = <A>(ref: AtomRef.ReadonlyRef<A>): Accessor<A> => {
|
|
190
|
-
const [value, setValue] = createSignal(
|
|
191
|
-
|
|
222
|
+
export const useAtomRef = <A>(ref: () => AtomRef.ReadonlyRef<A>): Accessor<A> => {
|
|
223
|
+
const [value, setValue] = createSignal(null as A)
|
|
224
|
+
createComputed(() => {
|
|
225
|
+
const r = ref()
|
|
226
|
+
setValue(r.value as any)
|
|
227
|
+
onCleanup(r.subscribe(setValue))
|
|
228
|
+
})
|
|
192
229
|
return value
|
|
193
230
|
}
|
|
194
231
|
|
|
@@ -196,12 +233,14 @@ export const useAtomRef = <A>(ref: AtomRef.ReadonlyRef<A>): Accessor<A> => {
|
|
|
196
233
|
* @since 1.0.0
|
|
197
234
|
* @category hooks
|
|
198
235
|
*/
|
|
199
|
-
export const useAtomRefProp = <A, K extends keyof A>(
|
|
200
|
-
ref
|
|
236
|
+
export const useAtomRefProp = <A, K extends keyof A>(
|
|
237
|
+
ref: () => AtomRef.AtomRef<A>,
|
|
238
|
+
prop: K
|
|
239
|
+
): Accessor<AtomRef.AtomRef<A[K]>> => createMemo(() => ref().prop(prop))
|
|
201
240
|
|
|
202
241
|
/**
|
|
203
242
|
* @since 1.0.0
|
|
204
243
|
* @category hooks
|
|
205
244
|
*/
|
|
206
|
-
export const useAtomRefPropValue = <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K): Accessor<A[K]> =>
|
|
245
|
+
export const useAtomRefPropValue = <A, K extends keyof A>(ref: () => AtomRef.AtomRef<A>, prop: K): Accessor<A[K]> =>
|
|
207
246
|
useAtomRef(useAtomRefProp(ref, prop))
|
package/src/RegistryContext.ts
CHANGED
|
@@ -27,7 +27,7 @@ export const RegistryProvider = (options: {
|
|
|
27
27
|
scheduleTask: options.scheduleTask,
|
|
28
28
|
initialValues: options.initialValues,
|
|
29
29
|
timeoutResolution: options.timeoutResolution,
|
|
30
|
-
defaultIdleTTL: options.defaultIdleTTL
|
|
30
|
+
defaultIdleTTL: options.defaultIdleTTL ?? 400
|
|
31
31
|
})
|
|
32
32
|
onCleanup(() => registry.dispose())
|
|
33
33
|
return createComponent(RegistryContext.Provider, {
|