Last active
February 25, 2020 00:43
-
-
Save csantiago132/0d2bc0b5e18fce613d70a6232f5ca42e to your computer and use it in GitHub Desktop.
Revisions
-
csantiago132 revised this gist
Feb 25, 2020 . 1 changed file with 44 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,51 @@ import * as Immutable from "immutable"; import * as React from "react"; export const combineReducer = reducers => { const globalState = {}; // set default state returned by reducer and its reducer for (const [key, value] of Object.entries(reducers)) { if (typeof value === "function") { globalState[key] = value(undefined, { type: "__@@PLACEHOLDER_ACTION__" }); } else { console.error(`${value} is not a function`); } } /** * Global reducer function; this is passed to the useReducer hook * * @param {object} state * @param {object} action */ const reducerFunction = (state, action) => { let hasStateChanged = false; const updatedStateByReducers = {}; /** * this is where dispatching happens; * the action is passed to all reducers one by one. * we iterate and pass the action to each reducer and this would return new * state if applicable. */ for (const reducer in reducers) { if (reducers.hasOwnProperty(reducer)) { const currentStateByKey = state[reducer]; const currentReducer = reducers[reducer]; const returnedStateByReducer = currentReducer(currentStateByKey, action); const areStateByKeyEqual = returnedStateByReducer !== currentStateByKey; hasStateChanged = hasStateChanged || areStateByKeyEqual; updatedStateByReducers[reducer] = returnedStateByReducer; } } return hasStateChanged ? updatedStateByReducers : state; }; // return the initial state and the global reducer return [globalState, reducerFunction]; }; -
csantiago132 revised this gist
Feb 25, 2020 . 1 changed file with 4 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,31 +5,9 @@ export const combineReducer = reducers => { const reducerFunction = (state, action) => { // ... prev. code ommited // finally, return the updatedStateByReducers if its not equal to the prevState return hasStateChanged ? updatedStateByReducers : state; }; }; -
csantiago132 revised this gist
Feb 25, 2020 . 1 changed file with 32 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,35 @@ // ... prev. code ommited export const combineReducer = reducers => { // ... prev. code ommited const reducerFunction = (state, action) => { // ... prev. code ommited /** * this is where dispatching happens; * the action is passed to all reducers one by one. */ for (const reducer in reducers) { if (reducers.hasOwnProperty(reducer)) { //match reducer with the state property it should update const currentStateByKey = state[reducer]; // get the reducer based on key const currentReducer = reducers[reducer]; // call each reducer and pass the action to it const returnedStateByReducer = currentReducer(currentStateByKey, action); // compared objects const areStateByKeyEqual = returnedStateByReducer !== currentStateByKey; hasStateChanged = hasStateChanged || areStateByKeyEqual; // replace prevState by the updated state we just got updatedStateByReducers[reducer] = returnedStateByReducer; } } }; }; -
csantiago132 revised this gist
Feb 24, 2020 . 1 changed file with 10 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,21 +1,16 @@ // ... prev. code ommited export const combineReducer = (reducers) => { const globalState = {}; for (const [key, value] of Object.entries(reducers)) { // check it its a reducer if (typeof value === 'function') { globalState[key] = value(undefined, { type: '__@@PLACEHOLDER_ACTION__' }); } else { // let the developer know the value is not a reducer console.error(`${value} is not a function`); } } } -
csantiago132 revised this gist
Feb 24, 2020 . 1 changed file with 2 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,23 +1,7 @@ // ... prev. code ommited export const combineReducer = (reducers) => { // ... prev. code ommited /** * Global reducer function; this is passed to the useReducer hook -
csantiago132 revised this gist
Feb 24, 2020 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,4 +18,21 @@ export const combineReducer = (reducers) => { console.error(`${value} is not a function`); } } /** * Global reducer function; this is passed to the useReducer hook * * @param {object} state * @param {object} action */ const reducerFunction = (state, action) => { // used to compare prevState vs the one returned by the reducer let hasStateChanged = false; // updated state object to be returned const updatedStateByReducers = {}; } } -
csantiago132 revised this gist
Feb 24, 2020 . 1 changed file with 11 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,5 +8,14 @@ import { reducers } from '../path' */ export const combineReducer = (reducers) => { const globalState = {}; for (const [key, value] of Object.entries(reducers)) { // check it its a reducer if (typeof value === 'function') { globalState[key] = value(undefined, { type: '__@@PLACEHOLDER_ACTION__' }); } else { // let the developer know the value is not a reducer console.error(`${value} is not a function`); } } } -
csantiago132 revised this gist
Feb 24, 2020 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,4 +8,5 @@ import { reducers } from '../path' */ export const combineReducer = (reducers) => { const globalState = {}; const test = {} } -
csantiago132 renamed this gist
Feb 24, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
csantiago132 created this gist
Feb 24, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ import { reducers } from '../path' /** * Combines all reducers into one to create the root reducer of the * application * * @param {object} reducers */ export const combineReducer = (reducers) => { const globalState = {}; }