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

introduce @shallowObservable #55

Closed
dzearing opened this issue Nov 18, 2015 · 4 comments
Closed

introduce @shallowObservable #55

dzearing opened this issue Nov 18, 2015 · 4 comments

Comments

@dzearing
Copy link
Contributor

When you make an array observable:

@observable foo: [];

... and assign it a value:

foo = [ { bar: 1, baz: ClassType } ];

...the baz value becomes a setter/getter implicitly, so saying something like:

new foo[0].baz() 

Ends up being broken and not what you expect. This is because mobservable is recursing through added things and implicitily observable-ifying them.

Instead what would be more intuitive would be to do shallow observation by default, with optional recursive behavior:

@observable foo: any; 

// This only fires change events when the foo is assigned a new instance. 

foo = {}; // mutates
foo.a = true; // doesn't mutate a by default

Then, if we want foo to recurse, we explain that explicitly:

@observable({ recurse: true }) foo: []; // Does the recursive thing, so saying foo[0].bar = 2 mutates foo.
foo = { a: false }; // mutates, but also instruments the object
foo.a = true; // mutates foo

Arrays in particular are a bit ambiguous and I could go either way. I think i'm ok mutating a vanilla array into an observable array by default, but I'm not keen on going any farther without an opt in behavior.

@observable foo: any[];

foo = []; // mutates foo
foo.push({}); // mutates foo
foo[0].bar = true; // doesn't mutate foo

... but you could opt in of course, to recurse.

Though, a more pure solution would be to require wrapping an array with an observable to make it observable.

foo = observable([]);
@kenotron
Copy link

I would rather you not break the current API, and just do @shallowObservable

@dzearing
Copy link
Contributor Author

makes sense to me.

@mweststrate
Copy link
Member

I really like the notion of @shallowObservable.

Besides that I'll add this issue for general reconsideration in a 2.0 api

@mweststrate mweststrate modified the milestones: 1.1, 2.0 Nov 19, 2015
@mweststrate mweststrate changed the title suggestion: make observable shallow by default. introduce @shallowObservable Feb 26, 2016
@mweststrate
Copy link
Member

Closed in favor of the discussion in #211

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

No branches or pull requests

3 participants