react-client-async
Advanced tools
Comparing version
@@ -83,3 +83,7 @@ import type { propsAreEqual } from '../types/react'; | ||
* @param options Options for using the async function. | ||
* @returns The task state and control. | ||
* @returns The task state and controllers. | ||
* @todo | ||
* - Support hooks in async function components. | ||
* - Approach 1: Move `promiseFn` to before the early return. | ||
* - Approach 2: Create a new function component to support hooks. | ||
*/ | ||
@@ -86,0 +90,0 @@ declare function useAsync<Args, Ret>( |
@@ -16,3 +16,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
* @param options Options for using the async function. | ||
* @returns The task state and control. | ||
* @returns The task state and controllers. | ||
* @todo | ||
* - Support hooks in async function components. | ||
* - Approach 1: Move `promiseFn` to before the early return. | ||
* - Approach 2: Create a new function component to support hooks. | ||
*/ | ||
@@ -49,17 +53,16 @@ function useAsync( | ||
useEffect(() => () => abortCtlRef.current?.abort($abortedByUnmounted), []); | ||
// The state of the async function. | ||
const state = useMemo(() => ({ pending, result, error }), [pending, result, error]); | ||
// The load function to run the async function. | ||
const load = useCallback(() => { | ||
setRefresh(() => Symbol()); | ||
resolversRef.current = Promise.withResolvers(); | ||
return resolversRef.current.promise; | ||
}, []); | ||
// The stop function to stop the async function. | ||
const stop = useCallback((reason) => { | ||
abortCtlRef.current?.abort(reason); | ||
}, []); | ||
// Create the hook return. | ||
const hookReturn = { | ||
// The state of the async function. | ||
state: useMemo(() => ({ pending, result, error }), [pending, result, error]), | ||
// The load function to run the async function. | ||
load: useCallback(() => { | ||
setRefresh(() => Symbol()); | ||
resolversRef.current = Promise.withResolvers(); | ||
return resolversRef.current.promise; | ||
}, []), | ||
// The stop function to stop the async function. | ||
stop: useCallback((reason) => { | ||
abortCtlRef.current?.abort(reason); | ||
}, []), | ||
}; | ||
const hookReturn = useMemo(() => ({ state, load, stop }), [state, load, stop]); | ||
// Check if no need to rerun the async function. | ||
@@ -89,6 +92,2 @@ const sameFn = fnRef.current === promiseFn; | ||
const curResolvers = resolversRef.current; | ||
/** | ||
* ToDo: Move `promiseFn` to before the early return | ||
* so that the Async component directly supports hooks like `useState`. | ||
*/ | ||
// Run the async function and get the promise. | ||
@@ -95,0 +94,0 @@ Promise.resolve(promiseFn(args, { signal })) |
{ | ||
"name": "react-client-async", | ||
"version": "1.3.6", | ||
"type": "module", | ||
"version": "1.3.7", | ||
"main": "dist/lib.js", | ||
@@ -27,3 +28,3 @@ "module": "dist/lib.js", | ||
"recursion", | ||
"componets", | ||
"components", | ||
"awaitable" | ||
@@ -35,3 +36,3 @@ ], | ||
"build:app:deploy": "bun run build:app && bun run push:pages", | ||
"build:lib": "bun run scripts/build-lib.sh", | ||
"build:lib": "rm -rf dist && CFG=tsconfig.lib.json && tsc -p $CFG && tsc-alias -p $CFG", | ||
"build:lib:publish": "bun run build:lib && bun publish", | ||
@@ -51,6 +52,6 @@ "push:pages": "bun run scripts/push-pages.ts", | ||
"@biomejs/biome": "^1.9.4", | ||
"@rsbuild/core": "^1.2.11", | ||
"@rsbuild/core": "^1.2.17", | ||
"@rsbuild/plugin-mdx": "^1.0.2", | ||
"@rsbuild/plugin-react": "^1.1.1", | ||
"@tailwindcss/postcss": "^4.0.7", | ||
"@tailwindcss/postcss": "^4.0.12", | ||
"@tailwindcss/typography": "^0.5.16", | ||
@@ -61,3 +62,3 @@ "@types/gh-pages": "^6.1.0", | ||
"@types/react-syntax-highlighter": "^15.5.13", | ||
"autoprefixer": "^10.4.20", | ||
"autoprefixer": "^10.4.21", | ||
"gh-pages": "^6.3.0", | ||
@@ -69,6 +70,6 @@ "postcss": "^8.5.3", | ||
"react-syntax-highlighter": "^15.6.1", | ||
"react-toastify": "^11.0.3", | ||
"tailwindcss": "^4.0.7", | ||
"tsc-alias": "^1.8.10", | ||
"typescript": "^5.7.3", | ||
"react-toastify": "^11.0.5", | ||
"tailwindcss": "^4.0.12", | ||
"tsc-alias": "^1.8.11", | ||
"typescript": "^5.8.2", | ||
"react": "^19.0.0", | ||
@@ -75,0 +76,0 @@ "react-dom": "^19.0.0" |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
19741
1.72%402
0.75%Yes
NaN1
Infinity%