UNPKG

atomico

Version:

Atomico is a small library for the creation of interfaces based on web-components, only using functions and hooks.

54 lines (44 loc) 1.56 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); require('./chunk/constants.js'); var utils = require('./chunk/utils.js'); var core = require('./core.js'); const LAZY_STATE_LOADING = "loading"; const LAZY_STATE_ERROR = "error"; const LAZY_STATE_DONE = "done"; function useLazy(callback, run, initWithLoading) { let initialState = initWithLoading ? [LAZY_STATE_LOADING] : []; let [state, setState] = core.useState(initialState); core.useEffect(() => { if (run) { if (!initWithLoading) { setTimeout(() => { if (run) setState([LAZY_STATE_LOADING]); }, 50); } callback() .then(md => { if (run) setState([LAZY_STATE_DONE, md.default || md]); run = false; }) .catch(e => { if (run) setState([LAZY_STATE_ERROR]); run = false; }); return () => { run = false; setState(state => utils.isEqualArray(state, initialState) ? state : [LAZY_STATE_LOADING] ); }; } }, [run]); return state; } exports.LAZY_STATE_DONE = LAZY_STATE_DONE; exports.LAZY_STATE_ERROR = LAZY_STATE_ERROR; exports.LAZY_STATE_LOADING = LAZY_STATE_LOADING; exports.useLazy = useLazy; //# sourceMappingURL=use-lazy.js.map