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

value propagation check #72

Open
dashed opened this issue Jun 8, 2015 · 1 comment
Open

value propagation check #72

dashed opened this issue Jun 8, 2015 · 1 comment

Comments

@dashed
Copy link
Contributor

dashed commented Jun 8, 2015

I was thinking on this some more: omniscientjs/omniscient#104 (comment)

Rather than doing this naive check in shouldComponentUpdate, this may be done during development as an indicator that it should never occur. This can warn users that they're not really using the immutable library effectively.

API sketch:

// enable debug mode
if ('production' !== process.env.NODE_ENV) {
    immstruct.debug();
}

const struct = immstruct({foo: [1, 2]});

// adding a listener through .on(), .observe(), etc enables value propagation check.
// 
// internally, do something like this when values are propagated to listeners:
//
// if('production' !== process.env.NODE_ENV && 
//    newRoot.getIn(path) !== oldRoot.getIn(path) && 
//    Immutable.is(newRoot.getIn(path), oldRoot.getIn(path))) {
//
//   console.warn('detected node with distinct reference but have same value');
// }
struct.on('swap', _ => _);

// deliberate bad practice
struct.cursor('foo').update(function() {
     return Immutable.List.of(1, 2);
});
@dashed
Copy link
Contributor Author

dashed commented Jun 8, 2015

The only time this is good

// deliberate bad practice
struct.cursor('foo').update(function() {
     return Immutable.List.of(1, 2);
});

is during bootstrap when you're loading your models before React.render(...) and before listeners are registered:

// enable debug mode
if ('production' !== process.env.NODE_ENV) {
    immstruct.debug();
}

const struct = immstruct({foo: [1, 2]});

// load model from database, xhr, etc. 
// this is done before any listeners are subscribed.
struct.cursor('foo').update(function() {
     return Immutable.List.of(1, 2);
});

function render() {
    React.render(<Component cursor={struct.cursor()}) />);    

    // value propagation check is enabled after the first render
    struct.on('swap', render);
}
render();

// deliberate bad practice
struct.cursor('foo').update(function() {
     return Immutable.List.of(1, 2);
});

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

1 participant