unwrapped 0.1.0 → 0.1.2

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.
@@ -1,106 +0,0 @@
1
- import { computed, onUnmounted, ref, toRef, triggerRef, watch, type Ref, type WatchSource } from "vue";
2
- import { AsyncResult, type FlatChainFunction, type Result } from "unwrapped/core";
3
-
4
- export function useAsyncResultRef<T, E>(asyncResult: AsyncResult<T, E>) {
5
- const state = ref<AsyncResult<T, E>>(asyncResult) as Ref<AsyncResult<T, E>>;
6
-
7
- const unsub = asyncResult.listen(() => {
8
- triggerRef(state);
9
- });
10
-
11
- onUnmounted(() => {
12
- unsub();
13
- });
14
-
15
- return state;
16
- }
17
-
18
- export function useReactiveResult<T, E, Inputs>(source: WatchSource<Inputs>, pipe: FlatChainFunction<Inputs, T, E>, options:{ immediate: boolean } = { immediate: true }): Ref<AsyncResult<T, E>> {
19
- const result = new AsyncResult<T, E>();
20
- const resultRef = useAsyncResultRef(result);
21
-
22
- let unsub: (() => void) | null = null;
23
-
24
- watch(source, (newInputs) => {
25
- unsub?.();
26
- unsub = result.mirror(pipe(newInputs));
27
- }, { immediate: options.immediate });
28
-
29
- onUnmounted(() => {
30
- unsub?.();
31
- });
32
-
33
- return resultRef;
34
- }
35
-
36
- export function useAsyncResultRefFromPromise<T, E>(promise: Promise<Result<T, E>>) {
37
- return useAsyncResultRef(AsyncResult.fromResultPromise(promise));
38
- }
39
-
40
- export type Action<T,E> = () => Promise<Result<T, E>>;
41
- export function useImmediateAction<T, E>(action: Action<T, E>): Ref<AsyncResult<T, E>> {
42
- return useAsyncResultRefFromPromise(action());
43
- }
44
- export interface LazyActionReturn<T, E> {
45
- resultRef: Ref<AsyncResult<T, E>>;
46
- trigger: () => void;
47
- }
48
-
49
- export function useLazyAction<T, E>(action: Action<T, E>): LazyActionReturn<T, E> {
50
- const result = new AsyncResult<T, E>();
51
- const resultRef = useAsyncResultRef(result);
52
-
53
- const trigger = () => {
54
- result.updateFromResultPromise(action());
55
- }
56
-
57
- return { resultRef, trigger };
58
- }
59
-
60
- export function useReactiveAction<I, O, E>(input: I | Ref<I> | (() => I), pipe: FlatChainFunction<I, O, E>, options:{ immediate: boolean } = { immediate: true }): Ref<AsyncResult<O, E>> {
61
- const source = typeof input === 'function' ? computed(input as () => I) : toRef(input)
62
-
63
- const outputRef = ref<AsyncResult<O, E>>(new AsyncResult()) as Ref<AsyncResult<O, E>>;
64
- let unsub: (() => void) | null = null;
65
-
66
- watch(source, () => {
67
- unsub?.();
68
- const newOutput = pipe(source.value);
69
- unsub = newOutput.listen((newState) => {
70
- outputRef.value.setState(newState.state);
71
- triggerRef(outputRef);
72
- });
73
- }, { immediate: options.immediate });
74
-
75
- onUnmounted(() => {
76
- unsub?.();
77
- });
78
-
79
- return outputRef;
80
- }
81
-
82
- export function useGenerator<T>(generatorFunc: () => Generator<AsyncResult<any, any>, T, any>): Ref<AsyncResult<T, any>> {
83
- const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));
84
- return resultRef;
85
- }
86
-
87
- export function useLazyGenerator<T>(generatorFunc: () => Generator<AsyncResult<any, any>, T, any>): { resultRef: Ref<AsyncResult<T, any>>, trigger: () => void } {
88
- const result = new AsyncResult<T, any>();
89
- const resultRef = useAsyncResultRef(result);
90
-
91
- const trigger = () => {
92
- result.runInPlace(generatorFunc);
93
- }
94
-
95
- return { resultRef, trigger };
96
- }
97
-
98
- export function useReactiveGenerator<T, E, Inputs>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs) => Generator<AsyncResult<any, any>, T, any>, options:{ immediate: boolean } = { immediate: true }): Ref<AsyncResult<T, E>> {
99
- const resultRef = useAsyncResultRef(new AsyncResult<T, E>());
100
-
101
- watch(source, (newInputs) => {
102
- resultRef.value.runInPlace(() => generatorFunc(newInputs));
103
- }, { immediate: options.immediate });
104
-
105
- return resultRef;
106
- }
package/src/vue/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./composables"
2
- export * from "./components/asyncResultLoader"
package/tsconfig.json DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- /* Base Options: */
4
- "esModuleInterop": true,
5
- "skipLibCheck": true,
6
- "target": "es2022",
7
- "verbatimModuleSyntax": true,
8
- "allowJs": true,
9
- "resolveJsonModule": true,
10
- "moduleDetection": "force",
11
- /* Strictness */
12
- "strict": true,
13
- "noUncheckedIndexedAccess": true,
14
- /* If NOT transpiling with TypeScript: */
15
- "moduleResolution": "Bundler",
16
- "module": "ESNext",
17
- "noEmit": true,
18
- /* If your code runs in the DOM: */
19
- "lib": ["es2022", "dom", "dom.iterable"],
20
- "baseUrl": ".",
21
- "paths": {
22
- "unwrapped/core": ["./src/core/index.ts"]
23
- }
24
- },
25
- "include": ["src"]
26
- }
@@ -1,11 +0,0 @@
1
- import { defineConfig } from "tsup"
2
-
3
- export default defineConfig([
4
- {
5
- entry: ["src/core/index.ts"],
6
- format: ["cjs", "esm"],
7
- sourcemap: true,
8
- dts: true,
9
- outDir: "dist/core"
10
- },
11
- ])
@@ -1,13 +0,0 @@
1
- import { defineConfig } from "tsup"
2
-
3
- export default defineConfig([
4
- {
5
- entry: ["src/vue/index.ts"],
6
- format: ["cjs", "esm"],
7
- sourcemap: true,
8
- dts: true,
9
- outDir: "dist/vue",
10
- external: ["vue", "unwrapped/core"],
11
- platform: "browser"
12
- }
13
- ])