From faa683cf22ac97dd3a3dcd0bb31fdf6055976b25 Mon Sep 17 00:00:00 2001 From: ri7nz Date: Mon, 12 Aug 2019 00:50:01 +0700 Subject: [PATCH 1/6] added: Actions Initiator in Provider --- lib/index.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 081676b..23caf53 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,12 +20,19 @@ import { */ const StateContext = createContext() +/** + * @ignore + * @var {Array} actions + */ +let actions = [] + /** * @name * @param {Object} props * @property {Function} reducer **{@link https://reactjs.org/docs/hooks-reference.html#usereducer | useReducer}** * @property {Object} initialState * @property {Element} children **{@link https://reactjs.org/docs/react-api.html#createelement | createElement}** + * @property {Array|Function} actions * @description * **** as Wrapper of your `React` Application. * @@ -55,9 +62,21 @@ const StateContext = createContext() * , document.getElementById('root')) * */ -export const StateProvider = ({reducer, initialState, children }) => createElement(StateContext.Provider, { - value: useReducer(reducer, initialState) -}, children) +export const StateProvider = ({reducer, initialState, children, actions:newActions}) => { + if(Array.isArray(newActions)){ + for(let i=0; i Date: Mon, 12 Aug 2019 01:08:41 +0700 Subject: [PATCH 2/6] added: memoize actions --- README.md | 6 ++++-- lib/index.js | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7fbe27f..14d5e45 100644 --- a/README.md +++ b/README.md @@ -54,12 +54,14 @@ $ npm i @evilfactory/global-state - `props.reducer` - `props.initialState` - `props.children` + - `props.actions` ### Properties - `reducer` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** **[| useReducer](https://reactjs.org/docs/hooks-reference.html#usereducer)** - `initialState` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** - `children` **[Element](https://developer.mozilla.org/docs/Web/API/Element)** **[| createElement](https://reactjs.org/docs/react-api.html#createelement)** +- `actions` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function))** ### Examples @@ -111,8 +113,8 @@ const createTodo = (state, action, todo) => { }) } -const [,addTodo] = useGlobalState(createTodo) +const [,actions.createTodo] = useGlobalState(createTodo) -addTodo({title: 'New Task'}) +actions.createTodo({title: 'New Task'}) ... ``` diff --git a/lib/index.js b/lib/index.js index 23caf53..d3d9d18 100644 --- a/lib/index.js +++ b/lib/index.js @@ -22,9 +22,10 @@ const StateContext = createContext() /** * @ignore - * @var {Array} actions + * @var {Object} actions */ -let actions = [] +let actions = {} +const memoActions = [] /** * @name @@ -94,14 +95,23 @@ export const StateProvider = ({reducer, initialState, children, actions:newActio * }) * } * - * const [,addTodo] = useGlobalState(createTodo) + * const [,actions.createTodo] = useGlobalState(createTodo) * - * addTodo({title: 'New Task'}) + * actions.createTodo({title: 'New Task'}) * ... */ export const useGlobalState = (newAction) => { const [state, action] = useContext(StateContext) // newAction is action injector - return [state, newAction ? newAction.bind(null, state, action) : action ] + + // bind action + // Object iteration + for(let i in actions){ + if(!memoActions.includes(i)) actions[i]=actions[i].bind(null,state, action) + } + newAction = newAction ? { + [newAction.name]: newAction.bind(null, state, action), + } : actions + return [state, newAction] } From 756928cfb636f1f67f7001060189556f4ab90aa6 Mon Sep 17 00:00:00 2001 From: ri7nz Date: Mon, 12 Aug 2019 01:16:07 +0700 Subject: [PATCH 3/6] change test for actions changed --- __tests__/index.js | 9 ++++++--- lib/index.js | 7 ++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/__tests__/index.js b/__tests__/index.js index 9f3e9d7..5b259a2 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -16,7 +16,10 @@ test('StateProvider & useGlobalState test', () => { default: return state } }, - initialState: {test: false} + initialState: {test: false}, + actions: [ + function changeTest(state, action){return action({ type: 'TEST' })} + ] } const ShowTest = () => { @@ -29,10 +32,10 @@ test('StateProvider & useGlobalState test', () => { } const ChangeTest = () => { - const [, dispatch] = useGlobalState() + const [, actions] = useGlobalState() const handleClick = () => { - dispatch({ + actions.changeTest({ type: 'TEST' }) } diff --git a/lib/index.js b/lib/index.js index d3d9d18..b61d45c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -108,10 +108,15 @@ export const useGlobalState = (newAction) => { // bind action // Object iteration for(let i in actions){ - if(!memoActions.includes(i)) actions[i]=actions[i].bind(null,state, action) + if(!memoActions.includes(i)) { + actions[i]=actions[i].bind(null,state, action) + memoActions.push(i) + } } + newAction = newAction ? { [newAction.name]: newAction.bind(null, state, action), } : actions + return [state, newAction] } From a9f0c1c0a042a586b66fd2da4a9dead1eda5c62f Mon Sep 17 00:00:00 2001 From: ri7nz Date: Mon, 12 Aug 2019 01:25:47 +0700 Subject: [PATCH 4/6] documentation: UPDATE README.md --- README.md | 9 ++++++++- lib/index.js | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 14d5e45..12285ba 100644 --- a/README.md +++ b/README.md @@ -87,8 +87,15 @@ function todoReducer(state, action) { } } +function createTodo(state,action, todo){ + return action({type: 'NEW_TODO', todo}) +} + +// const [state, actions] = useGlobalState() +// actions.createTodo({title: 'New Todo Created', status: 'pending'}) + ReactDOM.render( - + , document.getElementById('root')) diff --git a/lib/index.js b/lib/index.js index b61d45c..629c73e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -56,8 +56,15 @@ const memoActions = [] * } * } * + * function createTodo(state,action, todo){ + * return action({type: 'NEW_TODO', todo}) + * } + * + * // const [state, actions] = useGlobalState() + * // actions.createTodo({title: 'New Todo Created', status: 'pending'}) + * * ReactDOM.render( - * + * * * * , document.getElementById('root')) From 285bfcfc23aca5fb2968d0467a82a27db5ed97e6 Mon Sep 17 00:00:00 2001 From: Rin Date: Mon, 12 Aug 2019 01:28:50 +0700 Subject: [PATCH 5/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 12285ba..602c06c 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ function todoReducer(state, action) { } function createTodo(state,action, todo){ - return action({type: 'NEW_TODO', todo}) + return action({type: 'ADD_TODO', todo}) } // const [state, actions] = useGlobalState() From 1d6616db39c38eb1685d5fc5a336e09bcdcec028 Mon Sep 17 00:00:00 2001 From: Rin Date: Mon, 12 Aug 2019 17:15:02 +0700 Subject: [PATCH 6/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 602c06c..a1129db 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ const createTodo = (state, action, todo) => { }) } -const [,actions.createTodo] = useGlobalState(createTodo) +const [,actions] = useGlobalState(createTodo) actions.createTodo({title: 'New Task'}) ...