Computed members of observables don't show up in for...in or Object.keys
> let o = observable({a: 5, b: {c: 6}, f: () => 9})
< Object {$mobx: ObservableObjectAdministration}
> o.a
< 5
> o.b
< Object {$mobx: ObservableObjectAdministration}
> o.f
< 9
> for (let x in o) {console.log(x)}
< a
< b
< undefined
> Object.keys(o)
< ["a", "b"]
That is by design. Computed values are not enumerable so that objects are easier to generically copy past into other objects etc.
Hm I don't think I get it, what's an example?
An argument for the semantics I expected is that now these two expressions are equivalent:
@observable a
@computed get x() { return {a: this.a} }
x = { a: () => this.a }
@liron00 currently you can do something like:
class Todo {
@observable done = false;
@computed get isNotDone() {
return !this.isDone;
}
const t1 = new Todo();
const t2 = new Todo();
Object.assign(t1, t2);
The above works because isNotDone is non-enumerable and skipped. Otherwise it would try to copy isNotDone from t2 to t1, which is rightfully rejected because computed values cannot be assigned to. Similarly this makes it easier to create a json representation etc, computed properties are not part of the state of an object, just like it's methods, and hence it is nice that they are not enumerable. Sometimes it would be nice if they were enumerable, but I think there are more scenerios where it is convenient that they are left out.
Got it thanks. I agree your semantics are a better default for computed members of classes. Perhaps mine are preferable for plain objects but not a big deal.
@liron00
You can use Object.getOwnPropertyNames() to access the non enumerable props if you really want to get around it.
Is there an easy way to make computed properties enumerable, at least for unit-test purposes? I'd like to use it with partial object matches, something like this:
class State {
@observable done = false;
@computed get isNotDone() {
return !this.done;
}
}
// State.test.js
const state = new State
state.done = true
expect(state).toMatchObject({ isNotDone: false })
I needed this for testing purposes too and also for implementing an auto recorder function wrapping all its observable props into autorun and pushing changes into an array.
I used
[..._getAdministration(node).values.keys()])
Yet another use case for allowing enumerable computeds:
I use a @referenceTo(Model) decorator (kinda similar to MST's identifier/reference)
class User {
@referenceTo(User) friends
}
Which makes a hidden empty instance of the model in a Symbol and instead returns that when trying to access the property (friends in this example). This allows me to then retrieve the details of the Model async and apply them to the instance and none of my components have to null check. Then when serializing it returns just the identifier instead of the whole model... except that's where it can't because enumerable is hard-coded as false:
https://github.com/mobxjs/mobx/blob/e099329e5d1246632b05601d79220295af3a03cb/src/types/observableobject.ts#L374-L388
It'd be great if we could keep the default the way it is now so there is no change for any @computed users, but if we explicitly add enumerable: true to a computed() call...
const desc = computed(target, property, {
enumerable: true, // the magic sauce
get() {
...
},
set(value) {
...
}
})
return desc
... just let it pass through here:
https://github.com/mobxjs/mobx/blob/master/src/api/computed.ts#L19-L39
The only workarounds I can think of are pretty gross — using some specific name and explicitly checking for that in a custom serializing function and adding it to the serialized object?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.