unwrapped 0.1.5 → 0.1.6
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/CHANGELOG.md +6 -0
- package/dist/core/index.d.mts +112 -1
- package/dist/core/index.d.ts +112 -1
- package/dist/core/index.js +203 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +200 -2
- package/dist/core/index.mjs.map +1 -1
- package/dist/vue/index.d.mts +221 -3
- package/dist/vue/index.d.ts +221 -3
- package/dist/vue/index.js +12 -0
- package/dist/vue/index.js.map +1 -1
- package/dist/vue/index.mjs +13 -1
- package/dist/vue/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/vue/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import * as unwrapped_core from 'unwrapped/core';
|
|
2
|
+
import { ErrorBase, AsyncResult, Result, FlatChainStep, Action, AsyncResultGenerator, AsyncResultList } from 'unwrapped/core';
|
|
1
3
|
import * as vue from 'vue';
|
|
2
4
|
import { Ref, WatchSource, VNode } from 'vue';
|
|
3
|
-
import { ErrorBase, AsyncResult, Result, FlatChainStep, Action, AsyncResultGenerator } from 'unwrapped/core';
|
|
4
5
|
|
|
5
6
|
interface ReactiveProcessOptions {
|
|
6
7
|
immediate: boolean;
|
|
@@ -12,7 +13,7 @@ interface ReactiveProcessOptions {
|
|
|
12
13
|
* @param asyncResult the result to make reactive
|
|
13
14
|
* @returns the ref to the result
|
|
14
15
|
*/
|
|
15
|
-
declare function useAsyncResultRef<T, E extends ErrorBase = ErrorBase>(asyncResult
|
|
16
|
+
declare function useAsyncResultRef<T, E extends ErrorBase = ErrorBase>(asyncResult?: AsyncResult<T, E>): Ref<AsyncResult<T, E>, AsyncResult<T, E>>;
|
|
16
17
|
/**
|
|
17
18
|
* Creates an AsyncResult ref from a promise returning a Result.
|
|
18
19
|
* @param promise the promise returning a Result
|
|
@@ -78,6 +79,223 @@ declare function useLazyGenerator<T>(generatorFunc: () => AsyncResultGenerator<T
|
|
|
78
79
|
* @returns ref to the result
|
|
79
80
|
*/
|
|
80
81
|
declare function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs) => AsyncResultGenerator<T>, options?: ReactiveProcessOptions): Ref<AsyncResult<T, E>>;
|
|
82
|
+
/**
|
|
83
|
+
* Creates a reactive AsyncResultList wrapped in a Vue ref.
|
|
84
|
+
* The AsyncResultList notifies Vue's reactivity system whenever its state changes.
|
|
85
|
+
*
|
|
86
|
+
* @template T - The type of the values in the AsyncResultList.
|
|
87
|
+
* @template E - The type of the error, extending ErrorBase (default is ErrorBase).
|
|
88
|
+
* @returns a ref to the AsyncResultList
|
|
89
|
+
*/
|
|
90
|
+
declare function useAsyncResultList<T = any, E extends ErrorBase = ErrorBase>(): Ref<{
|
|
91
|
+
readonly tasks: Map<string, {
|
|
92
|
+
state: {
|
|
93
|
+
status: "idle";
|
|
94
|
+
} | {
|
|
95
|
+
status: "loading";
|
|
96
|
+
promise: {
|
|
97
|
+
then: <TResult1 = Result<T, E>, TResult2 = never>(onfulfilled?: ((value: Result<T, E>) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>;
|
|
98
|
+
catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<Result<T, E> | TResult>;
|
|
99
|
+
finally: (onfinally?: (() => void) | null | undefined) => Promise<Result<T, E>>;
|
|
100
|
+
readonly [Symbol.toStringTag]: string;
|
|
101
|
+
};
|
|
102
|
+
} | {
|
|
103
|
+
status: "success";
|
|
104
|
+
value: vue.UnwrapRef<T>;
|
|
105
|
+
} | {
|
|
106
|
+
status: "error";
|
|
107
|
+
error: vue.UnwrapRef<E>;
|
|
108
|
+
};
|
|
109
|
+
isSuccess: () => boolean;
|
|
110
|
+
isError: () => boolean;
|
|
111
|
+
isIdle: () => boolean;
|
|
112
|
+
isLoading: () => boolean;
|
|
113
|
+
unwrapOrNull: () => T | null;
|
|
114
|
+
unwrapOrThrow: () => T;
|
|
115
|
+
unwrapErrorOrNull: () => E | null;
|
|
116
|
+
updateFromValue: (value: T) => AsyncResult<T, E>;
|
|
117
|
+
updateFromError: (error: E) => AsyncResult<T, E>;
|
|
118
|
+
updateFromValuePromise: (promise: Promise<T>) => AsyncResult<T, E>;
|
|
119
|
+
waitForSettled: () => Promise<AsyncResult<T, E>>;
|
|
120
|
+
toResultPromise: () => Promise<Result<T, E>>;
|
|
121
|
+
toValueOrThrowPromise: () => Promise<T>;
|
|
122
|
+
toValueOrNullPromise: () => Promise<T | null>;
|
|
123
|
+
updateFromResultPromise: (promise: Promise<Result<T, E>>) => AsyncResult<T, E>;
|
|
124
|
+
listen: (listener: unwrapped_core.AsyncResultListener<T, E>, immediate?: boolean) => () => void;
|
|
125
|
+
listenUntilSettled: (listener: unwrapped_core.AsyncResultListener<T, E>, immediate?: boolean) => () => void;
|
|
126
|
+
mirror: (other: AsyncResult<T, E>) => () => void;
|
|
127
|
+
mirrorUntilSettled: (other: AsyncResult<T, E>) => () => void;
|
|
128
|
+
chain: <O, E2 extends ErrorBase = ErrorBase>(fn: unwrapped_core.ChainStep<T, O, E | E2>) => AsyncResult<O, E | E2>;
|
|
129
|
+
flatChain: <O, E2 extends ErrorBase = ErrorBase>(fn: FlatChainStep<T, O, E | E2>) => AsyncResult<O, E | E2>;
|
|
130
|
+
runInPlace: (generatorFunc: () => AsyncResultGenerator<T>) => AsyncResult<T, E>;
|
|
131
|
+
log: (name?: string) => void;
|
|
132
|
+
debug: (name?: string) => () => void;
|
|
133
|
+
[Symbol.iterator]: () => Generator<AsyncResult<T, E>, T, any>;
|
|
134
|
+
}> & Omit<Map<string, AsyncResult<T, E>>, keyof Map<any, any>>;
|
|
135
|
+
readonly length: number;
|
|
136
|
+
readonly items: {
|
|
137
|
+
state: {
|
|
138
|
+
status: "idle";
|
|
139
|
+
} | {
|
|
140
|
+
status: "loading";
|
|
141
|
+
promise: {
|
|
142
|
+
then: <TResult1 = Result<T, E>, TResult2 = never>(onfulfilled?: ((value: Result<T, E>) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>;
|
|
143
|
+
catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<Result<T, E> | TResult>;
|
|
144
|
+
finally: (onfinally?: (() => void) | null | undefined) => Promise<Result<T, E>>;
|
|
145
|
+
readonly [Symbol.toStringTag]: string;
|
|
146
|
+
};
|
|
147
|
+
} | {
|
|
148
|
+
status: "success";
|
|
149
|
+
value: vue.UnwrapRef<T>;
|
|
150
|
+
} | {
|
|
151
|
+
status: "error";
|
|
152
|
+
error: vue.UnwrapRef<E>;
|
|
153
|
+
};
|
|
154
|
+
isSuccess: () => boolean;
|
|
155
|
+
isError: () => boolean;
|
|
156
|
+
isIdle: () => boolean;
|
|
157
|
+
isLoading: () => boolean;
|
|
158
|
+
unwrapOrNull: () => T | null;
|
|
159
|
+
unwrapOrThrow: () => T;
|
|
160
|
+
unwrapErrorOrNull: () => E | null;
|
|
161
|
+
updateFromValue: (value: T) => AsyncResult<T, E>;
|
|
162
|
+
updateFromError: (error: E) => AsyncResult<T, E>;
|
|
163
|
+
updateFromValuePromise: (promise: Promise<T>) => AsyncResult<T, E>;
|
|
164
|
+
waitForSettled: () => Promise<AsyncResult<T, E>>;
|
|
165
|
+
toResultPromise: () => Promise<Result<T, E>>;
|
|
166
|
+
toValueOrThrowPromise: () => Promise<T>;
|
|
167
|
+
toValueOrNullPromise: () => Promise<T | null>;
|
|
168
|
+
updateFromResultPromise: (promise: Promise<Result<T, E>>) => AsyncResult<T, E>;
|
|
169
|
+
listen: (listener: unwrapped_core.AsyncResultListener<T, E>, immediate?: boolean) => () => void;
|
|
170
|
+
listenUntilSettled: (listener: unwrapped_core.AsyncResultListener<T, E>, immediate?: boolean) => () => void;
|
|
171
|
+
mirror: (other: AsyncResult<T, E>) => () => void;
|
|
172
|
+
mirrorUntilSettled: (other: AsyncResult<T, E>) => () => void;
|
|
173
|
+
chain: <O, E2 extends ErrorBase = ErrorBase>(fn: unwrapped_core.ChainStep<T, O, E | E2>) => AsyncResult<O, E | E2>;
|
|
174
|
+
flatChain: <O, E2 extends ErrorBase = ErrorBase>(fn: FlatChainStep<T, O, E | E2>) => AsyncResult<O, E | E2>;
|
|
175
|
+
runInPlace: (generatorFunc: () => AsyncResultGenerator<T>) => AsyncResult<T, E>;
|
|
176
|
+
log: (name?: string) => void;
|
|
177
|
+
debug: (name?: string) => () => void;
|
|
178
|
+
[Symbol.iterator]: () => Generator<AsyncResult<T, E>, T, any>;
|
|
179
|
+
}[];
|
|
180
|
+
state: unwrapped_core.AsyncResultListState;
|
|
181
|
+
listen: (listener: (taskQueue: AsyncResultList<T, E>) => void) => () => void;
|
|
182
|
+
add: (key: string, task: AsyncResult<T, E>, removeOnSettle?: boolean) => AsyncResult<T, E>;
|
|
183
|
+
anyLoading: () => boolean;
|
|
184
|
+
getAllFiltered: (predicate: (task: AsyncResult<T, E>) => boolean) => AsyncResult<T, E>[];
|
|
185
|
+
getAllFilteredAndMap: <U>(filterPredicate: (task: AsyncResult<T, E>) => boolean, mapFunc: (task: AsyncResult<T, E>) => U) => U[];
|
|
186
|
+
getAllSuccess: () => AsyncResult<T, E>[];
|
|
187
|
+
getAllSuccessValues: () => T[];
|
|
188
|
+
getAllErrors: () => AsyncResult<T, E>[];
|
|
189
|
+
getAllErrorValues: () => E[];
|
|
190
|
+
getAllLoading: () => AsyncResult<T, E>[];
|
|
191
|
+
getAllLoadingPromises: () => Promise<Result<T, E>>[];
|
|
192
|
+
log: (name?: string) => void;
|
|
193
|
+
debug: (name?: string) => () => void;
|
|
194
|
+
}, AsyncResultList<T, E> | {
|
|
195
|
+
readonly tasks: Map<string, {
|
|
196
|
+
state: {
|
|
197
|
+
status: "idle";
|
|
198
|
+
} | {
|
|
199
|
+
status: "loading";
|
|
200
|
+
promise: {
|
|
201
|
+
then: <TResult1 = Result<T, E>, TResult2 = never>(onfulfilled?: ((value: Result<T, E>) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>;
|
|
202
|
+
catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<Result<T, E> | TResult>;
|
|
203
|
+
finally: (onfinally?: (() => void) | null | undefined) => Promise<Result<T, E>>;
|
|
204
|
+
readonly [Symbol.toStringTag]: string;
|
|
205
|
+
};
|
|
206
|
+
} | {
|
|
207
|
+
status: "success";
|
|
208
|
+
value: vue.UnwrapRef<T>;
|
|
209
|
+
} | {
|
|
210
|
+
status: "error";
|
|
211
|
+
error: vue.UnwrapRef<E>;
|
|
212
|
+
};
|
|
213
|
+
isSuccess: () => boolean;
|
|
214
|
+
isError: () => boolean;
|
|
215
|
+
isIdle: () => boolean;
|
|
216
|
+
isLoading: () => boolean;
|
|
217
|
+
unwrapOrNull: () => T | null;
|
|
218
|
+
unwrapOrThrow: () => T;
|
|
219
|
+
unwrapErrorOrNull: () => E | null;
|
|
220
|
+
updateFromValue: (value: T) => AsyncResult<T, E>;
|
|
221
|
+
updateFromError: (error: E) => AsyncResult<T, E>;
|
|
222
|
+
updateFromValuePromise: (promise: Promise<T>) => AsyncResult<T, E>;
|
|
223
|
+
waitForSettled: () => Promise<AsyncResult<T, E>>;
|
|
224
|
+
toResultPromise: () => Promise<Result<T, E>>;
|
|
225
|
+
toValueOrThrowPromise: () => Promise<T>;
|
|
226
|
+
toValueOrNullPromise: () => Promise<T | null>;
|
|
227
|
+
updateFromResultPromise: (promise: Promise<Result<T, E>>) => AsyncResult<T, E>;
|
|
228
|
+
listen: (listener: unwrapped_core.AsyncResultListener<T, E>, immediate?: boolean) => () => void;
|
|
229
|
+
listenUntilSettled: (listener: unwrapped_core.AsyncResultListener<T, E>, immediate?: boolean) => () => void;
|
|
230
|
+
mirror: (other: AsyncResult<T, E>) => () => void;
|
|
231
|
+
mirrorUntilSettled: (other: AsyncResult<T, E>) => () => void;
|
|
232
|
+
chain: <O, E2 extends ErrorBase = ErrorBase>(fn: unwrapped_core.ChainStep<T, O, E | E2>) => AsyncResult<O, E | E2>;
|
|
233
|
+
flatChain: <O, E2 extends ErrorBase = ErrorBase>(fn: FlatChainStep<T, O, E | E2>) => AsyncResult<O, E | E2>;
|
|
234
|
+
runInPlace: (generatorFunc: () => AsyncResultGenerator<T>) => AsyncResult<T, E>;
|
|
235
|
+
log: (name?: string) => void;
|
|
236
|
+
debug: (name?: string) => () => void;
|
|
237
|
+
[Symbol.iterator]: () => Generator<AsyncResult<T, E>, T, any>;
|
|
238
|
+
}> & Omit<Map<string, AsyncResult<T, E>>, keyof Map<any, any>>;
|
|
239
|
+
readonly length: number;
|
|
240
|
+
readonly items: {
|
|
241
|
+
state: {
|
|
242
|
+
status: "idle";
|
|
243
|
+
} | {
|
|
244
|
+
status: "loading";
|
|
245
|
+
promise: {
|
|
246
|
+
then: <TResult1 = Result<T, E>, TResult2 = never>(onfulfilled?: ((value: Result<T, E>) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>;
|
|
247
|
+
catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<Result<T, E> | TResult>;
|
|
248
|
+
finally: (onfinally?: (() => void) | null | undefined) => Promise<Result<T, E>>;
|
|
249
|
+
readonly [Symbol.toStringTag]: string;
|
|
250
|
+
};
|
|
251
|
+
} | {
|
|
252
|
+
status: "success";
|
|
253
|
+
value: vue.UnwrapRef<T>;
|
|
254
|
+
} | {
|
|
255
|
+
status: "error";
|
|
256
|
+
error: vue.UnwrapRef<E>;
|
|
257
|
+
};
|
|
258
|
+
isSuccess: () => boolean;
|
|
259
|
+
isError: () => boolean;
|
|
260
|
+
isIdle: () => boolean;
|
|
261
|
+
isLoading: () => boolean;
|
|
262
|
+
unwrapOrNull: () => T | null;
|
|
263
|
+
unwrapOrThrow: () => T;
|
|
264
|
+
unwrapErrorOrNull: () => E | null;
|
|
265
|
+
updateFromValue: (value: T) => AsyncResult<T, E>;
|
|
266
|
+
updateFromError: (error: E) => AsyncResult<T, E>;
|
|
267
|
+
updateFromValuePromise: (promise: Promise<T>) => AsyncResult<T, E>;
|
|
268
|
+
waitForSettled: () => Promise<AsyncResult<T, E>>;
|
|
269
|
+
toResultPromise: () => Promise<Result<T, E>>;
|
|
270
|
+
toValueOrThrowPromise: () => Promise<T>;
|
|
271
|
+
toValueOrNullPromise: () => Promise<T | null>;
|
|
272
|
+
updateFromResultPromise: (promise: Promise<Result<T, E>>) => AsyncResult<T, E>;
|
|
273
|
+
listen: (listener: unwrapped_core.AsyncResultListener<T, E>, immediate?: boolean) => () => void;
|
|
274
|
+
listenUntilSettled: (listener: unwrapped_core.AsyncResultListener<T, E>, immediate?: boolean) => () => void;
|
|
275
|
+
mirror: (other: AsyncResult<T, E>) => () => void;
|
|
276
|
+
mirrorUntilSettled: (other: AsyncResult<T, E>) => () => void;
|
|
277
|
+
chain: <O, E2 extends ErrorBase = ErrorBase>(fn: unwrapped_core.ChainStep<T, O, E | E2>) => AsyncResult<O, E | E2>;
|
|
278
|
+
flatChain: <O, E2 extends ErrorBase = ErrorBase>(fn: FlatChainStep<T, O, E | E2>) => AsyncResult<O, E | E2>;
|
|
279
|
+
runInPlace: (generatorFunc: () => AsyncResultGenerator<T>) => AsyncResult<T, E>;
|
|
280
|
+
log: (name?: string) => void;
|
|
281
|
+
debug: (name?: string) => () => void;
|
|
282
|
+
[Symbol.iterator]: () => Generator<AsyncResult<T, E>, T, any>;
|
|
283
|
+
}[];
|
|
284
|
+
state: unwrapped_core.AsyncResultListState;
|
|
285
|
+
listen: (listener: (taskQueue: AsyncResultList<T, E>) => void) => () => void;
|
|
286
|
+
add: (key: string, task: AsyncResult<T, E>, removeOnSettle?: boolean) => AsyncResult<T, E>;
|
|
287
|
+
anyLoading: () => boolean;
|
|
288
|
+
getAllFiltered: (predicate: (task: AsyncResult<T, E>) => boolean) => AsyncResult<T, E>[];
|
|
289
|
+
getAllFilteredAndMap: <U>(filterPredicate: (task: AsyncResult<T, E>) => boolean, mapFunc: (task: AsyncResult<T, E>) => U) => U[];
|
|
290
|
+
getAllSuccess: () => AsyncResult<T, E>[];
|
|
291
|
+
getAllSuccessValues: () => T[];
|
|
292
|
+
getAllErrors: () => AsyncResult<T, E>[];
|
|
293
|
+
getAllErrorValues: () => E[];
|
|
294
|
+
getAllLoading: () => AsyncResult<T, E>[];
|
|
295
|
+
getAllLoadingPromises: () => Promise<Result<T, E>>[];
|
|
296
|
+
log: (name?: string) => void;
|
|
297
|
+
debug: (name?: string) => () => void;
|
|
298
|
+
}>;
|
|
81
299
|
|
|
82
300
|
/**
|
|
83
301
|
* A Vue component that displays different content based on the state of an AsyncResult.
|
|
@@ -142,4 +360,4 @@ declare function buildCustomAsyncResultLoader<T, E extends ErrorBase = ErrorBase
|
|
|
142
360
|
};
|
|
143
361
|
}>> & Readonly<{}>, {}, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
|
|
144
362
|
|
|
145
|
-
export { AsyncResultLoader, type LazyActionRef, buildCustomAsyncResultLoader, useAction, useAsyncResultRef, useAsyncResultRefFromPromise, useGenerator, useLazyAction, useLazyGenerator, useReactiveChain, useReactiveGenerator };
|
|
363
|
+
export { AsyncResultLoader, type LazyActionRef, buildCustomAsyncResultLoader, useAction, useAsyncResultList, useAsyncResultRef, useAsyncResultRefFromPromise, useGenerator, useLazyAction, useLazyGenerator, useReactiveChain, useReactiveGenerator };
|
package/dist/vue/index.js
CHANGED
|
@@ -23,6 +23,7 @@ __export(index_exports, {
|
|
|
23
23
|
AsyncResultLoader: () => AsyncResultLoader,
|
|
24
24
|
buildCustomAsyncResultLoader: () => buildCustomAsyncResultLoader,
|
|
25
25
|
useAction: () => useAction,
|
|
26
|
+
useAsyncResultList: () => useAsyncResultList,
|
|
26
27
|
useAsyncResultRef: () => useAsyncResultRef,
|
|
27
28
|
useAsyncResultRefFromPromise: () => useAsyncResultRefFromPromise,
|
|
28
29
|
useGenerator: () => useGenerator,
|
|
@@ -37,6 +38,9 @@ module.exports = __toCommonJS(index_exports);
|
|
|
37
38
|
var import_vue = require("vue");
|
|
38
39
|
var import_core = require("unwrapped/core");
|
|
39
40
|
function useAsyncResultRef(asyncResult) {
|
|
41
|
+
if (!asyncResult) {
|
|
42
|
+
asyncResult = new import_core.AsyncResult();
|
|
43
|
+
}
|
|
40
44
|
const state = (0, import_vue.ref)(asyncResult);
|
|
41
45
|
const unsub = asyncResult.listen(() => {
|
|
42
46
|
(0, import_vue.triggerRef)(state);
|
|
@@ -89,6 +93,14 @@ function useReactiveGenerator(source, generatorFunc, options = { immediate: true
|
|
|
89
93
|
}, { immediate: options.immediate });
|
|
90
94
|
return resultRef;
|
|
91
95
|
}
|
|
96
|
+
function useAsyncResultList() {
|
|
97
|
+
const list = new import_core.AsyncResultList();
|
|
98
|
+
const listRef = (0, import_vue.ref)(list);
|
|
99
|
+
list.listen(() => {
|
|
100
|
+
(0, import_vue.triggerRef)(listRef);
|
|
101
|
+
});
|
|
102
|
+
return listRef;
|
|
103
|
+
}
|
|
92
104
|
|
|
93
105
|
// src/vue/components/asyncResultLoader.ts
|
|
94
106
|
var import_vue2 = require("vue");
|
package/dist/vue/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/vue/index.ts","../../src/vue/composables.ts","../../src/vue/components/asyncResultLoader.ts"],"sourcesContent":["export * from \"./composables\"\nexport * from \"./components/asyncResultLoader\"","import { onUnmounted, ref, triggerRef, watch, type Ref, type WatchSource } from \"vue\";\nimport { AsyncResult, ErrorBase, type Action, type AsyncResultGenerator, type FlatChainStep, type Result } from \"unwrapped/core\";\n\n\n// === Vue specific types ===\n\ninterface ReactiveProcessOptions {\n immediate: boolean;\n}\n\n\n\n// === Vue Composables for AsyncResult ===\n\n/**\n * Makes a ref to the given AsyncResult. Whenever the state of the AsyncResult changes,\n * the ref gets retriggered, making those changes visible to Vue's reactivity system.\n * \n * @param asyncResult the result to make reactive\n * @returns the ref to the result\n */\nexport function useAsyncResultRef<T, E extends ErrorBase = ErrorBase>(asyncResult: AsyncResult<T, E>) {\n const state = ref<AsyncResult<T, E>>(asyncResult) as Ref<AsyncResult<T, E>>;\n\n const unsub = asyncResult.listen(() => {\n triggerRef(state);\n });\n\n onUnmounted(() => {\n unsub();\n });\n\n return state;\n}\n\n/**\n * Creates an AsyncResult ref from a promise returning a Result.\n * @param promise the promise returning a Result\n * @returns the ref to the AsyncResult\n */\nexport function useAsyncResultRefFromPromise<T, E extends ErrorBase = ErrorBase>(promise: Promise<Result<T, E>>) {\n return useAsyncResultRef(AsyncResult.fromResultPromise(promise));\n}\n\n\n\n// === Vue Composables for Chains ===\n\n/**\n * Watches a source, gives it as inputs to the function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param pipe the function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveChain<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, pipe: FlatChainStep<Inputs, T, E>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const result = new AsyncResult<T, E>();\n const resultRef = useAsyncResultRef(result);\n\n let unsub: (() => void) | null = null;\n\n watch(source, (newInputs) => {\n unsub?.();\n unsub = result.mirror(pipe(newInputs));\n }, { immediate: options.immediate });\n\n onUnmounted(() => {\n unsub?.();\n });\n\n return resultRef;\n}\n\n\n// === Vue Composables for Actions ===\n\n/**\n * The return type of useLazyAction.\n */\nexport interface LazyActionRef<T, E extends ErrorBase = ErrorBase> {\n resultRef: Ref<AsyncResult<T, E>>;\n trigger: () => void;\n}\n\n/**\n * Executes an action immediately and returns a ref to the AsyncResult representing the action's state.\n * \n * Same as useAsyncResultRefFromPromise(action()).\n * \n * @param action the action to execute immediately\n * @returns a ref to the AsyncResult representing the action's state\n */\nexport function useAction<T, E extends ErrorBase = ErrorBase>(action: Action<T, E>): Ref<AsyncResult<T, E>> {\n return useAsyncResultRefFromPromise(action());\n}\n\n/**\n * Creates a lazy action that can be triggered manually.\n * \n * Same as AsyncResult.makeLazyAction(action), but the AsyncResult is wrapped in a ref for Vue reactivity.\n * \n * @param action the action to execute when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyAction<T, E extends ErrorBase = ErrorBase>(action: Action<T, E>): LazyActionRef<T, E> {\n const lazyAction = AsyncResult.makeLazyAction<T, E>(action);\n const resultRef = useAsyncResultRef(lazyAction.result);\n\n return { resultRef, trigger: lazyAction.trigger };\n}\n\n\n// === Vue Composables for Generators ===\n\n/**\n * Runs a generator function immediately and returns a ref to the AsyncResult representing the generator's state.\n * @param generatorFunc the generator function to run immediately\n * @returns a ref to the AsyncResult representing the generator's state\n */\nexport function useGenerator<T>(generatorFunc: () => AsyncResultGenerator<T>): Ref<AsyncResult<T, any>> {\n const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));\n return resultRef;\n}\n\n/**\n * Creates a lazy generator that can be triggered manually.\n * \n * @param generatorFunc the generator function to run when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyGenerator<T>(generatorFunc: () => AsyncResultGenerator<T>): { resultRef: Ref<AsyncResult<T, any>>, trigger: () => void } {\n const result = new AsyncResult<T, any>();\n const resultRef = useAsyncResultRef(result);\n\n const trigger = () => {\n result.runInPlace(generatorFunc);\n }\n\n return { resultRef, trigger };\n}\n\n/**\n * Watches a source, gives it as inputs to the generator function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param generatorFunc the generator function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs) => AsyncResultGenerator<T>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const resultRef = useAsyncResultRef(new AsyncResult<T, E>());\n\n watch(source, (newInputs) => {\n resultRef.value.runInPlace(() => generatorFunc(newInputs));\n }, { immediate: options.immediate });\n\n return resultRef;\n}","import { defineComponent, watch, h, type VNode } from \"vue\"\nimport type { AsyncResult, ErrorBase } from \"unwrapped/core\"\nimport { useAsyncResultRef } from \"../composables\"\n\n/**\n * A Vue component that displays different content based on the state of an AsyncResult.\n * It supports slots for 'loading', 'error', 'success' (default), and 'idle' states.\n * \n * @example\n * <AsyncResultLoader :result=\"myAsyncResult\">\n * <template #loading>\n * <div>Loading data...</div>\n * </template>\n * <template #error=\"{ error }\">\n * <div>Error occurred: {{ error.message }}</div>\n * </template>\n * <template #default=\"{ value }\">\n * <div>Data loaded: {{ value }}</div>\n * </template>\n * <template #idle>\n * <div>Waiting to start...</div>\n * </template>\n * </AsyncResultLoader>\n */\nexport const AsyncResultLoader = defineComponent({\n name: \"AsyncResultLoader\",\n\n props: {\n result: {\n type: Object as () => AsyncResult<unknown>,\n required: true\n }\n },\n\n setup(props, { slots }) {\n let resultRef = useAsyncResultRef(props.result);\n\n // Watch for prop changes & update listener\n watch(\n () => props.result,\n (newResult, oldResult) => {\n if (newResult === oldResult) return;\n resultRef = useAsyncResultRef(newResult);\n },\n { immediate: true }\n )\n\n return () => {\n const s = resultRef.value.state;\n\n // Choose what to render based on status\n switch (s.status) {\n case \"loading\":\n return slots.loading\n ? slots.loading()\n : h(\"div\", { class: \"loading\" }, \"Loading…\");\n\n case \"error\":\n return slots.error\n ? slots.error({ error: s.error })\n : h(\"div\", { class: \"error\" }, `Error: ${s.error}`);\n\n case \"success\":\n return slots.default\n ? slots.default({ value: s.value })\n : null;\n\n default:\n // \"idle\"\n return slots.idle\n ? slots.idle()\n : h(\"div\", { class: \"idle\" }, \"Idle\");\n }\n }\n }\n})\n\n\n\ninterface CustomSlots<E extends ErrorBase = ErrorBase> {\n loading?: () => VNode;\n error?: (props: { error: E }) => VNode;\n}\n\n/**\n * Builds a custom AsyncResultLoader component with predefined slots for loading and error states.\n * \n * Useful for creating reusable components with consistent loading and error handling UI (eg. framework-specific spinners, etc...).\n * \n * @param slots the custom slots for loading and error states\n * @returns a Vue component that uses the provided slots\n */\nexport function buildCustomAsyncResultLoader<T, E extends ErrorBase = ErrorBase>(slots: CustomSlots<E>) {\n const comp = defineComponent({\n name: \"CustomAsyncResultLoader\",\n props: {\n result: {\n type: Object as () => AsyncResult<T, E>,\n required: true\n }\n },\n setup(props, context) {\n return () => {\n const renderLoading = context.slots.loading ?? slots.loading ?? (() => undefined);\n const renderError = context.slots.error ?? slots.error ?? (() => undefined);\n return h(\n AsyncResultLoader,\n { result: props.result },\n {\n default: context.slots.default ? (propsDefault: { value: T }) => context.slots.default!(propsDefault) : undefined,\n\n loading: () => renderLoading(),\n error: ((propsError: { error: E }) => renderError(propsError))\n }\n )\n }\n }\n });\n\n return comp;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAgF;AAChF,kBAAgH;AAoBzG,SAAS,kBAAsD,aAAgC;AAClG,QAAM,YAAQ,gBAAuB,WAAW;AAEhD,QAAM,QAAQ,YAAY,OAAO,MAAM;AACnC,+BAAW,KAAK;AAAA,EACpB,CAAC;AAED,8BAAY,MAAM;AACd,UAAM;AAAA,EACV,CAAC;AAED,SAAO;AACX;AAOO,SAAS,6BAAiE,SAAgC;AAC7G,SAAO,kBAAkB,wBAAY,kBAAkB,OAAO,CAAC;AACnE;AAcO,SAAS,iBAA6D,QAA6B,MAAmC,UAAkC,EAAE,WAAW,KAAK,GAA2B;AACxN,QAAM,SAAS,IAAI,w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AAQ,MAAM,QAAS,YAAY,IAAI;AAAA,YAExG,SAAS,MAAM,cAAc;AAAA,YAC7B,QAAQ,CAAC,eAA6B,YAAY,UAAU;AAAA,UAChE;AAAA,QACJ;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ,CAAC;AAED,SAAO;AACX;","names":["import_vue"]}
|
|
1
|
+
{"version":3,"sources":["../../src/vue/index.ts","../../src/vue/composables.ts","../../src/vue/components/asyncResultLoader.ts"],"sourcesContent":["export * from \"./composables\"\nexport * from \"./components/asyncResultLoader\"","import { onUnmounted, ref, triggerRef, watch, type Ref, type WatchSource } from \"vue\";\nimport { AsyncResult, AsyncResultList, ErrorBase, type Action, type AsyncResultGenerator, type FlatChainStep, type Result } from \"unwrapped/core\";\n\n\n// === Vue specific types ===\n\ninterface ReactiveProcessOptions {\n immediate: boolean;\n}\n\n\n\n// === Vue Composables for AsyncResult ===\n\n/**\n * Makes a ref to the given AsyncResult. Whenever the state of the AsyncResult changes,\n * the ref gets retriggered, making those changes visible to Vue's reactivity system.\n * \n * @param asyncResult the result to make reactive\n * @returns the ref to the result\n */\nexport function useAsyncResultRef<T, E extends ErrorBase = ErrorBase>(asyncResult?: AsyncResult<T, E>) {\n if (!asyncResult) {\n asyncResult = new AsyncResult<T, E>();\n }\n const state = ref<AsyncResult<T, E>>(asyncResult) as Ref<AsyncResult<T, E>>;\n\n const unsub = asyncResult.listen(() => {\n triggerRef(state);\n });\n\n onUnmounted(() => {\n unsub();\n });\n\n return state;\n}\n\n/**\n * Creates an AsyncResult ref from a promise returning a Result.\n * @param promise the promise returning a Result\n * @returns the ref to the AsyncResult\n */\nexport function useAsyncResultRefFromPromise<T, E extends ErrorBase = ErrorBase>(promise: Promise<Result<T, E>>) {\n return useAsyncResultRef(AsyncResult.fromResultPromise(promise));\n}\n\n\n\n// === Vue Composables for Chains ===\n\n/**\n * Watches a source, gives it as inputs to the function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param pipe the function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveChain<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, pipe: FlatChainStep<Inputs, T, E>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const result = new AsyncResult<T, E>();\n const resultRef = useAsyncResultRef(result);\n\n let unsub: (() => void) | null = null;\n\n watch(source, (newInputs) => {\n unsub?.();\n unsub = result.mirror(pipe(newInputs));\n }, { immediate: options.immediate });\n\n onUnmounted(() => {\n unsub?.();\n });\n\n return resultRef;\n}\n\n\n// === Vue Composables for Actions ===\n\n/**\n * The return type of useLazyAction.\n */\nexport interface LazyActionRef<T, E extends ErrorBase = ErrorBase> {\n resultRef: Ref<AsyncResult<T, E>>;\n trigger: () => void;\n}\n\n/**\n * Executes an action immediately and returns a ref to the AsyncResult representing the action's state.\n * \n * Same as useAsyncResultRefFromPromise(action()).\n * \n * @param action the action to execute immediately\n * @returns a ref to the AsyncResult representing the action's state\n */\nexport function useAction<T, E extends ErrorBase = ErrorBase>(action: Action<T, E>): Ref<AsyncResult<T, E>> {\n return useAsyncResultRefFromPromise(action());\n}\n\n/**\n * Creates a lazy action that can be triggered manually.\n * \n * Same as AsyncResult.makeLazyAction(action), but the AsyncResult is wrapped in a ref for Vue reactivity.\n * \n * @param action the action to execute when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyAction<T, E extends ErrorBase = ErrorBase>(action: Action<T, E>): LazyActionRef<T, E> {\n const lazyAction = AsyncResult.makeLazyAction<T, E>(action);\n const resultRef = useAsyncResultRef(lazyAction.result);\n\n return { resultRef, trigger: lazyAction.trigger };\n}\n\n\n// === Vue Composables for Generators ===\n\n/**\n * Runs a generator function immediately and returns a ref to the AsyncResult representing the generator's state.\n * @param generatorFunc the generator function to run immediately\n * @returns a ref to the AsyncResult representing the generator's state\n */\nexport function useGenerator<T>(generatorFunc: () => AsyncResultGenerator<T>): Ref<AsyncResult<T, any>> {\n const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));\n return resultRef;\n}\n\n/**\n * Creates a lazy generator that can be triggered manually.\n * \n * @param generatorFunc the generator function to run when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyGenerator<T>(generatorFunc: () => AsyncResultGenerator<T>): { resultRef: Ref<AsyncResult<T, any>>, trigger: () => void } {\n const result = new AsyncResult<T, any>();\n const resultRef = useAsyncResultRef(result);\n\n const trigger = () => {\n result.runInPlace(generatorFunc);\n }\n\n return { resultRef, trigger };\n}\n\n/**\n * Watches a source, gives it as inputs to the generator function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param generatorFunc the generator function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs) => AsyncResultGenerator<T>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const resultRef = useAsyncResultRef(new AsyncResult<T, E>());\n\n watch(source, (newInputs) => {\n resultRef.value.runInPlace(() => generatorFunc(newInputs));\n }, { immediate: options.immediate });\n\n return resultRef;\n}\n\n\n// === Vue Composables for AsyncResultList ===\n\n/**\n * Creates a reactive AsyncResultList wrapped in a Vue ref.\n * The AsyncResultList notifies Vue's reactivity system whenever its state changes.\n * \n * @template T - The type of the values in the AsyncResultList.\n * @template E - The type of the error, extending ErrorBase (default is ErrorBase).\n * @returns a ref to the AsyncResultList\n */\nexport function useAsyncResultList<T = any, E extends ErrorBase = ErrorBase>() {\n const list = new AsyncResultList<T, E>();\n const listRef = ref(list);\n\n list.listen(() => {\n triggerRef(listRef);\n });\n\n return listRef;\n}","import { defineComponent, watch, h, type VNode } from \"vue\"\nimport type { AsyncResult, ErrorBase } from \"unwrapped/core\"\nimport { useAsyncResultRef } from \"../composables\"\n\n/**\n * A Vue component that displays different content based on the state of an AsyncResult.\n * It supports slots for 'loading', 'error', 'success' (default), and 'idle' states.\n * \n * @example\n * <AsyncResultLoader :result=\"myAsyncResult\">\n * <template #loading>\n * <div>Loading data...</div>\n * </template>\n * <template #error=\"{ error }\">\n * <div>Error occurred: {{ error.message }}</div>\n * </template>\n * <template #default=\"{ value }\">\n * <div>Data loaded: {{ value }}</div>\n * </template>\n * <template #idle>\n * <div>Waiting to start...</div>\n * </template>\n * </AsyncResultLoader>\n */\nexport const AsyncResultLoader = defineComponent({\n name: \"AsyncResultLoader\",\n\n props: {\n result: {\n type: Object as () => AsyncResult<unknown>,\n required: true\n }\n },\n\n setup(props, { slots }) {\n let resultRef = useAsyncResultRef(props.result);\n\n // Watch for prop changes & update listener\n watch(\n () => props.result,\n (newResult, oldResult) => {\n if (newResult === oldResult) return;\n resultRef = useAsyncResultRef(newResult);\n },\n { immediate: true }\n )\n\n return () => {\n const s = resultRef.value.state;\n\n // Choose what to render based on status\n switch (s.status) {\n case \"loading\":\n return slots.loading\n ? slots.loading()\n : h(\"div\", { class: \"loading\" }, \"Loading…\");\n\n case \"error\":\n return slots.error\n ? slots.error({ error: s.error })\n : h(\"div\", { class: \"error\" }, `Error: ${s.error}`);\n\n case \"success\":\n return slots.default\n ? slots.default({ value: s.value })\n : null;\n\n default:\n // \"idle\"\n return slots.idle\n ? slots.idle()\n : h(\"div\", { class: \"idle\" }, \"Idle\");\n }\n }\n }\n})\n\n\n\ninterface CustomSlots<E extends ErrorBase = ErrorBase> {\n loading?: () => VNode;\n error?: (props: { error: E }) => VNode;\n}\n\n/**\n * Builds a custom AsyncResultLoader component with predefined slots for loading and error states.\n * \n * Useful for creating reusable components with consistent loading and error handling UI (eg. framework-specific spinners, etc...).\n * \n * @param slots the custom slots for loading and error states\n * @returns a Vue component that uses the provided slots\n */\nexport function buildCustomAsyncResultLoader<T, E extends ErrorBase = ErrorBase>(slots: CustomSlots<E>) {\n const comp = defineComponent({\n name: \"CustomAsyncResultLoader\",\n props: {\n result: {\n type: Object as () => AsyncResult<T, E>,\n required: true\n }\n },\n setup(props, context) {\n return () => {\n const renderLoading = context.slots.loading ?? slots.loading ?? (() => undefined);\n const renderError = context.slots.error ?? slots.error ?? (() => undefined);\n return h(\n AsyncResultLoader,\n { result: props.result },\n {\n default: context.slots.default ? (propsDefault: { value: T }) => context.slots.default!(propsDefault) : undefined,\n\n loading: () => renderLoading(),\n error: ((propsError: { error: E }) => renderError(propsError))\n }\n )\n }\n }\n });\n\n return comp;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAgF;AAChF,kBAAiI;AAoB1H,SAAS,kBAAsD,aAAiC;AACnG,MAAI,CAAC,aAAa;AACd,kBAAc,IAAI,wBAAkB;AAAA,EACxC;AACA,QAAM,YAAQ,gBAAuB,WAAW;AAEhD,QAAM,QAAQ,YAAY,OAAO,MAAM;AACnC,+BAAW,KAAK;AAAA,EACpB,CAAC;AAED,8BAAY,MAAM;AACd,UAAM;AAAA,EACV,CAAC;AAED,SAAO;AACX;AAOO,SAAS,6BAAiE,SAAgC;AAC7G,SAAO,kBAAkB,wBAAY,kBAAkB,OAAO,CAAC;AACnE;AAcO,SAAS,iBAA6D,QAA6B,MAAmC,UAAkC,EAAE,WAAW,KAAK,GAA2B;AACxN,QAAM,SAAS,IAAI,wBAAkB;AACrC,QAAM,YAAY,kBAAkB,MAAM;AAE1C,MAAI,QAA6B;AAEjC,wBAAM,QAAQ,CAAC,cAAc;AACzB,YAAQ;AACR,YAAQ,OAAO,OAAO,KAAK,SAAS,CAAC;AAAA,EACzC,GAAG,EAAE,WAAW,QAAQ,UAAU,CAAC;AAEnC,8BAAY,MAAM;AACd,YAAQ;AAAA,EACZ,CAAC;AAED,SAAO;AACX;AAqBO,SAAS,UAA8C,QAA8C;AACxG,SAAO,6BAA6B,OAAO,CAAC;AAChD;AAUO,SAAS,cAAkD,QAA2C;AAC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]}
|
package/dist/vue/index.mjs
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
// src/vue/composables.ts
|
|
2
2
|
import { onUnmounted, ref, triggerRef, watch } from "vue";
|
|
3
|
-
import { AsyncResult } from "unwrapped/core";
|
|
3
|
+
import { AsyncResult, AsyncResultList } from "unwrapped/core";
|
|
4
4
|
function useAsyncResultRef(asyncResult) {
|
|
5
|
+
if (!asyncResult) {
|
|
6
|
+
asyncResult = new AsyncResult();
|
|
7
|
+
}
|
|
5
8
|
const state = ref(asyncResult);
|
|
6
9
|
const unsub = asyncResult.listen(() => {
|
|
7
10
|
triggerRef(state);
|
|
@@ -54,6 +57,14 @@ function useReactiveGenerator(source, generatorFunc, options = { immediate: true
|
|
|
54
57
|
}, { immediate: options.immediate });
|
|
55
58
|
return resultRef;
|
|
56
59
|
}
|
|
60
|
+
function useAsyncResultList() {
|
|
61
|
+
const list = new AsyncResultList();
|
|
62
|
+
const listRef = ref(list);
|
|
63
|
+
list.listen(() => {
|
|
64
|
+
triggerRef(listRef);
|
|
65
|
+
});
|
|
66
|
+
return listRef;
|
|
67
|
+
}
|
|
57
68
|
|
|
58
69
|
// src/vue/components/asyncResultLoader.ts
|
|
59
70
|
import { defineComponent, watch as watch2, h } from "vue";
|
|
@@ -121,6 +132,7 @@ export {
|
|
|
121
132
|
AsyncResultLoader,
|
|
122
133
|
buildCustomAsyncResultLoader,
|
|
123
134
|
useAction,
|
|
135
|
+
useAsyncResultList,
|
|
124
136
|
useAsyncResultRef,
|
|
125
137
|
useAsyncResultRefFromPromise,
|
|
126
138
|
useGenerator,
|
package/dist/vue/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/vue/composables.ts","../../src/vue/components/asyncResultLoader.ts"],"sourcesContent":["import { onUnmounted, ref, triggerRef, watch, type Ref, type WatchSource } from \"vue\";\nimport { AsyncResult, ErrorBase, type Action, type AsyncResultGenerator, type FlatChainStep, type Result } from \"unwrapped/core\";\n\n\n// === Vue specific types ===\n\ninterface ReactiveProcessOptions {\n immediate: boolean;\n}\n\n\n\n// === Vue Composables for AsyncResult ===\n\n/**\n * Makes a ref to the given AsyncResult. Whenever the state of the AsyncResult changes,\n * the ref gets retriggered, making those changes visible to Vue's reactivity system.\n * \n * @param asyncResult the result to make reactive\n * @returns the ref to the result\n */\nexport function useAsyncResultRef<T, E extends ErrorBase = ErrorBase>(asyncResult: AsyncResult<T, E>) {\n const state = ref<AsyncResult<T, E>>(asyncResult) as Ref<AsyncResult<T, E>>;\n\n const unsub = asyncResult.listen(() => {\n triggerRef(state);\n });\n\n onUnmounted(() => {\n unsub();\n });\n\n return state;\n}\n\n/**\n * Creates an AsyncResult ref from a promise returning a Result.\n * @param promise the promise returning a Result\n * @returns the ref to the AsyncResult\n */\nexport function useAsyncResultRefFromPromise<T, E extends ErrorBase = ErrorBase>(promise: Promise<Result<T, E>>) {\n return useAsyncResultRef(AsyncResult.fromResultPromise(promise));\n}\n\n\n\n// === Vue Composables for Chains ===\n\n/**\n * Watches a source, gives it as inputs to the function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param pipe the function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveChain<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, pipe: FlatChainStep<Inputs, T, E>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const result = new AsyncResult<T, E>();\n const resultRef = useAsyncResultRef(result);\n\n let unsub: (() => void) | null = null;\n\n watch(source, (newInputs) => {\n unsub?.();\n unsub = result.mirror(pipe(newInputs));\n }, { immediate: options.immediate });\n\n onUnmounted(() => {\n unsub?.();\n });\n\n return resultRef;\n}\n\n\n// === Vue Composables for Actions ===\n\n/**\n * The return type of useLazyAction.\n */\nexport interface LazyActionRef<T, E extends ErrorBase = ErrorBase> {\n resultRef: Ref<AsyncResult<T, E>>;\n trigger: () => void;\n}\n\n/**\n * Executes an action immediately and returns a ref to the AsyncResult representing the action's state.\n * \n * Same as useAsyncResultRefFromPromise(action()).\n * \n * @param action the action to execute immediately\n * @returns a ref to the AsyncResult representing the action's state\n */\nexport function useAction<T, E extends ErrorBase = ErrorBase>(action: Action<T, E>): Ref<AsyncResult<T, E>> {\n return useAsyncResultRefFromPromise(action());\n}\n\n/**\n * Creates a lazy action that can be triggered manually.\n * \n * Same as AsyncResult.makeLazyAction(action), but the AsyncResult is wrapped in a ref for Vue reactivity.\n * \n * @param action the action to execute when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyAction<T, E extends ErrorBase = ErrorBase>(action: Action<T, E>): LazyActionRef<T, E> {\n const lazyAction = AsyncResult.makeLazyAction<T, E>(action);\n const resultRef = useAsyncResultRef(lazyAction.result);\n\n return { resultRef, trigger: lazyAction.trigger };\n}\n\n\n// === Vue Composables for Generators ===\n\n/**\n * Runs a generator function immediately and returns a ref to the AsyncResult representing the generator's state.\n * @param generatorFunc the generator function to run immediately\n * @returns a ref to the AsyncResult representing the generator's state\n */\nexport function useGenerator<T>(generatorFunc: () => AsyncResultGenerator<T>): Ref<AsyncResult<T, any>> {\n const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));\n return resultRef;\n}\n\n/**\n * Creates a lazy generator that can be triggered manually.\n * \n * @param generatorFunc the generator function to run when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyGenerator<T>(generatorFunc: () => AsyncResultGenerator<T>): { resultRef: Ref<AsyncResult<T, any>>, trigger: () => void } {\n const result = new AsyncResult<T, any>();\n const resultRef = useAsyncResultRef(result);\n\n const trigger = () => {\n result.runInPlace(generatorFunc);\n }\n\n return { resultRef, trigger };\n}\n\n/**\n * Watches a source, gives it as inputs to the generator function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param generatorFunc the generator function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs) => AsyncResultGenerator<T>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const resultRef = useAsyncResultRef(new AsyncResult<T, E>());\n\n watch(source, (newInputs) => {\n resultRef.value.runInPlace(() => generatorFunc(newInputs));\n }, { immediate: options.immediate });\n\n return resultRef;\n}","import { defineComponent, watch, h, type VNode } from \"vue\"\nimport type { AsyncResult, ErrorBase } from \"unwrapped/core\"\nimport { useAsyncResultRef } from \"../composables\"\n\n/**\n * A Vue component that displays different content based on the state of an AsyncResult.\n * It supports slots for 'loading', 'error', 'success' (default), and 'idle' states.\n * \n * @example\n * <AsyncResultLoader :result=\"myAsyncResult\">\n * <template #loading>\n * <div>Loading data...</div>\n * </template>\n * <template #error=\"{ error }\">\n * <div>Error occurred: {{ error.message }}</div>\n * </template>\n * <template #default=\"{ value }\">\n * <div>Data loaded: {{ value }}</div>\n * </template>\n * <template #idle>\n * <div>Waiting to start...</div>\n * </template>\n * </AsyncResultLoader>\n */\nexport const AsyncResultLoader = defineComponent({\n name: \"AsyncResultLoader\",\n\n props: {\n result: {\n type: Object as () => AsyncResult<unknown>,\n required: true\n }\n },\n\n setup(props, { slots }) {\n let resultRef = useAsyncResultRef(props.result);\n\n // Watch for prop changes & update listener\n watch(\n () => props.result,\n (newResult, oldResult) => {\n if (newResult === oldResult) return;\n resultRef = useAsyncResultRef(newResult);\n },\n { immediate: true }\n )\n\n return () => {\n const s = resultRef.value.state;\n\n // Choose what to render based on status\n switch (s.status) {\n case \"loading\":\n return slots.loading\n ? slots.loading()\n : h(\"div\", { class: \"loading\" }, \"Loading…\");\n\n case \"error\":\n return slots.error\n ? slots.error({ error: s.error })\n : h(\"div\", { class: \"error\" }, `Error: ${s.error}`);\n\n case \"success\":\n return slots.default\n ? slots.default({ value: s.value })\n : null;\n\n default:\n // \"idle\"\n return slots.idle\n ? slots.idle()\n : h(\"div\", { class: \"idle\" }, \"Idle\");\n }\n }\n }\n})\n\n\n\ninterface CustomSlots<E extends ErrorBase = ErrorBase> {\n loading?: () => VNode;\n error?: (props: { error: E }) => VNode;\n}\n\n/**\n * Builds a custom AsyncResultLoader component with predefined slots for loading and error states.\n * \n * Useful for creating reusable components with consistent loading and error handling UI (eg. framework-specific spinners, etc...).\n * \n * @param slots the custom slots for loading and error states\n * @returns a Vue component that uses the provided slots\n */\nexport function buildCustomAsyncResultLoader<T, E extends ErrorBase = ErrorBase>(slots: CustomSlots<E>) {\n const comp = defineComponent({\n name: \"CustomAsyncResultLoader\",\n props: {\n result: {\n type: Object as () => AsyncResult<T, E>,\n required: true\n }\n },\n setup(props, context) {\n return () => {\n const renderLoading = context.slots.loading ?? slots.loading ?? (() => undefined);\n const renderError = context.slots.error ?? slots.error ?? (() => undefined);\n return h(\n AsyncResultLoader,\n { result: props.result },\n {\n default: context.slots.default ? (propsDefault: { value: T }) => context.slots.default!(propsDefault) : undefined,\n\n loading: () => renderLoading(),\n error: ((propsError: { error: E }) => renderError(propsError))\n }\n )\n }\n }\n });\n\n return comp;\n}\n"],"mappings":";AAAA,SAAS,aAAa,KAAK,YAAY,aAAyC;AAChF,SAAS,mBAAuG;AAoBzG,SAAS,kBAAsD,aAAgC;AAClG,QAAM,QAAQ,IAAuB,WAAW;AAEhD,QAAM,QAAQ,YAAY,OAAO,MAAM;AACnC,eAAW,KAAK;AAAA,EACpB,CAAC;AAED,cAAY,MAAM;AACd,UAAM;AAAA,EACV,CAAC;AAED,SAAO;AACX;AAOO,SAAS,6BAAiE,SAAgC;AAC7G,SAAO,kBAAkB,YAAY,kBAAkB,OAAO,CAAC;AACnE;AAcO,SAAS,iBAA6D,QAA6B,MAAmC,UAAkC,EAAE,WAAW,KAAK,GAA2B;AACxN,QAAM,SAAS,IAAI,YAAkB;AACrC,QAAM,YAAY,kBAAkB,MAAM;AAE1C,MAAI,QAA6B;AAEjC,QAAM,QAAQ,CAAC,cAAc;AACzB,YAAQ;AACR,YAA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AY,UAAU;AAAA,UAChE;AAAA,QACJ;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ,CAAC;AAED,SAAO;AACX;","names":["watch","watch"]}
|
|
1
|
+
{"version":3,"sources":["../../src/vue/composables.ts","../../src/vue/components/asyncResultLoader.ts"],"sourcesContent":["import { onUnmounted, ref, triggerRef, watch, type Ref, type WatchSource } from \"vue\";\nimport { AsyncResult, AsyncResultList, ErrorBase, type Action, type AsyncResultGenerator, type FlatChainStep, type Result } from \"unwrapped/core\";\n\n\n// === Vue specific types ===\n\ninterface ReactiveProcessOptions {\n immediate: boolean;\n}\n\n\n\n// === Vue Composables for AsyncResult ===\n\n/**\n * Makes a ref to the given AsyncResult. Whenever the state of the AsyncResult changes,\n * the ref gets retriggered, making those changes visible to Vue's reactivity system.\n * \n * @param asyncResult the result to make reactive\n * @returns the ref to the result\n */\nexport function useAsyncResultRef<T, E extends ErrorBase = ErrorBase>(asyncResult?: AsyncResult<T, E>) {\n if (!asyncResult) {\n asyncResult = new AsyncResult<T, E>();\n }\n const state = ref<AsyncResult<T, E>>(asyncResult) as Ref<AsyncResult<T, E>>;\n\n const unsub = asyncResult.listen(() => {\n triggerRef(state);\n });\n\n onUnmounted(() => {\n unsub();\n });\n\n return state;\n}\n\n/**\n * Creates an AsyncResult ref from a promise returning a Result.\n * @param promise the promise returning a Result\n * @returns the ref to the AsyncResult\n */\nexport function useAsyncResultRefFromPromise<T, E extends ErrorBase = ErrorBase>(promise: Promise<Result<T, E>>) {\n return useAsyncResultRef(AsyncResult.fromResultPromise(promise));\n}\n\n\n\n// === Vue Composables for Chains ===\n\n/**\n * Watches a source, gives it as inputs to the function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param pipe the function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveChain<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, pipe: FlatChainStep<Inputs, T, E>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const result = new AsyncResult<T, E>();\n const resultRef = useAsyncResultRef(result);\n\n let unsub: (() => void) | null = null;\n\n watch(source, (newInputs) => {\n unsub?.();\n unsub = result.mirror(pipe(newInputs));\n }, { immediate: options.immediate });\n\n onUnmounted(() => {\n unsub?.();\n });\n\n return resultRef;\n}\n\n\n// === Vue Composables for Actions ===\n\n/**\n * The return type of useLazyAction.\n */\nexport interface LazyActionRef<T, E extends ErrorBase = ErrorBase> {\n resultRef: Ref<AsyncResult<T, E>>;\n trigger: () => void;\n}\n\n/**\n * Executes an action immediately and returns a ref to the AsyncResult representing the action's state.\n * \n * Same as useAsyncResultRefFromPromise(action()).\n * \n * @param action the action to execute immediately\n * @returns a ref to the AsyncResult representing the action's state\n */\nexport function useAction<T, E extends ErrorBase = ErrorBase>(action: Action<T, E>): Ref<AsyncResult<T, E>> {\n return useAsyncResultRefFromPromise(action());\n}\n\n/**\n * Creates a lazy action that can be triggered manually.\n * \n * Same as AsyncResult.makeLazyAction(action), but the AsyncResult is wrapped in a ref for Vue reactivity.\n * \n * @param action the action to execute when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyAction<T, E extends ErrorBase = ErrorBase>(action: Action<T, E>): LazyActionRef<T, E> {\n const lazyAction = AsyncResult.makeLazyAction<T, E>(action);\n const resultRef = useAsyncResultRef(lazyAction.result);\n\n return { resultRef, trigger: lazyAction.trigger };\n}\n\n\n// === Vue Composables for Generators ===\n\n/**\n * Runs a generator function immediately and returns a ref to the AsyncResult representing the generator's state.\n * @param generatorFunc the generator function to run immediately\n * @returns a ref to the AsyncResult representing the generator's state\n */\nexport function useGenerator<T>(generatorFunc: () => AsyncResultGenerator<T>): Ref<AsyncResult<T, any>> {\n const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));\n return resultRef;\n}\n\n/**\n * Creates a lazy generator that can be triggered manually.\n * \n * @param generatorFunc the generator function to run when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyGenerator<T>(generatorFunc: () => AsyncResultGenerator<T>): { resultRef: Ref<AsyncResult<T, any>>, trigger: () => void } {\n const result = new AsyncResult<T, any>();\n const resultRef = useAsyncResultRef(result);\n\n const trigger = () => {\n result.runInPlace(generatorFunc);\n }\n\n return { resultRef, trigger };\n}\n\n/**\n * Watches a source, gives it as inputs to the generator function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param generatorFunc the generator function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs) => AsyncResultGenerator<T>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const resultRef = useAsyncResultRef(new AsyncResult<T, E>());\n\n watch(source, (newInputs) => {\n resultRef.value.runInPlace(() => generatorFunc(newInputs));\n }, { immediate: options.immediate });\n\n return resultRef;\n}\n\n\n// === Vue Composables for AsyncResultList ===\n\n/**\n * Creates a reactive AsyncResultList wrapped in a Vue ref.\n * The AsyncResultList notifies Vue's reactivity system whenever its state changes.\n * \n * @template T - The type of the values in the AsyncResultList.\n * @template E - The type of the error, extending ErrorBase (default is ErrorBase).\n * @returns a ref to the AsyncResultList\n */\nexport function useAsyncResultList<T = any, E extends ErrorBase = ErrorBase>() {\n const list = new AsyncResultList<T, E>();\n const listRef = ref(list);\n\n list.listen(() => {\n triggerRef(listRef);\n });\n\n return listRef;\n}","import { defineComponent, watch, h, type VNode } from \"vue\"\nimport type { AsyncResult, ErrorBase } from \"unwrapped/core\"\nimport { useAsyncResultRef } from \"../composables\"\n\n/**\n * A Vue component that displays different content based on the state of an AsyncResult.\n * It supports slots for 'loading', 'error', 'success' (default), and 'idle' states.\n * \n * @example\n * <AsyncResultLoader :result=\"myAsyncResult\">\n * <template #loading>\n * <div>Loading data...</div>\n * </template>\n * <template #error=\"{ error }\">\n * <div>Error occurred: {{ error.message }}</div>\n * </template>\n * <template #default=\"{ value }\">\n * <div>Data loaded: {{ value }}</div>\n * </template>\n * <template #idle>\n * <div>Waiting to start...</div>\n * </template>\n * </AsyncResultLoader>\n */\nexport const AsyncResultLoader = defineComponent({\n name: \"AsyncResultLoader\",\n\n props: {\n result: {\n type: Object as () => AsyncResult<unknown>,\n required: true\n }\n },\n\n setup(props, { slots }) {\n let resultRef = useAsyncResultRef(props.result);\n\n // Watch for prop changes & update listener\n watch(\n () => props.result,\n (newResult, oldResult) => {\n if (newResult === oldResult) return;\n resultRef = useAsyncResultRef(newResult);\n },\n { immediate: true }\n )\n\n return () => {\n const s = resultRef.value.state;\n\n // Choose what to render based on status\n switch (s.status) {\n case \"loading\":\n return slots.loading\n ? slots.loading()\n : h(\"div\", { class: \"loading\" }, \"Loading…\");\n\n case \"error\":\n return slots.error\n ? slots.error({ error: s.error })\n : h(\"div\", { class: \"error\" }, `Error: ${s.error}`);\n\n case \"success\":\n return slots.default\n ? slots.default({ value: s.value })\n : null;\n\n default:\n // \"idle\"\n return slots.idle\n ? slots.idle()\n : h(\"div\", { class: \"idle\" }, \"Idle\");\n }\n }\n }\n})\n\n\n\ninterface CustomSlots<E extends ErrorBase = ErrorBase> {\n loading?: () => VNode;\n error?: (props: { error: E }) => VNode;\n}\n\n/**\n * Builds a custom AsyncResultLoader component with predefined slots for loading and error states.\n * \n * Useful for creating reusable components with consistent loading and error handling UI (eg. framework-specific spinners, etc...).\n * \n * @param slots the custom slots for loading and error states\n * @returns a Vue component that uses the provided slots\n */\nexport function buildCustomAsyncResultLoader<T, E extends ErrorBase = ErrorBase>(slots: CustomSlots<E>) {\n const comp = defineComponent({\n name: \"CustomAsyncResultLoader\",\n props: {\n result: {\n type: Object as () => AsyncResult<T, E>,\n required: true\n }\n },\n setup(props, context) {\n return () => {\n const renderLoading = context.slots.loading ?? slots.loading ?? (() => undefined);\n const renderError = context.slots.error ?? slots.error ?? (() => undefined);\n return h(\n AsyncResultLoader,\n { result: props.result },\n {\n default: context.slots.default ? (propsDefault: { value: T }) => context.slots.default!(propsDefault) : undefined,\n\n loading: () => renderLoading(),\n error: ((propsError: { error: E }) => renderError(propsError))\n }\n )\n }\n }\n });\n\n return comp;\n}\n"],"mappings":";AAAA,SAAS,aAAa,KAAK,YAAY,aAAyC;AAChF,SAAS,aAAa,uBAA2G;AAoB1H,SAAS,kBAAsD,aAAiC;AACnG,MAAI,CAAC,aAAa;AACd,kBAAc,IAAI,YAAkB;AAAA,EACxC;AACA,QAAM,QAAQ,IAAuB,WAAW;AAEhD,QAAM,QAAQ,YAAY,OAAO,MAAM;AACnC,eAAW,KAAK;AAAA,EACpB,CAAC;AAED,cAAY,MAAM;AACd,UAAM;AAAA,EACV,CAAC;AAED,SAAO;AACX;AAOO,SAAS,6BAAiE,SAAgC;AAC7G,SAAO,kBAAkB,YAAY,kBAAkB,OAAO,CAAC;AACnE;AAcO,SAAS,iBAA6D,QAA6B,MAAmC,UAAkC,EAAE,WAAW,KAAK,GAA2B;AACxN,QAAM,SAAS,IAAI,YAAkB;AACrC,QAAM,YAAY,kBAAkB,MAAM;AAE1C,MAAI,QAA6B;AAEjC,QAAM,QAAQ,CAAC,cAAc;AACzB,YAAQ;AACR,YAAQ,OAAO,OAAO,KAAK,SAAS,CAAC;AAAA,EACzC,GAAG,EAAE,WAAW,QAAQ,UAAU,CAAC;AAEnC,cAAY,MAAM;AACd,YAAQ;AAAA,EACZ,CAAC;AAED,SAAO;AACX;AAqBO,SAAS,UAA8C,QAA8C;AACxG,SAAO,6BAA6B,OAAO,CAAC;AAChD;AAUO,SAAS,cAAkD,QAA2C;AACzG,QAAM,aAAa,YAAY,eAAqB,MAAM;AAC1D,QAAM,YAAY,kBAAkB,WAAW,MAAM;AAErD,SAAO,EAAE,WAAW,SAAS,WAAW,QAAQ;AACpD;AAUO,SAAS,aAAgB,eAAwE;AACpG,QAAM,YAAY,kBAAkB,YAAY,IAAI,aAAa,CAAC;AAClE,SAAO;AACX;AAQO,SAAS,iBAAoB,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