0

I am using Object.observe() on node v0.11.13.

It looks like the time of the observation callback to be called can't be predicted. is it a bug or a feature?

Take a look at this code:

function observe(obj,name){
    Object.observe(obj, function(o){
        console.log(name,o);
    });
    return obj;
}
var boo = observe({foo:1},'a');
var doo = observe({foo:1},'b');
doo.foo=2;
boo.foo=2;

The output looks like:

a [ { type: 'update', object: { foo: 2 }, name: 'foo', oldValue: 1 } ]
b [ { type: 'update', object: { foo: 2 }, name: 'foo', oldValue: 1 } ]

I would expect an opposite order. I wonder if this is related to the spec or to node impl' of this feature.

DuduAlul
  • 5,822
  • 7
  • 34
  • 61
  • looks like question has nothing to do with any specific library, you are trying to create a javascript object and its constructor is called in order or execution. – Anshul Nigam Dec 25 '14 at 18:42

1 Answers1

1

It seems to be following the order in which observers are registered rather than the order in which values are changed.

var doo = observe({foo:1},'a');
var boo = observe({foo:1},'b');
var zoo = observe({foo:1},'c');
var too = observe({foo:1},'d');

zoo.foo = 2;
too.foo = 2;
doo.foo= 2;
boo.foo= 2;

a [Object]
b [Object]
c [Object]
d [Object]

That makes sense, because multiple changes to the same object during a synchronous run through execution stack processing will be batched together.

plalx
  • 39,329
  • 5
  • 63
  • 83