@effect/atom-vue 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/index.d.ts +80 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +127 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
- package/src/index.ts +211 -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/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import * as Exit from "effect/Exit";
|
|
2
|
+
import type * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
|
3
|
+
import type * as Atom from "effect/unstable/reactivity/Atom";
|
|
4
|
+
import type * as AtomRef from "effect/unstable/reactivity/AtomRef";
|
|
5
|
+
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
|
|
6
|
+
import { type InjectionKey, type Ref } from "vue";
|
|
7
|
+
/**
|
|
8
|
+
* @since 1.0.0
|
|
9
|
+
* @category modules
|
|
10
|
+
*/
|
|
11
|
+
export * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
* @category modules
|
|
15
|
+
*/
|
|
16
|
+
export * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
|
17
|
+
/**
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
* @category modules
|
|
20
|
+
*/
|
|
21
|
+
export * as Atom from "effect/unstable/reactivity/Atom";
|
|
22
|
+
/**
|
|
23
|
+
* @since 1.0.0
|
|
24
|
+
* @category modules
|
|
25
|
+
*/
|
|
26
|
+
export * as AtomRef from "effect/unstable/reactivity/AtomRef";
|
|
27
|
+
/**
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
* @category re-exports
|
|
30
|
+
*/
|
|
31
|
+
export * as AtomHttpApi from "effect/unstable/reactivity/AtomHttpApi";
|
|
32
|
+
/**
|
|
33
|
+
* @since 1.0.0
|
|
34
|
+
* @category modules
|
|
35
|
+
*/
|
|
36
|
+
export * as AtomRpc from "effect/unstable/reactivity/AtomRpc";
|
|
37
|
+
/**
|
|
38
|
+
* @since 1.0.0
|
|
39
|
+
* @category registry
|
|
40
|
+
*/
|
|
41
|
+
export declare const registryKey: InjectionKey<AtomRegistry.AtomRegistry>;
|
|
42
|
+
/**
|
|
43
|
+
* @since 1.0.0
|
|
44
|
+
* @category registry
|
|
45
|
+
*/
|
|
46
|
+
export declare const defaultRegistry: AtomRegistry.AtomRegistry;
|
|
47
|
+
/**
|
|
48
|
+
* @since 1.0.0
|
|
49
|
+
* @category registry
|
|
50
|
+
*/
|
|
51
|
+
export declare const injectRegistry: () => AtomRegistry.AtomRegistry;
|
|
52
|
+
/**
|
|
53
|
+
* @since 1.0.0
|
|
54
|
+
* @category composables
|
|
55
|
+
*/
|
|
56
|
+
export declare const useAtom: <R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: () => Atom.Writable<R, W>, options?: {
|
|
57
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined;
|
|
58
|
+
}) => readonly [Readonly<Ref<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)];
|
|
59
|
+
/**
|
|
60
|
+
* @since 1.0.0
|
|
61
|
+
* @category composables
|
|
62
|
+
*/
|
|
63
|
+
export declare const useAtomValue: <A>(atom: () => Atom.Atom<A>) => Readonly<Ref<A>>;
|
|
64
|
+
/**
|
|
65
|
+
* @since 1.0.0
|
|
66
|
+
* @category composables
|
|
67
|
+
*/
|
|
68
|
+
export declare const useAtomSet: <R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: () => Atom.Writable<R, W>, options?: {
|
|
69
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined;
|
|
70
|
+
}) => "promise" extends Mode ? ((value: W, options?: {
|
|
71
|
+
readonly signal?: AbortSignal | undefined;
|
|
72
|
+
} | undefined) => Promise<AsyncResult.AsyncResult.Success<R>>) : "promiseExit" extends Mode ? ((value: W, options?: {
|
|
73
|
+
readonly signal?: AbortSignal | undefined;
|
|
74
|
+
} | undefined) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>) : ((value: W | ((value: R) => W)) => void);
|
|
75
|
+
/**
|
|
76
|
+
* @since 1.0.0
|
|
77
|
+
* @category composables
|
|
78
|
+
*/
|
|
79
|
+
export declare const useAtomRef: <A>(atomRef: () => AtomRef.ReadonlyRef<A>) => Readonly<Ref<A>>;
|
|
80
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,IAAI,MAAM,aAAa,CAAA;AACnC,OAAO,KAAK,KAAK,WAAW,MAAM,wCAAwC,CAAA;AAC1E,OAAO,KAAK,KAAK,IAAI,MAAM,iCAAiC,CAAA;AAC5D,OAAO,KAAK,KAAK,OAAO,MAAM,oCAAoC,CAAA;AAClE,OAAO,KAAK,YAAY,MAAM,yCAAyC,CAAA;AACvE,OAAO,EAAsC,KAAK,YAAY,EAAE,KAAK,GAAG,EAA2B,MAAM,KAAK,CAAA;AAE9G;;;GAGG;AACH,OAAO,KAAK,YAAY,MAAM,yCAAyC,CAAA;AAEvE;;;GAGG;AACH,OAAO,KAAK,WAAW,MAAM,wCAAwC,CAAA;AAErE;;;GAGG;AACH,OAAO,KAAK,IAAI,MAAM,iCAAiC,CAAA;AAEvD;;;GAGG;AACH,OAAO,KAAK,OAAO,MAAM,oCAAoC,CAAA;AAE7D;;;GAGG;AACH,OAAO,KAAK,WAAW,MAAM,wCAAwC,CAAA;AAErE;;;GAGG;AACH,OAAO,KAAK,OAAO,MAAM,oCAAoC,CAAA;AAE7D;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAiD,YAAY,CAAC,YAAY,CAAC,YAAY,CAAC,CAAA;AAEhH;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,YAAY,CAAC,YAAkC,CAAA;AAE7E;;;GAGG;AACH,eAAO,MAAM,cAAc,QAAO,YAAY,CAAC,YAE9C,CAAA;AAcD;;;GAGG;AACH,eAAO,MAAM,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"effect/Exit";
|
|
7
|
+
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
|
|
8
|
+
import { computed, inject, shallowRef, watchEffect } from "vue";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category modules
|
|
12
|
+
*/
|
|
13
|
+
export * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
|
|
14
|
+
/**
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
* @category modules
|
|
17
|
+
*/
|
|
18
|
+
export * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
|
19
|
+
/**
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
* @category modules
|
|
22
|
+
*/
|
|
23
|
+
export * as Atom from "effect/unstable/reactivity/Atom";
|
|
24
|
+
/**
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
* @category modules
|
|
27
|
+
*/
|
|
28
|
+
export * as AtomRef from "effect/unstable/reactivity/AtomRef";
|
|
29
|
+
/**
|
|
30
|
+
* @since 1.0.0
|
|
31
|
+
* @category re-exports
|
|
32
|
+
*/
|
|
33
|
+
export * as AtomHttpApi from "effect/unstable/reactivity/AtomHttpApi";
|
|
34
|
+
/**
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
* @category modules
|
|
37
|
+
*/
|
|
38
|
+
export * as AtomRpc from "effect/unstable/reactivity/AtomRpc";
|
|
39
|
+
/**
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
* @category registry
|
|
42
|
+
*/
|
|
43
|
+
export const registryKey = /*#__PURE__*/Symbol.for("@effect/atom-vue/registryKey");
|
|
44
|
+
/**
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
* @category registry
|
|
47
|
+
*/
|
|
48
|
+
export const defaultRegistry = /*#__PURE__*/AtomRegistry.make();
|
|
49
|
+
/**
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
* @category registry
|
|
52
|
+
*/
|
|
53
|
+
export const injectRegistry = () => {
|
|
54
|
+
return inject(registryKey, defaultRegistry);
|
|
55
|
+
};
|
|
56
|
+
const useAtomValueRef = atom => {
|
|
57
|
+
const registry = injectRegistry();
|
|
58
|
+
const atomRef = computed(atom);
|
|
59
|
+
const value = shallowRef(undefined);
|
|
60
|
+
watchEffect(onCleanup => {
|
|
61
|
+
onCleanup(registry.subscribe(atomRef.value, nextValue => {
|
|
62
|
+
value.value = nextValue;
|
|
63
|
+
}, {
|
|
64
|
+
immediate: true
|
|
65
|
+
}));
|
|
66
|
+
});
|
|
67
|
+
return [value, atomRef, registry];
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* @since 1.0.0
|
|
71
|
+
* @category composables
|
|
72
|
+
*/
|
|
73
|
+
export const useAtom = (atom, options) => {
|
|
74
|
+
const [value, atomRef, registry] = useAtomValueRef(atom);
|
|
75
|
+
return [value, setAtom(registry, atomRef, options)];
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* @since 1.0.0
|
|
79
|
+
* @category composables
|
|
80
|
+
*/
|
|
81
|
+
export const useAtomValue = atom => useAtomValueRef(atom)[0];
|
|
82
|
+
const flattenExit = exit => {
|
|
83
|
+
if (Exit.isSuccess(exit)) return exit.value;
|
|
84
|
+
throw Cause.squash(exit.cause);
|
|
85
|
+
};
|
|
86
|
+
function setAtom(registry, atomRef, options) {
|
|
87
|
+
if (options?.mode === "promise" || options?.mode === "promiseExit") {
|
|
88
|
+
return (value, opts) => {
|
|
89
|
+
registry.set(atomRef.value, value);
|
|
90
|
+
const promise = Effect.runPromiseExit(AtomRegistry.getResult(registry, atomRef.value, {
|
|
91
|
+
suspendOnWaiting: true
|
|
92
|
+
}), opts);
|
|
93
|
+
return options.mode === "promise" ? promise.then(flattenExit) : promise;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return value => {
|
|
97
|
+
registry.set(atomRef.value, typeof value === "function" ? value(registry.get(atomRef.value)) : value);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @since 1.0.0
|
|
102
|
+
* @category composables
|
|
103
|
+
*/
|
|
104
|
+
export const useAtomSet = (atom, options) => {
|
|
105
|
+
const registry = injectRegistry();
|
|
106
|
+
const atomRef = computed(atom);
|
|
107
|
+
watchEffect(onCleanup => {
|
|
108
|
+
onCleanup(registry.mount(atomRef.value));
|
|
109
|
+
});
|
|
110
|
+
return setAtom(registry, atomRef, options);
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* @since 1.0.0
|
|
114
|
+
* @category composables
|
|
115
|
+
*/
|
|
116
|
+
export const useAtomRef = atomRef => {
|
|
117
|
+
const atomRefRef = computed(atomRef);
|
|
118
|
+
const value = shallowRef(atomRefRef.value.value);
|
|
119
|
+
watchEffect(onCleanup => {
|
|
120
|
+
const ref = atomRefRef.value;
|
|
121
|
+
onCleanup(ref.subscribe(next => {
|
|
122
|
+
value.value = next;
|
|
123
|
+
}));
|
|
124
|
+
});
|
|
125
|
+
return value;
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["Cause","Effect","Exit","AtomRegistry","computed","inject","shallowRef","watchEffect","AsyncResult","Atom","AtomRef","AtomHttpApi","AtomRpc","registryKey","Symbol","for","defaultRegistry","make","injectRegistry","useAtomValueRef","atom","registry","atomRef","value","undefined","onCleanup","subscribe","nextValue","immediate","useAtom","options","setAtom","useAtomValue","flattenExit","exit","isSuccess","squash","cause","mode","opts","set","promise","runPromiseExit","getResult","suspendOnWaiting","then","get","useAtomSet","mount","useAtomRef","atomRefRef","ref","next"],"sources":["../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,IAAI,MAAM,aAAa;AAInC,OAAO,KAAKC,YAAY,MAAM,yCAAyC;AACvE,SAASC,QAAQ,EAAoBC,MAAM,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CAAC;EAChH,CAAC;AACH;AAEA;;;;AAIA,OAAO,MAAMwB,UAAU,GAAGA,CAKxB3B,IAA+B,EAC/BU,OAEC,KAiB0C;EAE3C,MAAMT,QAAQ,GAAGH,cAAc,EAAE;EACjC,MAAMI,OAAO,GAAGlB,QAAQ,CAACgB,IAAI,CAAC;EAC9Bb,WAAW,CAAEkB,SAAS,IAAI;IACxBA,SAAS,CAACJ,QAAQ,CAAC2B,KAAK,CAAC1B,OAAO,CAACC,KAAK,CAAC,CAAC;EAC1C,CAAC,CAAC;EACF,OAAOQ,OAAO,CAACV,QAAQ,EAAEC,OAAO,EAAEQ,OAAO,CAAC;AAC5C,CAAC;AAED;;;;AAIA,OAAO,MAAMmB,UAAU,GAAO3B,OAAqC,IAAsB;EACvF,MAAM4B,UAAU,GAAG9C,QAAQ,CAACkB,OAAO,CAAC;EACpC,MAAMC,KAAK,GAAGjB,UAAU,CAAI4C,UAAU,CAAC3B,KAAK,CAACA,KAAK,CAAC;EACnDhB,WAAW,CAAEkB,SAAS,IAAI;IACxB,MAAM0B,GAAG,GAAGD,UAAU,CAAC3B,KAAK;IAC5BE,SAAS,CAAC0B,GAAG,CAACzB,SAAS,CAAE0B,IAAO,IAAI;MAClC7B,KAAK,CAACA,KAAK,GAAG6B,IAAI;IACpB,CAAC,CAAC,CAAC;EACL,CAAC,CAAC;EACF,OAAO7B,KAAyB;AAClC,CAAC","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect/atom-vue",
|
|
3
|
+
"version": "4.0.0-beta.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "Vue 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/vue"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Effect-TS/effect-smol/issues"
|
|
15
|
+
},
|
|
16
|
+
"tags": [
|
|
17
|
+
"typescript",
|
|
18
|
+
"vue",
|
|
19
|
+
"database"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"typescript",
|
|
23
|
+
"vue",
|
|
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
|
+
"devDependencies": {
|
|
46
|
+
"vue": "^3.5.27",
|
|
47
|
+
"effect": "^4.0.0-beta.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"vue": "^3.5.0",
|
|
51
|
+
"effect": "^4.0.0-beta.0"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsc -b tsconfig.json && pnpm babel",
|
|
55
|
+
"build:tsgo": "tsgo -b tsconfig.json && pnpm babel",
|
|
56
|
+
"babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps",
|
|
57
|
+
"check": "tsc -b tsconfig.json",
|
|
58
|
+
"test": "vitest",
|
|
59
|
+
"coverage": "vitest --coverage"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
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 type * 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 { computed, type ComputedRef, inject, type InjectionKey, type Ref, shallowRef, watchEffect } from "vue"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @since 1.0.0
|
|
15
|
+
* @category modules
|
|
16
|
+
*/
|
|
17
|
+
export * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry"
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
* @category modules
|
|
22
|
+
*/
|
|
23
|
+
export * as AsyncResult from "effect/unstable/reactivity/AsyncResult"
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @since 1.0.0
|
|
27
|
+
* @category modules
|
|
28
|
+
*/
|
|
29
|
+
export * as Atom from "effect/unstable/reactivity/Atom"
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @since 1.0.0
|
|
33
|
+
* @category modules
|
|
34
|
+
*/
|
|
35
|
+
export * as AtomRef from "effect/unstable/reactivity/AtomRef"
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @since 1.0.0
|
|
39
|
+
* @category re-exports
|
|
40
|
+
*/
|
|
41
|
+
export * as AtomHttpApi from "effect/unstable/reactivity/AtomHttpApi"
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @since 1.0.0
|
|
45
|
+
* @category modules
|
|
46
|
+
*/
|
|
47
|
+
export * as AtomRpc from "effect/unstable/reactivity/AtomRpc"
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
* @category registry
|
|
52
|
+
*/
|
|
53
|
+
export const registryKey = Symbol.for("@effect/atom-vue/registryKey") as InjectionKey<AtomRegistry.AtomRegistry>
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @since 1.0.0
|
|
57
|
+
* @category registry
|
|
58
|
+
*/
|
|
59
|
+
export const defaultRegistry: AtomRegistry.AtomRegistry = AtomRegistry.make()
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @since 1.0.0
|
|
63
|
+
* @category registry
|
|
64
|
+
*/
|
|
65
|
+
export const injectRegistry = (): AtomRegistry.AtomRegistry => {
|
|
66
|
+
return inject(registryKey, defaultRegistry)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const useAtomValueRef = <A extends Atom.Atom<any>>(atom: () => A) => {
|
|
70
|
+
const registry = injectRegistry()
|
|
71
|
+
const atomRef = computed(atom)
|
|
72
|
+
const value = shallowRef(undefined as any as A)
|
|
73
|
+
watchEffect((onCleanup) => {
|
|
74
|
+
onCleanup(registry.subscribe(atomRef.value, (nextValue: Atom.Type<A>) => {
|
|
75
|
+
value.value = nextValue
|
|
76
|
+
}, { immediate: true }))
|
|
77
|
+
})
|
|
78
|
+
return [value as Readonly<Ref<Atom.Type<A>>>, atomRef, registry] as const
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @since 1.0.0
|
|
83
|
+
* @category composables
|
|
84
|
+
*/
|
|
85
|
+
export const useAtom = <R, W, Mode extends "value" | "promise" | "promiseExit" = never>(
|
|
86
|
+
atom: () => Atom.Writable<R, W>,
|
|
87
|
+
options?: {
|
|
88
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
89
|
+
}
|
|
90
|
+
): readonly [
|
|
91
|
+
Readonly<Ref<R>>,
|
|
92
|
+
write: "promise" extends Mode ? (
|
|
93
|
+
(value: W) => Promise<AsyncResult.AsyncResult.Success<R>>
|
|
94
|
+
) :
|
|
95
|
+
"promiseExit" extends Mode ? (
|
|
96
|
+
(value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>
|
|
97
|
+
) :
|
|
98
|
+
((value: W | ((value: R) => W)) => void)
|
|
99
|
+
] => {
|
|
100
|
+
const [value, atomRef, registry] = useAtomValueRef(atom)
|
|
101
|
+
return [value as Readonly<Ref<R>>, setAtom(registry, atomRef, options)]
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @since 1.0.0
|
|
106
|
+
* @category composables
|
|
107
|
+
*/
|
|
108
|
+
export const useAtomValue = <A>(atom: () => Atom.Atom<A>): Readonly<Ref<A>> => useAtomValueRef(atom)[0]
|
|
109
|
+
|
|
110
|
+
const flattenExit = <A, E>(exit: Exit.Exit<A, E>): A => {
|
|
111
|
+
if (Exit.isSuccess(exit)) return exit.value
|
|
112
|
+
throw Cause.squash(exit.cause)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>(
|
|
116
|
+
registry: AtomRegistry.AtomRegistry,
|
|
117
|
+
atomRef: ComputedRef<Atom.Writable<R, W>>,
|
|
118
|
+
options?: {
|
|
119
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
120
|
+
}
|
|
121
|
+
): "promise" extends Mode ? (
|
|
122
|
+
(
|
|
123
|
+
value: W,
|
|
124
|
+
options?: {
|
|
125
|
+
readonly signal?: AbortSignal | undefined
|
|
126
|
+
} | undefined
|
|
127
|
+
) => Promise<AsyncResult.AsyncResult.Success<R>>
|
|
128
|
+
) :
|
|
129
|
+
"promiseExit" extends Mode ? (
|
|
130
|
+
(
|
|
131
|
+
value: W,
|
|
132
|
+
options?: {
|
|
133
|
+
readonly signal?: AbortSignal | undefined
|
|
134
|
+
} | undefined
|
|
135
|
+
) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>
|
|
136
|
+
) :
|
|
137
|
+
((value: W | ((value: R) => W)) => void)
|
|
138
|
+
{
|
|
139
|
+
if (options?.mode === "promise" || options?.mode === "promiseExit") {
|
|
140
|
+
return ((value: W, opts?: any) => {
|
|
141
|
+
registry.set(atomRef.value, value)
|
|
142
|
+
const promise = Effect.runPromiseExit(
|
|
143
|
+
AtomRegistry.getResult(
|
|
144
|
+
registry,
|
|
145
|
+
atomRef.value as Atom.Atom<AsyncResult.AsyncResult<any, any>>,
|
|
146
|
+
{ suspendOnWaiting: true }
|
|
147
|
+
),
|
|
148
|
+
opts
|
|
149
|
+
)
|
|
150
|
+
return options!.mode === "promise" ? promise.then(flattenExit) : promise
|
|
151
|
+
}) as any
|
|
152
|
+
}
|
|
153
|
+
return ((value: W | ((value: R) => W)) => {
|
|
154
|
+
registry.set(atomRef.value, typeof value === "function" ? (value as any)(registry.get(atomRef.value)) : value)
|
|
155
|
+
}) as any
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @since 1.0.0
|
|
160
|
+
* @category composables
|
|
161
|
+
*/
|
|
162
|
+
export const useAtomSet = <
|
|
163
|
+
R,
|
|
164
|
+
W,
|
|
165
|
+
Mode extends "value" | "promise" | "promiseExit" = never
|
|
166
|
+
>(
|
|
167
|
+
atom: () => Atom.Writable<R, W>,
|
|
168
|
+
options?: {
|
|
169
|
+
readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
|
|
170
|
+
}
|
|
171
|
+
): "promise" extends Mode ? (
|
|
172
|
+
(
|
|
173
|
+
value: W,
|
|
174
|
+
options?: {
|
|
175
|
+
readonly signal?: AbortSignal | undefined
|
|
176
|
+
} | undefined
|
|
177
|
+
) => Promise<AsyncResult.AsyncResult.Success<R>>
|
|
178
|
+
) :
|
|
179
|
+
"promiseExit" extends Mode ? (
|
|
180
|
+
(
|
|
181
|
+
value: W,
|
|
182
|
+
options?: {
|
|
183
|
+
readonly signal?: AbortSignal | undefined
|
|
184
|
+
} | undefined
|
|
185
|
+
) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>
|
|
186
|
+
) :
|
|
187
|
+
((value: W | ((value: R) => W)) => void) =>
|
|
188
|
+
{
|
|
189
|
+
const registry = injectRegistry()
|
|
190
|
+
const atomRef = computed(atom)
|
|
191
|
+
watchEffect((onCleanup) => {
|
|
192
|
+
onCleanup(registry.mount(atomRef.value))
|
|
193
|
+
})
|
|
194
|
+
return setAtom(registry, atomRef, options)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @since 1.0.0
|
|
199
|
+
* @category composables
|
|
200
|
+
*/
|
|
201
|
+
export const useAtomRef = <A>(atomRef: () => AtomRef.ReadonlyRef<A>): Readonly<Ref<A>> => {
|
|
202
|
+
const atomRefRef = computed(atomRef)
|
|
203
|
+
const value = shallowRef<A>(atomRefRef.value.value)
|
|
204
|
+
watchEffect((onCleanup) => {
|
|
205
|
+
const ref = atomRefRef.value
|
|
206
|
+
onCleanup(ref.subscribe((next: A) => {
|
|
207
|
+
value.value = next
|
|
208
|
+
}))
|
|
209
|
+
})
|
|
210
|
+
return value as Readonly<Ref<A>>
|
|
211
|
+
}
|