1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 02:44:45 +00:00

ci: add registry-url

This commit is contained in:
2025-05-09 13:38:29 +07:00
parent 09e72d904c
commit 88f6cec9b2
4 changed files with 102 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ jobs:
with: with:
node-version: ${{ env.NODE_VERSION }} node-version: ${{ env.NODE_VERSION }}
cache: pnpm cache: pnpm
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies - name: Install dependencies
run: pnpm install --frozen-lockfile run: pnpm install --frozen-lockfile

View File

@@ -0,0 +1,38 @@
export interface RetryOptions {
times?: number;
delay?: number;
backoff: (options: RetryOptions & { count: number }) => number;
}
/**
* @name retry
* @category Async
* @description Retries a function a specified number of times with a delay between each retry
*
* @param {Promise<unknown>} fn - The function to retry
* @param {RetryOptions} options - The options for the retry
* @returns {Promise<unknown>} - The result of the function
*
* @example
* const result = await retry(() => {
* return fetch('https://jsonplaceholder.typicode.com/todos/1')
* .then(response => response.json())
* });
*
* @example
* const result = await retry(() => {
* return fetch('https://jsonplaceholder.typicode.com/todos/1')
* .then(response => response.json())
* }, { times: 3, delay: 1000 });
*
*/
export async function retry<Return>(
fn: () => Promise<Return>,
options: RetryOptions
) {
const {
times = 3,
} = options;
let count = 0;
}

View File

@@ -0,0 +1,59 @@
import { ref, shallowRef } from 'vue';
import { isFunction } from '@robonen/stdlib';
export enum AsyncStateStatus {
PENDING,
FULFILLED,
REJECTED,
}
export interface UseAsyncStateOptions<Shallow extends boolean, Data = any> {
shallow?: Shallow;
immediate?: boolean;
resetOnExecute?: boolean;
throwError?: boolean;
onError?: (error: unknown) => void;
onSuccess?: (data: Data) => void;
}
/**
* @name useAsyncState
* @category State
* @description A composable that provides a state for async operations without setup blocking
*/
export function useAsyncState<Data, Params extends any[] = [], Shallow extends boolean = true>(
maybePromise: Promise<Data> | ((...args: Params) => Promise<Data>),
initialState: Data,
options?: UseAsyncStateOptions<Shallow, Data>,
) {
const state = options?.shallow ? shallowRef(initialState) : ref(initialState);
const status = ref<AsyncStateStatus | null>(null);
const execute = async (...params: any[]) => {
if (options?.resetOnExecute)
state.value = initialState;
status.value = AsyncStateStatus.PENDING;
const promise = isFunction(maybePromise) ? maybePromise(...params as Params) : maybePromise;
try {
const data = await promise;
state.value = data;
status.value = AsyncStateStatus.FULFILLED;
options?.onSuccess?.(data);
}
catch (error) {
status.value = AsyncStateStatus.REJECTED;
options?.onError?.(error);
if (options?.throwError)
throw error;
}
return state.value as Data;
};
if (options?.immediate)
execute();
}