podefr / observe-plus

A library that wraps Object.observe/Array.observe and adds useful features such as observeOnce, pause/resume, fine-grained events and the disposable pattern

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nested array not triggering

slifin opened this issue · comments

What am I doing wrong here?

oPlus = require('observe-plus');
var game = {
    names: []
};
observer = oPlus.observe(game);
observer.observeValue('names',function(){
    console.log('this does not trigger');
});
observer.observe('update',function(){
    console.log('this does not trigger');
});
game.names.push('test');

it seems that you're doing it right. My tests are failing on the last version of observe-plus, no idea how it happened but I'm investigating and will let you know when it's fixed. Thanks for reporting this!

Besides that my tests are failing (fixing that), I can see two issues with the example:

observeValue is meant to be used with tree leaves. In your example, if you want to know what happens to the first item in your names array, then you should do:

observeValue('names.0', fn...)

If you want to know when changes are made to an object/array, you should use observe, just like in your example, but the update event is for watching changes on the properties of an objects. For items in an array, you should listen to the splice event instead:

oPlus = require('observe-plus');
var game = {
    names: []
};
observer = oPlus.observe(game);
observer.observeValue('names.0', function(ev, originalEvent) {
    console.log('this triggers', ev, originalEvent);
});
observer.observe('spice', function(ev, originalEvent) {
    console.log('this triggers', ev, originalEvent);
});
game.names.push('test');

Let me know if this helps, it should also work with observe-plus 3.0.3 as I just tested it

ah ok I thought listening to splice would only trigger when .splice() was called not .push()

everything appears to be working with the corrected code I find it interesting that observeValue('names.0') also appears to monitor update changes on key items other than .0 for example names.2 triggers on that,
one command appears to update on create of array elems and the other on update of array values

is there a way to watch the entire object for array updates? the same way observing splice checks the whole object instead of having to define keys? (this functionality isn't essential for me personally just curious really)

ah ok I thought listening to splice would only trigger when .splice() was called not .push()

Right, it's confusing but all mutations (pop, push, splice...) actually trigger a splice event in Object.observe.

observeValue('names.0') also appears to monitor update changes on key items other than .0 for example names.2 triggers on that

Can you file another bug for that with a snippet and your version of observe-plus? I'd like to investigate and possibly add a test case.

is there a way to watch the entire object for array updates?

what's the difference with doing a observe('splice', ...)? can you provide an example?

Thanks!

seems like only my tests were failing due to an upgrade to a more recent node.js. I pushed 3.0.4 which fixes the tests.

I'd appreciate though if you could still post me a snippet showcasing what doesn't work for you so that I can further investigate :)

nothing appears broken if this is intended functionality:

oPlus = require('observe-plus');
var game = {
    names: []
};
observer = oPlus.observe(game);

observer.observe('splice',function(){
    console.log('splice event triggered');
});

observer.observeValue('names.0',function(){
    console.log('names.0 triggered');
});

game.names.push('test'); //triggers both events
game.names.push('test2'); //triggers splice event
game.names[5] = 'test3'; //triggers splice event 
game.names[5] = 'test5'; //triggers names.0 event (but does not match criteria names.0 - note that names.5 was updated not names.0)

the bit I was curious about is that I can watch an entire object for new elements with

observer.observe('splice'

and I can watch a specific key for array updates with

observer.observeValue('names.0'

but can I watch an entire object for array updates without specifying keys?

when I say array updates, what I mean is taking an existing value within an array and updating it this doesn't seem to trigger the splice event but does trigger under observeValue.

It would be nice to have a method that triggers on push of new elements and updates to old array values