unwrapped 0.1.10 → 0.1.11
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 +52 -18
- package/dist/core/index.d.ts +52 -18
- package/dist/core/index.js +67 -16
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +67 -16
- package/dist/core/index.mjs.map +1 -1
- package/dist/vue/index.d.mts +120 -96
- package/dist/vue/index.d.ts +120 -96
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/core/index.d.mts
CHANGED
|
@@ -172,24 +172,25 @@ declare class Result<T, E extends ErrorBase = ErrorBase> {
|
|
|
172
172
|
|
|
173
173
|
/**
|
|
174
174
|
* Type representing the state of an AsyncResult.
|
|
175
|
-
* It can be 'idle', 'loading' with a promise, or a settled ResultState (success or error).
|
|
175
|
+
* It can be 'idle', 'loading' with a promise and an optional progress information of type P, or a settled ResultState (success or error).
|
|
176
176
|
*/
|
|
177
|
-
type AsyncResultState<T, E extends ErrorBase = ErrorBase> = {
|
|
177
|
+
type AsyncResultState<T, E extends ErrorBase = ErrorBase, P = unknown> = {
|
|
178
178
|
status: 'idle';
|
|
179
179
|
} | {
|
|
180
180
|
status: 'loading';
|
|
181
181
|
promise: Promise<Result<T, E>>;
|
|
182
|
+
progress?: P;
|
|
182
183
|
} | ResultState<T, E>;
|
|
183
184
|
/**
|
|
184
185
|
* An Action is a function returning a Promise of a Result.
|
|
185
186
|
*/
|
|
186
|
-
type Action<T, E extends ErrorBase = ErrorBase> = () => Promise<Result<T, E>>;
|
|
187
|
+
type Action<T, E extends ErrorBase = ErrorBase, P = unknown> = (notifyProgress?: (progress: P) => void) => Promise<Result<T, E>>;
|
|
187
188
|
/**
|
|
188
189
|
* A LazyAction is an object containing a trigger function to start the action, and the AsyncResult representing the action's state.
|
|
189
190
|
*/
|
|
190
|
-
type LazyAction<T, E extends ErrorBase = ErrorBase> = {
|
|
191
|
+
type LazyAction<T, E extends ErrorBase = ErrorBase, P = unknown> = {
|
|
191
192
|
trigger: () => void;
|
|
192
|
-
result: AsyncResult<T, E>;
|
|
193
|
+
result: AsyncResult<T, E, P>;
|
|
193
194
|
};
|
|
194
195
|
/**
|
|
195
196
|
* A ChainStep is a function that takes an arbitrary input and returns a Result or a Promise of a Result.
|
|
@@ -204,7 +205,7 @@ type ChainStep<I, O, E extends ErrorBase = ErrorBase> = (input: I) => Result<O,
|
|
|
204
205
|
*
|
|
205
206
|
* Used for flat-chaining operations on AsyncResult.
|
|
206
207
|
*/
|
|
207
|
-
type FlatChainStep<I, O, E extends ErrorBase = ErrorBase> = (input: I) => AsyncResult<O, E>;
|
|
208
|
+
type FlatChainStep<I, O, E extends ErrorBase = ErrorBase, P = unknown> = (input: I) => AsyncResult<O, E, P>;
|
|
208
209
|
/**
|
|
209
210
|
* Type representing a generator function that yields AsyncResult instances and returns a final value of type T.
|
|
210
211
|
*
|
|
@@ -214,22 +215,27 @@ type AsyncResultGenerator<T> = Generator<AsyncResult<any, any>, T, any>;
|
|
|
214
215
|
/**
|
|
215
216
|
* Type representing a listener function for AsyncResult state changes.
|
|
216
217
|
*/
|
|
217
|
-
type AsyncResultListener<T, E extends ErrorBase = ErrorBase> = (result: AsyncResult<T, E>) => any;
|
|
218
|
+
type AsyncResultListener<T, E extends ErrorBase = ErrorBase, P = unknown> = (result: AsyncResult<T, E, P>) => any;
|
|
219
|
+
interface AsyncResultListenerOptions {
|
|
220
|
+
immediate?: boolean;
|
|
221
|
+
callOnProgressUpdates?: boolean;
|
|
222
|
+
}
|
|
218
223
|
/**
|
|
219
224
|
* Class representing the asynchronous result of an operation that can be idle, loading, successful, or failed.
|
|
220
225
|
* Provides methods for listening to state changes, updating state, chaining operations, and converting to and from promises.
|
|
221
226
|
* @class AsyncResult
|
|
222
227
|
* @template T - The type of the successful result value.
|
|
223
228
|
* @template E - The type of the error, extending ErrorBase (default is ErrorBase).
|
|
229
|
+
* @template P - The type of the progress information for loading state (default is unknown).
|
|
224
230
|
*/
|
|
225
|
-
declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
231
|
+
declare class AsyncResult<T, E extends ErrorBase = ErrorBase, P = unknown> {
|
|
226
232
|
private _state;
|
|
227
233
|
private _listeners;
|
|
228
|
-
constructor(state?: AsyncResultState<T, E>);
|
|
234
|
+
constructor(state?: AsyncResultState<T, E, P>);
|
|
229
235
|
/**
|
|
230
236
|
* Returns the internal state of the AsyncResult.
|
|
231
237
|
*/
|
|
232
|
-
get state(): AsyncResultState<T, E>;
|
|
238
|
+
get state(): AsyncResultState<T, E, P>;
|
|
233
239
|
/**
|
|
234
240
|
* Checks if the AsyncResult is successful.
|
|
235
241
|
* @returns whether or not the result is successful
|
|
@@ -266,6 +272,11 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
266
272
|
* @returns the error value or null
|
|
267
273
|
*/
|
|
268
274
|
unwrapErrorOrNull(): E | null;
|
|
275
|
+
/**
|
|
276
|
+
* Returns the progress information if the AsyncResult is in a loading state, otherwise returns null.
|
|
277
|
+
* @returns the progress information or null
|
|
278
|
+
*/
|
|
279
|
+
getProgressOrNull(): P | null;
|
|
269
280
|
private set state(value);
|
|
270
281
|
private setState;
|
|
271
282
|
/**
|
|
@@ -301,6 +312,11 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
301
312
|
* @param error the error
|
|
302
313
|
*/
|
|
303
314
|
updateFromError(error: E): this;
|
|
315
|
+
/**
|
|
316
|
+
* Updates the progress information of the AsyncResult if it is currently loading.
|
|
317
|
+
* @param progress progress information to include in the loading state
|
|
318
|
+
*/
|
|
319
|
+
updateProgress(progress: P): this;
|
|
304
320
|
/**
|
|
305
321
|
* Creates an AsyncResult from a promise that resolves to a value.
|
|
306
322
|
* The AsyncResult is initially in a loading state, and updates to a successful state once the promise resolves.
|
|
@@ -326,7 +342,7 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
326
342
|
* Waits for the AsyncResult to settle (either success or error) if it is currently loading.
|
|
327
343
|
* @returns itself once settled
|
|
328
344
|
*/
|
|
329
|
-
waitForSettled(): Promise<AsyncResult<T, E>>;
|
|
345
|
+
waitForSettled(): Promise<AsyncResult<T, E, P>>;
|
|
330
346
|
/**
|
|
331
347
|
* Waits for the AsyncResult to settle (either success or error) if it is currently loading, and returns a Result representing the settled state.
|
|
332
348
|
* @returns a Result representing the settled state
|
|
@@ -368,14 +384,14 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
368
384
|
* @param immediate whether to call the listener immediately with the current state (default is true)
|
|
369
385
|
* @returns a function to remove the listener
|
|
370
386
|
*/
|
|
371
|
-
listen(listener: AsyncResultListener<T, E>,
|
|
387
|
+
listen(listener: AsyncResultListener<T, E, P>, options?: AsyncResultListenerOptions): () => void;
|
|
372
388
|
/**
|
|
373
389
|
* Adds a listener that is called whenever the AsyncResult state changes, and automatically unsubscribes once it is settled (success or error).
|
|
374
390
|
* @param listener the listener function to add
|
|
375
391
|
* @param immediate whether to call the listener immediately with the current state (default is true)
|
|
376
392
|
* @returns a function to remove the listener
|
|
377
393
|
*/
|
|
378
|
-
listenUntilSettled(listener: AsyncResultListener<T, E>,
|
|
394
|
+
listenUntilSettled(listener: AsyncResultListener<T, E, P>, options?: AsyncResultListenerOptions): () => void;
|
|
379
395
|
/**
|
|
380
396
|
* Adds a one-time listener that is called once the AsyncResult settles on a success.
|
|
381
397
|
* @param callback callback called once the AsyncResult settled on a success
|
|
@@ -406,14 +422,32 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
406
422
|
* @param other the AsyncResult to mirror
|
|
407
423
|
* @returns a function to stop mirroring
|
|
408
424
|
*/
|
|
409
|
-
mirror(other: AsyncResult<T, E
|
|
425
|
+
mirror(other: AsyncResult<T, E, P>, options?: AsyncResultListenerOptions): () => void;
|
|
410
426
|
/**
|
|
411
427
|
* Mirrors the state of another AsyncResult into this one, until the other AsyncResult is settled (success or error).
|
|
412
428
|
* Whenever the other AsyncResult changes state, this AsyncResult is updated to match, until the other AsyncResult is settled.
|
|
413
429
|
* @param other the AsyncResult to mirror
|
|
414
430
|
* @returns a function to stop mirroring
|
|
415
431
|
*/
|
|
416
|
-
mirrorUntilSettled(other: AsyncResult<T, E
|
|
432
|
+
mirrorUntilSettled(other: AsyncResult<T, E, P>, options?: AsyncResultListenerOptions): () => void;
|
|
433
|
+
/**
|
|
434
|
+
* Creates an AsyncResult from an Action.
|
|
435
|
+
* The AsyncResult is initially in a loading state, and updates to the settled state once the Action's promise resolves.
|
|
436
|
+
* If the Action's promise rejects, the AsyncResult is updated to an error state with a default ErrorBase.
|
|
437
|
+
*
|
|
438
|
+
* Like AsyncResult.fromResultPromise, but for Actions.
|
|
439
|
+
*
|
|
440
|
+
* @param action the Action to run to produce the AsyncResult
|
|
441
|
+
* @return an AsyncResult representing the state of the Action
|
|
442
|
+
*/
|
|
443
|
+
static fromAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): AsyncResult<T, E, P>;
|
|
444
|
+
/**
|
|
445
|
+
* Updates the AsyncResult based on the given Action.
|
|
446
|
+
* Like AsyncResult.fromAction, but in place.
|
|
447
|
+
* @param action an action that will be called directly
|
|
448
|
+
* @returns itself
|
|
449
|
+
*/
|
|
450
|
+
updateFromAction(action: Action<T, E, P>): this;
|
|
417
451
|
/**
|
|
418
452
|
* Creates a LazyAction that can be triggered to run the given Action.
|
|
419
453
|
* @param action the Action to run when triggered
|
|
@@ -468,7 +502,7 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
468
502
|
* return result1 + result2;
|
|
469
503
|
* }
|
|
470
504
|
*/
|
|
471
|
-
[Symbol.iterator](): Generator<AsyncResult<T, E>, T, any>;
|
|
505
|
+
[Symbol.iterator](): Generator<AsyncResult<T, E, P>, T, any>;
|
|
472
506
|
private static _runGeneratorProcessor;
|
|
473
507
|
/**
|
|
474
508
|
* Runs a generator function that yields AsyncResult instances, propagating errors automatically.
|
|
@@ -488,7 +522,7 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
488
522
|
* return value1 + value2;
|
|
489
523
|
* }
|
|
490
524
|
*/
|
|
491
|
-
static run<T, E extends ErrorBase = ErrorBase>(generatorFunc: () => AsyncResultGenerator<T>): AsyncResult<T, E>;
|
|
525
|
+
static run<T, E extends ErrorBase = ErrorBase, P = unknown>(generatorFunc: (notifyProgress?: (progress: P) => void) => AsyncResultGenerator<T>): AsyncResult<T, E, P>;
|
|
492
526
|
/**
|
|
493
527
|
* Runs a generator function that yields AsyncResult instances, propagating errors automatically, and updates this AsyncResult in place.
|
|
494
528
|
* If any yielded AsyncResult is an error, the execution stops and this AsyncResult is updated to that error.
|
|
@@ -754,4 +788,4 @@ declare class KeyedAsyncCache<P, V, E extends ErrorBase = ErrorBase> {
|
|
|
754
788
|
|
|
755
789
|
declare function delay(ms: number): AsyncResult<true>;
|
|
756
790
|
|
|
757
|
-
export { type Action, AsyncResult, AsyncResultCollection, type AsyncResultCollectionItem, type AsyncResultCollectionState, type AsyncResultGenerator, type AsyncResultListener, type AsyncResultState, type ChainStep, ErrorBase, type FlatChainStep, KeyedAsyncCache, type LazyAction, Result, type ResultState, delay };
|
|
791
|
+
export { type Action, AsyncResult, AsyncResultCollection, type AsyncResultCollectionItem, type AsyncResultCollectionState, type AsyncResultGenerator, type AsyncResultListener, type AsyncResultListenerOptions, type AsyncResultState, type ChainStep, ErrorBase, type FlatChainStep, KeyedAsyncCache, type LazyAction, Result, type ResultState, delay };
|
package/dist/core/index.d.ts
CHANGED
|
@@ -172,24 +172,25 @@ declare class Result<T, E extends ErrorBase = ErrorBase> {
|
|
|
172
172
|
|
|
173
173
|
/**
|
|
174
174
|
* Type representing the state of an AsyncResult.
|
|
175
|
-
* It can be 'idle', 'loading' with a promise, or a settled ResultState (success or error).
|
|
175
|
+
* It can be 'idle', 'loading' with a promise and an optional progress information of type P, or a settled ResultState (success or error).
|
|
176
176
|
*/
|
|
177
|
-
type AsyncResultState<T, E extends ErrorBase = ErrorBase> = {
|
|
177
|
+
type AsyncResultState<T, E extends ErrorBase = ErrorBase, P = unknown> = {
|
|
178
178
|
status: 'idle';
|
|
179
179
|
} | {
|
|
180
180
|
status: 'loading';
|
|
181
181
|
promise: Promise<Result<T, E>>;
|
|
182
|
+
progress?: P;
|
|
182
183
|
} | ResultState<T, E>;
|
|
183
184
|
/**
|
|
184
185
|
* An Action is a function returning a Promise of a Result.
|
|
185
186
|
*/
|
|
186
|
-
type Action<T, E extends ErrorBase = ErrorBase> = () => Promise<Result<T, E>>;
|
|
187
|
+
type Action<T, E extends ErrorBase = ErrorBase, P = unknown> = (notifyProgress?: (progress: P) => void) => Promise<Result<T, E>>;
|
|
187
188
|
/**
|
|
188
189
|
* A LazyAction is an object containing a trigger function to start the action, and the AsyncResult representing the action's state.
|
|
189
190
|
*/
|
|
190
|
-
type LazyAction<T, E extends ErrorBase = ErrorBase> = {
|
|
191
|
+
type LazyAction<T, E extends ErrorBase = ErrorBase, P = unknown> = {
|
|
191
192
|
trigger: () => void;
|
|
192
|
-
result: AsyncResult<T, E>;
|
|
193
|
+
result: AsyncResult<T, E, P>;
|
|
193
194
|
};
|
|
194
195
|
/**
|
|
195
196
|
* A ChainStep is a function that takes an arbitrary input and returns a Result or a Promise of a Result.
|
|
@@ -204,7 +205,7 @@ type ChainStep<I, O, E extends ErrorBase = ErrorBase> = (input: I) => Result<O,
|
|
|
204
205
|
*
|
|
205
206
|
* Used for flat-chaining operations on AsyncResult.
|
|
206
207
|
*/
|
|
207
|
-
type FlatChainStep<I, O, E extends ErrorBase = ErrorBase> = (input: I) => AsyncResult<O, E>;
|
|
208
|
+
type FlatChainStep<I, O, E extends ErrorBase = ErrorBase, P = unknown> = (input: I) => AsyncResult<O, E, P>;
|
|
208
209
|
/**
|
|
209
210
|
* Type representing a generator function that yields AsyncResult instances and returns a final value of type T.
|
|
210
211
|
*
|
|
@@ -214,22 +215,27 @@ type AsyncResultGenerator<T> = Generator<AsyncResult<any, any>, T, any>;
|
|
|
214
215
|
/**
|
|
215
216
|
* Type representing a listener function for AsyncResult state changes.
|
|
216
217
|
*/
|
|
217
|
-
type AsyncResultListener<T, E extends ErrorBase = ErrorBase> = (result: AsyncResult<T, E>) => any;
|
|
218
|
+
type AsyncResultListener<T, E extends ErrorBase = ErrorBase, P = unknown> = (result: AsyncResult<T, E, P>) => any;
|
|
219
|
+
interface AsyncResultListenerOptions {
|
|
220
|
+
immediate?: boolean;
|
|
221
|
+
callOnProgressUpdates?: boolean;
|
|
222
|
+
}
|
|
218
223
|
/**
|
|
219
224
|
* Class representing the asynchronous result of an operation that can be idle, loading, successful, or failed.
|
|
220
225
|
* Provides methods for listening to state changes, updating state, chaining operations, and converting to and from promises.
|
|
221
226
|
* @class AsyncResult
|
|
222
227
|
* @template T - The type of the successful result value.
|
|
223
228
|
* @template E - The type of the error, extending ErrorBase (default is ErrorBase).
|
|
229
|
+
* @template P - The type of the progress information for loading state (default is unknown).
|
|
224
230
|
*/
|
|
225
|
-
declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
231
|
+
declare class AsyncResult<T, E extends ErrorBase = ErrorBase, P = unknown> {
|
|
226
232
|
private _state;
|
|
227
233
|
private _listeners;
|
|
228
|
-
constructor(state?: AsyncResultState<T, E>);
|
|
234
|
+
constructor(state?: AsyncResultState<T, E, P>);
|
|
229
235
|
/**
|
|
230
236
|
* Returns the internal state of the AsyncResult.
|
|
231
237
|
*/
|
|
232
|
-
get state(): AsyncResultState<T, E>;
|
|
238
|
+
get state(): AsyncResultState<T, E, P>;
|
|
233
239
|
/**
|
|
234
240
|
* Checks if the AsyncResult is successful.
|
|
235
241
|
* @returns whether or not the result is successful
|
|
@@ -266,6 +272,11 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
266
272
|
* @returns the error value or null
|
|
267
273
|
*/
|
|
268
274
|
unwrapErrorOrNull(): E | null;
|
|
275
|
+
/**
|
|
276
|
+
* Returns the progress information if the AsyncResult is in a loading state, otherwise returns null.
|
|
277
|
+
* @returns the progress information or null
|
|
278
|
+
*/
|
|
279
|
+
getProgressOrNull(): P | null;
|
|
269
280
|
private set state(value);
|
|
270
281
|
private setState;
|
|
271
282
|
/**
|
|
@@ -301,6 +312,11 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
301
312
|
* @param error the error
|
|
302
313
|
*/
|
|
303
314
|
updateFromError(error: E): this;
|
|
315
|
+
/**
|
|
316
|
+
* Updates the progress information of the AsyncResult if it is currently loading.
|
|
317
|
+
* @param progress progress information to include in the loading state
|
|
318
|
+
*/
|
|
319
|
+
updateProgress(progress: P): this;
|
|
304
320
|
/**
|
|
305
321
|
* Creates an AsyncResult from a promise that resolves to a value.
|
|
306
322
|
* The AsyncResult is initially in a loading state, and updates to a successful state once the promise resolves.
|
|
@@ -326,7 +342,7 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
326
342
|
* Waits for the AsyncResult to settle (either success or error) if it is currently loading.
|
|
327
343
|
* @returns itself once settled
|
|
328
344
|
*/
|
|
329
|
-
waitForSettled(): Promise<AsyncResult<T, E>>;
|
|
345
|
+
waitForSettled(): Promise<AsyncResult<T, E, P>>;
|
|
330
346
|
/**
|
|
331
347
|
* Waits for the AsyncResult to settle (either success or error) if it is currently loading, and returns a Result representing the settled state.
|
|
332
348
|
* @returns a Result representing the settled state
|
|
@@ -368,14 +384,14 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
368
384
|
* @param immediate whether to call the listener immediately with the current state (default is true)
|
|
369
385
|
* @returns a function to remove the listener
|
|
370
386
|
*/
|
|
371
|
-
listen(listener: AsyncResultListener<T, E>,
|
|
387
|
+
listen(listener: AsyncResultListener<T, E, P>, options?: AsyncResultListenerOptions): () => void;
|
|
372
388
|
/**
|
|
373
389
|
* Adds a listener that is called whenever the AsyncResult state changes, and automatically unsubscribes once it is settled (success or error).
|
|
374
390
|
* @param listener the listener function to add
|
|
375
391
|
* @param immediate whether to call the listener immediately with the current state (default is true)
|
|
376
392
|
* @returns a function to remove the listener
|
|
377
393
|
*/
|
|
378
|
-
listenUntilSettled(listener: AsyncResultListener<T, E>,
|
|
394
|
+
listenUntilSettled(listener: AsyncResultListener<T, E, P>, options?: AsyncResultListenerOptions): () => void;
|
|
379
395
|
/**
|
|
380
396
|
* Adds a one-time listener that is called once the AsyncResult settles on a success.
|
|
381
397
|
* @param callback callback called once the AsyncResult settled on a success
|
|
@@ -406,14 +422,32 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
406
422
|
* @param other the AsyncResult to mirror
|
|
407
423
|
* @returns a function to stop mirroring
|
|
408
424
|
*/
|
|
409
|
-
mirror(other: AsyncResult<T, E
|
|
425
|
+
mirror(other: AsyncResult<T, E, P>, options?: AsyncResultListenerOptions): () => void;
|
|
410
426
|
/**
|
|
411
427
|
* Mirrors the state of another AsyncResult into this one, until the other AsyncResult is settled (success or error).
|
|
412
428
|
* Whenever the other AsyncResult changes state, this AsyncResult is updated to match, until the other AsyncResult is settled.
|
|
413
429
|
* @param other the AsyncResult to mirror
|
|
414
430
|
* @returns a function to stop mirroring
|
|
415
431
|
*/
|
|
416
|
-
mirrorUntilSettled(other: AsyncResult<T, E
|
|
432
|
+
mirrorUntilSettled(other: AsyncResult<T, E, P>, options?: AsyncResultListenerOptions): () => void;
|
|
433
|
+
/**
|
|
434
|
+
* Creates an AsyncResult from an Action.
|
|
435
|
+
* The AsyncResult is initially in a loading state, and updates to the settled state once the Action's promise resolves.
|
|
436
|
+
* If the Action's promise rejects, the AsyncResult is updated to an error state with a default ErrorBase.
|
|
437
|
+
*
|
|
438
|
+
* Like AsyncResult.fromResultPromise, but for Actions.
|
|
439
|
+
*
|
|
440
|
+
* @param action the Action to run to produce the AsyncResult
|
|
441
|
+
* @return an AsyncResult representing the state of the Action
|
|
442
|
+
*/
|
|
443
|
+
static fromAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): AsyncResult<T, E, P>;
|
|
444
|
+
/**
|
|
445
|
+
* Updates the AsyncResult based on the given Action.
|
|
446
|
+
* Like AsyncResult.fromAction, but in place.
|
|
447
|
+
* @param action an action that will be called directly
|
|
448
|
+
* @returns itself
|
|
449
|
+
*/
|
|
450
|
+
updateFromAction(action: Action<T, E, P>): this;
|
|
417
451
|
/**
|
|
418
452
|
* Creates a LazyAction that can be triggered to run the given Action.
|
|
419
453
|
* @param action the Action to run when triggered
|
|
@@ -468,7 +502,7 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
468
502
|
* return result1 + result2;
|
|
469
503
|
* }
|
|
470
504
|
*/
|
|
471
|
-
[Symbol.iterator](): Generator<AsyncResult<T, E>, T, any>;
|
|
505
|
+
[Symbol.iterator](): Generator<AsyncResult<T, E, P>, T, any>;
|
|
472
506
|
private static _runGeneratorProcessor;
|
|
473
507
|
/**
|
|
474
508
|
* Runs a generator function that yields AsyncResult instances, propagating errors automatically.
|
|
@@ -488,7 +522,7 @@ declare class AsyncResult<T, E extends ErrorBase = ErrorBase> {
|
|
|
488
522
|
* return value1 + value2;
|
|
489
523
|
* }
|
|
490
524
|
*/
|
|
491
|
-
static run<T, E extends ErrorBase = ErrorBase>(generatorFunc: () => AsyncResultGenerator<T>): AsyncResult<T, E>;
|
|
525
|
+
static run<T, E extends ErrorBase = ErrorBase, P = unknown>(generatorFunc: (notifyProgress?: (progress: P) => void) => AsyncResultGenerator<T>): AsyncResult<T, E, P>;
|
|
492
526
|
/**
|
|
493
527
|
* Runs a generator function that yields AsyncResult instances, propagating errors automatically, and updates this AsyncResult in place.
|
|
494
528
|
* If any yielded AsyncResult is an error, the execution stops and this AsyncResult is updated to that error.
|
|
@@ -754,4 +788,4 @@ declare class KeyedAsyncCache<P, V, E extends ErrorBase = ErrorBase> {
|
|
|
754
788
|
|
|
755
789
|
declare function delay(ms: number): AsyncResult<true>;
|
|
756
790
|
|
|
757
|
-
export { type Action, AsyncResult, AsyncResultCollection, type AsyncResultCollectionItem, type AsyncResultCollectionState, type AsyncResultGenerator, type AsyncResultListener, type AsyncResultState, type ChainStep, ErrorBase, type FlatChainStep, KeyedAsyncCache, type LazyAction, Result, type ResultState, delay };
|
|
791
|
+
export { type Action, AsyncResult, AsyncResultCollection, type AsyncResultCollectionItem, type AsyncResultCollectionState, type AsyncResultGenerator, type AsyncResultListener, type AsyncResultListenerOptions, type AsyncResultState, type ChainStep, ErrorBase, type FlatChainStep, KeyedAsyncCache, type LazyAction, Result, type ResultState, delay };
|
package/dist/core/index.js
CHANGED
|
@@ -320,10 +320,25 @@ var AsyncResult = class _AsyncResult {
|
|
|
320
320
|
}
|
|
321
321
|
return null;
|
|
322
322
|
}
|
|
323
|
+
/**
|
|
324
|
+
* Returns the progress information if the AsyncResult is in a loading state, otherwise returns null.
|
|
325
|
+
* @returns the progress information or null
|
|
326
|
+
*/
|
|
327
|
+
getProgressOrNull() {
|
|
328
|
+
if (this._state.status === "loading") {
|
|
329
|
+
return this._state.progress ?? null;
|
|
330
|
+
}
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
323
333
|
// === Creating/updating from settled values ===
|
|
324
334
|
set state(newState) {
|
|
335
|
+
const oldState = this._state;
|
|
325
336
|
this._state = newState;
|
|
326
|
-
this._listeners.forEach((
|
|
337
|
+
this._listeners.forEach((listenerEntry) => {
|
|
338
|
+
if (oldState.status !== "loading" || newState.status !== "loading" || listenerEntry.options.callOnProgressUpdates) {
|
|
339
|
+
listenerEntry.listener(this);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
327
342
|
}
|
|
328
343
|
setState(newState) {
|
|
329
344
|
this.state = newState;
|
|
@@ -374,6 +389,16 @@ var AsyncResult = class _AsyncResult {
|
|
|
374
389
|
this.state = { status: "error", error };
|
|
375
390
|
return this;
|
|
376
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* Updates the progress information of the AsyncResult if it is currently loading.
|
|
394
|
+
* @param progress progress information to include in the loading state
|
|
395
|
+
*/
|
|
396
|
+
updateProgress(progress) {
|
|
397
|
+
if (this._state.status === "loading") {
|
|
398
|
+
this.state = { ...this._state, progress };
|
|
399
|
+
}
|
|
400
|
+
return this;
|
|
401
|
+
}
|
|
377
402
|
// === Creating/updating from promises ===
|
|
378
403
|
/**
|
|
379
404
|
* Creates an AsyncResult from a promise that resolves to a value.
|
|
@@ -495,13 +520,14 @@ var AsyncResult = class _AsyncResult {
|
|
|
495
520
|
* @param immediate whether to call the listener immediately with the current state (default is true)
|
|
496
521
|
* @returns a function to remove the listener
|
|
497
522
|
*/
|
|
498
|
-
listen(listener,
|
|
499
|
-
|
|
500
|
-
|
|
523
|
+
listen(listener, options = { immediate: true, callOnProgressUpdates: true }) {
|
|
524
|
+
const entry = { listener, options };
|
|
525
|
+
this._listeners.add(entry);
|
|
526
|
+
if (options.immediate) {
|
|
501
527
|
listener(this);
|
|
502
528
|
}
|
|
503
529
|
return () => {
|
|
504
|
-
this._listeners.delete(
|
|
530
|
+
this._listeners.delete(entry);
|
|
505
531
|
};
|
|
506
532
|
}
|
|
507
533
|
/**
|
|
@@ -510,13 +536,13 @@ var AsyncResult = class _AsyncResult {
|
|
|
510
536
|
* @param immediate whether to call the listener immediately with the current state (default is true)
|
|
511
537
|
* @returns a function to remove the listener
|
|
512
538
|
*/
|
|
513
|
-
listenUntilSettled(listener,
|
|
539
|
+
listenUntilSettled(listener, options = { immediate: true, callOnProgressUpdates: true }) {
|
|
514
540
|
const unsub = this.listen((result) => {
|
|
515
541
|
listener(result);
|
|
516
542
|
if (result.state.status === "success" || result.state.status === "error") {
|
|
517
543
|
unsub();
|
|
518
544
|
}
|
|
519
|
-
},
|
|
545
|
+
}, options);
|
|
520
546
|
return unsub;
|
|
521
547
|
}
|
|
522
548
|
/**
|
|
@@ -574,10 +600,10 @@ var AsyncResult = class _AsyncResult {
|
|
|
574
600
|
* @param other the AsyncResult to mirror
|
|
575
601
|
* @returns a function to stop mirroring
|
|
576
602
|
*/
|
|
577
|
-
mirror(other) {
|
|
603
|
+
mirror(other, options = { immediate: true, callOnProgressUpdates: true }) {
|
|
578
604
|
return other.listen((newState) => {
|
|
579
605
|
this.setState(newState.state);
|
|
580
|
-
},
|
|
606
|
+
}, options);
|
|
581
607
|
}
|
|
582
608
|
/**
|
|
583
609
|
* Mirrors the state of another AsyncResult into this one, until the other AsyncResult is settled (success or error).
|
|
@@ -585,12 +611,35 @@ var AsyncResult = class _AsyncResult {
|
|
|
585
611
|
* @param other the AsyncResult to mirror
|
|
586
612
|
* @returns a function to stop mirroring
|
|
587
613
|
*/
|
|
588
|
-
mirrorUntilSettled(other) {
|
|
614
|
+
mirrorUntilSettled(other, options = { immediate: true, callOnProgressUpdates: true }) {
|
|
589
615
|
return other.listenUntilSettled((newState) => {
|
|
590
616
|
this.setState(newState.state);
|
|
591
|
-
},
|
|
617
|
+
}, options);
|
|
592
618
|
}
|
|
593
619
|
// === Actions ===
|
|
620
|
+
/**
|
|
621
|
+
* Creates an AsyncResult from an Action.
|
|
622
|
+
* The AsyncResult is initially in a loading state, and updates to the settled state once the Action's promise resolves.
|
|
623
|
+
* If the Action's promise rejects, the AsyncResult is updated to an error state with a default ErrorBase.
|
|
624
|
+
*
|
|
625
|
+
* Like AsyncResult.fromResultPromise, but for Actions.
|
|
626
|
+
*
|
|
627
|
+
* @param action the Action to run to produce the AsyncResult
|
|
628
|
+
* @return an AsyncResult representing the state of the Action
|
|
629
|
+
*/
|
|
630
|
+
static fromAction(action) {
|
|
631
|
+
return new _AsyncResult().updateFromAction(action);
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Updates the AsyncResult based on the given Action.
|
|
635
|
+
* Like AsyncResult.fromAction, but in place.
|
|
636
|
+
* @param action an action that will be called directly
|
|
637
|
+
* @returns itself
|
|
638
|
+
*/
|
|
639
|
+
updateFromAction(action) {
|
|
640
|
+
const promise = action((progress) => this.updateProgress(progress));
|
|
641
|
+
return this.updateFromResultPromise(promise);
|
|
642
|
+
}
|
|
594
643
|
/**
|
|
595
644
|
* Creates a LazyAction that can be triggered to run the given Action.
|
|
596
645
|
* @param action the Action to run when triggered
|
|
@@ -599,7 +648,7 @@ var AsyncResult = class _AsyncResult {
|
|
|
599
648
|
static makeLazyAction(action) {
|
|
600
649
|
const result = new _AsyncResult();
|
|
601
650
|
const trigger = () => {
|
|
602
|
-
result.
|
|
651
|
+
result.updateFromAction(action);
|
|
603
652
|
};
|
|
604
653
|
return { trigger, result };
|
|
605
654
|
}
|
|
@@ -732,8 +781,10 @@ var AsyncResult = class _AsyncResult {
|
|
|
732
781
|
* }
|
|
733
782
|
*/
|
|
734
783
|
static run(generatorFunc) {
|
|
735
|
-
|
|
736
|
-
|
|
784
|
+
return _AsyncResult.fromAction(async (notifyProgress) => {
|
|
785
|
+
const iterator = generatorFunc(notifyProgress);
|
|
786
|
+
return _AsyncResult._runGeneratorProcessor(iterator)();
|
|
787
|
+
});
|
|
737
788
|
}
|
|
738
789
|
/**
|
|
739
790
|
* Runs a generator function that yields AsyncResult instances, propagating errors automatically, and updates this AsyncResult in place.
|
|
@@ -858,12 +909,12 @@ var AsyncResultCollection = class {
|
|
|
858
909
|
if (r.isLoading() || r.isIdle()) return;
|
|
859
910
|
this._onTaskFinished();
|
|
860
911
|
this._list.delete(key);
|
|
861
|
-
}, true);
|
|
912
|
+
}, { immediate: true, callOnProgressUpdates: true });
|
|
862
913
|
} else {
|
|
863
914
|
unsub = task.listen((r) => {
|
|
864
915
|
if (r.isLoading() || r.isIdle()) return;
|
|
865
916
|
this._onTaskFinished();
|
|
866
|
-
}, true);
|
|
917
|
+
}, { immediate: true, callOnProgressUpdates: true });
|
|
867
918
|
}
|
|
868
919
|
this._list.set(key, { key, result: task, unsub });
|
|
869
920
|
this.state = "any-loading";
|