Comparing version 0.2.1 to 0.2.2
@@ -193,51 +193,13 @@ import _regeneratorRuntime from "@babel/runtime/regenerator"; | ||
} | ||
/* export function useAction(name, action, ...args) { | ||
const forceRender = useForceRender(); | ||
const Context = getContext(ASN); | ||
const store = useContext(Context); | ||
const actionWithName = `${name}/${action}`; | ||
const status = store.getState(s => s[actionWithName] || {}); | ||
const statusRef = useRef(status); | ||
const actions = getStore(name).actions; | ||
const actionFn = actions[action]; | ||
// if (!status.pending) actionFn.apply(null, args); | ||
useIsomorphicLayoutEffect(() => { | ||
if (!status.pending) { | ||
// (async () => await actionFn.apply(null, args)); | ||
actionFn.apply(null, args); | ||
} | ||
statusRef.current = { ...status }; | ||
}); | ||
useIsomorphicLayoutEffect(() => { | ||
const prevState = store.getState(); | ||
// init async action status when call `useStatus` | ||
if (!prevState[actionWithName]) { | ||
prevState[actionWithName] = DEFAULT_STATUS; | ||
} | ||
function checkStatus() { | ||
const newStatus = store.getState(s => s[actionWithName]); | ||
const { pending, error, state } = statusRef.current; | ||
if ( | ||
newStatus.pending === pending && | ||
newStatus.error === error && | ||
Object.is(state, newStatus.state) | ||
) return; | ||
statusRef.current = newStatus; | ||
forceRender({}); | ||
} | ||
const unSubscribe = store.subscribe(actionWithName, checkStatus); | ||
return unSubscribe; | ||
}, [actionWithName]); | ||
return status; | ||
} */ | ||
/** | ||
* @see https://github.com/zeit/swr | ||
* @description call async action and listen status | ||
* function Foo() { | ||
* const { pending, data } = useAction('foo/asyncSet', options); | ||
* | ||
* return pending ? <div>loading...</div> : <div>{data}</div>; | ||
* } | ||
*/ | ||
// export function useAction(name, action, ...args) { | ||
// ; | ||
// } |
@@ -24,7 +24,5 @@ import React, { createElement, memo, useContext, useEffect, useMemo, useRef, useDebugValue } from 'react'; | ||
useEffect(function () { | ||
// cleanup | ||
// cleanup context | ||
return function () { | ||
deleteContext(name); // reset middlewares | ||
applyMiddlewares([]); | ||
deleteContext(name); | ||
}; | ||
@@ -56,2 +54,8 @@ }, [name]); | ||
useEffect(function () { | ||
// reset global middlewares | ||
return function () { | ||
applyMiddlewares([]); | ||
}; | ||
}, []); | ||
return providers; | ||
@@ -58,0 +62,0 @@ }); // access state in FC |
@@ -212,51 +212,13 @@ "use strict"; | ||
} | ||
/* export function useAction(name, action, ...args) { | ||
const forceRender = useForceRender(); | ||
const Context = getContext(ASN); | ||
const store = useContext(Context); | ||
const actionWithName = `${name}/${action}`; | ||
const status = store.getState(s => s[actionWithName] || {}); | ||
const statusRef = useRef(status); | ||
const actions = getStore(name).actions; | ||
const actionFn = actions[action]; | ||
// if (!status.pending) actionFn.apply(null, args); | ||
useIsomorphicLayoutEffect(() => { | ||
if (!status.pending) { | ||
// (async () => await actionFn.apply(null, args)); | ||
actionFn.apply(null, args); | ||
} | ||
statusRef.current = { ...status }; | ||
}); | ||
useIsomorphicLayoutEffect(() => { | ||
const prevState = store.getState(); | ||
// init async action status when call `useStatus` | ||
if (!prevState[actionWithName]) { | ||
prevState[actionWithName] = DEFAULT_STATUS; | ||
} | ||
function checkStatus() { | ||
const newStatus = store.getState(s => s[actionWithName]); | ||
const { pending, error, state } = statusRef.current; | ||
if ( | ||
newStatus.pending === pending && | ||
newStatus.error === error && | ||
Object.is(state, newStatus.state) | ||
) return; | ||
statusRef.current = newStatus; | ||
forceRender({}); | ||
} | ||
const unSubscribe = store.subscribe(actionWithName, checkStatus); | ||
return unSubscribe; | ||
}, [actionWithName]); | ||
return status; | ||
} */ | ||
/** | ||
* @see https://github.com/zeit/swr | ||
* @description call async action and listen status | ||
* function Foo() { | ||
* const { pending, data } = useAction('foo/asyncSet', options); | ||
* | ||
* return pending ? <div>loading...</div> : <div>{data}</div>; | ||
* } | ||
*/ | ||
// export function useAction(name, action, ...args) { | ||
// ; | ||
// } |
@@ -44,7 +44,5 @@ "use strict"; | ||
(0, _react.useEffect)(function () { | ||
// cleanup | ||
// cleanup context | ||
return function () { | ||
(0, _context.deleteContext)(name); // reset middlewares | ||
(0, _middlewares.applyMiddlewares)([]); | ||
(0, _context.deleteContext)(name); | ||
}; | ||
@@ -76,2 +74,8 @@ }, [name]); | ||
(0, _react.useEffect)(function () { | ||
// reset global middlewares | ||
return function () { | ||
(0, _middlewares.applyMiddlewares)([]); | ||
}; | ||
}, []); | ||
return providers; | ||
@@ -78,0 +82,0 @@ }); // access state in FC |
{ | ||
"name": "hookstore", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "React Hook based and lightweight state manage library.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -12,3 +12,2 @@ import React, { | ||
import isEqual from 'lodash.isequal'; | ||
// import clonedeep from 'lodash.clonedeep'; | ||
@@ -39,7 +38,5 @@ import { | ||
useEffect(() => { | ||
// cleanup | ||
// cleanup context | ||
return () => { | ||
deleteContext(name); | ||
// reset middlewares | ||
applyMiddlewares([]); | ||
}; | ||
@@ -67,2 +64,9 @@ }, [name]); | ||
useEffect(() => { | ||
// reset global middlewares | ||
return () => { | ||
applyMiddlewares([]); | ||
}; | ||
}, []); | ||
return providers; | ||
@@ -69,0 +73,0 @@ }); |
80106
1688