wiresjs / async-watch

AsyncWatch - watching js objects asynchronously
GNU General Public License v3.0
17 stars 3 forks source link

old value in callback #1

Closed devmondo closed 8 years ago

devmondo commented 8 years ago

hi,

great work there, could we get old value in the call back ?

something like this

AsyncWatch(obj, 'a.b.c', function(propertyName,oldVal,NewVal){
    console.log('set', value);
});

thanks in advanced.

nchanged commented 8 years ago

Hi! It supports oldValues - however, they would become inconsistent due to "transactions",

For example

let obj = {
        a: 0
};
for (var i = 0; i < 100; i++) {
         obj.a++;
}
watch(obj, 'a', function(newVal, oldVal) {
});

Will give you 99 as an old value (You would expect 0 right?). Please feel free to suggest if you have some ideas on the matter.

devmondo commented 8 years ago

thanks for the reply,

actually the example above dont run at all, i had to remove the for loop below the watch function, and it works fine and i get the correct old and new values, infact if i keep your example and just later modify the a prop like below i get correct old value as 100 and new value as 1111

let obj = {
    a: 0
};
for (var i = 0; i < 100; i++) {
    obj.a++;
}

watch(obj, 'a', function(newVal, oldVal) {
    console.log(44444444,newVal, oldVal);
});
obj.a = 1111;

so i am lost if you could explain more :)

nchanged commented 8 years ago

My bad, i am not really tracking old values there. (i did not put any efforts in testing those). Did not really think that anyone would need old values using async approach ;-) I will check it when i have time.

devmondo commented 8 years ago

thank you :)

nchanged commented 8 years ago

Hi! I have released a new version, that should fix few corner cases i encountered

If you i think it's all covered, we can close the issue =)

devmondo commented 8 years ago

awesome mate i tested it and it works flawlessly so far :)