react-async-hook
Advanced tools
Comparing version 3.2.2 to 3.3.0
@@ -35,8 +35,19 @@ 'use strict'; | ||
const [value, setValue] = react.useState(() => options.initialState()); | ||
const reset = react.useCallback(() => setValue(options.initialState()), [ | ||
setValue, | ||
options, | ||
]); | ||
const setLoading = react.useCallback(() => setValue(options.setLoading(value)), [ | ||
value, | ||
setValue, | ||
]); | ||
const setResult = react.useCallback((result) => setValue(options.setResult(result, value)), [value, setValue]); | ||
const setError = react.useCallback((error) => setValue(options.setError(error, value)), [value, setValue]); | ||
return { | ||
value, | ||
set: setValue, | ||
setLoading: () => setValue(options.setLoading(value)), | ||
setResult: result => setValue(options.setResult(result, value)), | ||
setError: error => setValue(options.setError(error, value)), | ||
reset, | ||
setLoading, | ||
setResult, | ||
setError, | ||
}; | ||
@@ -43,0 +54,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e=require("react");const t={loading:!0,result:void 0,error:void 0},r={initialState:()=>t,executeOnMount:!0,executeOnUpdate:!0,setLoading:e=>t,setResult:(e,t)=>({loading:!1,result:e,error:void 0}),setError:(e,t)=>({loading:!1,result:void 0,error:e})},n=(t,n,s)=>{const u=(e=>({...r,...e}))(s),o=(t=>{const[r,n]=e.useState(()=>t.initialState());return{value:r,set:n,setLoading:()=>n(t.setLoading(r)),setResult:e=>n(t.setResult(e,r)),setError:e=>n(t.setError(e,r))}})(u),c=(()=>{const t=e.useRef(!1);return e.useEffect(()=>(t.current=!0,()=>{t.current=!1}),[]),()=>t.current})(),i=(()=>{const t=e.useRef(null);return{set:e=>t.current=e,get:()=>t.current,is:e=>t.current===e}})(),a=e=>c()&&i.is(e),l=(...e)=>{const r=t(...e);if(r instanceof Promise)return i.set(r),o.setLoading(),r.then(e=>{a(r)&&o.setResult(e)},e=>{a(r)&&o.setError(e)}),r;{const e=r;return o.setResult(e),Promise.resolve(e)}},d=!c();return e.useEffect(()=>{d?u.executeOnMount&&l(...n):u.executeOnUpdate&&l(...n)},n),{...o.value,set:o.set,execute:l,currentPromise:i.get()}};function s(e,t,r){return n(e,t,r)}exports.useAsync=s,exports.useAsyncAbortable=((t,r,n)=>{const u=e.useRef();return s(async(...e)=>{u.current&&u.current.abort();const r=new AbortController;u.current=r;try{return await t(r.signal,...e)}finally{u.current===r&&(u.current=void 0)}},r,n)}),exports.useAsyncCallback=(e=>n(e,[],{executeOnMount:!1,executeOnUpdate:!1,initialState:()=>({loading:!1,result:void 0,error:void 0})})); | ||
"use strict";var e=require("react");const t={loading:!0,result:void 0,error:void 0},r={initialState:()=>t,executeOnMount:!0,executeOnUpdate:!0,setLoading:e=>t,setResult:(e,t)=>({loading:!1,result:e,error:void 0}),setError:(e,t)=>({loading:!1,result:void 0,error:e})},s=(t,s,n)=>{const u=(e=>({...r,...e}))(n),o=(t=>{const[r,s]=e.useState(()=>t.initialState()),n=e.useCallback(()=>s(t.initialState()),[s,t]),u=e.useCallback(()=>s(t.setLoading(r)),[r,s]),o=e.useCallback(e=>s(t.setResult(e,r)),[r,s]),c=e.useCallback(e=>s(t.setError(e,r)),[r,s]);return{value:r,set:s,reset:n,setLoading:u,setResult:o,setError:c}})(u),c=(()=>{const t=e.useRef(!1);return e.useEffect(()=>(t.current=!0,()=>{t.current=!1}),[]),()=>t.current})(),a=(()=>{const t=e.useRef(null);return{set:e=>t.current=e,get:()=>t.current,is:e=>t.current===e}})(),i=e=>c()&&a.is(e),l=(...e)=>{const r=t(...e);if(r instanceof Promise)return a.set(r),o.setLoading(),r.then(e=>{i(r)&&o.setResult(e)},e=>{i(r)&&o.setError(e)}),r;{const e=r;return o.setResult(e),Promise.resolve(e)}},d=!c();return e.useEffect(()=>{d?u.executeOnMount&&l(...s):u.executeOnUpdate&&l(...s)},s),{...o.value,set:o.set,execute:l,currentPromise:a.get()}};function n(e,t,r){return s(e,t,r)}exports.useAsync=n,exports.useAsyncAbortable=((t,r,s)=>{const u=e.useRef();return n(async(...e)=>{u.current&&u.current.abort();const r=new AbortController;u.current=r;try{return await t(r.signal,...e)}finally{u.current===r&&(u.current=void 0)}},r,s)}),exports.useAsyncCallback=(e=>s(e,[],{executeOnMount:!1,executeOnUpdate:!1,initialState:()=>({loading:!1,result:void 0,error:void 0})})); | ||
//# sourceMappingURL=react-async-hook.cjs.production.js.map |
@@ -1,2 +0,2 @@ | ||
import{useRef as e,useEffect as t,useState as r}from"react";const n={loading:!0,result:void 0,error:void 0},o={initialState:()=>n,executeOnMount:!0,executeOnUpdate:!0,setLoading:e=>n,setResult:(e,t)=>({loading:!1,result:e,error:void 0}),setError:(e,t)=>({loading:!1,result:void 0,error:e})},s=(n,s,u)=>{const i=(e=>({...o,...e}))(u),c=(e=>{const[t,n]=r(()=>e.initialState());return{value:t,set:n,setLoading:()=>n(e.setLoading(t)),setResult:r=>n(e.setResult(r,t)),setError:r=>n(e.setError(r,t))}})(i),a=(()=>{const r=e(!1);return t(()=>(r.current=!0,()=>{r.current=!1}),[]),()=>r.current})(),l=(()=>{const t=e(null);return{set:e=>t.current=e,get:()=>t.current,is:e=>t.current===e}})(),d=e=>a()&&l.is(e),g=(...e)=>{const t=n(...e);if(t instanceof Promise)return l.set(t),c.setLoading(),t.then(e=>{d(t)&&c.setResult(e)},e=>{d(t)&&c.setError(e)}),t;{const e=t;return c.setResult(e),Promise.resolve(e)}},v=!a();return t(()=>{v?i.executeOnMount&&g(...s):i.executeOnUpdate&&g(...s)},s),{...c.value,set:c.set,execute:g,currentPromise:l.get()}};function u(e,t,r){return s(e,t,r)}const i=(t,r,n)=>{const o=e();return u(async(...e)=>{o.current&&o.current.abort();const r=new AbortController;o.current=r;try{return await t(r.signal,...e)}finally{o.current===r&&(o.current=void 0)}},r,n)},c=e=>s(e,[],{executeOnMount:!1,executeOnUpdate:!1,initialState:()=>({loading:!1,result:void 0,error:void 0})});export{u as useAsync,i as useAsyncAbortable,c as useAsyncCallback}; | ||
import{useRef as e,useEffect as t,useState as r,useCallback as n}from"react";const o={loading:!0,result:void 0,error:void 0},s={initialState:()=>o,executeOnMount:!0,executeOnUpdate:!0,setLoading:e=>o,setResult:(e,t)=>({loading:!1,result:e,error:void 0}),setError:(e,t)=>({loading:!1,result:void 0,error:e})},u=(o,u,i)=>{const c=(e=>({...s,...e}))(i),a=(e=>{const[t,o]=r(()=>e.initialState()),s=n(()=>o(e.initialState()),[o,e]),u=n(()=>o(e.setLoading(t)),[t,o]),i=n(r=>o(e.setResult(r,t)),[t,o]),c=n(r=>o(e.setError(r,t)),[t,o]);return{value:t,set:o,reset:s,setLoading:u,setResult:i,setError:c}})(c),l=(()=>{const r=e(!1);return t(()=>(r.current=!0,()=>{r.current=!1}),[]),()=>r.current})(),d=(()=>{const t=e(null);return{set:e=>t.current=e,get:()=>t.current,is:e=>t.current===e}})(),g=e=>l()&&d.is(e),v=(...e)=>{const t=o(...e);if(t instanceof Promise)return d.set(t),a.setLoading(),t.then(e=>{g(t)&&a.setResult(e)},e=>{g(t)&&a.setError(e)}),t;{const e=t;return a.setResult(e),Promise.resolve(e)}},x=!l();return t(()=>{x?c.executeOnMount&&v(...u):c.executeOnUpdate&&v(...u)},u),{...a.value,set:a.set,execute:v,currentPromise:d.get()}};function i(e,t,r){return u(e,t,r)}const c=(t,r,n)=>{const o=e();return i(async(...e)=>{o.current&&o.current.abort();const r=new AbortController;o.current=r;try{return await t(r.signal,...e)}finally{o.current===r&&(o.current=void 0)}},r,n)},a=e=>u(e,[],{executeOnMount:!1,executeOnUpdate:!1,initialState:()=>({loading:!1,result:void 0,error:void 0})});export{i as useAsync,c as useAsyncAbortable,a as useAsyncCallback}; | ||
//# sourceMappingURL=react-async-hook.es.production.js.map |
@@ -37,8 +37,19 @@ (function (global, factory) { | ||
const [value, setValue] = react.useState(() => options.initialState()); | ||
const reset = react.useCallback(() => setValue(options.initialState()), [ | ||
setValue, | ||
options, | ||
]); | ||
const setLoading = react.useCallback(() => setValue(options.setLoading(value)), [ | ||
value, | ||
setValue, | ||
]); | ||
const setResult = react.useCallback((result) => setValue(options.setResult(result, value)), [value, setValue]); | ||
const setError = react.useCallback((error) => setValue(options.setError(error, value)), [value, setValue]); | ||
return { | ||
value, | ||
set: setValue, | ||
setLoading: () => setValue(options.setLoading(value)), | ||
setResult: result => setValue(options.setResult(result, value)), | ||
setError: error => setValue(options.setError(error, value)), | ||
reset, | ||
setLoading, | ||
setResult, | ||
setError, | ||
}; | ||
@@ -45,0 +56,0 @@ }; |
@@ -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},n={initialState:()=>r,executeOnMount:!0,executeOnUpdate:!0,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,r,s)=>{const u=(e=>({...n,...e}))(s),o=(e=>{const[r,n]=t.useState(()=>e.initialState());return{value:r,set:n,setLoading:()=>n(e.setLoading(r)),setResult:t=>n(e.setResult(t,r)),setError:t=>n(e.setError(t,r))}})(u),c=(()=>{const e=t.useRef(!1);return t.useEffect(()=>(e.current=!0,()=>{e.current=!1}),[]),()=>e.current})(),i=(()=>{const e=t.useRef(null);return{set:t=>e.current=t,get:()=>e.current,is:t=>e.current===t}})(),a=e=>c()&&i.is(e),l=(...t)=>{const r=e(...t);if(r instanceof Promise)return i.set(r),o.setLoading(),r.then(e=>{a(r)&&o.setResult(e)},e=>{a(r)&&o.setError(e)}),r;{const e=r;return o.setResult(e),Promise.resolve(e)}},d=!c();return t.useEffect(()=>{d?u.executeOnMount&&l(...r):u.executeOnUpdate&&l(...r)},r),{...o.value,set:o.set,execute:l,currentPromise:i.get()}};function u(e,t,r){return s(e,t,r)}e.useAsync=u,e.useAsyncAbortable=((e,r,n)=>{const s=t.useRef();return u(async(...t)=>{s.current&&s.current.abort();const r=new AbortController;s.current=r;try{return await e(r.signal,...t)}finally{s.current===r&&(s.current=void 0)}},r,n)}),e.useAsyncCallback=(e=>s(e,[],{executeOnMount:!1,executeOnUpdate:!1,initialState:()=>({loading:!1,result:void 0,error:void 0})}))}); | ||
!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={initialState:()=>r,executeOnMount:!0,executeOnUpdate:!0,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,r,s)=>{const u=(e=>({...n,...e}))(s),o=(e=>{const[r,n]=t.useState(()=>e.initialState()),s=t.useCallback(()=>n(e.initialState()),[n,e]),u=t.useCallback(()=>n(e.setLoading(r)),[r,n]),o=t.useCallback(t=>n(e.setResult(t,r)),[r,n]),c=t.useCallback(t=>n(e.setError(t,r)),[r,n]);return{value:r,set:n,reset:s,setLoading:u,setResult:o,setError:c}})(u),c=(()=>{const e=t.useRef(!1);return t.useEffect(()=>(e.current=!0,()=>{e.current=!1}),[]),()=>e.current})(),i=(()=>{const e=t.useRef(null);return{set:t=>e.current=t,get:()=>e.current,is:t=>e.current===t}})(),a=e=>c()&&i.is(e),l=(...t)=>{const r=e(...t);if(r instanceof Promise)return i.set(r),o.setLoading(),r.then(e=>{a(r)&&o.setResult(e)},e=>{a(r)&&o.setError(e)}),r;{const e=r;return o.setResult(e),Promise.resolve(e)}},d=!c();return t.useEffect(()=>{d?u.executeOnMount&&l(...r):u.executeOnUpdate&&l(...r)},r),{...o.value,set:o.set,execute:l,currentPromise:i.get()}};function u(e,t,r){return s(e,t,r)}e.useAsync=u,e.useAsyncAbortable=((e,r,n)=>{const s=t.useRef();return u(async(...t)=>{s.current&&s.current.abort();const r=new AbortController;s.current=r;try{return await e(r.signal,...t)}finally{s.current===r&&(s.current=void 0)}},r,n)}),e.useAsyncCallback=(e=>s(e,[],{executeOnMount:!1,executeOnUpdate:!1,initialState:()=>({loading:!1,result:void 0,error:void 0})}))}); | ||
//# sourceMappingURL=react-async-hook.umd.production.js.map |
{ | ||
"name": "react-async-hook", | ||
"version": "3.2.2", | ||
"version": "3.3.0", | ||
"description": "Async hook", | ||
@@ -5,0 +5,0 @@ "author": "Sébastien Lorber", |
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
87861
393