Skip to content

Commit

Permalink
Merge pull request #104 from Starcounter-Jack/dirty_check_fix_#103_#98
Browse files Browse the repository at this point in the history
Fix json-patch check #103 #98
  • Loading branch information
aloscha committed May 30, 2016
2 parents de0163c + 565ffa5 commit 40ff8fb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 71 deletions.
36 changes: 8 additions & 28 deletions src/json-patch-duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ var jsonpatch;
this.obj = obj;
}
return Mirror;
})();
}());
var ObserverInfo = (function () {
function ObserverInfo(callback, observer) {
this.callback = callback;
this.observer = observer;
}
return ObserverInfo;
})();
}());
function getMirror(obj) {
for (var i = 0, ilen = beforeDict.length; i < ilen; i++) {
if (beforeDict[i].obj === obj) {
Expand Down Expand Up @@ -288,41 +288,23 @@ var jsonpatch;
if (callback) {
observer.callback = callback;
observer.next = null;
var intervals = this.intervals || [100, 1000, 10000, 60000];
if (intervals.push === void 0) {
throw new OriginalError("jsonpatch.intervals must be an array");
}
var currentInterval = 0;
var dirtyCheck = function () {
generate(observer);
};
var fastCheck = function () {
clearTimeout(observer.next);
observer.next = setTimeout(function () {
dirtyCheck();
currentInterval = 0;
observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
}, 0);
};
var slowCheck = function () {
dirtyCheck();
if (currentInterval == intervals.length)
currentInterval = intervals.length - 1;
observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
observer.next = setTimeout(dirtyCheck);
};
if (typeof window !== 'undefined') {
if (window.addEventListener) {
window.addEventListener('mousedown', fastCheck);
window.addEventListener('mouseup', fastCheck);
window.addEventListener('keydown', fastCheck);
window.addEventListener('keyup', fastCheck);
}
else {
document.documentElement.attachEvent('onmousedown', fastCheck);
document.documentElement.attachEvent('onmouseup', fastCheck);
document.documentElement.attachEvent('onkeydown', fastCheck);
document.documentElement.attachEvent('onkeyup', fastCheck);
}
}
observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
}
observer.patches = patches;
observer.object = obj;
Expand All @@ -332,14 +314,12 @@ var jsonpatch;
removeObserverFromMirror(mirror, observer);
if (typeof window !== 'undefined') {
if (window.removeEventListener) {
window.removeEventListener('mousedown', fastCheck);
window.removeEventListener('mouseup', fastCheck);
window.removeEventListener('keydown', fastCheck);
window.removeEventListener('keyup', fastCheck);
}
else {
document.documentElement.detachEvent('onmousedown', fastCheck);
document.documentElement.detachEvent('onmouseup', fastCheck);
document.documentElement.detachEvent('onkeydown', fastCheck);
document.documentElement.detachEvent('onkeyup', fastCheck);
}
}
};
Expand Down Expand Up @@ -514,7 +494,7 @@ var jsonpatch;
this.tree = tree;
}
return JsonPatchError;
})(OriginalError);
}(OriginalError));
jsonpatch.JsonPatchError = JsonPatchError;
jsonpatch.Error = JsonPatchError;
/**
Expand Down
42 changes: 10 additions & 32 deletions src/json-patch-duplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ module jsonpatch {

var beforeDict = [];

export var intervals;

class Mirror {
obj: any;
observers = [];
Expand Down Expand Up @@ -324,61 +322,41 @@ module jsonpatch {
if (callback) {
observer.callback = callback;
observer.next = null;
var intervals = this.intervals || [100, 1000, 10000, 60000];
if (intervals.push === void 0) {
throw new OriginalError("jsonpatch.intervals must be an array");
}
var currentInterval = 0;

var dirtyCheck = function () {
generate(observer);
};
var fastCheck = function () {
clearTimeout(observer.next);
observer.next = setTimeout(function () {
dirtyCheck();
currentInterval = 0;
observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
}, 0);
var dirtyCheck = () => {
generate(observer);
};
var slowCheck = function () {
dirtyCheck();
if (currentInterval == intervals.length)
currentInterval = intervals.length - 1;
observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
var fastCheck = () => {
clearTimeout(observer.next);
observer.next = setTimeout(dirtyCheck);
};
if (typeof window !== 'undefined') { //not Node
if (window.addEventListener) { //standards
window.addEventListener('mousedown', fastCheck);
window.addEventListener('mouseup', fastCheck);
window.addEventListener('keydown', fastCheck);
window.addEventListener('keyup', fastCheck);
}
else { //IE8
document.documentElement.attachEvent('onmousedown', fastCheck);
document.documentElement.attachEvent('onmouseup', fastCheck);
document.documentElement.attachEvent('onkeydown', fastCheck);
document.documentElement.attachEvent('onkeyup', fastCheck);
}
}
observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
}
observer.patches = patches;
observer.object = obj;

observer.unobserve = function () {
observer.unobserve = () => {
generate(observer);
clearTimeout(observer.next);
removeObserverFromMirror(mirror, observer);

if (typeof window !== 'undefined') {
if (window.removeEventListener) {
window.removeEventListener('mousedown', fastCheck);
window.removeEventListener('mouseup', fastCheck);
window.removeEventListener('keydown', fastCheck);
window.removeEventListener('keyup', fastCheck);
}
else {
document.documentElement.detachEvent('onmousedown', fastCheck);
document.documentElement.detachEvent('onmouseup', fastCheck);
document.documentElement.detachEvent('onkeydown', fastCheck);
document.documentElement.detachEvent('onkeyup', fastCheck);
}
}
};
Expand Down
48 changes: 37 additions & 11 deletions test/spec/duplexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ function triggerMouseup(elem) {
}
}

function triggerKeyup(elem) {
if (typeof document !== 'undefined') {
fireEvent((elem || document.documentElement), 'keyup');
}
}

//http://stackoverflow.com/questions/827716/emulate-clicking-a-link-with-javascript-that-works-with-ie
function fireEvent(obj, evt) {
var fireOnThis = obj;
Expand Down Expand Up @@ -426,8 +432,7 @@ describe("duplex", function () {
delete obj.foo;

var patches = jsonpatch.generate(observer);
expect(patches).toEqual([{op: 'remove', path: '/foo'}]);

expect(patches).toEqual([{ op: 'remove', path: '/foo' }]);
});
});
});
Expand Down Expand Up @@ -458,6 +463,8 @@ describe("duplex", function () {
obj.phoneNumbers[0].number = "123";
obj.phoneNumbers[1].number = "456";

triggerKeyup();

waitsFor(function(){
return typeof patches === 'object';
}, 100);
Expand All @@ -478,13 +485,14 @@ describe("duplex", function () {
obj = { firstName:"Albert", lastName:"Einstein",
phoneNumbers:[ {number:"12345"}, {number:"45353"} ]};

jsonpatch.intervals = [50];
jsonpatch.observe(obj, function(patches) {
called++;
lastPatches = patches;
});
obj.firstName = "Marcin";

triggerKeyup();

waitsFor(function(){
return called > 0;
}, 100, 'firstName change to Marcin');
Expand All @@ -494,6 +502,7 @@ describe("duplex", function () {
expect(lastPatches).toEqual([{op: 'replace', path: '/firstName', value: 'Marcin'}]);

obj.lastName = "Warp";
triggerKeyup();
});

waitsFor(function(){
Expand All @@ -516,13 +525,14 @@ describe("duplex", function () {
obj = { firstName:"Albert", lastName:"Einstein",
phoneNumbers:[ {number:"12345"}, {number:"45353"} ]};

jsonpatch.intervals = [50];
jsonpatch.observe(obj, function(patches) {
called++;
lastPatches = patches;
});
obj.phoneNumbers[0].number = "123";

triggerKeyup();

waitsFor(function(){
return called > 0;
}, 100, 'phoneNumbers[0].number change to 123');
Expand All @@ -532,6 +542,7 @@ describe("duplex", function () {
expect(lastPatches).toEqual([{op: 'replace', path: '/phoneNumbers/0/number', value: '123'}]);

obj.phoneNumbers[1].number = "456";
triggerKeyup();
});

waitsFor(function(){
Expand All @@ -555,7 +566,6 @@ describe("duplex", function () {
obj = { firstName:"Albert", lastName:"Einstein",
phoneNumbers:[ {number:"12345"}, {number:"45353"} ]};

jsonpatch.intervals = [10];
var observer = jsonpatch.observe(obj, function(patches) {
called++;
lastPatches = patches;
Expand All @@ -582,13 +592,14 @@ describe("duplex", function () {
obj = { firstName:"Albert", lastName:"Einstein",
phoneNumbers:[ {number:"12345"}, {number:"45353"} ]};

jsonpatch.intervals = [10];
var observer = jsonpatch.observe(obj, function(patches) {
called++;
});

obj.firstName = 'Malvin';

triggerKeyup();

waits(20);

runs(function(){
Expand All @@ -597,6 +608,8 @@ describe("duplex", function () {
jsonpatch.unobserve(obj, observer);

obj.firstName = 'Wilfred';

triggerKeyup();
});

waits(20);
Expand All @@ -609,6 +622,7 @@ describe("duplex", function () {
});

obj.firstName = 'Megan';
triggerKeyup();
});

waits(20);
Expand All @@ -622,15 +636,17 @@ describe("duplex", function () {
var called = 0;

obj = { firstName:"Albert", lastName:"Einstein",
phoneNumbers:[ {number:"12345"}, {number:"45353"} ]};
phoneNumbers: [{ number: "12345" }, { number: "45353" }]
};

jsonpatch.intervals = [10];
var observer = jsonpatch.observe(obj, function(patches) {
called++;
});

obj.phoneNumbers[1].number = '555';

triggerKeyup();

waits(20);

runs(function(){
Expand All @@ -639,6 +655,8 @@ describe("duplex", function () {
jsonpatch.unobserve(obj, observer);

obj.phoneNumbers[1].number = '556';

triggerKeyup();
});

waits(20);
Expand All @@ -651,6 +669,8 @@ describe("duplex", function () {
});

obj.phoneNumbers[1].number = '557';

triggerKeyup();
});

waits(20);
Expand All @@ -669,7 +689,6 @@ describe("duplex", function () {
{number: "45353"}
]};

jsonpatch.intervals = [10];
var observer = jsonpatch.observe(obj, function (patches) {
lastPatches = patches;
});
Expand Down Expand Up @@ -710,6 +729,8 @@ describe("duplex", function () {

obj.foo = 'bazz';

triggerKeyup();

waitsFor(function () {
return callback.calls.length > 0;
}, 'callback calls', 1000);
Expand All @@ -720,6 +741,8 @@ describe("duplex", function () {
callback.reset();

obj.foo = 'bazinga';

triggerKeyup();
});

waitsFor(function () {
Expand All @@ -745,7 +768,7 @@ describe("duplex", function () {
expect(lastPatches).toEqual([
{ op: 'replace', path: '/lastName', value: 'Hawking' }
]);
}, 0);
}, 100);
});

});
Expand Down Expand Up @@ -815,6 +838,8 @@ describe("duplex", function () {

obj.foo = 'bazz';

triggerKeyup()

waitsFor(function () {
return callback.calls.length > 0;
}, 'callback call', 1000);
Expand Down Expand Up @@ -863,13 +888,14 @@ describe("duplex", function () {
obj = { firstName:"Albert", lastName:"Einstein",
phoneNumbers:[ {number:"12345"}, {number:"45353"} ]};

jsonpatch.intervals = [10];
var observer = jsonpatch.observe(obj, function(patches) {
called++;
});

obj.firstName = 'Malvin';

triggerKeyup();

waits(20);

runs(function(){
Expand Down

0 comments on commit 40ff8fb

Please sign in to comment.