react-async-hook
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -19,2 +19,4 @@ export declare type AsyncState<R> = { | ||
export declare const useAsync: <R, Args extends any[]>(asyncFunction: (...args: Args) => Promise<R>, params: Args, options?: UseAsyncOptions<R>) => UseAsyncReturn<R>; | ||
declare type AddArg<H, T extends any[]> = ((h: H, ...t: T) => void) extends ((...r: infer R) => void) ? R : never; | ||
export declare const useAsyncAbortable: <R, Args extends any[]>(asyncFunction: (...args: AddArg<AbortSignal, Args>) => Promise<R>, params: Args, options?: UseAsyncOptions<R>) => UseAsyncReturn<R>; | ||
export {}; |
@@ -86,4 +86,30 @@ 'use strict'; | ||
}; | ||
const useAsyncAbortable = (asyncFunction, params, options) => { | ||
const abortControllerRef = react.useRef(); | ||
// Wrap the original async function and enhance it with abortion login | ||
const asyncFunctionWrapper = async (...p) => { | ||
// Cancel previous async call | ||
if (abortControllerRef.current) { | ||
abortControllerRef.current.abort(); | ||
} | ||
// Create/store new abort controller for next async call | ||
const abortController = new AbortController(); | ||
abortControllerRef.current = abortController; | ||
try { | ||
// @ts-ignore // TODO how to type this? | ||
return await asyncFunction(abortController.signal, ...p); | ||
} | ||
finally { | ||
// Unset abortController ref if response is already there, | ||
// as it's not needed anymore to try to abort it (would it be no-op?) | ||
if (abortControllerRef.current === abortController) { | ||
abortControllerRef.current = undefined; | ||
} | ||
} | ||
}; | ||
return useAsync(asyncFunctionWrapper, params, options); | ||
}; | ||
exports.useAsync = useAsync; | ||
exports.useAsyncAbortable = useAsyncAbortable; | ||
//# sourceMappingURL=react-async-hook.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";var e=require("react");const r={loading:!0,result:void 0,error:void 0},t={setLoading:e=>r,setResult:(e,r)=>({loading:!1,result:e,error:void 0}),setError:(e,r)=>({loading:!1,result:void 0,error:e})};exports.useAsync=((s,u,n)=>{const o=(e=>({...t,...e}))(n),c=(t=>{const[s,u]=e.useState(r);return{value:s,setLoading:()=>u(t.setLoading(s)),setResult:e=>u(t.setResult(e,s)),setError:e=>u(t.setError(e,s))}})(o),i=(()=>{const r=e.useRef(!1);return e.useEffect(()=>(r.current=!0,()=>{r.current=!1}),[]),()=>r.current})(),l=(()=>{const r=e.useRef(null);return{set:e=>r.current=e,is:e=>r.current===e}})(),a=e=>i()&&l.is(e),d=()=>{const e=s(...u);l.set(e),c.setLoading(),e.then(r=>{a(e)&&c.setResult(r)},r=>{a(e)&&c.setError(r)})};return e.useEffect(()=>{d()},u),{...c.value,execute:d}}); | ||
"use strict";var e=require("react");const r={loading:!0,result:void 0,error:void 0},t={setLoading:e=>r,setResult:(e,r)=>({loading:!1,result:e,error:void 0}),setError:(e,r)=>({loading:!1,result:void 0,error:e})},s=(s,n,u)=>{const o=(e=>({...t,...e}))(u),c=(t=>{const[s,n]=e.useState(r);return{value:s,setLoading:()=>n(t.setLoading(s)),setResult:e=>n(t.setResult(e,s)),setError:e=>n(t.setError(e,s))}})(o),l=(()=>{const r=e.useRef(!1);return e.useEffect(()=>(r.current=!0,()=>{r.current=!1}),[]),()=>r.current})(),a=(()=>{const r=e.useRef(null);return{set:e=>r.current=e,is:e=>r.current===e}})(),i=e=>l()&&a.is(e),d=()=>{const e=s(...n);a.set(e),c.setLoading(),e.then(r=>{i(e)&&c.setResult(r)},r=>{i(e)&&c.setError(r)})};return e.useEffect(()=>{d()},n),{...c.value,execute:d}};exports.useAsync=s,exports.useAsyncAbortable=((r,t,n)=>{const u=e.useRef();return s(async(...e)=>{u.current&&u.current.abort();const t=new AbortController;u.current=t;try{return await r(t.signal,...e)}finally{u.current===t&&(u.current=void 0)}},t,n)}); | ||
//# sourceMappingURL=react-async-hook.cjs.production.js.map |
@@ -1,2 +0,2 @@ | ||
import{useEffect as r,useState as e,useRef as t}from"react";const s={loading:!0,result:void 0,error:void 0},o={setLoading:r=>s,setResult:(r,e)=>({loading:!1,result:r,error:void 0}),setError:(r,e)=>({loading:!1,result:void 0,error:r})},n=(n,u,i)=>{const l=(r=>({...o,...r}))(i),c=(r=>{const[t,o]=e(s);return{value:t,setLoading:()=>o(r.setLoading(t)),setResult:e=>o(r.setResult(e,t)),setError:e=>o(r.setError(e,t))}})(l),d=(()=>{const e=t(!1);return r(()=>(e.current=!0,()=>{e.current=!1}),[]),()=>e.current})(),a=(()=>{const r=t(null);return{set:e=>r.current=e,is:e=>r.current===e}})(),g=r=>d()&&a.is(r),v=()=>{const r=n(...u);a.set(r),c.setLoading(),r.then(e=>{g(r)&&c.setResult(e)},e=>{g(r)&&c.setError(e)})};return r(()=>{v()},u),{...c.value,execute:v}};export{n as useAsync}; | ||
import{useEffect as r,useRef as t,useState as e}from"react";const n={loading:!0,result:void 0,error:void 0},o={setLoading:r=>n,setResult:(r,t)=>({loading:!1,result:r,error:void 0}),setError:(r,t)=>({loading:!1,result:void 0,error:r})},s=(s,u,c)=>{const l=(r=>({...o,...r}))(c),i=(r=>{const[t,o]=e(n);return{value:t,setLoading:()=>o(r.setLoading(t)),setResult:e=>o(r.setResult(e,t)),setError:e=>o(r.setError(e,t))}})(l),a=(()=>{const e=t(!1);return r(()=>(e.current=!0,()=>{e.current=!1}),[]),()=>e.current})(),d=(()=>{const r=t(null);return{set:t=>r.current=t,is:t=>r.current===t}})(),g=r=>a()&&d.is(r),v=()=>{const r=s(...u);d.set(r),i.setLoading(),r.then(t=>{g(r)&&i.setResult(t)},t=>{g(r)&&i.setError(t)})};return r(()=>{v()},u),{...i.value,execute:v}},u=(r,e,n)=>{const o=t();return s(async(...t)=>{o.current&&o.current.abort();const e=new AbortController;o.current=e;try{return await r(e.signal,...t)}finally{o.current===e&&(o.current=void 0)}},e,n)};export{s as useAsync,u as useAsyncAbortable}; | ||
//# sourceMappingURL=react-async-hook.es.production.js.map |
@@ -88,6 +88,32 @@ (function (global, factory) { | ||
}; | ||
const useAsyncAbortable = (asyncFunction, params, options) => { | ||
const abortControllerRef = react.useRef(); | ||
// Wrap the original async function and enhance it with abortion login | ||
const asyncFunctionWrapper = async (...p) => { | ||
// Cancel previous async call | ||
if (abortControllerRef.current) { | ||
abortControllerRef.current.abort(); | ||
} | ||
// Create/store new abort controller for next async call | ||
const abortController = new AbortController(); | ||
abortControllerRef.current = abortController; | ||
try { | ||
// @ts-ignore // TODO how to type this? | ||
return await asyncFunction(abortController.signal, ...p); | ||
} | ||
finally { | ||
// Unset abortController ref if response is already there, | ||
// as it's not needed anymore to try to abort it (would it be no-op?) | ||
if (abortControllerRef.current === abortController) { | ||
abortControllerRef.current = undefined; | ||
} | ||
} | ||
}; | ||
return useAsync(asyncFunctionWrapper, params, options); | ||
}; | ||
exports.useAsync = useAsync; | ||
exports.useAsyncAbortable = useAsyncAbortable; | ||
})); | ||
//# sourceMappingURL=react-async-hook.umd.development.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):(e=e||self,t(e["react-async-hook"]={},e.React))}(this,function(e,t){"use strict";const r={loading:!0,result:void 0,error:void 0},s={setLoading:e=>r,setResult:(e,t)=>({loading:!1,result:e,error:void 0}),setError:(e,t)=>({loading:!1,result:void 0,error:e})};e.useAsync=((e,n,o)=>{const u=(e=>({...s,...e}))(o),c=(e=>{const[s,n]=t.useState(r);return{value:s,setLoading:()=>n(e.setLoading(s)),setResult:t=>n(e.setResult(t,s)),setError:t=>n(e.setError(t,s))}})(u),i=(()=>{const e=t.useRef(!1);return t.useEffect(()=>(e.current=!0,()=>{e.current=!1}),[]),()=>e.current})(),d=(()=>{const e=t.useRef(null);return{set:t=>e.current=t,is:t=>e.current===t}})(),f=e=>i()&&d.is(e),a=()=>{const t=e(...n);d.set(t),c.setLoading(),t.then(e=>{f(t)&&c.setResult(e)},e=>{f(t)&&c.setError(e)})};return t.useEffect(()=>{a()},n),{...c.value,execute:a}})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):(e=e||self,t(e["react-async-hook"]={},e.React))}(this,function(e,t){"use strict";const r={loading:!0,result:void 0,error:void 0},n={setLoading:e=>r,setResult:(e,t)=>({loading:!1,result:e,error:void 0}),setError:(e,t)=>({loading:!1,result:void 0,error:e})},s=(e,s,o)=>{const u=(e=>({...n,...e}))(o),c=(e=>{const[n,s]=t.useState(r);return{value:n,setLoading:()=>s(e.setLoading(n)),setResult:t=>s(e.setResult(t,n)),setError:t=>s(e.setError(t,n))}})(u),i=(()=>{const e=t.useRef(!1);return t.useEffect(()=>(e.current=!0,()=>{e.current=!1}),[]),()=>e.current})(),a=(()=>{const e=t.useRef(null);return{set:t=>e.current=t,is:t=>e.current===t}})(),l=e=>i()&&a.is(e),d=()=>{const t=e(...s);a.set(t),c.setLoading(),t.then(e=>{l(t)&&c.setResult(e)},e=>{l(t)&&c.setError(e)})};return t.useEffect(()=>{d()},s),{...c.value,execute:d}};e.useAsync=s,e.useAsyncAbortable=((e,r,n)=>{const o=t.useRef();return s(async(...t)=>{o.current&&o.current.abort();const r=new AbortController;o.current=r;try{return await e(r.signal,...t)}finally{o.current===r&&(o.current=void 0)}},r,n)})}); | ||
//# sourceMappingURL=react-async-hook.umd.production.js.map |
{ | ||
"name": "react-async-hook", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Async hook", | ||
@@ -19,3 +19,11 @@ "author": "Sébastien Lorber", | ||
"hooks", | ||
"hook" | ||
"hook", | ||
"useAsync", | ||
"useState", | ||
"useFetch", | ||
"usePromise", | ||
"use-async", | ||
"use-state", | ||
"use-fetch", | ||
"use-promise" | ||
], | ||
@@ -22,0 +30,0 @@ "engines": { |
@@ -29,3 +29,3 @@ # react-async-hook | ||
``` | ||
```tsx | ||
type StarwarsHero = { | ||
@@ -32,0 +32,0 @@ id: string; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53324
268