Skip to content

Instantly share code, notes, and snippets.

@lanwin
Created June 7, 2016 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanwin/7ca2df3b9b582acfc66530f31790d483 to your computer and use it in GitHub Desktop.
Save lanwin/7ca2df3b9b582acfc66530f31790d483 to your computer and use it in GitHub Desktop.
Redux devtools extension with mori
<!DOCTYPE html>
<html>
<body>
<script src="/node_modules/redux/dist/redux.js" type="text/javascript"></script>
<script src="/node_modules/mori/mori.js" type="text/javascript"></script>
<script type="text/javascript">
"use strict";
function reducer(){
return mori.hashMap('key','value');
}
var store = Redux.createStore(reducer, null, window.devToolsExtension({
select:function(state){return mori.toJs(state);}
}));
store.dispatch({type:'test'});
console.log('state: ',store.getState());
</script>
</body>
</html>
@lanwin
Copy link
Author

lanwin commented Jun 7, 2016

npm install mori
npm install redux
npm install http-server
.\node_modules.bin\htt-server

Mori uses its own data structures (here a hashMap). It would be nice if the devtools extension could get an selector function (as in my example obove) to transform the state before its getting display by the devtools extension.

@zalmoxisus
Copy link

zalmoxisus commented Jun 7, 2016

What do you think of passing instead a function for serialization like

    var store = Redux.createStore(reducer, window.devToolsExtension && window.devToolsExtension({
        serializeState: function(key, value){
            if (value && mori.isMap(value)) { return mori.toJs(value); }
            return value;
        }
    }));

It would allow to have mixed data in states (and for action payloads especially), and also to do better serialization (with mori.intoArray for example).

@lanwin
Copy link
Author

lanwin commented Jun 8, 2016

Thanks for fixing it this fast!!!

I am not sure about that API. For what exactly this is called? The key value thing confuses me a little bit.

mori.toJs can convert an entire state tree to plain js even if not all objects are mori collections.

@lanwin
Copy link
Author

lanwin commented Jun 8, 2016

Check it. Works like a charm 💃 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment