@stacksjs/browser 0.65.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +127 -0
- package/dist/index.js.map +90 -0
- package/dist/utils/base.d.ts +2 -0
- package/dist/utils/debounce.d.ts +1 -0
- package/dist/utils/function.d.ts +18 -0
- package/dist/utils/guards.d.ts +28 -0
- package/dist/utils/index.d.ts +11 -0
- package/dist/utils/math.d.ts +3 -0
- package/dist/utils/promise.d.ts +58 -0
- package/dist/utils/regex.d.ts +3 -0
- package/dist/utils/retry.d.ts +2 -0
- package/dist/utils/sleep.d.ts +37 -0
- package/dist/utils/throttle.d.ts +20 -0
- package/dist/utils/vendors.d.ts +4 -0
- package/package.json +44 -0
- package/src/index.ts +1 -0
- package/src/utils/base.ts +12 -0
- package/src/utils/debounce.ts +1 -0
- package/src/utils/function.ts +32 -0
- package/src/utils/guards.ts +39 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/math.ts +30 -0
- package/src/utils/promise.ts +134 -0
- package/src/utils/regex.ts +42 -0
- package/src/utils/retry.ts +43 -0
- package/src/utils/sleep.ts +125 -0
- package/src/utils/throttle.ts +47 -0
- package/src/utils/vendors.ts +258 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { debounce } from "perfect-debounce";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Fn, Nullable } from "@stacksjs/types";
|
|
2
|
+
/**
|
|
3
|
+
* Call every function in an array
|
|
4
|
+
*/
|
|
5
|
+
export declare function batchInvoke(functions: Nullable<Fn>[]): void;
|
|
6
|
+
/**
|
|
7
|
+
* Pass the value through the callback, and return the value
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```
|
|
11
|
+
* function createUser(name: string): User {
|
|
12
|
+
* return tap(new User, user => {
|
|
13
|
+
* user.name = name
|
|
14
|
+
* })
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function tap<T>(value: T, callback: (value: T) => void): T;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to filter out null-ish values
|
|
3
|
+
*
|
|
4
|
+
* @category Guards
|
|
5
|
+
* @example array.filter(notNullish)
|
|
6
|
+
*/
|
|
7
|
+
export declare function notNullish<T>(v: T | null | undefined): v is NonNullable<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Type guard to filter out null values
|
|
10
|
+
*
|
|
11
|
+
* @category Guards
|
|
12
|
+
* @example array.filter(noNull)
|
|
13
|
+
*/
|
|
14
|
+
export declare function noNull<T>(v: T | null): v is Exclude<T, null>;
|
|
15
|
+
/**
|
|
16
|
+
* Type guard to filter out null-ish values
|
|
17
|
+
*
|
|
18
|
+
* @category Guards
|
|
19
|
+
* @example array.filter(notUndefined)
|
|
20
|
+
*/
|
|
21
|
+
export declare function notUndefined<T>(v: T): v is Exclude<T, undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* Type guard to filter out falsy values
|
|
24
|
+
*
|
|
25
|
+
* @category Guards
|
|
26
|
+
* @example array.filter(isTruthy)
|
|
27
|
+
*/
|
|
28
|
+
export declare function isTruthy<T>(v: T): v is NonNullable<T>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./base";
|
|
2
|
+
export * from "./debounce";
|
|
3
|
+
export * from "./function";
|
|
4
|
+
export * from "./guards";
|
|
5
|
+
export * from "./math";
|
|
6
|
+
export * from "./promise";
|
|
7
|
+
export * from "./regex";
|
|
8
|
+
export * from "./retry";
|
|
9
|
+
export * from "./sleep";
|
|
10
|
+
export * from "./throttle";
|
|
11
|
+
export * from "./vendors";
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const clamp: (n: number, min: number, max: number) => number;
|
|
2
|
+
export declare function rand(min: number, max: number): number;
|
|
3
|
+
export { and, createGenericProjection, createProjection, logicNot, logicOr, or, useAbs, useAverage, useCeil, useClamp, useFloor, useMath, useMax, useMin, usePrecision, useProjection, useRound, useSum, useTrunc } from "@vueuse/math";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface SingletonPromiseReturn<T> {
|
|
2
|
+
(): Promise<T>;
|
|
3
|
+
/**
|
|
4
|
+
* Reset current staled promise.
|
|
5
|
+
* Await it to have proper shutdown.
|
|
6
|
+
*/
|
|
7
|
+
reset: () => Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Promise with `resolve` and `reject` methods of itself
|
|
11
|
+
*/
|
|
12
|
+
export interface ControlledPromise<T = void> extends Promise<T> {
|
|
13
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
14
|
+
reject: (reason?: any) => void;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create singleton promise function.
|
|
18
|
+
*
|
|
19
|
+
* @category Promise
|
|
20
|
+
*/
|
|
21
|
+
export declare function createSingletonPromise<T>(fn: () => Promise<T>): SingletonPromiseReturn<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Create a promise lock.
|
|
24
|
+
*
|
|
25
|
+
* @category Promise
|
|
26
|
+
* @example
|
|
27
|
+
* ```
|
|
28
|
+
* const lock = createPromiseLock()
|
|
29
|
+
*
|
|
30
|
+
* lock.run(async () => {
|
|
31
|
+
* await doSomething()
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* // in anther context:
|
|
35
|
+
* await lock.wait() // it will wait all tasking finished
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function createPromiseLock(): {
|
|
39
|
+
run<T = void>(fn: () => Promise<T>): Promise<T>;
|
|
40
|
+
wait(): Promise<void>;
|
|
41
|
+
isWaiting(): boolean;
|
|
42
|
+
clear(): void;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Return a Promise with `resolve` and `reject` methods
|
|
46
|
+
*
|
|
47
|
+
* @category Promise
|
|
48
|
+
* @example
|
|
49
|
+
* ```
|
|
50
|
+
* const promise = createControlledPromise()
|
|
51
|
+
*
|
|
52
|
+
* await promise
|
|
53
|
+
*
|
|
54
|
+
* // in anther context:
|
|
55
|
+
* promise.resolve(data)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function createControlledPromise<T>(): ControlledPromise<T>;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export type { Flag, Input, MagicRegExp, MagicRegExpMatchArray, MapToStringCapturedBy, StringCapturedBy } from "magic-regexp";
|
|
2
|
+
export { anyOf, carriageReturn, caseInsensitive, char, charIn, charNotIn, digit, dotAll, exactly, global, letter, linefeed, maybe, multiline, not, oneOrMore, sticky, tab, unicode, whitespace, withIndices, word, wordBoundary, wordChar } from "magic-regexp";
|
|
3
|
+
export declare function createRegExp(pattern: string, options?: { flags?: string }): RegExp;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type NonNegativeInteger<T extends number> = number extends T ? never : `${T}` extends `-${string}` | `${string}.${string}` ? never : T;
|
|
2
|
+
/**
|
|
3
|
+
* Pauses execution for a specified number of milliseconds.
|
|
4
|
+
* @param ms The number of milliseconds to pause execution.
|
|
5
|
+
* @returns A promise that resolves after the specified delay.
|
|
6
|
+
*/
|
|
7
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Pauses execution for a specified number of milliseconds.
|
|
10
|
+
* @param ms The number of milliseconds to pause execution.
|
|
11
|
+
* @returns A promise that resolves after the specified delay.
|
|
12
|
+
*/
|
|
13
|
+
export declare function wait(ms: number): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Pauses execution for a specified number of milliseconds.
|
|
16
|
+
* @param ms The number of milliseconds to pause execution.
|
|
17
|
+
* @returns A promise that resolves after the specified delay.
|
|
18
|
+
*/
|
|
19
|
+
export declare function delay(ms: number): Promise<void>;
|
|
20
|
+
export type WaitOptions = number | {
|
|
21
|
+
interval?: number;
|
|
22
|
+
timeout?: number;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Pauses execution until a specified condition is met.
|
|
26
|
+
* @param condition The condition to wait for.
|
|
27
|
+
* @param interval The interval at which to check the condition, in milliseconds. Defaults to 1000.
|
|
28
|
+
* @returns A promise that resolves when the condition is met.
|
|
29
|
+
*/
|
|
30
|
+
export declare function waitUntil(condition: () => boolean, options?: WaitOptions): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Pauses execution while a specified condition is met.
|
|
33
|
+
* @param condition The condition to wait while.
|
|
34
|
+
* @param interval The interval at which to check the condition, in milliseconds. Defaults to 1000.
|
|
35
|
+
* @returns A promise that resolves when the condition is no longer met.
|
|
36
|
+
*/
|
|
37
|
+
export declare function waitWhile(condition: () => boolean, options?: WaitOptions): Promise<void>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throttle a function.
|
|
3
|
+
*
|
|
4
|
+
* @param {Function} fn - The function to throttle.
|
|
5
|
+
* @param {number} [wait] - The time to wait between function calls.
|
|
6
|
+
* @returns {Function} - A new function that throttles the calls to the original function.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // logs window dimensions at most every 250ms
|
|
11
|
+
* window.addEventListener(
|
|
12
|
+
* "resize",
|
|
13
|
+
* throttle(function (evt) {
|
|
14
|
+
* console.log(window.innerWidth)
|
|
15
|
+
* console.log(window.innerHeight)
|
|
16
|
+
* }, 250)
|
|
17
|
+
* )
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function throttle(fn: Function, wait?: number): (this: unknown, ...args: any[]) => void;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { asyncComputed, autoResetRef, breakpointsAntDesign, breakpointsBootstrapV5, breakpointsMasterCss, breakpointsQuasar, breakpointsSematic, breakpointsTailwind, breakpointsVuetify, cloneFnJSON, computedAsync, computedEager, computedInject, computedWithControl, controlledComputed, controlledRef, createEventHook, createFetch, createGlobalState, createInjectionState, createReactiveFn, createReusableTemplate, createSharedComposable, createTemplatePromise, createUnrefFn, customStorageEventName, debouncedRef, debouncedWatch, defaultDocument, defaultLocation, defaultNavigator, defaultWindow, eagerComputed, executeTransition, extendRef, formatTimeAgo, getSSRHandler, ignorableWatch, injectLocal, isDefined, makeDestructurable, mapGamepadToXbox360Controller, onClickOutside, onKeyDown, onKeyPressed, onKeyStroke, onKeyUp, onLongPress, onStartTyping, pausableWatch, provideLocal, reactify, reactifyObject, reactiveComputed, reactiveOmit, reactivePick, refAutoReset, refDebounced, refDefault, refThrottled, refWithControl, resolveRef, resolveUnref, setSSRHandler, syncRef, syncRefs, templateRef, throttledRef, throttledWatch, toReactive, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, unrefElement, until, useActiveElement, useAnimate, useArrayDifference, useArrayEvery, useArrayFilter, useArrayFind, useArrayFindIndex, useArrayIncludes, useArrayMap, useArrayReduce, useArraySome, useArrayUnique, useAsyncQueue, useAsyncState, useBase64, useBattery, useBluetooth, useBreakpoints, useBroadcastChannel, useBrowserLocation, useCached, useClipboard, useClipboardItems, useCloned, useColorMode, useConfirmDialog, useCounter, useCssVar, useCurrentElement, useCycleList, useDark, useDateFormat, useDebounce, useDebouncedRefHistory, useDebounceFn, useDeviceMotion, useDeviceOrientation, useDevicePixelRatio, useDevicesList, useDisplayMedia, useDocumentVisibility, useDraggable, useDropZone, useElementBounding, useElementByPoint, useElementHover, useElementSize, useElementVisibility, useEventBus, useEventListener, useEventSource, useEyeDropper, useFavicon, useFetch, useFileDialog, useFileSystemAccess, useFocus, useFocusWithin, useFps, useFullscreen, useGamepad, useGeolocation, useIdle, useImage, useInfiniteScroll, useIntersectionObserver, useInterval, useIntervalFn, useKeyModifier, useLastChanged, useLocalStorage, useMagicKeys, useManualRefHistory, useMediaControls, useMediaQuery, useMemoize, useMemory, useMounted, useMouse, useMouseInElement, useMousePressed, useMutationObserver, useNavigatorLanguage, useNetwork, useNow, useObjectUrl, useOffsetPagination, useOnline, usePageLeave, useParallax, useParentElement, usePerformanceObserver, usePermission, usePointer, usePointerLock, usePointerSwipe, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePreferredLanguages, usePreferredReducedMotion, usePrevious, useRafFn, useRefHistory, useResizeObserver, useScreenOrientation, useScreenSafeArea, useScriptTag, useScroll, useScrollLock, useSessionStorage, useShare, useSorted, useSpeechRecognition, useSpeechSynthesis, useStepper, useStorage, useStorageAsync, useStyleTag, useSupported, useSwipe, useTemplateRefsList, useTextareaAutosize, useTextDirection, useTextSelection, useThrottle, useThrottledRefHistory, useThrottleFn, useTimeAgo, useTimeout, useTimeoutFn, useTimeoutPoll, useTimestamp, useTitle, useToggle, useToNumber, useToString, useTransition, useUrlSearchParams, useUserMedia, useVibrate, useVirtualList, useVModel, useVModels, useWakeLock, useWebNotification, useWebSocket, useWebWorker, useWebWorkerFn, useWindowFocus, useWindowScroll, useWindowSize, watchArray, watchAtMost, watchDebounced, watchDeep, watchIgnorable, watchImmediate, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever } from "@vueuse/core";
|
|
2
|
+
export type { HeadObject, HeadObjectPlain } from "@vueuse/head";
|
|
3
|
+
export { createHead, Head, HeadVuePlugin, renderHeadToString } from "@vueuse/head";
|
|
4
|
+
export { default as readableSize } from "pretty-bytes";
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stacksjs/browser",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.65.0",
|
|
5
|
+
"description": "Stacks core frontend/browser functionalities.",
|
|
6
|
+
"author": "Chris Breuer",
|
|
7
|
+
"contributors": ["Chris Breuer <chris@stacksjs.org>"],
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
10
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/browser#readme",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
14
|
+
"directory": "./storage/framework/core/browser"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/stacksjs/stacks/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["frontend", "browser", "stacks", "api"],
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"bun": "./src/index.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./*": {
|
|
26
|
+
"bun": "./src/*",
|
|
27
|
+
"import": "./dist/*"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"module": "dist/index.js",
|
|
31
|
+
"types": "dist/index.d.ts",
|
|
32
|
+
"files": ["README.md", "dist", "src"],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "bun build.ts",
|
|
35
|
+
"typecheck": "bun tsc --noEmit",
|
|
36
|
+
"prepublishOnly": "bun run build"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@stacksjs/utils": "0.64.6"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@stacksjs/development": "0.64.6"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils/'
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { toString } from '@stacksjs/strings'
|
|
2
|
+
|
|
3
|
+
// export function assert(condition: boolean, message: string): asserts condition {
|
|
4
|
+
// if (!condition)
|
|
5
|
+
// throw new Error(message)
|
|
6
|
+
// }
|
|
7
|
+
|
|
8
|
+
// export function noop() {}
|
|
9
|
+
|
|
10
|
+
export async function loop(times: number, callback: any): Promise<void> {
|
|
11
|
+
Array.from({ length: times }).forEach(async (_, i) => await callback(i))
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { debounce } from 'perfect-debounce'
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Fn, Nullable } from '@stacksjs/types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Call every function in an array
|
|
5
|
+
*/
|
|
6
|
+
export function batchInvoke(functions: Nullable<Fn>[]): void {
|
|
7
|
+
functions.forEach(fn => fn?.())
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// /**
|
|
11
|
+
// * Call the function
|
|
12
|
+
// */
|
|
13
|
+
// export function invoke(fn: Fn) {
|
|
14
|
+
// return fn()
|
|
15
|
+
// }
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Pass the value through the callback, and return the value
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```
|
|
22
|
+
* function createUser(name: string): User {
|
|
23
|
+
* return tap(new User, user => {
|
|
24
|
+
* user.name = name
|
|
25
|
+
* })
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function tap<T>(value: T, callback: (value: T) => void): T {
|
|
30
|
+
callback(value)
|
|
31
|
+
return value
|
|
32
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to filter out null-ish values
|
|
3
|
+
*
|
|
4
|
+
* @category Guards
|
|
5
|
+
* @example array.filter(notNullish)
|
|
6
|
+
*/
|
|
7
|
+
export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> {
|
|
8
|
+
return v != null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Type guard to filter out null values
|
|
13
|
+
*
|
|
14
|
+
* @category Guards
|
|
15
|
+
* @example array.filter(noNull)
|
|
16
|
+
*/
|
|
17
|
+
export function noNull<T>(v: T | null): v is Exclude<T, null> {
|
|
18
|
+
return v !== null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Type guard to filter out null-ish values
|
|
23
|
+
*
|
|
24
|
+
* @category Guards
|
|
25
|
+
* @example array.filter(notUndefined)
|
|
26
|
+
*/
|
|
27
|
+
export function notUndefined<T>(v: T): v is Exclude<T, undefined> {
|
|
28
|
+
return v !== undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Type guard to filter out falsy values
|
|
33
|
+
*
|
|
34
|
+
* @category Guards
|
|
35
|
+
* @example array.filter(isTruthy)
|
|
36
|
+
*/
|
|
37
|
+
export function isTruthy<T>(v: T): v is NonNullable<T> {
|
|
38
|
+
return Boolean(v)
|
|
39
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './base'
|
|
2
|
+
export * from './debounce'
|
|
3
|
+
export * from './function'
|
|
4
|
+
export * from './guards'
|
|
5
|
+
export * from './math'
|
|
6
|
+
export * from './promise'
|
|
7
|
+
export * from './regex'
|
|
8
|
+
export * from './retry'
|
|
9
|
+
export * from './sleep'
|
|
10
|
+
export * from './throttle'
|
|
11
|
+
export * from './vendors'
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const clamp = (n: number, min: number, max: number): number => Math.min(max, Math.max(min, n))
|
|
2
|
+
|
|
3
|
+
export function rand(min: number, max: number): number {
|
|
4
|
+
min = Math.ceil(min)
|
|
5
|
+
max = Math.floor(max)
|
|
6
|
+
return Math.floor(Math.random() * (max - min + 1)) + min
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// not, -> exported as well in ../regex
|
|
10
|
+
export {
|
|
11
|
+
and,
|
|
12
|
+
createGenericProjection,
|
|
13
|
+
createProjection,
|
|
14
|
+
logicNot,
|
|
15
|
+
logicOr,
|
|
16
|
+
or,
|
|
17
|
+
useAbs,
|
|
18
|
+
useAverage,
|
|
19
|
+
useCeil,
|
|
20
|
+
useClamp,
|
|
21
|
+
useFloor,
|
|
22
|
+
useMath,
|
|
23
|
+
useMax,
|
|
24
|
+
useMin,
|
|
25
|
+
usePrecision,
|
|
26
|
+
useProjection,
|
|
27
|
+
useRound,
|
|
28
|
+
useSum,
|
|
29
|
+
useTrunc,
|
|
30
|
+
} from '@vueuse/math'
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
export interface SingletonPromiseReturn<T> {
|
|
2
|
+
(): Promise<T>
|
|
3
|
+
/**
|
|
4
|
+
* Reset current staled promise.
|
|
5
|
+
* Await it to have proper shutdown.
|
|
6
|
+
*/
|
|
7
|
+
reset: () => Promise<void>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// export { peek } from 'bun'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Promise with `resolve` and `reject` methods of itself
|
|
14
|
+
*/
|
|
15
|
+
export interface ControlledPromise<T = void> extends Promise<T> {
|
|
16
|
+
resolve: (value: T | PromiseLike<T>) => void
|
|
17
|
+
reject: (reason?: any) => void
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Create singleton promise function.
|
|
22
|
+
*
|
|
23
|
+
* @category Promise
|
|
24
|
+
*/
|
|
25
|
+
export function createSingletonPromise<T>(fn: () => Promise<T>): SingletonPromiseReturn<T> {
|
|
26
|
+
let _promise: Promise<T> | undefined
|
|
27
|
+
|
|
28
|
+
function wrapper() {
|
|
29
|
+
if (!_promise)
|
|
30
|
+
_promise = fn()
|
|
31
|
+
return _promise
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
wrapper.reset = async () => {
|
|
35
|
+
const _prev = _promise
|
|
36
|
+
_promise = undefined
|
|
37
|
+
if (_prev)
|
|
38
|
+
await _prev
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return wrapper
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Create a promise lock.
|
|
46
|
+
*
|
|
47
|
+
* @category Promise
|
|
48
|
+
* @example
|
|
49
|
+
* ```
|
|
50
|
+
* const lock = createPromiseLock()
|
|
51
|
+
*
|
|
52
|
+
* lock.run(async () => {
|
|
53
|
+
* await doSomething()
|
|
54
|
+
* })
|
|
55
|
+
*
|
|
56
|
+
* // in anther context:
|
|
57
|
+
* await lock.wait() // it will wait all tasking finished
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function createPromiseLock() {
|
|
61
|
+
let currentPromise = Promise.resolve()
|
|
62
|
+
const queue: Promise<any>[] = []
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
async run<T = void>(fn: () => Promise<T>): Promise<T> {
|
|
66
|
+
const runTask = async (): Promise<T> => {
|
|
67
|
+
await currentPromise
|
|
68
|
+
return fn()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const taskPromise = runTask()
|
|
72
|
+
queue.push(taskPromise)
|
|
73
|
+
|
|
74
|
+
currentPromise = taskPromise
|
|
75
|
+
.catch(() => {})
|
|
76
|
+
.finally(() => {
|
|
77
|
+
const index = queue.indexOf(taskPromise)
|
|
78
|
+
if (index > -1)
|
|
79
|
+
queue.splice(index, 1)
|
|
80
|
+
}) as Promise<void>
|
|
81
|
+
|
|
82
|
+
return taskPromise
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
async wait(): Promise<void> {
|
|
86
|
+
while (queue.length > 0) {
|
|
87
|
+
await Promise.all(queue)
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
isWaiting(): boolean {
|
|
92
|
+
return queue.length > 0
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
clear(): void {
|
|
96
|
+
queue.length = 0
|
|
97
|
+
currentPromise = Promise.resolve()
|
|
98
|
+
},
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Return a Promise with `resolve` and `reject` methods
|
|
104
|
+
*
|
|
105
|
+
* @category Promise
|
|
106
|
+
* @example
|
|
107
|
+
* ```
|
|
108
|
+
* const promise = createControlledPromise()
|
|
109
|
+
*
|
|
110
|
+
* await promise
|
|
111
|
+
*
|
|
112
|
+
* // in anther context:
|
|
113
|
+
* promise.resolve(data)
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export function createControlledPromise<T>(): ControlledPromise<T> {
|
|
117
|
+
let resolve: any
|
|
118
|
+
let reject: any
|
|
119
|
+
|
|
120
|
+
const promise = new Promise<T>((_resolve, _reject) => {
|
|
121
|
+
resolve = _resolve
|
|
122
|
+
reject = _reject
|
|
123
|
+
}) as ControlledPromise<T>
|
|
124
|
+
|
|
125
|
+
promise.resolve = resolve
|
|
126
|
+
|
|
127
|
+
promise.reject = reject
|
|
128
|
+
return promise
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Create a promise that will be resolved after `ms` milliseconds.
|
|
133
|
+
*/
|
|
134
|
+
// export { sleep, sleepSync } from 'bun'
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
Flag,
|
|
3
|
+
Input,
|
|
4
|
+
MagicRegExp,
|
|
5
|
+
MagicRegExpMatchArray,
|
|
6
|
+
MapToStringCapturedBy,
|
|
7
|
+
StringCapturedBy,
|
|
8
|
+
} from 'magic-regexp'
|
|
9
|
+
export {
|
|
10
|
+
anyOf,
|
|
11
|
+
carriageReturn,
|
|
12
|
+
caseInsensitive,
|
|
13
|
+
char,
|
|
14
|
+
charIn,
|
|
15
|
+
charNotIn,
|
|
16
|
+
digit,
|
|
17
|
+
dotAll,
|
|
18
|
+
exactly,
|
|
19
|
+
global,
|
|
20
|
+
letter,
|
|
21
|
+
linefeed,
|
|
22
|
+
maybe,
|
|
23
|
+
multiline,
|
|
24
|
+
not,
|
|
25
|
+
oneOrMore,
|
|
26
|
+
sticky,
|
|
27
|
+
tab,
|
|
28
|
+
unicode,
|
|
29
|
+
whitespace,
|
|
30
|
+
withIndices,
|
|
31
|
+
word,
|
|
32
|
+
wordBoundary,
|
|
33
|
+
wordChar,
|
|
34
|
+
} from 'magic-regexp'
|
|
35
|
+
|
|
36
|
+
// export function caseInsensitive(pattern: string): RegExp {
|
|
37
|
+
// return new RegExp(pattern, 'i')
|
|
38
|
+
// }
|
|
39
|
+
|
|
40
|
+
export function createRegExp(pattern: string, options: { flags?: string } = {}): RegExp {
|
|
41
|
+
return new RegExp(pattern, options.flags)
|
|
42
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// eslint-disable-next-line ts/no-unsafe-function-type
|
|
2
|
+
export function retry(fn: Function, options: any): Promise<any> {
|
|
3
|
+
const { retries = 3, initialDelay = 1000, backoffFactor = 2, jitter = true } = options
|
|
4
|
+
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
let attemptCount = 0
|
|
7
|
+
|
|
8
|
+
const attempt = async () => {
|
|
9
|
+
try {
|
|
10
|
+
resolve(await fn())
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
if (attemptCount >= retries) {
|
|
14
|
+
reject(err)
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
const delay = calculateDelay(attemptCount, initialDelay, backoffFactor, jitter)
|
|
18
|
+
setTimeout(() => attempt(), delay)
|
|
19
|
+
attemptCount++
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
attempt()
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function calculateDelay(
|
|
29
|
+
attemptCount: number,
|
|
30
|
+
initialDelay: number,
|
|
31
|
+
backoffFactor: number,
|
|
32
|
+
jitter?: boolean,
|
|
33
|
+
): number {
|
|
34
|
+
let delay = initialDelay * backoffFactor ** attemptCount
|
|
35
|
+
|
|
36
|
+
if (jitter) {
|
|
37
|
+
const random = Math.random() // Generates a number between 0 and 1
|
|
38
|
+
const jitterValue = delay * 0.3 // Jitter will be up to 30% of the delay
|
|
39
|
+
delay = delay + jitterValue * (random - 0.5) * 2 // Adjust delay randomly within ±30%
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return delay
|
|
43
|
+
}
|