Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redux devtools usability question #4

Open
BartAdv opened this issue Jan 16, 2017 · 7 comments
Open

Redux devtools usability question #4

BartAdv opened this issue Jan 16, 2017 · 7 comments

Comments

@BartAdv
Copy link

BartAdv commented Jan 16, 2017

Hi,

Do you have any tips for improving experience with redux devtools? It prints JSON representation of the runtime data, which means that sometimes it just doesn't show relevant info, for example empty constructor of a sum type is displayed as {} (chrome would display ConName {} where ConName is just constructor.name of the JS object).

I was able to improve it a bit by hooking own JSON replacer, but it's far from complete.

@ethul
Copy link
Owner

ethul commented Jan 17, 2017 via email

@BartAdv
Copy link
Author

BartAdv commented Jan 17, 2017

I currently got away with following hack:

var cfg = {
    serialize: {
        // hacks for PS ADTs
        replacer: function (key, value) {
            if (typeof value === "object" && !Array.isArray(value)) {
                var replacement = {};
                for (var k in value) {
                    if (Object.hasOwnProperty.call(value, k)) {
                        if(k == "value0") {
                            replacement[value[k].constructor.name] = value[k];
                        } else {
                                replacement[k] = value[k];
                        }
                    }
                }
                return replacement;
            }
            return value;
        }
    }
};

exports.reduxDevtoolsExtensionEnhancer_ = !window.__REDUX_DEVTOOLS_EXTENSION__ ? function(k){return k;} : window.__REDUX_DEVTOOLS_EXTENSION__(cfg);

This gives something like this:

{
  type: '@@PURESCRIPT_REACT_REDUX/Login',
  action: {
    AttemptLogin: {
      LoginData: {
        Object: {
          username: 'admin',
          password: 'pass'
        }
      }
    }
  }
}

but it doesn't do anything useful with contructors that don't take any values. (And I don't have corresponding reviver, so it probably breaks possibility to store state and load from there)...

@ethul
Copy link
Owner

ethul commented Jan 18, 2017

Nice. Thanks for sharing.

I wonder if there is a way to move the serialize/deserialize into PureScript and use Argonaut's EncodeJson and DecodeJson. This would mean that Action would have to have these instances though...

@BartAdv
Copy link
Author

BartAdv commented Jan 19, 2017

I guess just Generic instance would do. I asked around an some people on IRC suggested using 'purescript-foreign-generic'. Now, I dont know what are exact differences yet, but you're right it would first require change in devtools to allow own serialise/deserialize. Probably doable, but will require some time, so for now Im Rolling with above hack.

@ethul
Copy link
Owner

ethul commented Jan 19, 2017

Indeed, generic would work. Good idea.

@zalmoxisus
Copy link

Maybe it could be similar to the way we support ImmutableJS. Redux Extension allows you to mark specific data, adding a __serializedType__ key. So you can deserialize it back while importing or persisting data. Moreover, it will also show a nice preview showing the native type:

screen shot 2017-01-08 at 1 00 22 pm

As for classes, apart from replacer and reviver for serializeparameter, there's an undocumented refs key. So in replacer you can compare if the data is an instance of that class and mark correspondingly, like we do for Immutable Record classes.

Also you could send data to the extension right from PureScript, but you'll have to implement monitor actions by yourself in this case.

@ethul
Copy link
Owner

ethul commented Jan 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants