Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event handling improvements #229

Merged
merged 2 commits into from Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 10 additions & 26 deletions src/duplex.ts
Expand Up @@ -96,20 +96,11 @@ export function observe<T>(obj: Object|Array<T>, 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
(<any>document.documentElement).attachEvent('onmouseup', fastCheck);
(<any>document.documentElement).attachEvent('onkeyup', fastCheck);
(<any>document.documentElement).attachEvent('onmousedown', fastCheck);
(<any>document.documentElement).attachEvent('onkeydown', fastCheck);
(<any>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;
Expand All @@ -121,18 +112,11 @@ export function observe<T>(obj: Object|Array<T>, 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 {
(<any>document.documentElement).detachEvent('onmouseup', fastCheck);
(<any>document.documentElement).detachEvent('onkeyup', fastCheck);
(<any>document.documentElement).detachEvent('onmousedown', fastCheck);
(<any>document.documentElement).detachEvent('onkeydown', fastCheck);
}
window.removeEventListener('mouseup', fastCheck);
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
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