react-async-hook
Advanced tools
Comparing version 3.4.0 to 3.5.0
@@ -0,2 +1,4 @@ | ||
export declare type AsyncStateStatus = 'not-requested' | 'loading' | 'success' | 'error'; | ||
export declare type AsyncState<R> = { | ||
status: AsyncStateStatus; | ||
loading: boolean; | ||
@@ -10,4 +12,7 @@ error: Error | undefined; | ||
declare type MaybePromise<T> = Promise<T> | T; | ||
declare type PromiseCallbackOptions = { | ||
isCurrent: () => boolean; | ||
}; | ||
export declare type UseAsyncOptionsNormalized<R> = { | ||
initialState: () => AsyncState<R>; | ||
initialState: (options?: UseAsyncOptionsNormalized<R>) => AsyncState<R>; | ||
executeOnMount: boolean; | ||
@@ -18,2 +23,4 @@ executeOnUpdate: boolean; | ||
setError: SetError<R>; | ||
onSuccess: (r: R, options: PromiseCallbackOptions) => void; | ||
onError: (e: Error, options: PromiseCallbackOptions) => void; | ||
}; | ||
@@ -26,2 +33,3 @@ export declare type UseAsyncOptions<R> = Partial<UseAsyncOptionsNormalized<R>> | undefined | null; | ||
currentPromise: Promise<R> | null; | ||
currentParams: Args | null; | ||
}; | ||
@@ -28,0 +36,0 @@ export declare function useAsync<R, Args extends any[]>(asyncFunction: () => Promise<R>, params: Args, options?: UseAsyncOptions<R>): UseAsyncReturn<R, Args>; |
@@ -43,2 +43,9 @@ 'use strict'; | ||
var InitialAsyncState = { | ||
status: 'not-requested', | ||
loading: false, | ||
result: undefined, | ||
error: undefined | ||
}; | ||
var InitialAsyncLoadingState = { | ||
status: 'loading', | ||
loading: true, | ||
@@ -50,3 +57,3 @@ result: undefined, | ||
var defaultSetLoading = function defaultSetLoading(_asyncState) { | ||
return InitialAsyncState; | ||
return InitialAsyncLoadingState; | ||
}; | ||
@@ -56,2 +63,3 @@ | ||
return { | ||
status: 'success', | ||
loading: false, | ||
@@ -65,2 +73,3 @@ result: result, | ||
return { | ||
status: 'error', | ||
loading: false, | ||
@@ -72,5 +81,7 @@ result: undefined, | ||
var noop = function noop() {}; | ||
var DefaultOptions = { | ||
initialState: function initialState() { | ||
return InitialAsyncState; | ||
initialState: function initialState(options) { | ||
return options && options.executeOnMount ? InitialAsyncLoadingState : InitialAsyncState; | ||
}, | ||
@@ -81,3 +92,5 @@ executeOnMount: true, | ||
setResult: defaultSetResult, | ||
setError: defaultSetError | ||
setError: defaultSetError, | ||
onSuccess: noop, | ||
onError: noop | ||
}; | ||
@@ -91,3 +104,3 @@ | ||
var _useState = react.useState(function () { | ||
return options.initialState(); | ||
return options.initialState(options); | ||
}), | ||
@@ -98,3 +111,3 @@ value = _useState[0], | ||
var reset = react.useCallback(function () { | ||
return setValue(options.initialState()); | ||
return setValue(options.initialState(options)); | ||
}, [setValue, options]); | ||
@@ -152,2 +165,7 @@ var setLoading = react.useCallback(function () { | ||
var normalizedOptions = normalizeOptions(options); | ||
var _useState2 = react.useState(null), | ||
currentParams = _useState2[0], | ||
setCurrentParams = _useState2[1]; | ||
var AsyncState = useAsyncState(normalizedOptions); | ||
@@ -163,4 +181,9 @@ var isMounted = useIsMounted(); | ||
var executeAsyncOperation = function executeAsyncOperation() { | ||
var promise = asyncFunction.apply(void 0, arguments); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var promise = asyncFunction.apply(void 0, args); | ||
setCurrentParams(args); | ||
if (promise instanceof Promise) { | ||
@@ -173,2 +196,8 @@ CurrentPromise.set(promise); | ||
} | ||
normalizedOptions.onSuccess(result, { | ||
isCurrent: function isCurrent() { | ||
return CurrentPromise.is(promise); | ||
} | ||
}); | ||
}, function (error) { | ||
@@ -178,2 +207,8 @@ if (shouldHandlePromise(promise)) { | ||
} | ||
normalizedOptions.onError(error, { | ||
isCurrent: function isCurrent() { | ||
return CurrentPromise.is(promise); | ||
} | ||
}); | ||
}); | ||
@@ -195,2 +230,3 @@ return promise; | ||
normalizedOptions.executeOnMount && executeAsyncOperation.apply(void 0, params); | ||
normalizedOptions.executeOnMount && executeAsyncOperation.apply(void 0, params); | ||
} else { | ||
@@ -204,3 +240,4 @@ normalizedOptions.executeOnUpdate && executeAsyncOperation.apply(void 0, params); | ||
execute: executeAsyncOperation, | ||
currentPromise: CurrentPromise.get() | ||
currentPromise: CurrentPromise.get(), | ||
currentParams: currentParams | ||
}); | ||
@@ -216,4 +253,4 @@ }; | ||
var asyncFunctionWrapper = function asyncFunctionWrapper() { | ||
for (var _len = arguments.length, p = new Array(_len), _key = 0; _key < _len; _key++) { | ||
p[_key] = arguments[_key]; | ||
for (var _len2 = arguments.length, p = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
p[_key2] = arguments[_key2]; | ||
} | ||
@@ -255,10 +292,3 @@ | ||
executeOnMount: false, | ||
executeOnUpdate: false, | ||
initialState: function initialState() { | ||
return { | ||
loading: false, | ||
result: undefined, | ||
error: undefined | ||
}; | ||
} | ||
executeOnUpdate: false | ||
}); | ||
@@ -265,0 +295,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var t=require("react");function e(){return(e=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var r={loading:!0,result:void 0,error:void 0},n={initialState:function(){return r},executeOnMount:!0,executeOnUpdate:!0,setLoading:function(t){return r},setResult:function(t,e){return{loading:!1,result:t,error:void 0}},setError:function(t,e){return{loading:!1,result:void 0,error:t}}},u=function(r,u,o){var i,c=function(t){return e({},n,{},t)}(o),a=function(e){var r=t.useState(function(){return e.initialState()}),n=r[0],u=r[1],o=t.useCallback(function(){return u(e.initialState())},[u,e]),i=t.useCallback(function(){return u(e.setLoading(n))},[n,u]),c=t.useCallback(function(t){return u(e.setResult(t,n))},[n,u]),a=t.useCallback(function(t){return u(e.setError(t,n))},[n,u]);return{value:n,set:u,reset:o,setLoading:i,setResult:c,setError:a}}(c),s=(i=t.useRef(!1),t.useEffect(function(){return i.current=!0,function(){i.current=!1}},[]),function(){return i.current}),l=function(){var e=t.useRef(null);return{set:function(t){return e.current=t},get:function(){return e.current},is:function(t){return e.current===t}}}(),f=function(t){return s()&&l.is(t)},v=function(){var t=r.apply(void 0,arguments);if(t instanceof Promise)return l.set(t),a.setLoading(),t.then(function(e){f(t)&&a.setResult(e)},function(e){f(t)&&a.setError(e)}),t;var e=t;return a.setResult(e),Promise.resolve(e)},d=!s();return t.useEffect(function(){d?c.executeOnMount&&v.apply(void 0,u):c.executeOnUpdate&&v.apply(void 0,u)},u),e({},a.value,{set:a.set,reset:a.reset,execute:v,currentPromise:l.get()})};function o(t,e,r){return u(t,e,r)}exports.useAsync=o,exports.useAsyncAbortable=function(e,r,n){var u=t.useRef();return o(function(){for(var t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];try{u.current&&u.current.abort();var o=new AbortController;return u.current=o,Promise.resolve(function(t,n){try{var u=Promise.resolve(e.apply(void 0,[o.signal].concat(r)))}catch(t){return n(!0,t)}return u&&u.then?u.then(n.bind(null,!1),n.bind(null,!0)):n(!1,value)}(0,function(t,e){if(u.current===o&&(u.current=void 0),t)throw e;return e}))}catch(t){return Promise.reject(t)}},r,n)},exports.useAsyncCallback=function(t){return u(t,[],{executeOnMount:!1,executeOnUpdate:!1,initialState:function(){return{loading:!1,result:void 0,error:void 0}}})}; | ||
"use strict";var r=require("react");function t(){return(t=Object.assign||function(r){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n])}return r}).apply(this,arguments)}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var e={status:"not-requested",loading:!1,result:void 0,error:void 0},n={status:"loading",loading:!0,result:void 0,error:void 0},u=function(){},o={initialState:function(r){return r&&r.executeOnMount?n:e},executeOnMount:!0,executeOnUpdate:!0,setLoading:function(r){return n},setResult:function(r,t){return{status:"success",loading:!1,result:r,error:void 0}},setError:function(r,t){return{status:"error",loading:!1,result:void 0,error:r}},onSuccess:u,onError:u},i=function(e,n,u){var i,s=function(r){return t({},o,{},r)}(u),c=r.useState(null),a=c[0],l=c[1],f=function(t){var e=r.useState(function(){return t.initialState(t)}),n=e[0],u=e[1],o=r.useCallback(function(){return u(t.initialState(t))},[u,t]),i=r.useCallback(function(){return u(t.setLoading(n))},[n,u]),s=r.useCallback(function(r){return u(t.setResult(r,n))},[n,u]),c=r.useCallback(function(r){return u(t.setError(r,n))},[n,u]);return{value:n,set:u,reset:o,setLoading:i,setResult:s,setError:c}}(s),v=(i=r.useRef(!1),r.useEffect(function(){return i.current=!0,function(){i.current=!1}},[]),function(){return i.current}),d=function(){var t=r.useRef(null);return{set:function(r){return t.current=r},get:function(){return t.current},is:function(r){return t.current===r}}}(),y=function(r){return v()&&d.is(r)},b=function(){for(var r=arguments.length,t=new Array(r),n=0;n<r;n++)t[n]=arguments[n];var u=e.apply(void 0,t);if(l(t),u instanceof Promise)return d.set(u),f.setLoading(),u.then(function(r){y(u)&&f.setResult(r),s.onSuccess(r,{isCurrent:function(){return d.is(u)}})},function(r){y(u)&&f.setError(r),s.onError(r,{isCurrent:function(){return d.is(u)}})}),u;var o=u;return f.setResult(o),Promise.resolve(o)},p=!v();return r.useEffect(function(){p?(s.executeOnMount&&b.apply(void 0,n),s.executeOnMount&&b.apply(void 0,n)):s.executeOnUpdate&&b.apply(void 0,n)},n),t({},f.value,{set:f.set,reset:f.reset,execute:b,currentPromise:d.get(),currentParams:a})};function s(r,t,e){return i(r,t,e)}exports.useAsync=s,exports.useAsyncAbortable=function(t,e,n){var u=r.useRef();return s(function(){for(var r=arguments.length,e=new Array(r),n=0;n<r;n++)e[n]=arguments[n];try{u.current&&u.current.abort();var o=new AbortController;return u.current=o,Promise.resolve(function(r,n){try{var u=Promise.resolve(t.apply(void 0,[o.signal].concat(e)))}catch(r){return n(!0,r)}return u&&u.then?u.then(n.bind(null,!1),n.bind(null,!0)):n(!1,value)}(0,function(r,t){if(u.current===o&&(u.current=void 0),r)throw t;return t}))}catch(r){return Promise.reject(r)}},e,n)},exports.useAsyncCallback=function(r){return i(r,[],{executeOnMount:!1,executeOnUpdate:!1})}; | ||
//# sourceMappingURL=react-async-hook.cjs.production.min.js.map |
@@ -1,2 +0,2 @@ | ||
import { useRef, useEffect, useState, useCallback } from 'react'; | ||
import { useRef, useState, useEffect, useCallback } from 'react'; | ||
@@ -41,2 +41,9 @@ function _extends() { | ||
var InitialAsyncState = { | ||
status: 'not-requested', | ||
loading: false, | ||
result: undefined, | ||
error: undefined | ||
}; | ||
var InitialAsyncLoadingState = { | ||
status: 'loading', | ||
loading: true, | ||
@@ -48,3 +55,3 @@ result: undefined, | ||
var defaultSetLoading = function defaultSetLoading(_asyncState) { | ||
return InitialAsyncState; | ||
return InitialAsyncLoadingState; | ||
}; | ||
@@ -54,2 +61,3 @@ | ||
return { | ||
status: 'success', | ||
loading: false, | ||
@@ -63,2 +71,3 @@ result: result, | ||
return { | ||
status: 'error', | ||
loading: false, | ||
@@ -70,5 +79,7 @@ result: undefined, | ||
var noop = function noop() {}; | ||
var DefaultOptions = { | ||
initialState: function initialState() { | ||
return InitialAsyncState; | ||
initialState: function initialState(options) { | ||
return options && options.executeOnMount ? InitialAsyncLoadingState : InitialAsyncState; | ||
}, | ||
@@ -79,3 +90,5 @@ executeOnMount: true, | ||
setResult: defaultSetResult, | ||
setError: defaultSetError | ||
setError: defaultSetError, | ||
onSuccess: noop, | ||
onError: noop | ||
}; | ||
@@ -89,3 +102,3 @@ | ||
var _useState = useState(function () { | ||
return options.initialState(); | ||
return options.initialState(options); | ||
}), | ||
@@ -96,3 +109,3 @@ value = _useState[0], | ||
var reset = useCallback(function () { | ||
return setValue(options.initialState()); | ||
return setValue(options.initialState(options)); | ||
}, [setValue, options]); | ||
@@ -150,2 +163,7 @@ var setLoading = useCallback(function () { | ||
var normalizedOptions = normalizeOptions(options); | ||
var _useState2 = useState(null), | ||
currentParams = _useState2[0], | ||
setCurrentParams = _useState2[1]; | ||
var AsyncState = useAsyncState(normalizedOptions); | ||
@@ -161,4 +179,9 @@ var isMounted = useIsMounted(); | ||
var executeAsyncOperation = function executeAsyncOperation() { | ||
var promise = asyncFunction.apply(void 0, arguments); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var promise = asyncFunction.apply(void 0, args); | ||
setCurrentParams(args); | ||
if (promise instanceof Promise) { | ||
@@ -171,2 +194,8 @@ CurrentPromise.set(promise); | ||
} | ||
normalizedOptions.onSuccess(result, { | ||
isCurrent: function isCurrent() { | ||
return CurrentPromise.is(promise); | ||
} | ||
}); | ||
}, function (error) { | ||
@@ -176,2 +205,8 @@ if (shouldHandlePromise(promise)) { | ||
} | ||
normalizedOptions.onError(error, { | ||
isCurrent: function isCurrent() { | ||
return CurrentPromise.is(promise); | ||
} | ||
}); | ||
}); | ||
@@ -193,2 +228,3 @@ return promise; | ||
normalizedOptions.executeOnMount && executeAsyncOperation.apply(void 0, params); | ||
normalizedOptions.executeOnMount && executeAsyncOperation.apply(void 0, params); | ||
} else { | ||
@@ -202,3 +238,4 @@ normalizedOptions.executeOnUpdate && executeAsyncOperation.apply(void 0, params); | ||
execute: executeAsyncOperation, | ||
currentPromise: CurrentPromise.get() | ||
currentPromise: CurrentPromise.get(), | ||
currentParams: currentParams | ||
}); | ||
@@ -214,4 +251,4 @@ }; | ||
var asyncFunctionWrapper = function asyncFunctionWrapper() { | ||
for (var _len = arguments.length, p = new Array(_len), _key = 0; _key < _len; _key++) { | ||
p[_key] = arguments[_key]; | ||
for (var _len2 = arguments.length, p = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
p[_key2] = arguments[_key2]; | ||
} | ||
@@ -253,10 +290,3 @@ | ||
executeOnMount: false, | ||
executeOnUpdate: false, | ||
initialState: function initialState() { | ||
return { | ||
loading: false, | ||
result: undefined, | ||
error: undefined | ||
}; | ||
} | ||
executeOnUpdate: false | ||
}); | ||
@@ -263,0 +293,0 @@ }; |
{ | ||
"name": "react-async-hook", | ||
"version": "3.4.0", | ||
"version": "3.5.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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
145036
542
0