From 0250c0538f41283ef9b5713a67eb04ea19eaca0c Mon Sep 17 00:00:00 2001 From: Marcin Warpechowski Date: Tue, 2 Jul 2019 12:25:22 +0200 Subject: [PATCH 1/2] remove remaining traces of support for IE8 because IE<11 is not supported since https://github.com/Starcounter-Jack/JSON-Patch/releases/tag/v2.0.7 --- src/duplex.ts | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/src/duplex.ts b/src/duplex.ts index faefea24..97474409 100644 --- a/src/duplex.ts +++ b/src/duplex.ts @@ -96,20 +96,11 @@ export function observe(obj: Object|Array, callback?: (patches: Operation[ observer.next = setTimeout(dirtyCheck); }; if (typeof window !== 'undefined') { //not Node - if (window.addEventListener) { //standards - window.addEventListener('mouseup', fastCheck); - window.addEventListener('keyup', fastCheck); - window.addEventListener('mousedown', fastCheck); - window.addEventListener('keydown', fastCheck); - window.addEventListener('change', fastCheck); - } - else { //IE8 - (document.documentElement).attachEvent('onmouseup', fastCheck); - (document.documentElement).attachEvent('onkeyup', fastCheck); - (document.documentElement).attachEvent('onmousedown', fastCheck); - (document.documentElement).attachEvent('onkeydown', fastCheck); - (document.documentElement).attachEvent('onchange', fastCheck); - } + window.addEventListener('mouseup', fastCheck); + window.addEventListener('keyup', fastCheck); + window.addEventListener('mousedown', fastCheck); + window.addEventListener('keydown', fastCheck); + window.addEventListener('change', fastCheck); } } observer.patches = patches; @@ -121,18 +112,10 @@ export function observe(obj: Object|Array, callback?: (patches: Operation[ removeObserverFromMirror(mirror, observer); if (typeof window !== 'undefined') { - if (window.removeEventListener) { - window.removeEventListener('mouseup', fastCheck); - window.removeEventListener('keyup', fastCheck); - window.removeEventListener('mousedown', fastCheck); - window.removeEventListener('keydown', fastCheck); - } - else { - (document.documentElement).detachEvent('onmouseup', fastCheck); - (document.documentElement).detachEvent('onkeyup', fastCheck); - (document.documentElement).detachEvent('onmousedown', fastCheck); - (document.documentElement).detachEvent('onkeydown', fastCheck); - } + window.removeEventListener('mouseup', fastCheck); + window.removeEventListener('keyup', fastCheck); + window.removeEventListener('mousedown', fastCheck); + window.removeEventListener('keydown', fastCheck); } }; From 43e72dd8119419c87e94e42e6c1733686a45363a Mon Sep 17 00:00:00 2001 From: Marcin Warpechowski Date: Tue, 2 Jul 2019 12:43:36 +0200 Subject: [PATCH 2/2] fix removal of "change" event listener because this was a memory leak at least. also, added tests for removal of any event listeners --- src/duplex.ts | 1 + test/spec/duplexSpec.js | 92 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/duplex.ts b/src/duplex.ts index 97474409..e879bc11 100644 --- a/src/duplex.ts +++ b/src/duplex.ts @@ -116,6 +116,7 @@ export function observe(obj: Object|Array, callback?: (patches: Operation[ window.removeEventListener('keyup', fastCheck); window.removeEventListener('mousedown', fastCheck); window.removeEventListener('keydown', fastCheck); + window.removeEventListener('change', fastCheck); } }; diff --git a/test/spec/duplexSpec.js b/test/spec/duplexSpec.js index 19f52b1f..27c530aa 100644 --- a/test/spec/duplexSpec.js +++ b/test/spec/duplexSpec.js @@ -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; }); @@ -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; }); @@ -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; }); @@ -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; }); @@ -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); }); });