mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
Compare commits
5 Commits
renovate/a
...
copilot/ad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d9bd6fea1 | ||
|
|
6b2707e24a | ||
|
|
da17d2d068 | ||
|
|
d9e9ee4e7f | ||
|
|
0b64e91eba |
69
core/stdlib/src/async/cancellablePromise/index.test.ts
Normal file
69
core/stdlib/src/async/cancellablePromise/index.test.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import { describe, it, expect, vi } from 'vitest';
|
||||||
|
import { cancellablePromise, CancelledError } from '.';
|
||||||
|
|
||||||
|
describe('cancellablePromise', () => {
|
||||||
|
it('resolve the promise normally when not cancelled', async () => {
|
||||||
|
const { promise } = cancellablePromise(Promise.resolve('data'));
|
||||||
|
|
||||||
|
await expect(promise).resolves.toBe('data');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reject the promise normally when not cancelled', async () => {
|
||||||
|
const error = new Error('test-error');
|
||||||
|
const { promise } = cancellablePromise(Promise.reject(error));
|
||||||
|
|
||||||
|
await expect(promise).rejects.toThrow(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reject with CancelledError when cancelled before resolve', async () => {
|
||||||
|
const { promise, cancel } = cancellablePromise(
|
||||||
|
new Promise<string>((resolve) => setTimeout(() => resolve('data'), 100)),
|
||||||
|
);
|
||||||
|
|
||||||
|
cancel();
|
||||||
|
|
||||||
|
await expect(promise).rejects.toBeInstanceOf(CancelledError);
|
||||||
|
await expect(promise).rejects.toThrow('Promise was cancelled');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reject with CancelledError with custom reason', async () => {
|
||||||
|
const { promise, cancel } = cancellablePromise(
|
||||||
|
new Promise<string>((resolve) => setTimeout(() => resolve('data'), 100)),
|
||||||
|
);
|
||||||
|
|
||||||
|
cancel('Request aborted');
|
||||||
|
|
||||||
|
await expect(promise).rejects.toBeInstanceOf(CancelledError);
|
||||||
|
await expect(promise).rejects.toThrow('Request aborted');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('cancel prevents then callback from being called', async () => {
|
||||||
|
const onFulfilled = vi.fn();
|
||||||
|
|
||||||
|
const { promise, cancel } = cancellablePromise(
|
||||||
|
new Promise<string>((resolve) => setTimeout(() => resolve('data'), 100)),
|
||||||
|
);
|
||||||
|
|
||||||
|
const chained = promise.then(onFulfilled).catch(() => {});
|
||||||
|
|
||||||
|
cancel();
|
||||||
|
|
||||||
|
await chained;
|
||||||
|
|
||||||
|
expect(onFulfilled).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('CancelledError has correct name property', () => {
|
||||||
|
const error = new CancelledError();
|
||||||
|
|
||||||
|
expect(error.name).toBe('CancelledError');
|
||||||
|
expect(error).toBeInstanceOf(Error);
|
||||||
|
expect(error.message).toBe('Promise was cancelled');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('CancelledError accepts custom message', () => {
|
||||||
|
const error = new CancelledError('Custom reason');
|
||||||
|
|
||||||
|
expect(error.message).toBe('Custom reason');
|
||||||
|
});
|
||||||
|
});
|
||||||
49
core/stdlib/src/async/cancellablePromise/index.ts
Normal file
49
core/stdlib/src/async/cancellablePromise/index.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
export class CancelledError extends Error {
|
||||||
|
constructor(reason?: string) {
|
||||||
|
super(reason ?? 'Promise was cancelled');
|
||||||
|
this.name = 'CancelledError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CancellablePromise<T> {
|
||||||
|
promise: Promise<T>;
|
||||||
|
cancel: (reason?: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name cancellablePromise
|
||||||
|
* @category Async
|
||||||
|
* @description Wraps a promise with a cancel capability, allowing the promise to be rejected with a CancelledError
|
||||||
|
*
|
||||||
|
* @param {Promise<T>} promise - The promise to make cancellable
|
||||||
|
* @returns {CancellablePromise<T>} - An object with the wrapped promise and a cancel function
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const { promise, cancel } = cancellablePromise(fetch('/api/data'));
|
||||||
|
* cancel(); // Rejects with CancelledError
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const { promise, cancel } = cancellablePromise(longRunningTask());
|
||||||
|
* setTimeout(() => cancel('Timeout'), 5000);
|
||||||
|
* const [error] = await tryIt(() => promise)();
|
||||||
|
*
|
||||||
|
* @since 0.0.10
|
||||||
|
*/
|
||||||
|
export function cancellablePromise<T>(promise: Promise<T>): CancellablePromise<T> {
|
||||||
|
let rejectPromise: (reason: CancelledError) => void;
|
||||||
|
|
||||||
|
const wrappedPromise = new Promise<T>((resolve, reject) => {
|
||||||
|
rejectPromise = reject;
|
||||||
|
|
||||||
|
promise.then(resolve, reject);
|
||||||
|
});
|
||||||
|
|
||||||
|
const cancel = (reason?: string) => {
|
||||||
|
rejectPromise(new CancelledError(reason));
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
promise: wrappedPromise,
|
||||||
|
cancel,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
|
export * from './cancellablePromise';
|
||||||
export * from './sleep';
|
export * from './sleep';
|
||||||
export * from './tryIt';
|
export * from './tryIt';
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ describe(useAsyncState, () => {
|
|||||||
expect(isLoading.value).toBeTruthy();
|
expect(isLoading.value).toBeTruthy();
|
||||||
expect(error.value).toBe(null);
|
expect(error.value).toBe(null);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(state.value).toBe('data');
|
expect(state.value).toBe('data');
|
||||||
@@ -41,6 +42,7 @@ describe(useAsyncState, () => {
|
|||||||
expect(isLoading.value).toBeTruthy();
|
expect(isLoading.value).toBeTruthy();
|
||||||
expect(error.value).toBe(null);
|
expect(error.value).toBe(null);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(state.value).toBe('data');
|
expect(state.value).toBe('data');
|
||||||
@@ -60,6 +62,7 @@ describe(useAsyncState, () => {
|
|||||||
expect(isLoading.value).toBeTruthy();
|
expect(isLoading.value).toBeTruthy();
|
||||||
expect(error.value).toBe(null);
|
expect(error.value).toBe(null);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(state.value).toBe('initial');
|
expect(state.value).toBe('initial');
|
||||||
@@ -77,6 +80,7 @@ describe(useAsyncState, () => {
|
|||||||
{ onSuccess },
|
{ onSuccess },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(onSuccess).toHaveBeenCalledWith('data');
|
expect(onSuccess).toHaveBeenCalledWith('data');
|
||||||
@@ -92,6 +96,7 @@ describe(useAsyncState, () => {
|
|||||||
{ onError },
|
{ onError },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(onError).toHaveBeenCalledWith(error);
|
expect(onError).toHaveBeenCalledWith(error);
|
||||||
@@ -164,6 +169,7 @@ describe(useAsyncState, () => {
|
|||||||
expect(isReady.value).toBeFalsy();
|
expect(isReady.value).toBeFalsy();
|
||||||
expect(error.value).toBe(null);
|
expect(error.value).toBe(null);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(state.value).toBe('data');
|
expect(state.value).toBe('data');
|
||||||
@@ -206,4 +212,114 @@ describe(useAsyncState, () => {
|
|||||||
expect(state.value.a).toBe(1);
|
expect(state.value.a).toBe(1);
|
||||||
expect(isShallow(state)).toBeFalsy();
|
expect(isShallow(state)).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('aborts pending execution', async () => {
|
||||||
|
let resolvePromise: (value: string) => void;
|
||||||
|
const promiseFn = () => new Promise<string>(resolve => { resolvePromise = resolve; });
|
||||||
|
|
||||||
|
const { state, isLoading, abort, executeImmediately } = useAsyncState(
|
||||||
|
promiseFn,
|
||||||
|
'initial',
|
||||||
|
{ immediate: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
executeImmediately();
|
||||||
|
expect(isLoading.value).toBeTruthy();
|
||||||
|
|
||||||
|
abort();
|
||||||
|
expect(isLoading.value).toBeFalsy();
|
||||||
|
|
||||||
|
resolvePromise!('data');
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(state.value).toBe('initial');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('abort prevents state update from resolved promise', async () => {
|
||||||
|
let resolvePromise: (value: string) => void;
|
||||||
|
const promiseFn = () => new Promise<string>(resolve => { resolvePromise = resolve; });
|
||||||
|
|
||||||
|
const { state, isLoading, isReady, abort, executeImmediately } = useAsyncState(
|
||||||
|
promiseFn,
|
||||||
|
'initial',
|
||||||
|
{ immediate: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
executeImmediately();
|
||||||
|
expect(isLoading.value).toBeTruthy();
|
||||||
|
|
||||||
|
abort();
|
||||||
|
expect(isLoading.value).toBeFalsy();
|
||||||
|
|
||||||
|
resolvePromise!('data');
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(state.value).toBe('initial');
|
||||||
|
expect(isReady.value).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('abort prevents error handling from rejected promise', async () => {
|
||||||
|
let rejectPromise: (error: Error) => void;
|
||||||
|
const promiseFn = () => new Promise<string>((_, reject) => { rejectPromise = reject; });
|
||||||
|
const onError = vi.fn();
|
||||||
|
|
||||||
|
const { error, abort, executeImmediately } = useAsyncState(
|
||||||
|
promiseFn,
|
||||||
|
'initial',
|
||||||
|
{ immediate: false, onError },
|
||||||
|
);
|
||||||
|
|
||||||
|
executeImmediately();
|
||||||
|
abort();
|
||||||
|
|
||||||
|
rejectPromise!(new Error('test-error'));
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(error.value).toBe(null);
|
||||||
|
expect(onError).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('new execute after abort works correctly', async () => {
|
||||||
|
let resolvePromise: (value: string) => void;
|
||||||
|
const promiseFn = () => new Promise<string>(resolve => { resolvePromise = resolve; });
|
||||||
|
|
||||||
|
const { state, isReady, abort, executeImmediately } = useAsyncState(
|
||||||
|
promiseFn,
|
||||||
|
'initial',
|
||||||
|
{ immediate: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
executeImmediately();
|
||||||
|
abort();
|
||||||
|
|
||||||
|
executeImmediately();
|
||||||
|
resolvePromise!('new data');
|
||||||
|
await nextTick();
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(state.value).toBe('new data');
|
||||||
|
expect(isReady.value).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('re-execute cancels previous pending execution', async () => {
|
||||||
|
let callCount = 0;
|
||||||
|
const promiseFn = (value: string) => new Promise<string>(resolve => {
|
||||||
|
callCount++;
|
||||||
|
setTimeout(() => resolve(value), 100);
|
||||||
|
});
|
||||||
|
|
||||||
|
const { state, executeImmediately } = useAsyncState(
|
||||||
|
promiseFn,
|
||||||
|
'initial',
|
||||||
|
{ immediate: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
executeImmediately('first');
|
||||||
|
executeImmediately('second');
|
||||||
|
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
|
||||||
|
expect(state.value).toBe('second');
|
||||||
|
expect(callCount).toBe(2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { ref, shallowRef, watch } from 'vue';
|
import { ref, shallowRef, watch } from 'vue';
|
||||||
import type { Ref, ShallowRef, UnwrapRef } from 'vue';
|
import type { Ref, ShallowRef, UnwrapRef } from 'vue';
|
||||||
import { isFunction, sleep } from '@robonen/stdlib';
|
import { isFunction, sleep, cancellablePromise as makeCancellable, CancelledError } from '@robonen/stdlib';
|
||||||
|
|
||||||
export interface UseAsyncStateOptions<Shallow extends boolean, Data = any> {
|
export interface UseAsyncStateOptions<Shallow extends boolean, Data = any> {
|
||||||
delay?: number;
|
delay?: number;
|
||||||
@@ -19,6 +19,7 @@ export interface UseAsyncStateReturnBase<Data, Params extends any[], Shallow ext
|
|||||||
error: Ref<unknown | null>;
|
error: Ref<unknown | null>;
|
||||||
execute: (delay?: number, ...params: Params) => Promise<Data>;
|
execute: (delay?: number, ...params: Params) => Promise<Data>;
|
||||||
executeImmediately: (...params: Params) => Promise<Data>;
|
executeImmediately: (...params: Params) => Promise<Data>;
|
||||||
|
abort: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UseAsyncStateReturn<Data, Params extends any[], Shallow extends boolean> =
|
export type UseAsyncStateReturn<Data, Params extends any[], Shallow extends boolean> =
|
||||||
@@ -50,7 +51,14 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
const isLoading = ref(false);
|
const isLoading = ref(false);
|
||||||
const isReady = ref(false);
|
const isReady = ref(false);
|
||||||
|
|
||||||
|
let cancelPending: ((reason?: string) => void) | undefined;
|
||||||
|
|
||||||
const execute = async (actualDelay = delay, ...params: any[]) => {
|
const execute = async (actualDelay = delay, ...params: any[]) => {
|
||||||
|
cancelPending?.();
|
||||||
|
|
||||||
|
let active = true;
|
||||||
|
cancelPending = () => { active = false; };
|
||||||
|
|
||||||
if (resetOnExecute)
|
if (resetOnExecute)
|
||||||
state.value = initialState;
|
state.value = initialState;
|
||||||
|
|
||||||
@@ -61,15 +69,27 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
if (actualDelay > 0)
|
if (actualDelay > 0)
|
||||||
await sleep(actualDelay);
|
await sleep(actualDelay);
|
||||||
|
|
||||||
const promise = isFunction(maybePromise) ? maybePromise(...params as Params) : maybePromise;
|
if (!active)
|
||||||
|
return state.value as Data;
|
||||||
|
|
||||||
|
const rawPromise = isFunction(maybePromise) ? maybePromise(...params as Params) : maybePromise;
|
||||||
|
const { promise, cancel } = makeCancellable(rawPromise);
|
||||||
|
cancelPending = (reason?: string) => {
|
||||||
|
active = false;
|
||||||
|
cancel(reason);
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await promise;
|
const data = await promise;
|
||||||
|
|
||||||
state.value = data;
|
state.value = data;
|
||||||
isReady.value = true;
|
isReady.value = true;
|
||||||
onSuccess?.(data);
|
onSuccess?.(data);
|
||||||
}
|
}
|
||||||
catch (e: unknown) {
|
catch (e: unknown) {
|
||||||
|
if (e instanceof CancelledError)
|
||||||
|
return state.value as Data;
|
||||||
|
|
||||||
error.value = e;
|
error.value = e;
|
||||||
onError?.(e);
|
onError?.(e);
|
||||||
|
|
||||||
@@ -77,7 +97,8 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
isLoading.value = false;
|
if (active)
|
||||||
|
isLoading.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return state.value as Data;
|
return state.value as Data;
|
||||||
@@ -87,6 +108,12 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
return execute(0, ...params);
|
return execute(0, ...params);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const abort = () => {
|
||||||
|
cancelPending?.();
|
||||||
|
cancelPending = undefined;
|
||||||
|
isLoading.value = false;
|
||||||
|
};
|
||||||
|
|
||||||
if (immediate)
|
if (immediate)
|
||||||
execute();
|
execute();
|
||||||
|
|
||||||
@@ -97,14 +124,17 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
error,
|
error,
|
||||||
execute,
|
execute,
|
||||||
executeImmediately,
|
executeImmediately,
|
||||||
|
abort,
|
||||||
};
|
};
|
||||||
|
|
||||||
function waitResolve() {
|
function waitResolve() {
|
||||||
return new Promise<UseAsyncStateReturnBase<Data, Params, Shallow>>((resolve, reject) => {
|
return new Promise<UseAsyncStateReturnBase<Data, Params, Shallow>>((resolve, reject) => {
|
||||||
watch(
|
const unwatch = watch(
|
||||||
isLoading,
|
isLoading,
|
||||||
(loading) => {
|
(loading) => {
|
||||||
if (loading === false) {
|
if (loading === false) {
|
||||||
|
unwatch();
|
||||||
|
|
||||||
if (error.value)
|
if (error.value)
|
||||||
reject(error.value);
|
reject(error.value);
|
||||||
else
|
else
|
||||||
@@ -113,7 +143,6 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
immediate: true,
|
immediate: true,
|
||||||
once: true,
|
|
||||||
flush: 'sync',
|
flush: 'sync',
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user