mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
Compare commits
1 Commits
copilot/ad
...
renovate/a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6399922d70 |
@@ -16,9 +16,9 @@
|
|||||||
"url": "git+https://github.com/robonen/tools.git",
|
"url": "git+https://github.com/robonen/tools.git",
|
||||||
"directory": "configs/oxlint"
|
"directory": "configs/oxlint"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.29.3",
|
"packageManager": "pnpm@10.32.1",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24.13.1"
|
"node": ">=24.14.0"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
@@ -45,7 +45,7 @@
|
|||||||
"tsdown": "catalog:"
|
"tsdown": "catalog:"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"oxlint": ">=1.0.0"
|
"oxlint": ">=1.56.0"
|
||||||
},
|
},
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"access": "public"
|
||||||
|
|||||||
@@ -15,9 +15,9 @@
|
|||||||
"url": "git+https://github.com/robonen/tools.git",
|
"url": "git+https://github.com/robonen/tools.git",
|
||||||
"directory": "packages/tsconfig"
|
"directory": "packages/tsconfig"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.29.3",
|
"packageManager": "pnpm@10.32.1",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24.13.1"
|
"node": ">=24.14.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"**tsconfig.json"
|
"**tsconfig.json"
|
||||||
|
|||||||
@@ -15,9 +15,9 @@
|
|||||||
"url": "git+https://github.com/robonen/tools.git",
|
"url": "git+https://github.com/robonen/tools.git",
|
||||||
"directory": "configs/tsdown"
|
"directory": "configs/tsdown"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.29.3",
|
"packageManager": "pnpm@10.32.1",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24.13.1"
|
"node": ">=24.14.0"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"exports": {
|
"exports": {
|
||||||
|
|||||||
@@ -18,9 +18,9 @@
|
|||||||
"url": "git+https://github.com/robonen/tools.git",
|
"url": "git+https://github.com/robonen/tools.git",
|
||||||
"directory": "packages/platform"
|
"directory": "packages/platform"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.29.3",
|
"packageManager": "pnpm@10.32.1",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24.13.1"
|
"node": ">=24.14.0"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
|
|||||||
@@ -18,9 +18,9 @@
|
|||||||
"url": "git+https://github.com/robonen/tools.git",
|
"url": "git+https://github.com/robonen/tools.git",
|
||||||
"directory": "packages/stdlib"
|
"directory": "packages/stdlib"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.29.3",
|
"packageManager": "pnpm@10.32.1",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24.13.1"
|
"node": ">=24.14.0"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
|
|||||||
@@ -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';
|
||||||
|
|||||||
@@ -16,9 +16,9 @@
|
|||||||
"url": "git+https://github.com/robonen/tools.git",
|
"url": "git+https://github.com/robonen/tools.git",
|
||||||
"directory": "packages/renovate"
|
"directory": "packages/renovate"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.29.3",
|
"packageManager": "pnpm@10.32.1",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24.13.1"
|
"node": ">=24.14.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"default.json"
|
"default.json"
|
||||||
@@ -27,6 +27,6 @@
|
|||||||
"test": "renovate-config-validator ./default.json"
|
"test": "renovate-config-validator ./default.json"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"renovate": "^43.12.0"
|
"renovate": "^43.84.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,13 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/robonen/tools.git"
|
"url": "git+https://github.com/robonen/tools.git"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.29.3",
|
"packageManager": "pnpm@10.32.1",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24.13.1"
|
"node": ">=24.14.0"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^24.10.13",
|
"@types/node": "^24.12.0",
|
||||||
"@vitest/coverage-v8": "catalog:",
|
"@vitest/coverage-v8": "catalog:",
|
||||||
"@vitest/ui": "catalog:",
|
"@vitest/ui": "catalog:",
|
||||||
"citty": "^0.2.1",
|
"citty": "^0.2.1",
|
||||||
|
|||||||
4065
pnpm-lock.yaml
generated
4065
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -6,12 +6,12 @@ packages:
|
|||||||
- docs
|
- docs
|
||||||
|
|
||||||
catalog:
|
catalog:
|
||||||
'@vitest/coverage-v8': ^4.0.18
|
'@vitest/coverage-v8': ^4.1.0
|
||||||
'@vue/test-utils': ^2.4.6
|
'@vue/test-utils': ^2.4.6
|
||||||
jsdom: ^28.0.0
|
jsdom: ^28.1.0
|
||||||
oxlint: ^1.2.0
|
oxlint: ^1.56.0
|
||||||
tsdown: ^0.12.5
|
tsdown: ^0.21.4
|
||||||
vitest: ^4.0.18
|
vitest: ^4.1.0
|
||||||
'@vitest/ui': ^4.0.18
|
'@vitest/ui': ^4.1.0
|
||||||
vue: ^3.5.28
|
vue: ^3.5.30
|
||||||
nuxt: ^4.3.1
|
nuxt: ^4.4.2
|
||||||
|
|||||||
@@ -16,9 +16,9 @@
|
|||||||
"url": "git+https://github.com/robonen/tools.git",
|
"url": "git+https://github.com/robonen/tools.git",
|
||||||
"directory": "./packages/vue"
|
"directory": "./packages/vue"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.29.3",
|
"packageManager": "pnpm@10.32.1",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24.13.1"
|
"node": ">=24.14.0"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
|
|||||||
@@ -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