@effect-atom/atom-react 0.1.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/Hooks/package.json +6 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/ReactHydration/package.json +6 -0
- package/RegistryContext/package.json +6 -0
- package/dist/cjs/Hooks.js +237 -0
- package/dist/cjs/Hooks.js.map +1 -0
- package/dist/cjs/ReactHydration.js +95 -0
- package/dist/cjs/ReactHydration.js.map +1 -0
- package/dist/cjs/RegistryContext.js +83 -0
- package/dist/cjs/RegistryContext.js.map +1 -0
- package/dist/cjs/index.js +68 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/dts/Hooks.d.ts +80 -0
- package/dist/dts/Hooks.d.ts.map +1 -0
- package/dist/dts/ReactHydration.d.ts +16 -0
- package/dist/dts/ReactHydration.d.ts.map +1 -0
- package/dist/dts/RegistryContext.d.ts +25 -0
- package/dist/dts/RegistryContext.d.ts.map +1 -0
- package/dist/dts/index.d.ts +39 -0
- package/dist/dts/index.d.ts.map +1 -0
- package/dist/esm/Hooks.js +200 -0
- package/dist/esm/Hooks.js.map +1 -0
- package/dist/esm/ReactHydration.js +68 -0
- package/dist/esm/ReactHydration.js.map +1 -0
- package/dist/esm/RegistryContext.js +55 -0
- package/dist/esm/RegistryContext.js.map +1 -0
- package/dist/esm/index.js +39 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +4 -0
- package/package.json +60 -0
- package/src/Hooks.ts +341 -0
- package/src/ReactHydration.ts +84 -0
- package/src/RegistryContext.ts +64 -0
- package/src/index.ts +45 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Hooks.d.ts","sourceRoot":"","sources":["../../src/Hooks.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAA;AAC9C,OAAO,KAAK,KAAK,OAAO,MAAM,2BAA2B,CAAA;AAEzD,OAAO,KAAK,KAAK,M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AM,UAAU,GAAI,CAAC,EAAE,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,KAAG,CAI3D,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAC1D,CAAA;AAElD;;;GAGG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAAG,CAAC,CAAC,CAAC,CACzD,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as Hydration from "@effect-atom/atom/Hydration";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
/**
|
|
4
|
+
* @since 1.0.0
|
|
5
|
+
* @category components
|
|
6
|
+
*/
|
|
7
|
+
export interface HydrationBoundaryProps {
|
|
8
|
+
state?: Iterable<Hydration.DehydratedAtom>;
|
|
9
|
+
children?: React.ReactNode;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* @since 1.0.0
|
|
13
|
+
* @category components
|
|
14
|
+
*/
|
|
15
|
+
export declare const HydrationBoundary: React.FC<HydrationBoundaryProps>;
|
|
16
|
+
//# sourceMappingURL=ReactHydration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReactHydration.d.ts","sourceRoot":"","sources":["../../src/ReactHydration.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,SAAS,MAAM,6BAA6B,CAAA;AACxD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAG9B;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACrC,KAAK,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,cAAc,CAAC,CAAA;IAC1C,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;CAC3B;AAED;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,KAAK,CAAC,EAAE,CAAC,sBAAsB,CA8D9D,CAAA"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type * as Atom from "@effect-atom/atom/Atom";
|
|
2
|
+
import * as Registry from "@effect-atom/atom/Registry";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
/**
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
* @category context
|
|
7
|
+
*/
|
|
8
|
+
export declare function scheduleTask(f: () => void): void;
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category context
|
|
12
|
+
*/
|
|
13
|
+
export declare const RegistryContext: React.Context<Registry.Registry>;
|
|
14
|
+
/**
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
* @category context
|
|
17
|
+
*/
|
|
18
|
+
export declare const RegistryProvider: (options: {
|
|
19
|
+
readonly children?: React.ReactNode | undefined;
|
|
20
|
+
readonly initialValues?: Iterable<readonly [Atom.Atom<any>, any]> | undefined;
|
|
21
|
+
readonly scheduleTask?: ((f: () => void) => void) | undefined;
|
|
22
|
+
readonly timeoutResolution?: number | undefined;
|
|
23
|
+
readonly defaultIdleTTL?: number | undefined;
|
|
24
|
+
}) => React.FunctionComponentElement<React.ProviderProps<Registry.Registry>>;
|
|
25
|
+
//# sourceMappingURL=RegistryContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RegistryContext.d.ts","sourceRoot":"","sources":["../../src/RegistryContext.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAA;AACnD,OAAO,KAAK,QAAQ,MAAM,4BAA4B,CAAA;AACtD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAG9B;;;GAGG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,IAAI,GAAG,IAAI,CAEhD;AAED;;;GAGG;AACH,eAAO,MAAM,eAAe,kCAGzB,CAAA;AAEH;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAS;IACxC,QAAQ,CAAC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,GAAG,SAAS,CAAA;IAC/C,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,IAAI,CAAC,GAAG,SAAS,CAAA;IAC7D,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/C,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC7C,2EA2BA,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
* @category re-exports
|
|
7
|
+
*/
|
|
8
|
+
export * as Atom from "@effect-atom/atom/Atom";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category re-exports
|
|
12
|
+
*/
|
|
13
|
+
export * as Registry from "@effect-atom/atom/Registry";
|
|
14
|
+
/**
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
* @category re-exports
|
|
17
|
+
*/
|
|
18
|
+
export * as Result from "@effect-atom/atom/Result";
|
|
19
|
+
/**
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
* @category re-exports
|
|
22
|
+
*/
|
|
23
|
+
export * as AtomRef from "@effect-atom/atom/AtomRef";
|
|
24
|
+
/**
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
* @category re-exports
|
|
27
|
+
*/
|
|
28
|
+
export * as Hydration from "@effect-atom/atom/Hydration";
|
|
29
|
+
/**
|
|
30
|
+
* @since 1.0.0
|
|
31
|
+
* @category hooks
|
|
32
|
+
*/
|
|
33
|
+
export * from "./Hooks.js";
|
|
34
|
+
/**
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
* @category context
|
|
37
|
+
*/
|
|
38
|
+
export * from "./RegistryContext.js";
|
|
39
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAA;AAE9C;;;GAGG;AACH,OAAO,KAAK,QAAQ,MAAM,4BAA4B,CAAA;AAEtD;;;GAGG;AACH,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAA;AAElD;;;GAGG;AACH,OAAO,KAAK,OAAO,MAAM,2BAA2B,CAAA;AAEpD;;;GAGG;AACH,OAAO,KAAK,SAAS,MAAM,6BAA6B,CAAA;AAExD;;;GAGG;AACH,cAAc,YAAY,CAAA;AAE1B;;;GAGG;AACH,cAAc,sBAAsB,CAAA"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
"use client";
|
|
5
|
+
|
|
6
|
+
import * as Atom from "@effect-atom/atom/Atom";
|
|
7
|
+
import * as Registry from "@effect-atom/atom/Registry";
|
|
8
|
+
import { Effect } from "effect";
|
|
9
|
+
import * as Cause from "effect/Cause";
|
|
10
|
+
import * as Exit from "effect/Exit";
|
|
11
|
+
import { globalValue } from "effect/GlobalValue";
|
|
12
|
+
import * as React from "react";
|
|
13
|
+
import { RegistryContext } from "./RegistryContext.js";
|
|
14
|
+
const storeRegistry = /*#__PURE__*/globalValue("@effect-atom/atom-react/storeRegistry", () => new WeakMap());
|
|
15
|
+
function makeStore(registry, atom) {
|
|
16
|
+
let stores = storeRegistry.get(registry);
|
|
17
|
+
if (stores === undefined) {
|
|
18
|
+
stores = new WeakMap();
|
|
19
|
+
storeRegistry.set(registry, stores);
|
|
20
|
+
}
|
|
21
|
+
const store = stores.get(atom);
|
|
22
|
+
if (store !== undefined) {
|
|
23
|
+
return store;
|
|
24
|
+
}
|
|
25
|
+
const newStore = {
|
|
26
|
+
subscribe(f) {
|
|
27
|
+
return registry.subscribe(atom, f);
|
|
28
|
+
},
|
|
29
|
+
snapshot() {
|
|
30
|
+
return registry.get(atom);
|
|
31
|
+
},
|
|
32
|
+
getServerSnapshot() {
|
|
33
|
+
return Atom.getServerValue(atom, registry);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
stores.set(atom, newStore);
|
|
37
|
+
return newStore;
|
|
38
|
+
}
|
|
39
|
+
function useStore(registry, atom) {
|
|
40
|
+
const store = makeStore(registry, atom);
|
|
41
|
+
return React.useSyncExternalStore(store.subscribe, store.snapshot, store.getServerSnapshot);
|
|
42
|
+
}
|
|
43
|
+
const initialValuesSet = /*#__PURE__*/globalValue("@effect-atom/atom-react/initialValuesSet", () => new WeakMap());
|
|
44
|
+
/**
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
* @category hooks
|
|
47
|
+
*/
|
|
48
|
+
export const useAtomInitialValues = initialValues => {
|
|
49
|
+
const registry = React.useContext(RegistryContext);
|
|
50
|
+
let set = initialValuesSet.get(registry);
|
|
51
|
+
if (set === undefined) {
|
|
52
|
+
set = new WeakSet();
|
|
53
|
+
initialValuesSet.set(registry, set);
|
|
54
|
+
}
|
|
55
|
+
for (const [atom, value] of initialValues) {
|
|
56
|
+
if (!set.has(atom)) {
|
|
57
|
+
set.add(atom);
|
|
58
|
+
registry.ensureNode(atom).setValue(value);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* @since 1.0.0
|
|
64
|
+
* @category hooks
|
|
65
|
+
*/
|
|
66
|
+
export const useAtomValue = (atom, f) => {
|
|
67
|
+
const registry = React.useContext(RegistryContext);
|
|
68
|
+
if (f) {
|
|
69
|
+
const atomB = React.useMemo(() => Atom.map(atom, f), [atom, f]);
|
|
70
|
+
return useStore(registry, atomB);
|
|
71
|
+
}
|
|
72
|
+
return useStore(registry, atom);
|
|
73
|
+
};
|
|
74
|
+
function mountAtom(registry, atom) {
|
|
75
|
+
React.useEffect(() => registry.mount(atom), [atom, registry]);
|
|
76
|
+
}
|
|
77
|
+
function setAtom(registry, atom, options) {
|
|
78
|
+
if (options?.mode === "promise" || options?.mode === "promiseExit") {
|
|
79
|
+
return React.useCallback((value, opts) => {
|
|
80
|
+
registry.set(atom, value);
|
|
81
|
+
const promise = Effect.runPromiseExit(Registry.getResult(registry, atom, {
|
|
82
|
+
suspendOnWaiting: true
|
|
83
|
+
}), opts);
|
|
84
|
+
return options.mode === "promise" ? promise.then(flattenExit) : promise;
|
|
85
|
+
}, [registry, atom, options.mode]);
|
|
86
|
+
}
|
|
87
|
+
return React.useCallback(value => {
|
|
88
|
+
registry.set(atom, typeof value === "function" ? value(registry.get(atom)) : value);
|
|
89
|
+
}, [registry, atom]);
|
|
90
|
+
}
|
|
91
|
+
const flattenExit = exit => {
|
|
92
|
+
if (Exit.isSuccess(exit)) return exit.value;
|
|
93
|
+
throw Cause.squash(exit.cause);
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* @since 1.0.0
|
|
97
|
+
* @category hooks
|
|
98
|
+
*/
|
|
99
|
+
export const useAtomMount = atom => {
|
|
100
|
+
const registry = React.useContext(RegistryContext);
|
|
101
|
+
mountAtom(registry, atom);
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* @since 1.0.0
|
|
105
|
+
* @category hooks
|
|
106
|
+
*/
|
|
107
|
+
export const useAtomSet = (atom, options) => {
|
|
108
|
+
const registry = React.useContext(RegistryContext);
|
|
109
|
+
mountAtom(registry, atom);
|
|
110
|
+
return setAtom(registry, atom, options);
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* @since 1.0.0
|
|
114
|
+
* @category hooks
|
|
115
|
+
*/
|
|
116
|
+
export const useAtomRefresh = atom => {
|
|
117
|
+
const registry = React.useContext(RegistryContext);
|
|
118
|
+
mountAtom(registry, atom);
|
|
119
|
+
return React.useCallback(() => {
|
|
120
|
+
registry.refresh(atom);
|
|
121
|
+
}, [registry, atom]);
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* @since 1.0.0
|
|
125
|
+
* @category hooks
|
|
126
|
+
*/
|
|
127
|
+
export const useAtom = (atom, options) => {
|
|
128
|
+
const registry = React.useContext(RegistryContext);
|
|
129
|
+
return [useStore(registry, atom), setAtom(registry, atom, options)];
|
|
130
|
+
};
|
|
131
|
+
const atomPromiseMap = /*#__PURE__*/globalValue("@effect-atom/atom-react/atomPromiseMap", () => ({
|
|
132
|
+
suspendOnWaiting: new Map(),
|
|
133
|
+
default: new Map()
|
|
134
|
+
}));
|
|
135
|
+
function atomToPromise(registry, atom, suspendOnWaiting) {
|
|
136
|
+
const map = suspendOnWaiting ? atomPromiseMap.suspendOnWaiting : atomPromiseMap.default;
|
|
137
|
+
let promise = map.get(atom);
|
|
138
|
+
if (promise !== undefined) {
|
|
139
|
+
return promise;
|
|
140
|
+
}
|
|
141
|
+
promise = new Promise(resolve => {
|
|
142
|
+
const dispose = registry.subscribe(atom, result => {
|
|
143
|
+
if (result._tag === "Initial" || suspendOnWaiting && result.waiting) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
setTimeout(dispose, 1000);
|
|
147
|
+
resolve();
|
|
148
|
+
map.delete(atom);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
map.set(atom, promise);
|
|
152
|
+
return promise;
|
|
153
|
+
}
|
|
154
|
+
function atomResultOrSuspend(registry, atom, suspendOnWaiting) {
|
|
155
|
+
const value = useStore(registry, atom);
|
|
156
|
+
if (value._tag === "Initial" || suspendOnWaiting && value.waiting) {
|
|
157
|
+
throw atomToPromise(registry, atom, suspendOnWaiting);
|
|
158
|
+
}
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* @since 1.0.0
|
|
163
|
+
* @category hooks
|
|
164
|
+
*/
|
|
165
|
+
export const useAtomSuspense = (atom, options) => {
|
|
166
|
+
const registry = React.useContext(RegistryContext);
|
|
167
|
+
const result = atomResultOrSuspend(registry, atom, options?.suspendOnWaiting ?? false);
|
|
168
|
+
if (result._tag === "Failure" && !options?.includeFailure) {
|
|
169
|
+
throw Cause.squash(result.cause);
|
|
170
|
+
}
|
|
171
|
+
return result;
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* @since 1.0.0
|
|
175
|
+
* @category hooks
|
|
176
|
+
*/
|
|
177
|
+
export const useAtomSubscribe = (atom, f, options) => {
|
|
178
|
+
const registry = React.useContext(RegistryContext);
|
|
179
|
+
React.useEffect(() => registry.subscribe(atom, f, options), [registry, atom, f, options?.immediate]);
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* @since 1.0.0
|
|
183
|
+
* @category hooks
|
|
184
|
+
*/
|
|
185
|
+
export const useAtomRef = ref => {
|
|
186
|
+
const [, setValue] = React.useState(ref.value);
|
|
187
|
+
React.useEffect(() => ref.subscribe(setValue), [ref]);
|
|
188
|
+
return ref.value;
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* @since 1.0.0
|
|
192
|
+
* @category hooks
|
|
193
|
+
*/
|
|
194
|
+
export const useAtomRefProp = (ref, prop) => React.useMemo(() => ref.prop(prop), [ref, prop]);
|
|
195
|
+
/**
|
|
196
|
+
* @since 1.0.0
|
|
197
|
+
* @category hooks
|
|
198
|
+
*/
|
|
199
|
+
export const useAtomRefPropValue = (ref, prop) => useAtomRef(useAtomRefProp(ref, prop));
|
|
200
|
+
//# sourceMappingURL=Hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Hooks.js","names":["Atom","Registry","Effect","Cause","Exit","globalValue","React","RegistryContext","storeRegistry","WeakMap","makeStore","registry","atom","stores","get","undefined","set","store","newStore","subscribe","f","snapshot","getServerSnapshot","getServerValue","useStore","useSyncExternalStore","initialValuesSet","useAtomInitialValues","initialValues","useContext","WeakSet","value","has","add","ensureNode","setValue","useAtomValue","atomB","useMemo","map","mountAtom","useEffect","mount","setAtom","options","mode","useCallback","opts","promise","runPromiseExit","getResult","suspendOnWaiting","then","flattenExit","exit","isSuccess","squash","cause","useAtomMount","useAtomSet","useAtomRefresh","refresh","useAtom","atomPromiseMap","Map","default","atomToPromise","Promise","resolve","dispose","result","_tag","waiting","setTimeout","delete","atomResultOrSuspend","useAtomSuspense","includeFailure","useAtomSubscribe","immediate","useAtomRef","ref","useState","useAtomRefProp","prop","useAtomRefPropValue"],"sources":["../../src/Hooks.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,YAAY;;AAEZ,OAAO,KAAKA,IAAI,MAAM,wBAAwB;AAE9C,OAAO,KAAKC,QAAQ,MAAM,4BAA4B;AAEtD,SAASC,MAAM,QAAQ,QAAQ;AAC/B,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,IAAI,MAAM,aAAa;AACnC,SAASC,WAAW,QAAQ,oBAAoB;AAChD,OAAO,KAAKC,KAAK,MAAM,OAAO;AAC9B,SAASC,eAAe,QAAQ,sBAAsB;AAQtD,MAAMC,aAAa,gBAAGH,WAAW,CAC/B,uCAAuC,EACvC,MAAM,IAAII,OAAO,EAA8D,CAChF;AAED,SAASC,SAASA,CAAIC,QAA2B,EAAEC,IAAkB;EACnE,IAAIC,MAAM,GAAGL,aAAa,CAACM,GAAG,CAACH,QAAQ,CAAC;EACxC,IAAIE,MAAM,KAAKE,SAAS,EAAE;IACxBF,MAAM,GAAG,IAAIJ,OAAO,EAAE;IACtBD,aAAa,CAACQ,GAAG,CAACL,QAAQ,EAAEE,MAAM,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K,CAACuB,UAAU,CAACtB,eAAe,CAAC;EAClDD,KAAK,CAACmC,SAAS,CACb,MAAM9B,QAAQ,CAACQ,SAAS,CAACP,IAAI,EAAEQ,CAAC,EAAEwB,OAAO,CAAC,EAC1C,CAACjC,QAAQ,EAAEC,IAAI,EAAEQ,CAAC,EAAEwB,OAAO,EAAEmC,SAAS,CAAC,CACxC;AACH,CAAC;AAED;;;;AAIA,OAAO,MAAMC,UAAU,GAAOC,GAA2B,IAAO;EAC9D,MAAM,GAAG9C,QAAQ,CAAC,GAAG7B,KAAK,CAAC4E,QAAQ,CAACD,GAAG,CAAClD,KAAK,CAAC;EAC9CzB,KAAK,CAACmC,SAAS,CAAC,MAAMwC,GAAG,CAAC9D,SAAS,CAACgB,QAAQ,CAAC,EAAE,CAAC8C,GAAG,CAAC,CAAC;EACrD,OAAOA,GAAG,CAAClD,KAAK;AAClB,CAAC;AAED;;;;AAIA,OAAO,MAAMoD,cAAc,GAAGA,CAAuBF,GAAuB,EAAEG,IAAO,KACnF9E,KAAK,CAACgC,OAAO,CAAC,MAAM2C,GAAG,CAACG,IAAI,CAACA,IAAI,CAAC,EAAE,CAACH,GAAG,EAAEG,IAAI,CAAC,CAAC;AAElD;;;;AAIA,OAAO,MAAMC,mBAAmB,GAAGA,CAAuBJ,GAAuB,EAAEG,IAAO,KACxFJ,UAAU,CAACG,cAAc,CAACF,GAAG,EAAEG,IAAI,CAAC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
"use client";
|
|
5
|
+
|
|
6
|
+
import * as Hydration from "@effect-atom/atom/Hydration";
|
|
7
|
+
import * as React from "react";
|
|
8
|
+
import { RegistryContext } from "./RegistryContext.js";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category components
|
|
12
|
+
*/
|
|
13
|
+
export const HydrationBoundary = ({
|
|
14
|
+
children,
|
|
15
|
+
state
|
|
16
|
+
}) => {
|
|
17
|
+
const registry = React.useContext(RegistryContext);
|
|
18
|
+
// This useMemo is for performance reasons only, everything inside it must
|
|
19
|
+
// be safe to run in every render and code here should be read as "in render".
|
|
20
|
+
//
|
|
21
|
+
// This code needs to happen during the render phase, because after initial
|
|
22
|
+
// SSR, hydration needs to happen _before_ children render. Also, if hydrating
|
|
23
|
+
// during a transition, we want to hydrate as much as is safe in render so
|
|
24
|
+
// we can prerender as much as possible.
|
|
25
|
+
//
|
|
26
|
+
// For any Atom values that already exist in the registry, we want to hold back on
|
|
27
|
+
// hydrating until _after_ the render phase. The reason for this is that during
|
|
28
|
+
// transitions, we don't want the existing Atom values and subscribers to update to
|
|
29
|
+
// the new data on the current page, only _after_ the transition is committed.
|
|
30
|
+
// If the transition is aborted, we will have hydrated any _new_ Atom values, but
|
|
31
|
+
// we throw away the fresh data for any existing ones to avoid unexpectedly
|
|
32
|
+
// updating the UI.
|
|
33
|
+
const hydrationQueue = React.useMemo(() => {
|
|
34
|
+
if (state) {
|
|
35
|
+
const dehydratedAtoms = Array.from(state);
|
|
36
|
+
const nodes = registry.getNodes();
|
|
37
|
+
const newDehydratedAtoms = [];
|
|
38
|
+
const existingDehydratedAtoms = [];
|
|
39
|
+
for (const dehydratedAtom of dehydratedAtoms) {
|
|
40
|
+
const existingNode = nodes.get(dehydratedAtom.key);
|
|
41
|
+
if (!existingNode) {
|
|
42
|
+
// This is a new Atom value, safe to hydrate immediately
|
|
43
|
+
newDehydratedAtoms.push(dehydratedAtom);
|
|
44
|
+
} else {
|
|
45
|
+
// This Atom value already exists, queue it for later hydration
|
|
46
|
+
// TODO: Add logic to check if hydration data is newer
|
|
47
|
+
existingDehydratedAtoms.push(dehydratedAtom);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (newDehydratedAtoms.length > 0) {
|
|
51
|
+
// It's actually fine to call this with state that already exists
|
|
52
|
+
// in the registry, or is older. hydrate() is idempotent.
|
|
53
|
+
Hydration.hydrate(registry, newDehydratedAtoms);
|
|
54
|
+
}
|
|
55
|
+
if (existingDehydratedAtoms.length > 0) {
|
|
56
|
+
return existingDehydratedAtoms;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
}, [registry, state]);
|
|
61
|
+
React.useEffect(() => {
|
|
62
|
+
if (hydrationQueue) {
|
|
63
|
+
Hydration.hydrate(registry, hydrationQueue);
|
|
64
|
+
}
|
|
65
|
+
}, [registry, hydrationQueue]);
|
|
66
|
+
return React.createElement(React.Fragment, {}, children);
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=ReactHydration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReactHydration.js","names":["Hydration","React","RegistryContext","HydrationBoundary","children","state","registry","useContext","hydrationQueue","useMemo","dehydratedAtoms","Array","from","nodes","getNodes","newDehydratedAtoms","existingDehydratedAtoms","dehydratedAtom","existingNode","get","key","push","length","hydrate","undefined","useEffect","createElement","Fragment"],"sources":["../../src/ReactHydration.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,YAAY;;AACZ,OAAO,KAAKA,SAAS,MAAM,6BAA6B;AACxD,OAAO,KAAKC,KAAK,MAAM,OAAO;AAC9B,SAASC,eAAe,QAAQ,sBAAsB;AAWtD;;;;AAIA,OAAO,MAAMC,iBAAiB,GAAqCA,CAAC;EAClEC,QAAQ;EACRC;AAAK,CACN,KAAI;EACH,MAAMC,QAAQ,GAAGL,KAAK,CAACM,UAAU,CAACL,eAAe,CAAC;EAElD;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAMM,cAAc,GAAgDP,KAAK,CAACQ,OAAO,CAAC,MAAK;IACrF,IAAIJ,KAAK,EAAE;MACT,MAAMK,eAAe,GAAGC,KAAK,CAACC,IAAI,CAACP,KAAK,CAAC;MACzC,MAAMQ,KAAK,GAAGP,QAAQ,CAACQ,QAAQ,EAAE;MAEjC,MAAMC,kBAAkB,GAAoC,EAAE;MAC9D,MAAMC,uBAAuB,GAAoC,EAAE;MAEnE,KAAK,MAAMC,cAAc,IAAIP,eAAe,EAAE;QAC5C,MAAMQ,YAAY,GAAGL,KAAK,CAACM,GAAG,CAACF,cAAc,CAACG,GAAG,CAAC;QAElD,IAAI,CAACF,YAAY,EAAE;UACjB;UACAH,kBAAkB,CAACM,IAAI,CAACJ,cAAc,CAAC;QACzC,CAAC,MAAM;UACL;UACA;UACAD,uBAAuB,CAACK,IAAI,CAACJ,cAAc,CAAC;QAC9C;MACF;MAEA,IAAIF,kBAAkB,CAACO,MAAM,GAAG,CAAC,EAAE;QACjC;QACA;QACAtB,SAAS,CAACuB,OAAO,CAACjB,QAAQ,EAAES,kBAAkB,CAAC;MACjD;MAEA,IAAIC,uBAAuB,CAACM,MAAM,GAAG,CAAC,EAAE;QACtC,OAAON,uBAAuB;MAChC;IACF;IACA,OAAOQ,SAAS;EAClB,CAAC,EAAE,CAAClB,QAAQ,EAAED,KAAK,CAAC,CAAC;EAErBJ,KAAK,CAACwB,SAAS,CAAC,MAAK;IACnB,IAAIjB,cAAc,EAAE;MAClBR,SAAS,CAACuB,OAAO,CAACjB,QAAQ,EAAEE,cAAc,CAAC;IAC7C;EACF,CAAC,EAAE,CAACF,QAAQ,EAAEE,cAAc,CAAC,CAAC;EAE9B,OAAOP,KAAK,CAACyB,aAAa,CAACzB,KAAK,CAAC0B,QAAQ,EAAE,EAAE,EAAEvB,QAAQ,CAAC;AAC1D,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
"use client";
|
|
5
|
+
|
|
6
|
+
import * as Registry from "@effect-atom/atom/Registry";
|
|
7
|
+
import * as React from "react";
|
|
8
|
+
import * as Scheduler from "scheduler";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category context
|
|
12
|
+
*/
|
|
13
|
+
export function scheduleTask(f) {
|
|
14
|
+
Scheduler.unstable_scheduleCallback(Scheduler.unstable_LowPriority, f);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
* @category context
|
|
19
|
+
*/
|
|
20
|
+
export const RegistryContext = /*#__PURE__*/React.createContext(/*#__PURE__*/Registry.make({
|
|
21
|
+
scheduleTask,
|
|
22
|
+
defaultIdleTTL: 400
|
|
23
|
+
}));
|
|
24
|
+
/**
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
* @category context
|
|
27
|
+
*/
|
|
28
|
+
export const RegistryProvider = options => {
|
|
29
|
+
const ref = React.useRef(null);
|
|
30
|
+
if (ref.current === null) {
|
|
31
|
+
ref.current = {
|
|
32
|
+
registry: Registry.make({
|
|
33
|
+
scheduleTask: options.scheduleTask ?? scheduleTask,
|
|
34
|
+
initialValues: options.initialValues,
|
|
35
|
+
timeoutResolution: options.timeoutResolution,
|
|
36
|
+
defaultIdleTTL: options.defaultIdleTTL
|
|
37
|
+
})
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
React.useEffect(() => {
|
|
41
|
+
if (ref.current?.timeout !== undefined) {
|
|
42
|
+
clearTimeout(ref.current.timeout);
|
|
43
|
+
}
|
|
44
|
+
return () => {
|
|
45
|
+
ref.current.timeout = setTimeout(() => {
|
|
46
|
+
ref.current?.registry.dispose();
|
|
47
|
+
ref.current = null;
|
|
48
|
+
}, 500);
|
|
49
|
+
};
|
|
50
|
+
}, [ref]);
|
|
51
|
+
return React.createElement(RegistryContext.Provider, {
|
|
52
|
+
value: ref.current.registry
|
|
53
|
+
}, options?.children);
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=RegistryContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RegistryContext.js","names":["Registry","React","Scheduler","scheduleTask","f","unstable_scheduleCallback","unstable_LowPriority","RegistryContext","createContext","make","defaultIdleTTL","RegistryProvider","options","ref","useRef","current","registry","initialValues","timeoutResolution","useEffect","timeout","undefined","clearTimeout","setTimeout","dispose","createElement","Provider","value","children"],"sources":["../../src/RegistryContext.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,YAAY;;AAEZ,OAAO,KAAKA,QAAQ,MAAM,4BAA4B;AACtD,OAAO,KAAKC,KAAK,MAAM,OAAO;AAC9B,OAAO,KAAKC,SAAS,MAAM,WAAW;AAEtC;;;;AAIA,OAAM,SAAUC,YAAYA,CAACC,CAAa;EACxCF,SAAS,CAACG,yBAAyB,CAACH,SAAS,CAACI,oBAAoB,EAAEF,CAAC,CAAC;AACxE;AAEA;;;;AAIA,OAAO,MAAMG,eAAe,gBAAGN,KAAK,CAACO,aAAa,cAAoBR,QAAQ,CAACS,IAAI,CAAC;EAClFN,YAAY;EACZO,cAAc,EAAE;CACjB,CAAC,CAAC;AAEH;;;;AAIA,OAAO,MAAMC,gBAAgB,GAAIC,OAMhC,IAAI;EACH,MAAMC,GAAG,GAAGZ,KAAK,CAACa,MAAM,CAGrB,IAAI,CAAC;EACR,IAAID,GAAG,CAACE,OAAO,KAAK,IAAI,EAAE;IACxBF,GAAG,CAACE,OAAO,GAAG;MACZC,QAAQ,EAAEhB,QAAQ,CAACS,IAAI,CAAC;QACtBN,YAAY,EAAES,OAAO,CAACT,YAAY,IAAIA,YAAY;QAClDc,aAAa,EAAEL,OAAO,CAACK,aAAa;QACpCC,iBAAiB,EAAEN,OAAO,CAACM,iBAAiB;QAC5CR,cAAc,EAAEE,OAAO,CAACF;OACzB;KACF;EACH;EACAT,KAAK,CAACkB,SAAS,CAAC,MAAK;IACnB,IAAIN,GAAG,CAACE,OAAO,EAAEK,OAAO,KAAKC,SAAS,EAAE;MACtCC,YAAY,CAACT,GAAG,CAACE,OAAO,CAACK,OAAO,CAAC;IACnC;IACA,OAAO,MAAK;MACVP,GAAG,CAACE,OAAQ,CAACK,OAAO,GAAGG,UAAU,CAAC,MAAK;QACrCV,GAAG,CAACE,OAAO,EAAEC,QAAQ,CAACQ,OAAO,EAAE;QAC/BX,GAAG,CAACE,OAAO,GAAG,IAAI;MACpB,CAAC,EAAE,GAAG,CAAC;IACT,CAAC;EACH,CAAC,EAAE,CAACF,GAAG,CAAC,CAAC;EACT,OAAOZ,KAAK,CAACwB,aAAa,CAAClB,eAAe,CAACmB,QAAQ,EAAE;IAAEC,KAAK,EAAEd,GAAG,CAACE,OAAO,CAACC;EAAQ,CAAE,EAAEJ,OAAO,EAAEgB,QAAQ,CAAC;AAC1G,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
* @category re-exports
|
|
7
|
+
*/
|
|
8
|
+
export * as Atom from "@effect-atom/atom/Atom";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category re-exports
|
|
12
|
+
*/
|
|
13
|
+
export * as Registry from "@effect-atom/atom/Registry";
|
|
14
|
+
/**
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
* @category re-exports
|
|
17
|
+
*/
|
|
18
|
+
export * as Result from "@effect-atom/atom/Result";
|
|
19
|
+
/**
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
* @category re-exports
|
|
22
|
+
*/
|
|
23
|
+
export * as AtomRef from "@effect-atom/atom/AtomRef";
|
|
24
|
+
/**
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
* @category re-exports
|
|
27
|
+
*/
|
|
28
|
+
export * as Hydration from "@effect-atom/atom/Hydration";
|
|
29
|
+
/**
|
|
30
|
+
* @since 1.0.0
|
|
31
|
+
* @category hooks
|
|
32
|
+
*/
|
|
33
|
+
export * from "./Hooks.js";
|
|
34
|
+
/**
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
* @category context
|
|
37
|
+
*/
|
|
38
|
+
export * from "./RegistryContext.js";
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["Atom","Registry","Result","AtomRef","Hydration"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA;;;;AAIA,OAAO,KAAKA,IAAI,MAAM,wBAAwB;AAE9C;;;;AAIA,OAAO,KAAKC,QAAQ,MAAM,4BAA4B;AAEtD;;;;AAIA,OAAO,KAAKC,MAAM,MAAM,0BAA0B;AAElD;;;;AAIA,OAAO,KAAKC,OAAO,MAAM,2BAA2B;AAEpD;;;;AAIA,OAAO,KAAKC,SAAS,MAAM,6BAA6B;AAExD;;;;AAIA,cAAc,YAAY;AAE1B;;;;AAIA,cAAc,sBAAsB","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect-atom/atom-react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Reactive toolkit for Effect",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/tim-smart/effect-atom.git"
|
|
9
|
+
},
|
|
10
|
+
"sideEffects": [],
|
|
11
|
+
"author": "Effect contributors",
|
|
12
|
+
"homepage": "https://github.com/tim-smart/effect-atom",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@effect-atom/atom": "^0.1.0"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"effect": "^3.17",
|
|
18
|
+
"react": ">=18 <20",
|
|
19
|
+
"scheduler": "*"
|
|
20
|
+
},
|
|
21
|
+
"main": "./dist/cjs/index.js",
|
|
22
|
+
"module": "./dist/esm/index.js",
|
|
23
|
+
"types": "./dist/dts/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
"./package.json": "./package.json",
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/dts/index.d.ts",
|
|
28
|
+
"import": "./dist/esm/index.js",
|
|
29
|
+
"default": "./dist/cjs/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./Hooks": {
|
|
32
|
+
"types": "./dist/dts/Hooks.d.ts",
|
|
33
|
+
"import": "./dist/esm/Hooks.js",
|
|
34
|
+
"default": "./dist/cjs/Hooks.js"
|
|
35
|
+
},
|
|
36
|
+
"./ReactHydration": {
|
|
37
|
+
"types": "./dist/dts/ReactHydration.d.ts",
|
|
38
|
+
"import": "./dist/esm/ReactHydration.js",
|
|
39
|
+
"default": "./dist/cjs/ReactHydration.js"
|
|
40
|
+
},
|
|
41
|
+
"./RegistryContext": {
|
|
42
|
+
"types": "./dist/dts/RegistryContext.d.ts",
|
|
43
|
+
"import": "./dist/esm/RegistryContext.js",
|
|
44
|
+
"default": "./dist/cjs/RegistryContext.js"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"typesVersions": {
|
|
48
|
+
"*": {
|
|
49
|
+
"Hooks": [
|
|
50
|
+
"./dist/dts/Hooks.d.ts"
|
|
51
|
+
],
|
|
52
|
+
"ReactHydration": [
|
|
53
|
+
"./dist/dts/ReactHydration.d.ts"
|
|
54
|
+
],
|
|
55
|
+
"RegistryContext": [
|
|
56
|
+
"./dist/dts/RegistryContext.d.ts"
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|