mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
Compare commits
1 Commits
copilot/ad
...
renovate/p
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49d70f1f39 |
2
.github/workflows/ci.yaml
vendored
2
.github/workflows/ci.yaml
vendored
@@ -19,7 +19,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Install pnpm
|
- name: Install pnpm
|
||||||
uses: pnpm/action-setup@v4
|
uses: pnpm/action-setup@v5
|
||||||
with:
|
with:
|
||||||
run_install: false
|
run_install: false
|
||||||
|
|
||||||
|
|||||||
2
.github/workflows/publish.yaml
vendored
2
.github/workflows/publish.yaml
vendored
@@ -18,7 +18,7 @@ jobs:
|
|||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Install pnpm
|
- name: Install pnpm
|
||||||
uses: pnpm/action-setup@v4
|
uses: pnpm/action-setup@v5
|
||||||
with:
|
with:
|
||||||
run_install: false
|
run_install: false
|
||||||
|
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
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');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
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,3 +1,2 @@
|
|||||||
export * from './cancellablePromise';
|
|
||||||
export * from './sleep';
|
export * from './sleep';
|
||||||
export * from './tryIt';
|
export * from './tryIt';
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ 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');
|
||||||
@@ -42,7 +41,6 @@ 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');
|
||||||
@@ -62,7 +60,6 @@ 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');
|
||||||
@@ -80,7 +77,6 @@ describe(useAsyncState, () => {
|
|||||||
{ onSuccess },
|
{ onSuccess },
|
||||||
);
|
);
|
||||||
|
|
||||||
await nextTick();
|
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(onSuccess).toHaveBeenCalledWith('data');
|
expect(onSuccess).toHaveBeenCalledWith('data');
|
||||||
@@ -96,7 +92,6 @@ describe(useAsyncState, () => {
|
|||||||
{ onError },
|
{ onError },
|
||||||
);
|
);
|
||||||
|
|
||||||
await nextTick();
|
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(onError).toHaveBeenCalledWith(error);
|
expect(onError).toHaveBeenCalledWith(error);
|
||||||
@@ -169,7 +164,6 @@ 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');
|
||||||
@@ -212,114 +206,4 @@ 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, cancellablePromise as makeCancellable, CancelledError } from '@robonen/stdlib';
|
import { isFunction, sleep } from '@robonen/stdlib';
|
||||||
|
|
||||||
export interface UseAsyncStateOptions<Shallow extends boolean, Data = any> {
|
export interface UseAsyncStateOptions<Shallow extends boolean, Data = any> {
|
||||||
delay?: number;
|
delay?: number;
|
||||||
@@ -19,7 +19,6 @@ 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> =
|
||||||
@@ -51,14 +50,7 @@ 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;
|
||||||
|
|
||||||
@@ -69,27 +61,15 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
if (actualDelay > 0)
|
if (actualDelay > 0)
|
||||||
await sleep(actualDelay);
|
await sleep(actualDelay);
|
||||||
|
|
||||||
if (!active)
|
const promise = isFunction(maybePromise) ? maybePromise(...params as Params) : maybePromise;
|
||||||
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);
|
||||||
|
|
||||||
@@ -97,8 +77,7 @@ export function useAsyncState<Data, Params extends any[] = [], Shallow extends b
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (active)
|
isLoading.value = false;
|
||||||
isLoading.value = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return state.value as Data;
|
return state.value as Data;
|
||||||
@@ -108,12 +87,6 @@ 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();
|
||||||
|
|
||||||
@@ -124,17 +97,14 @@ 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) => {
|
||||||
const unwatch = watch(
|
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
|
||||||
@@ -143,6 +113,7 @@ 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