@effect/atom-solid 4.0.0-beta.0
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 +21 -0
- package/README.md +7 -0
- package/dist/Hooks.d.ts +73 -0
- package/dist/Hooks.d.ts.map +1 -0
- package/dist/Hooks.js +125 -0
- package/dist/Hooks.js.map +1 -0
- package/dist/RegistryContext.d.ts +23 -0
- package/dist/RegistryContext.d.ts.map +1 -0
- package/dist/RegistryContext.js +27 -0
- package/dist/RegistryContext.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
- package/src/Hooks.ts +207 -0
- package/src/RegistryContext.ts +39 -0
- package/src/index.ts +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present The Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/Hooks.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as Exit from "effect/Exit";
|
|
2
|
+
import type * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
|
3
|
+
import * as Atom from "effect/unstable/reactivity/Atom";
|
|
4
|
+
import type * as AtomRef from "effect/unstable/reactivity/AtomRef";
|
|
5
|
+
import type { Accessor } from "solid-js";
|
|
6
|
+
/**
|
|
7
|
+
* @since 1.0.0
|
|
8
|
+
* @category hooks
|
|
9
|
+
*/
|
|
10
|
+
export declare const useAtomInitialValues: (initialValues: Iterable<readonly [Atom.Atom<any>, any]>) => void;
|
|
11
|
+
/**
|
|
12
|
+
* @since 1.0.0
|
|
13
|
+
* @category hooks
|
|
14
|
+
*/
|
|
15
|
+
export declare const useAtomValue: {
|
|
16
|
+
/**
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
* @category hooks
|
|
19
|
+
*/
|
|
20
|
+
<A>(atom: Atom.Atom<A>): Accessor<A>;
|
|
21
|
+
/**
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
* @category hooks
|
|
24
|
+
*/
|
|
25
|
+
<A, B>(atom: Atom.Atom<A>, f: (_: A) => B): Accessor<B>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
* @category hooks
|
|
30
|
+
*/
|
|
31
|
+
export declare const useAtomMount: <A>(atom: Atom.Atom<A>) => void;
|
|
32
|
+
/**
|
|
33
|
+
* @since 1.0.0
|
|
34
|
+
* @category hooks
|
|
35
|
+
*/
|
|
36
|
+
export declare const useAtomSet: <R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: Atom.Writable<R, W>, options?: {
|
|
37
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined;
|
|
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
|
+
/**
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
* @category hooks
|
|
42
|
+
*/
|
|
43
|
+
export declare const useAtomRefresh: <A>(atom: Atom.Atom<A>) => () => void;
|
|
44
|
+
/**
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
* @category hooks
|
|
47
|
+
*/
|
|
48
|
+
export declare const useAtom: <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(atom: Atom.Writable<R, W>, options?: {
|
|
49
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined;
|
|
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
|
+
/**
|
|
52
|
+
* @since 1.0.0
|
|
53
|
+
* @category hooks
|
|
54
|
+
*/
|
|
55
|
+
export declare const useAtomSubscribe: <A>(atom: Atom.Atom<A>, f: (_: A) => void, options?: {
|
|
56
|
+
readonly immediate?: boolean;
|
|
57
|
+
}) => void;
|
|
58
|
+
/**
|
|
59
|
+
* @since 1.0.0
|
|
60
|
+
* @category hooks
|
|
61
|
+
*/
|
|
62
|
+
export declare const useAtomRef: <A>(ref: AtomRef.ReadonlyRef<A>) => Accessor<A>;
|
|
63
|
+
/**
|
|
64
|
+
* @since 1.0.0
|
|
65
|
+
* @category hooks
|
|
66
|
+
*/
|
|
67
|
+
export declare const useAtomRefProp: <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K) => AtomRef.AtomRef<A[K]>;
|
|
68
|
+
/**
|
|
69
|
+
* @since 1.0.0
|
|
70
|
+
* @category hooks
|
|
71
|
+
*/
|
|
72
|
+
export declare const useAtomRefPropValue: <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K) => Accessor<A[K]>;
|
|
73
|
+
//# sourceMappingURL=Hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Hooks.d.ts","sourceRoot":"","sources":["../src/Hooks.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,IAAI,MAAM,aAAa,CAAA;AACnC,OAAO,KAAK,KAAK,WAAW,MAAM,wCAAwC,CAAA;AAC1E,OAAO,KAAK,IAAI,MAAM,iCAAiC,CAAA;AACvD,OAAO,KAAK,KAAK,OAAO,MAAM,oCAAoC,CAAA;AAElE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAMxC;;;GAGG;AACH,eAAO,MAAM,oBAAoB,GAAI,eAAe,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,KAAG,IAa9F,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE;IACzB;;;OAGG;IACH,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;IACpC;;;OAGG;IACH,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;CAIxD,CAAA;AA+CD;;;GAGG;AACH,eAAO,MAAM,YAAY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C,CAAC,CAAC,EAAE,MAAM,CAAC,KAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAC5F,CAAA;AAEhB;;;GAGG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CACnE,CAAA"}
|
package/dist/Hooks.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Cause from "effect/Cause";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Exit from "effect/Exit";
|
|
7
|
+
import * as Atom from "effect/unstable/reactivity/Atom";
|
|
8
|
+
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
|
|
9
|
+
import { createSignal, onCleanup, useContext } from "solid-js";
|
|
10
|
+
import { RegistryContext } from "./RegistryContext.js";
|
|
11
|
+
const initialValuesSet = /*#__PURE__*/new WeakMap();
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
* @category hooks
|
|
15
|
+
*/
|
|
16
|
+
export const useAtomInitialValues = initialValues => {
|
|
17
|
+
const registry = useContext(RegistryContext);
|
|
18
|
+
let set = initialValuesSet.get(registry);
|
|
19
|
+
if (set === undefined) {
|
|
20
|
+
set = new WeakSet();
|
|
21
|
+
initialValuesSet.set(registry, set);
|
|
22
|
+
}
|
|
23
|
+
for (const [atom, value] of initialValues) {
|
|
24
|
+
if (!set.has(atom)) {
|
|
25
|
+
set.add(atom);
|
|
26
|
+
registry.ensureNode(atom).setValue(value);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
* @category hooks
|
|
33
|
+
*/
|
|
34
|
+
export const useAtomValue = (atom, f) => {
|
|
35
|
+
const registry = useContext(RegistryContext);
|
|
36
|
+
return createAtomAccessor(registry, f ? Atom.map(atom, f) : atom);
|
|
37
|
+
};
|
|
38
|
+
function createAtomAccessor(registry, atom) {
|
|
39
|
+
const [value, setValue] = createSignal(registry.get(atom));
|
|
40
|
+
onCleanup(registry.subscribe(atom, setValue));
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
function mountAtom(registry, atom) {
|
|
44
|
+
onCleanup(registry.mount(atom));
|
|
45
|
+
}
|
|
46
|
+
function setAtom(registry, atom, options) {
|
|
47
|
+
if (options?.mode === "promise" || options?.mode === "promiseExit") {
|
|
48
|
+
return value => {
|
|
49
|
+
registry.set(atom, value);
|
|
50
|
+
const promise = Effect.runPromiseExit(AtomRegistry.getResult(registry, atom, {
|
|
51
|
+
suspendOnWaiting: true
|
|
52
|
+
}));
|
|
53
|
+
return options.mode === "promise" ? promise.then(flattenExit) : promise;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return value => {
|
|
57
|
+
registry.set(atom, typeof value === "function" ? value(registry.get(atom)) : value);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const flattenExit = exit => {
|
|
61
|
+
if (Exit.isSuccess(exit)) return exit.value;
|
|
62
|
+
throw Cause.squash(exit.cause);
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* @since 1.0.0
|
|
66
|
+
* @category hooks
|
|
67
|
+
*/
|
|
68
|
+
export const useAtomMount = atom => {
|
|
69
|
+
const registry = useContext(RegistryContext);
|
|
70
|
+
mountAtom(registry, atom);
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* @since 1.0.0
|
|
74
|
+
* @category hooks
|
|
75
|
+
*/
|
|
76
|
+
export const useAtomSet = (atom, options) => {
|
|
77
|
+
const registry = useContext(RegistryContext);
|
|
78
|
+
mountAtom(registry, atom);
|
|
79
|
+
return setAtom(registry, atom, options);
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* @since 1.0.0
|
|
83
|
+
* @category hooks
|
|
84
|
+
*/
|
|
85
|
+
export const useAtomRefresh = atom => {
|
|
86
|
+
const registry = useContext(RegistryContext);
|
|
87
|
+
mountAtom(registry, atom);
|
|
88
|
+
return () => registry.refresh(atom);
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* @since 1.0.0
|
|
92
|
+
* @category hooks
|
|
93
|
+
*/
|
|
94
|
+
export const useAtom = (atom, options) => {
|
|
95
|
+
const registry = useContext(RegistryContext);
|
|
96
|
+
return [createAtomAccessor(registry, atom), setAtom(registry, atom, options)];
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* @since 1.0.0
|
|
100
|
+
* @category hooks
|
|
101
|
+
*/
|
|
102
|
+
export const useAtomSubscribe = (atom, f, options) => {
|
|
103
|
+
const registry = useContext(RegistryContext);
|
|
104
|
+
onCleanup(registry.subscribe(atom, f, options));
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* @since 1.0.0
|
|
108
|
+
* @category hooks
|
|
109
|
+
*/
|
|
110
|
+
export const useAtomRef = ref => {
|
|
111
|
+
const [value, setValue] = createSignal(ref.value);
|
|
112
|
+
onCleanup(ref.subscribe(setValue));
|
|
113
|
+
return value;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* @since 1.0.0
|
|
117
|
+
* @category hooks
|
|
118
|
+
*/
|
|
119
|
+
export const useAtomRefProp = (ref, prop) => ref.prop(prop);
|
|
120
|
+
/**
|
|
121
|
+
* @since 1.0.0
|
|
122
|
+
* @category hooks
|
|
123
|
+
*/
|
|
124
|
+
export const useAtomRefPropValue = (ref, prop) => useAtomRef(useAtomRefProp(ref, prop));
|
|
125
|
+
//# sourceMappingURL=Hooks.js.map
|
|
@@ -0,0 +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;AAEnC,OAAO,KAAKC,IAAI,MAAM,iCAAiC;AAEvD,OAAO,KAAKC,YAAY,MAAM,yCAAyC;AAEvE,SAASC,YAAY,EAAEC,SAAS,EAAEC,UAAU,QAAQ,UAAU;AAC9D,SAASC,eAAe,QAAQ,sBAAsB;AAEtD,MAAMC,gBAAgB,gBAAG,IAAIC,OAAO,EAAsD;AAE1F;;;;AAIA,OAAO,MAAMC,oBAAoB,GAAIC,aAAuD,IAAU;EACpG,MAAMC,QAAQ,GAAGN,UAAU,CAACC,eAAe,CAAC;EAC5C,IAAIM,GAAG,GAAGL,gBAAgB,CAACM,GAAG,CAACF,QAAQ,CAAC;EACxC,IAAIC,GAAG,KAAKE,SAAS,EAAE;IACrBF,GAAG,GAAG,IAAIG,OAAO,EAAE;IACnBR,gBAAgB,CAACK,GAAG,CAACD,QAAQ,EAAEC,GAAG,CAAC;EACrC;EACA,KAAK,MAAM,CAACI,IAAI,EAAEC,KAAK,CAAC,IAAIP,aAAa,EAAE;IACzC,IAAI,CAACE,GAAG,CAACM,GAAG,CAACF,IAAI,CAAC,EAAE;MAClBJ,GAAG,CAACO,GAAG,CAACH,IAAI,CAAC;MACXL,QAAgB,CAACS,UAAU,CAACJ,IAAI,CAAC,CAACK,QAAQ,CAACJ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AAEK,IAAI,EAAEc,OAAO,CAAC,CACxB;AACZ,CAAC;AAED;;;;AAIA,OAAO,MAAMiB,gBAAgB,GAAGA,CAC9B/B,IAAkB,EAClBO,CAAiB,EACjBO,OAA0C,KAClC;EACR,MAAMnB,QAAQ,GAAGN,UAAU,CAACC,eAAe,CAAC;EAC5CF,SAAS,CAACO,QAAQ,CAACe,SAAS,CAACV,IAAI,EAAEO,CAAC,EAAEO,OAAO,CAAC,CAAC;AACjD,CAAC;AAED;;;;AAIA,OAAO,MAAMkB,UAAU,GAAOC,GAA2B,IAAiB;EACxE,MAAM,CAAChC,KAAK,EAAEI,QAAQ,CAAC,GAAGlB,YAAY,CAAC8C,GAAG,CAAChC,KAAK,CAAC;EACjDb,SAAS,CAAC6C,GAAG,CAACvB,SAAS,CAACL,QAAQ,CAAC,CAAC;EAClC,OAAOJ,KAAK;AACd,CAAC;AAED;;;;AAIA,OAAO,MAAMiC,cAAc,GAAGA,CAAuBD,GAAuB,EAAEE,IAAO,KACnFF,GAAG,CAACE,IAAI,CAACA,IAAI,CAAC;AAEhB;;;;AAIA,OAAO,MAAMC,mBAAmB,GAAGA,CAAuBH,GAAuB,EAAEE,IAAO,KACxFH,UAAU,CAACE,cAAc,CAACD,GAAG,EAAEE,IAAI,CAAC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import type * as Atom from "effect/unstable/reactivity/Atom";
|
|
5
|
+
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
|
|
6
|
+
import type { JSX } from "solid-js";
|
|
7
|
+
/**
|
|
8
|
+
* @since 1.0.0
|
|
9
|
+
* @category context
|
|
10
|
+
*/
|
|
11
|
+
export declare const RegistryContext: import("solid-js").Context<AtomRegistry.AtomRegistry>;
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
* @category context
|
|
15
|
+
*/
|
|
16
|
+
export declare const RegistryProvider: (options: {
|
|
17
|
+
readonly children?: JSX.Element | undefined;
|
|
18
|
+
readonly initialValues?: Iterable<readonly [Atom.Atom<any>, any]> | undefined;
|
|
19
|
+
readonly scheduleTask?: ((f: () => void) => () => void) | undefined;
|
|
20
|
+
readonly timeoutResolution?: number | undefined;
|
|
21
|
+
readonly defaultIdleTTL?: number | undefined;
|
|
22
|
+
}) => JSX.Element;
|
|
23
|
+
//# sourceMappingURL=RegistryContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RegistryContext.d.ts","sourceRoot":"","sources":["../src/RegistryContext.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,KAAK,IAAI,MAAM,iCAAiC,CAAA;AAC5D,OAAO,KAAK,YAAY,MAAM,yCAAyC,CAAA;AACvE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAGnC;;;GAGG;AACH,eAAO,MAAM,eAAe,uDAAgE,CAAA;AAE5F;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAS;IACxC,QAAQ,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,OAAO,GAAG,SAAS,CAAA;IAC3C,QAAQ,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,GAAG,SAAS,CAAA;IAC7E,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC,GAAG,SAAS,CAAA;IACnE,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/C,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC7C,gBAcA,CAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
|
|
2
|
+
import { createComponent, createContext, onCleanup } from "solid-js";
|
|
3
|
+
/**
|
|
4
|
+
* @since 1.0.0
|
|
5
|
+
* @category context
|
|
6
|
+
*/
|
|
7
|
+
export const RegistryContext = /*#__PURE__*/createContext(/*#__PURE__*/AtomRegistry.make());
|
|
8
|
+
/**
|
|
9
|
+
* @since 1.0.0
|
|
10
|
+
* @category context
|
|
11
|
+
*/
|
|
12
|
+
export const RegistryProvider = options => {
|
|
13
|
+
const registry = AtomRegistry.make({
|
|
14
|
+
scheduleTask: options.scheduleTask,
|
|
15
|
+
initialValues: options.initialValues,
|
|
16
|
+
timeoutResolution: options.timeoutResolution,
|
|
17
|
+
defaultIdleTTL: options.defaultIdleTTL
|
|
18
|
+
});
|
|
19
|
+
onCleanup(() => registry.dispose());
|
|
20
|
+
return createComponent(RegistryContext.Provider, {
|
|
21
|
+
value: registry,
|
|
22
|
+
get children() {
|
|
23
|
+
return options.children;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=RegistryContext.js.map
|
|
@@ -0,0 +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;GACzB,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/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,cAAc,YAAY,CAAA;AAE1B;;GAEG;AACH,cAAc,sBAAsB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA;;;AAGA,cAAc,YAAY;AAE1B;;;AAGA,cAAc,sBAAsB","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect/atom-solid",
|
|
3
|
+
"version": "4.0.0-beta.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "SolidJS bindings for the Effect Atom modules",
|
|
7
|
+
"homepage": "https://effect.website",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/Effect-TS/effect-smol.git",
|
|
11
|
+
"directory": "packages/atom/solid"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Effect-TS/effect-smol/issues"
|
|
15
|
+
},
|
|
16
|
+
"tags": [
|
|
17
|
+
"typescript",
|
|
18
|
+
"solid",
|
|
19
|
+
"database"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"typescript",
|
|
23
|
+
"solid",
|
|
24
|
+
"database"
|
|
25
|
+
],
|
|
26
|
+
"sideEffects": [],
|
|
27
|
+
"exports": {
|
|
28
|
+
"./package.json": "./package.json",
|
|
29
|
+
".": "./dist/index.js",
|
|
30
|
+
"./*": "./dist/*.js",
|
|
31
|
+
"./internal/*": null,
|
|
32
|
+
"./*/index": null
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"src/**/*.ts",
|
|
36
|
+
"dist/**/*.js",
|
|
37
|
+
"dist/**/*.js.map",
|
|
38
|
+
"dist/**/*.d.ts",
|
|
39
|
+
"dist/**/*.d.ts.map"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public",
|
|
43
|
+
"provenance": true
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"solid-js": ">=1 <2",
|
|
47
|
+
"effect": "^4.0.0-beta.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@solidjs/testing-library": "^0.8.0",
|
|
51
|
+
"@testing-library/dom": "^10.4.1",
|
|
52
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
53
|
+
"jsdom": "^27.1.0",
|
|
54
|
+
"solid-js": "^1.9.0",
|
|
55
|
+
"effect": "^4.0.0-beta.0"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsc -b tsconfig.json && pnpm babel",
|
|
59
|
+
"build:tsgo": "tsgo -b tsconfig.json && pnpm babel",
|
|
60
|
+
"babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps",
|
|
61
|
+
"check": "tsc -b tsconfig.json",
|
|
62
|
+
"test": "vitest",
|
|
63
|
+
"coverage": "vitest --coverage"
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/Hooks.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Cause from "effect/Cause"
|
|
5
|
+
import * as Effect from "effect/Effect"
|
|
6
|
+
import * as Exit from "effect/Exit"
|
|
7
|
+
import type * as AsyncResult from "effect/unstable/reactivity/AsyncResult"
|
|
8
|
+
import * as Atom from "effect/unstable/reactivity/Atom"
|
|
9
|
+
import type * as AtomRef from "effect/unstable/reactivity/AtomRef"
|
|
10
|
+
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry"
|
|
11
|
+
import type { Accessor } from "solid-js"
|
|
12
|
+
import { createSignal, onCleanup, useContext } from "solid-js"
|
|
13
|
+
import { RegistryContext } from "./RegistryContext.ts"
|
|
14
|
+
|
|
15
|
+
const initialValuesSet = new WeakMap<AtomRegistry.AtomRegistry, WeakSet<Atom.Atom<any>>>()
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
* @category hooks
|
|
20
|
+
*/
|
|
21
|
+
export const useAtomInitialValues = (initialValues: Iterable<readonly [Atom.Atom<any>, any]>): void => {
|
|
22
|
+
const registry = useContext(RegistryContext)
|
|
23
|
+
let set = initialValuesSet.get(registry)
|
|
24
|
+
if (set === undefined) {
|
|
25
|
+
set = new WeakSet()
|
|
26
|
+
initialValuesSet.set(registry, set)
|
|
27
|
+
}
|
|
28
|
+
for (const [atom, value] of initialValues) {
|
|
29
|
+
if (!set.has(atom)) {
|
|
30
|
+
set.add(atom)
|
|
31
|
+
;(registry as any).ensureNode(atom).setValue(value)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @since 1.0.0
|
|
38
|
+
* @category hooks
|
|
39
|
+
*/
|
|
40
|
+
export const useAtomValue: {
|
|
41
|
+
/**
|
|
42
|
+
* @since 1.0.0
|
|
43
|
+
* @category hooks
|
|
44
|
+
*/
|
|
45
|
+
<A>(atom: Atom.Atom<A>): Accessor<A>
|
|
46
|
+
/**
|
|
47
|
+
* @since 1.0.0
|
|
48
|
+
* @category hooks
|
|
49
|
+
*/
|
|
50
|
+
<A, B>(atom: Atom.Atom<A>, f: (_: A) => B): Accessor<B>
|
|
51
|
+
} = <A>(atom: Atom.Atom<A>, f?: (_: A) => A): Accessor<A> => {
|
|
52
|
+
const registry = useContext(RegistryContext)
|
|
53
|
+
return createAtomAccessor(registry, f ? Atom.map(atom, f) : atom)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function createAtomAccessor<A>(registry: AtomRegistry.AtomRegistry, atom: Atom.Atom<A>): Accessor<A> {
|
|
57
|
+
const [value, setValue] = createSignal<A>(registry.get(atom))
|
|
58
|
+
onCleanup(registry.subscribe(atom, setValue as any))
|
|
59
|
+
return value
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function mountAtom<A>(registry: AtomRegistry.AtomRegistry, atom: Atom.Atom<A>): void {
|
|
63
|
+
onCleanup(registry.mount(atom))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>(
|
|
67
|
+
registry: AtomRegistry.AtomRegistry,
|
|
68
|
+
atom: Atom.Writable<R, W>,
|
|
69
|
+
options?: {
|
|
70
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
71
|
+
}
|
|
72
|
+
): "promise" extends Mode ? (
|
|
73
|
+
(value: W) => Promise<AsyncResult.AsyncResult.Success<R>>
|
|
74
|
+
) :
|
|
75
|
+
"promiseExit" extends Mode ? (
|
|
76
|
+
(value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>
|
|
77
|
+
) :
|
|
78
|
+
((value: W | ((value: R) => W)) => void)
|
|
79
|
+
{
|
|
80
|
+
if (options?.mode === "promise" || options?.mode === "promiseExit") {
|
|
81
|
+
return ((value: W) => {
|
|
82
|
+
registry.set(atom, value)
|
|
83
|
+
const promise = Effect.runPromiseExit(
|
|
84
|
+
AtomRegistry.getResult(registry, atom as Atom.Atom<AsyncResult.AsyncResult<any, any>>, {
|
|
85
|
+
suspendOnWaiting: true
|
|
86
|
+
})
|
|
87
|
+
)
|
|
88
|
+
return options!.mode === "promise" ? promise.then(flattenExit) : promise
|
|
89
|
+
}) as any
|
|
90
|
+
}
|
|
91
|
+
return ((value: W | ((value: R) => W)) => {
|
|
92
|
+
registry.set(atom, typeof value === "function" ? (value as any)(registry.get(atom)) : value)
|
|
93
|
+
}) as any
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const flattenExit = <A, E>(exit: Exit.Exit<A, E>): A => {
|
|
97
|
+
if (Exit.isSuccess(exit)) return exit.value
|
|
98
|
+
throw Cause.squash(exit.cause)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @since 1.0.0
|
|
103
|
+
* @category hooks
|
|
104
|
+
*/
|
|
105
|
+
export const useAtomMount = <A>(atom: Atom.Atom<A>): void => {
|
|
106
|
+
const registry = useContext(RegistryContext)
|
|
107
|
+
mountAtom(registry, atom)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @since 1.0.0
|
|
112
|
+
* @category hooks
|
|
113
|
+
*/
|
|
114
|
+
export const useAtomSet = <
|
|
115
|
+
R,
|
|
116
|
+
W,
|
|
117
|
+
Mode extends "value" | "promise" | "promiseExit" = never
|
|
118
|
+
>(
|
|
119
|
+
atom: Atom.Writable<R, W>,
|
|
120
|
+
options?: {
|
|
121
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
122
|
+
}
|
|
123
|
+
): "promise" extends Mode ? (
|
|
124
|
+
(value: W) => Promise<AsyncResult.AsyncResult.Success<R>>
|
|
125
|
+
) :
|
|
126
|
+
"promiseExit" extends Mode ? (
|
|
127
|
+
(value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>
|
|
128
|
+
) :
|
|
129
|
+
((value: W | ((value: R) => W)) => void) =>
|
|
130
|
+
{
|
|
131
|
+
const registry = useContext(RegistryContext)
|
|
132
|
+
mountAtom(registry, atom)
|
|
133
|
+
return setAtom(registry, atom, options)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @since 1.0.0
|
|
138
|
+
* @category hooks
|
|
139
|
+
*/
|
|
140
|
+
export const useAtomRefresh = <A>(atom: Atom.Atom<A>): () => void => {
|
|
141
|
+
const registry = useContext(RegistryContext)
|
|
142
|
+
mountAtom(registry, atom)
|
|
143
|
+
return () => registry.refresh(atom)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @since 1.0.0
|
|
148
|
+
* @category hooks
|
|
149
|
+
*/
|
|
150
|
+
export const useAtom = <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(
|
|
151
|
+
atom: Atom.Writable<R, W>,
|
|
152
|
+
options?: {
|
|
153
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
154
|
+
}
|
|
155
|
+
): readonly [
|
|
156
|
+
value: Accessor<R>,
|
|
157
|
+
write: "promise" extends Mode ? (
|
|
158
|
+
(value: W) => Promise<AsyncResult.AsyncResult.Success<R>>
|
|
159
|
+
) :
|
|
160
|
+
"promiseExit" extends Mode ? (
|
|
161
|
+
(value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>
|
|
162
|
+
) :
|
|
163
|
+
((value: W | ((value: R) => W)) => void)
|
|
164
|
+
] => {
|
|
165
|
+
const registry = useContext(RegistryContext)
|
|
166
|
+
return [
|
|
167
|
+
createAtomAccessor(registry, atom),
|
|
168
|
+
setAtom(registry, atom, options)
|
|
169
|
+
] as const
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @since 1.0.0
|
|
174
|
+
* @category hooks
|
|
175
|
+
*/
|
|
176
|
+
export const useAtomSubscribe = <A>(
|
|
177
|
+
atom: Atom.Atom<A>,
|
|
178
|
+
f: (_: A) => void,
|
|
179
|
+
options?: { readonly immediate?: boolean }
|
|
180
|
+
): void => {
|
|
181
|
+
const registry = useContext(RegistryContext)
|
|
182
|
+
onCleanup(registry.subscribe(atom, f, options))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @since 1.0.0
|
|
187
|
+
* @category hooks
|
|
188
|
+
*/
|
|
189
|
+
export const useAtomRef = <A>(ref: AtomRef.ReadonlyRef<A>): Accessor<A> => {
|
|
190
|
+
const [value, setValue] = createSignal(ref.value)
|
|
191
|
+
onCleanup(ref.subscribe(setValue))
|
|
192
|
+
return value
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @since 1.0.0
|
|
197
|
+
* @category hooks
|
|
198
|
+
*/
|
|
199
|
+
export const useAtomRefProp = <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K): AtomRef.AtomRef<A[K]> =>
|
|
200
|
+
ref.prop(prop)
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @since 1.0.0
|
|
204
|
+
* @category hooks
|
|
205
|
+
*/
|
|
206
|
+
export const useAtomRefPropValue = <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K): Accessor<A[K]> =>
|
|
207
|
+
useAtomRef(useAtomRefProp(ref, prop))
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import type * as Atom from "effect/unstable/reactivity/Atom"
|
|
5
|
+
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry"
|
|
6
|
+
import type { JSX } from "solid-js"
|
|
7
|
+
import { createComponent, createContext, onCleanup } from "solid-js"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category context
|
|
12
|
+
*/
|
|
13
|
+
export const RegistryContext = createContext<AtomRegistry.AtomRegistry>(AtomRegistry.make())
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @since 1.0.0
|
|
17
|
+
* @category context
|
|
18
|
+
*/
|
|
19
|
+
export const RegistryProvider = (options: {
|
|
20
|
+
readonly children?: JSX.Element | undefined
|
|
21
|
+
readonly initialValues?: Iterable<readonly [Atom.Atom<any>, any]> | undefined
|
|
22
|
+
readonly scheduleTask?: ((f: () => void) => () => void) | undefined
|
|
23
|
+
readonly timeoutResolution?: number | undefined
|
|
24
|
+
readonly defaultIdleTTL?: number | undefined
|
|
25
|
+
}) => {
|
|
26
|
+
const registry = AtomRegistry.make({
|
|
27
|
+
scheduleTask: options.scheduleTask,
|
|
28
|
+
initialValues: options.initialValues,
|
|
29
|
+
timeoutResolution: options.timeoutResolution,
|
|
30
|
+
defaultIdleTTL: options.defaultIdleTTL
|
|
31
|
+
})
|
|
32
|
+
onCleanup(() => registry.dispose())
|
|
33
|
+
return createComponent(RegistryContext.Provider, {
|
|
34
|
+
value: registry,
|
|
35
|
+
get children() {
|
|
36
|
+
return options.children
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
}
|