The way object monitoring works.
The observer will shoot at the next tick of the watch with a collection of notes. It does not start synchronously on individual changes as they are created. See Also Object.Observe Synchronous Callback .
To accomplish what you want, one approach is to rewrite your iterator so that it βsleepsβ each time through the loop to give Object.observe chance to fire. I do not necessarily recommend this exact approach, but as an example:
function iterate(a, fn) { (function loop() { if (!a.length) return; fn(a.shift()); setTimeout(loop); })(); }
Now, during this iteration of the loop, any property changes made on the fn observation object will be reported.
You can do the same using promises:
function iterate(a, fn) { a.reduce((p, e) => p.then(() => fn(e)), Promise.resolve()); }
If you find yourself in an async / wait environment (this is an ES7 function, but available in transpilers such as Babel), you can also do the following, which under the covers is equivalent to the promises approach above:
async function iterate(a, fn) { for (i of a) await fn(i); }
As an aside, you don't need IIFE here. Also, self not declared - I expect a runtime error in the line self.progress = M.progress .
user663031
source share