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:
1
.github/workflows/npm-publish.yaml
vendored
1
.github/workflows/npm-publish.yaml
vendored
@@ -26,6 +26,7 @@ jobs:
|
||||
with:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
cache: pnpm
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
38
packages/stdlib/src/async/retry/index.ts
Normal file
38
packages/stdlib/src/async/retry/index.ts
Normal 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;
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
* @name timestamp
|
||||
* @category Utils
|
||||
* @description Returns the current timestamp
|
||||
*
|
||||
*
|
||||
* @returns {number} The current timestamp
|
||||
*
|
||||
*
|
||||
* @since 0.0.2
|
||||
*/
|
||||
export const timestamp = () => Date.now();
|
||||
@@ -13,9 +13,9 @@ export const timestamp = () => Date.now();
|
||||
* @name noop
|
||||
* @category Utils
|
||||
* @description A function that does nothing
|
||||
*
|
||||
*
|
||||
* @returns {void} Nothing
|
||||
*
|
||||
*
|
||||
* @since 0.0.2
|
||||
*/
|
||||
export const noop = () => {};
|
||||
|
||||
59
packages/vue/src/composables/useAsyncState/index.ts
Normal file
59
packages/vue/src/composables/useAsyncState/index.ts
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user