From eb008c31fbd5dcd1e56362d026d63c5f89f6d4a7 Mon Sep 17 00:00:00 2001 From: ElliotNB Date: Tue, 21 Aug 2018 20:49:38 -0700 Subject: [PATCH] Bug fix for issue where multiple observers on same object can trigger before mutation has been applied. --- observable-slim.js | 18 ++++++++---------- observable-slim.min.js | 2 +- test/test.js | 16 +++++++++++++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/observable-slim.js b/observable-slim.js index fff06b0..7dae880 100644 --- a/observable-slim.js +++ b/observable-slim.js @@ -219,6 +219,9 @@ var ObservableSlim = (function() { if (originalChange === true) { + // perform the delete that we've trapped if changes are not paused for this observable + if (!observable.changesPaused) delete target[property]; + for (var a = 0, l = targets.length; a < l; a++) if (target === targets[a]) break; // loop over each proxy and see if the target for this change has any other proxies @@ -238,10 +241,6 @@ var ObservableSlim = (function() { } } - // perform the delete that we've trapped if changes are not paused for this observable - if (!observable.changesPaused) - delete target[property]; - } _notifyObservers(changes.length); @@ -307,6 +306,11 @@ var ObservableSlim = (function() { // observers can be modified of the change that has occurred. if (originalChange === true) { + // because the value actually differs than the previous value + // we need to store the new value on the original target object, + // but only as long as changes have not been paused + if (!observable.changesPaused) target[property] = value; + for (var a = 0, l = targets.length; a < l; a++) if (target === targets[a]) break; foundObservable = (a < l); @@ -397,12 +401,6 @@ var ObservableSlim = (function() { },10000); } - // because the value actually differs than the previous value - // we need to store the new value on the original target object, - // but only as long as changes have not been paused - if (!observable.changesPaused) - target[property] = value; - // TO DO: the next block of code resolves test case #24, but it results in poor IE11 performance. Find a solution. // // if the value we've just set is an object, then we'll need to iterate over it in order to initialize the diff --git a/observable-slim.min.js b/observable-slim.min.js index 6b3a535..b7baaed 100644 --- a/observable-slim.min.js +++ b/observable-slim.min.js @@ -1 +1 @@ -var ObservableSlim=function(){paths=[];var l=[],d=[],_=[],x=null,f=function(e,i,r,t){var h=r||null,u=t||[{target:e,property:""}];paths.push(u),e instanceof Array&&(e.__length=e.length);var c=[],v=function(e,r,t){for(var n="",a=null,o=0;o { + + var data = {"foo":"bar"}; + var p = ObservableSlim.create(data, false, function(changes) { + expect(p.foo).to.equal("test"); + }); + + var pp = ObservableSlim.create(p, false, function(changes) { + expect(p.foo).to.equal("test"); + }); + + p.foo = "test"; + }); + // This test currently does not have an expectation or assertion. For now it simply // ensures that the garbage clean-up code runs and that the garbage clean-up doesn't throw an error. - it('30. Clean-up observers of overwritten (orphaned) objects.', (done) => { + it('31. Clean-up observers of overwritten (orphaned) objects.', (done) => { var data = {"testing":{"test":{"testb":"hello world"},"testc":"hello again"},"blah":{"tree":"world"}}; var p = ObservableSlim.create(data, false, function(changes) {