@tamagui/use-async 2.7.4 → 2.7.6
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/package.json +2 -2
- package/types/errors.d.ts.map +11 -0
- package/types/idle.d.ts.map +11 -0
- package/types/index.d.ts.map +11 -0
- package/types/sleep.d.ts.map +11 -0
- package/types/useAsyncEffect.d.ts.map +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/use-async",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.6",
|
|
4
4
|
"files": [
|
|
5
5
|
"src",
|
|
6
6
|
"types",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"clean:build": "tamagui-build clean:build"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@tamagui/build": "2.7.
|
|
36
|
+
"@tamagui/build": "2.7.6",
|
|
37
37
|
"react": ">=19"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAAA,OAAO,cAAM,mBAAmB,MAAM;CACpC,YAAY",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/errors.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"export class AbortError extends Error {\n constructor(message = '') {\n super(message)\n this.name = 'AbortError'\n }\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAqBA,OAAO,cAAM,OACX,SAAS,aACT,UAAU;CACR;CACA;CACA;MAED",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/idle.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"import { AbortError } from './errors'\nimport { sleep } from './sleep'\n\n/**\n * Two helpers wrapping requestIdleCallback in async, abortable.\n */\n\ntype CallbackFn = (...args: any[]) => any | (() => any)\ntype CallbackerFn = (cb: CallbackFn) => void\n\nconst idleCb: CallbackerFn =\n typeof requestIdleCallback === 'undefined'\n ? (cb: CallbackFn) => setTimeout(cb, 1)\n : requestIdleCallback\n\nconst idleAsync = () => {\n return new Promise((res) => {\n idleCb(res)\n })\n}\n\nexport const idle = async (\n signal?: AbortSignal,\n options?: {\n max?: number\n min?: number\n fully?: boolean\n }\n): Promise<void> => {\n const { max, min, fully } = options || {}\n\n const idleFn = fully ? fullyIdle : idleAsync\n\n if (max && min && min < max) {\n await Promise.race([Promise.all([idleFn(), sleep(min)]), sleep(max)])\n } else if (max) {\n await Promise.race([idleFn(), sleep(max)])\n } else if (min) {\n await Promise.all([idleFn(), sleep(min)])\n } else {\n await idleFn()\n }\n\n if (signal?.aborted) {\n throw new AbortError()\n }\n}\n\nconst fullyIdle = async (signal?: AbortSignal): Promise<void> => {\n while (true) {\n const startTime = Date.now()\n await idle(signal)\n const endTime = Date.now()\n const duration = endTime - startTime\n\n // If idle callback took less than 15ms, consider it truly idle\n if (duration < 15) {\n break\n }\n\n // Check for abort signal after each iteration\n if (signal?.aborted) {\n throw new AbortError()\n }\n }\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/index.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"export * from './useAsyncEffect'\nexport * from './sleep'\nexport * from './idle'\nexport * from './errors'\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAEA,OAAO,cAAM,QAAe,YAAY,SAAS,gBAAc",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/sleep.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"import { AbortError } from './errors'\n\nexport const sleep = async (ms: number, signal?: AbortSignal): Promise<void> => {\n await new Promise((res) => setTimeout(res, ms))\n if (signal?.aborted) {\n throw new AbortError()\n }\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "KAOK;KAEA,uBACH,QAAQ,aACR,GAAG,gBACA,QAAQ;AAEb,OAAO,iBAAS,eAAe,IAAI,qBAAqB;AAIxD,OAAO,iBAAS,qBAAqB,IAAI,qBAAqB;AAI9D,OAAO,iBAAS,qBACd,MAAM,UACN,IAAI,qBACJ",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/useAsyncEffect.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"// adopted from https://github.com/franciscop/use-async/blob/master/src/index.js\n\nimport { useEffect, useLayoutEffect } from 'react'\nimport { AbortError } from './errors'\n\nconst DEBUG_LEVEL = 0 // You can adjust this based on your needs\n\ntype Cleanup = () => void\n\ntype AsyncEffectCallback = (\n signal: AbortSignal,\n ...deps: any[]\n) => Promise<Cleanup | void> | void\n\nexport function useAsyncEffect(cb: AsyncEffectCallback, deps: any[] = []): void {\n useAsyncEffectOfType(useEffect, cb, deps)\n}\n\nexport function useAsyncLayoutEffect(cb: AsyncEffectCallback, deps: any[] = []): void {\n useAsyncEffectOfType(useLayoutEffect, cb, deps)\n}\n\nexport function useAsyncEffectOfType(\n type: Function,\n cb: AsyncEffectCallback,\n deps: any[] = []\n): void {\n type(() => {\n const controller = new AbortController()\n const signal = controller.signal\n\n // wrap in try in case its not async (for simple use cases)\n try {\n const value = cb(signal, ...deps)\n\n Promise.resolve(value)\n .then(async (res) => {\n if (res && typeof res === 'function') {\n if (signal.aborted) return res()\n signal.addEventListener('abort', res)\n }\n })\n .catch(handleError)\n } catch (error) {\n handleError(error)\n }\n\n function handleError(error: any) {\n if (error instanceof AbortError) {\n if (DEBUG_LEVEL > 2) {\n console.info(`🐛 useAsyncEffect aborted: ${error.message}`)\n }\n return null\n }\n\n // JS handles aborting a promise as an error. Thus, ignore them since they're a normal part\n // of the expected async workflow\n if (typeof error === 'object' && error.name === 'AbortError') {\n return null\n }\n\n // Real errors, bubble it up since the CB is expected to handle the\n // errors by itself, so this is like a \"fatal error\" that should've\n // been handled by the devs\n throw error\n }\n\n return () => {\n if (signal.aborted) return\n controller.abort()\n }\n }, deps)\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|