mobx icon indicating copy to clipboard operation
mobx copied to clipboard

Computed members of observables don't show up in for...in or Object.keys

Open liron00 opened this issue 10 years ago • 9 comments

> 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"]

liron00 avatar Jun 20 '16 00:06 liron00

That is by design. Computed values are not enumerable so that objects are easier to generically copy past into other objects etc.

mweststrate avatar Jun 20 '16 06:06 mweststrate

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 avatar Jun 20 '16 11:06 liron00

@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.

mweststrate avatar Jun 20 '16 14:06 mweststrate

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 avatar Jun 20 '16 14:06 liron00

@liron00

You can use Object.getOwnPropertyNames() to access the non enumerable props if you really want to get around it.

AriaFallah avatar Jun 20 '16 16:06 AriaFallah

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 })

anatoliyarkhipov avatar Mar 29 '17 05:03 anatoliyarkhipov

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()])

ckwong90 avatar Aug 01 '18 09:08 ckwong90

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?

sarendipitee avatar Nov 24 '18 01:11 sarendipitee

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.

lock[bot] avatar Jul 21 '19 16:07 lock[bot]