UNPKG

4.6 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/controlled_store.js"],"names":["ActionTypes","UPDATE","onChange","initialStateOverride","createStore","controlledProps","reducer","initialState","enhancer","store","controlledReducer","controlledUpdate","state","action","type","payload","hasChanged","newState","forEach","key","value","process","nextTick","indexOf","stateOverride","dispatch"],"mappings":";;;;;;;;;;AAAA;;;;;;AAEA;AACA,IAAMA,cAAc;AAClBC,UAAQ;;AAGV;;;;;;;;;;;;;;;AAJoB,CAApB;kBAmBe,UAACC,QAAD;AAAA,MAAWC,oBAAX,uEAAkC,EAAlC;AAAA,SAAyC,UAACC,WAAD,EAAiB;AACvE;AACA,QAAIC,kBAAkB,oBAAYF,oBAAZ,CAAtB;;AAEA,WAAO,UAACG,OAAD,EAAUC,YAAV,EAAwBC,QAAxB,EAAqC;AAC1CD,qBAAe,4BAAO,EAAP,EAAWA,YAAX,EAAyBJ,oBAAzB,CAAf;AACA;AACA,UAAMM,QAAQL,YAAYM,iBAAZ,EAA+BH,YAA/B,EAA6CC,QAA7C,CAAd;;AAEA;AACA,aAAO,4BAAO,EAAP,EAAWC,KAAX,EAAkB;AACvBE;AADuB,OAAlB,CAAP;;AAIA,eAASD,iBAAT,CAA4BE,KAA5B,EAAmCC,MAAnC,EAA2C;AACzC;AACA,YAAIA,OAAOC,IAAP,KAAgBd,YAAYC,MAAhC,EAAwC;AACtC,iBAAO,4BAAO,EAAP,EAAWW,KAAX,EAAkBC,OAAOE,OAAzB,CAAP;AACD;AACD,YAAIC,aAAa,KAAjB;AACA,YAAMC,WAAWX,QAAQM,KAAR,EAAeC,MAAf,CAAjB;AACA,4BAAYI,QAAZ,EAAsBC,OAAtB,CAA8B,eAAO;AACnC,cAAID,SAASE,GAAT,MAAkBP,MAAMO,GAAN,CAAtB,EAAkC;AAClC,cAAMC,QAAQH,SAASE,GAAT,CAAd;AACAE,kBAAQC,QAAR,CAAiB;AAAA,mBAAMpB,SAASiB,GAAT,EAAcC,KAAd,CAAN;AAAA,WAAjB;AACA,cAAIf,gBAAgBkB,OAAhB,CAAwBJ,GAAxB,IAA+B,CAAC,CAApC,EAAuC;AACrC;AACA;AACA;AACA;AACA;AACA;AACAF,qBAASE,GAAT,IAAgBP,MAAMO,GAAN,CAAhB;AACD,WARD,MAQO;AACL;AACAH,yBAAa,IAAb;AACD;AACF,SAhBD;AAiBA,eAAOA,aAAaC,QAAb,GAAwBL,KAA/B;AACD;;AAED,eAASD,gBAAT,CAA2Ba,aAA3B,EAA0C;AACxCnB,0BAAkB,oBAAYmB,aAAZ,CAAlB;AACAf,cAAMgB,QAAN,CAAe;AACbX,gBAAMd,YAAYC,MADL;AAEbc,mBAASS;AAFI,SAAf;AAID;AACF,KA5CD;AA6CD,GAjDc;AAAA,C","file":"controlled_store.js","sourcesContent":["import assign from 'object-assign'\n\n// Private action type for controlled-store\nconst ActionTypes = {\n UPDATE: '@@controlled/UPDATE'\n}\n\n/**\n * A Redux store enhancer that allows a redux app to operate as a controlled\n * component, selectively moving state out of the app and into a container.\n *\n * Enhances the store with an additional method `controlledUpdate()` that will\n * override the redux store state. Any keys on the state object passed to\n * `controlledUpdate()` will be \"locked\" in that any actions in the app will no\n * longer directly update that part of the state, but instead call the `onChange`\n * function passed to the constructor, with the state key that has been updated\n * and the new value.\n *\n * @param {Function} onChange\n * @param {Object} stateOverride\n * @return {Function} Redux Store Enhancer\n */\nexport default (onChange, initialStateOverride = {}) => (createStore) => {\n // These properties of the app state are now controlled\n let controlledProps = Object.keys(initialStateOverride)\n\n return (reducer, initialState, enhancer) => {\n initialState = assign({}, initialState, initialStateOverride)\n // Create the store with an enhanced reducer\n const store = createStore(controlledReducer, initialState, enhancer)\n\n // Enhance the store with an additional method `controlledUpdate()`\n return assign({}, store, {\n controlledUpdate\n })\n\n function controlledReducer (state, action) {\n // Controlled updates skip app reducers and override the state\n if (action.type === ActionTypes.UPDATE) {\n return assign({}, state, action.payload)\n }\n let hasChanged = false\n const newState = reducer(state, action)\n Object.keys(newState).forEach(key => {\n if (newState[key] === state[key]) return\n const value = newState[key]\n process.nextTick(() => onChange(key, value))\n if (controlledProps.indexOf(key) > -1) {\n // If any controlled props of the state are updated, we hide the\n // initial change in state from the redux store and instead\n // call the `onChange` function with the key that has been updated\n // and the new value. Needs to run on nextTick to avoid `controlledUpdate()`\n // being called in the same tick and resulting in a `store.dispatch()`\n // inside this reducer.\n newState[key] = state[key]\n } else {\n // Unless an uncontrolled prop has been changed, we'll just return the existing state\n hasChanged = true\n }\n })\n return hasChanged ? newState : state\n }\n\n function controlledUpdate (stateOverride) {\n controlledProps = Object.keys(stateOverride)\n store.dispatch({\n type: ActionTypes.UPDATE,\n payload: stateOverride\n })\n }\n }\n}\n"]}
\No newline at end of file