Skip to content

Commit

Permalink
fix removal of "change" event listener
Browse files Browse the repository at this point in the history
because this was a memory leak at least.
also, added tests for removal of any event listeners
  • Loading branch information
warpech committed Jul 2, 2019
1 parent 0250c05 commit 43e72dd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/duplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export function observe<T>(obj: Object|Array<T>, callback?: (patches: Operation[
window.removeEventListener('keyup', fastCheck);
window.removeEventListener('mousedown', fastCheck);
window.removeEventListener('keydown', fastCheck);
window.removeEventListener('change', fastCheck);
}
};

Expand Down
92 changes: 83 additions & 9 deletions test/spec/duplexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,12 +1330,14 @@ describe('duplex', function() {
}
});

it('should generate patch after `mouseup` event', function(done) {
it('should generate patch after `mouseup` event while observing', function(done) {
obj = {
lastName: 'Einstein'
};
var lastPatches;
var callCount = 0;
var observer = jsonpatch.observe(obj, function(patches) {
callCount++;
lastPatches = patches;
});

Expand All @@ -1351,16 +1353,26 @@ describe('duplex', function() {
value: 'Hawking'
}
]);
done();
});

observer.unobserve();
obj.lastName = 'Musk';
trigger('mouseup');

setTimeout(function() {
expect(callCount).toEqual(1);
done();
}, 20);
}, 20);
});

it('should generate patch after `mousedown` event', function(done) {
it('should generate patch after `mousedown` event while observing', function(done) {
obj = {
lastName: 'Einstein'
};
var lastPatches;
var callCount = 0;
var observer = jsonpatch.observe(obj, function(patches) {
callCount++;
lastPatches = patches;
});

Expand All @@ -1375,16 +1387,60 @@ describe('duplex', function() {
value: 'Hawking'
}
]);
done();

observer.unobserve();
obj.lastName = 'Musk';
trigger('mousedown');

setTimeout(function() {
expect(callCount).toEqual(1);
done();
}, 20);
}, 20);
});

it('should generate patch after `keydown` event', function(done) {
it('should generate patch after `keyup` event while observing', function(done) {
obj = {
lastName: 'Einstein'
};
var lastPatches;
var callCount = 0;
var observer = jsonpatch.observe(obj, function(patches) {
callCount++;
lastPatches = patches;
});

obj.lastName = 'Hawking';
trigger('keyup');

setTimeout(function() {
expect(lastPatches).toEqual([
{
op: 'replace',
path: '/lastName',
value: 'Hawking'
}
]);

observer.unobserve();
obj.lastName = 'Musk';
trigger('keyup');

setTimeout(function() {
expect(callCount).toEqual(1);
done();
}, 20);
}, 20);
});

it('should generate patch after `keydown` event while observing', function(done) {
obj = {
lastName: 'Einstein'
};
var lastPatches;
var callCount = 0;
var observer = jsonpatch.observe(obj, function(patches) {
callCount++;
lastPatches = patches;
});

Expand All @@ -1399,16 +1455,26 @@ describe('duplex', function() {
value: 'Hawking'
}
]);
done();

observer.unobserve();
obj.lastName = 'Musk';
trigger('keydown');

setTimeout(function() {
expect(callCount).toEqual(1);
done();
}, 20);
}, 20);
});

it('should generate patch after `change` event', function(done) {
it('should generate patch after `change` event while observing', function(done) {
obj = {
lastName: 'Einstein'
};
var lastPatches;
var callCount = 0;
var observer = jsonpatch.observe(obj, function(patches) {
callCount++;
lastPatches = patches;
});

Expand All @@ -1423,7 +1489,15 @@ describe('duplex', function() {
value: 'Hawking'
}
]);
done();

observer.unobserve();
obj.lastName = 'Musk';
trigger('change');

setTimeout(function() {
expect(callCount).toEqual(1);
done();
}, 20);
}, 20);
});
});
Expand Down

0 comments on commit 43e72dd

Please sign in to comment.