Merge pull request #9889 from ahocevar/event-performance
Event performance improvements
This commit is contained in:
@@ -270,9 +270,11 @@ class MapBrowserEventHandler extends EventTarget {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
relayEvent_(pointerEvent) {
|
relayEvent_(pointerEvent) {
|
||||||
const dragging = !!(this.down_ && this.isMoving_(pointerEvent));
|
if (this.map_.hasListener(pointerEvent.type)) {
|
||||||
this.dispatchEvent(new MapBrowserPointerEvent(
|
const dragging = !!(this.down_ && this.isMoving_(pointerEvent));
|
||||||
pointerEvent.type, this.map_, pointerEvent, dragging));
|
this.dispatchEvent(new MapBrowserPointerEvent(
|
||||||
|
pointerEvent.type, this.map_, pointerEvent, dragging));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+13
-8
@@ -32,14 +32,19 @@ import {clear} from './obj.js';
|
|||||||
* @return {ListenerFunction} Bound listener.
|
* @return {ListenerFunction} Bound listener.
|
||||||
*/
|
*/
|
||||||
export function bindListener(listenerObj) {
|
export function bindListener(listenerObj) {
|
||||||
const boundListener = function(evt) {
|
let boundListener;
|
||||||
const listener = listenerObj.listener;
|
if (listenerObj.bindTo || listenerObj.callOnce) {
|
||||||
const bindTo = listenerObj.bindTo || listenerObj.target;
|
boundListener = function(evt) {
|
||||||
if (listenerObj.callOnce) {
|
const listener = listenerObj.listener;
|
||||||
unlistenByKey(listenerObj);
|
const bindTo = listenerObj.bindTo || listenerObj.target;
|
||||||
}
|
if (listenerObj.callOnce) {
|
||||||
return listener.call(bindTo, evt);
|
unlistenByKey(listenerObj);
|
||||||
};
|
}
|
||||||
|
return listener.call(bindTo, evt);
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
boundListener = listenerObj.listener;
|
||||||
|
}
|
||||||
listenerObj.boundListener = boundListener;
|
listenerObj.boundListener = boundListener;
|
||||||
return boundListener;
|
return boundListener;
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-12
@@ -24,15 +24,14 @@ describe('ol.events', function() {
|
|||||||
boundListener();
|
boundListener();
|
||||||
expect(listenerObj.listener.thisValues[0]).to.equal(listenerObj.bindTo);
|
expect(listenerObj.listener.thisValues[0]).to.equal(listenerObj.bindTo);
|
||||||
});
|
});
|
||||||
it('binds to the target when bindTo is not provided', function() {
|
it('binds to the target when bindTo is not provided', function(done) {
|
||||||
const listenerObj = {
|
const target = new EventTarget();
|
||||||
listener: sinon.spy(),
|
const listenerObj = listen(target, 'foo', function() {
|
||||||
target: {id: 1}
|
expect(this).to.equal(target);
|
||||||
};
|
done();
|
||||||
const boundListener = bindListener(listenerObj);
|
});
|
||||||
expect(listenerObj.boundListener).to.equal(boundListener);
|
expect(listenerObj.boundListener).to.equal(listenerObj.listener);
|
||||||
boundListener();
|
target.dispatchEvent('foo');
|
||||||
expect(listenerObj.listener.thisValues[0]).to.equal(listenerObj.target);
|
|
||||||
});
|
});
|
||||||
it('binds a self-unregistering listener when callOnce is true', function() {
|
it('binds a self-unregistering listener when callOnce is true', function() {
|
||||||
const bindTo = {id: 1};
|
const bindTo = {id: 1};
|
||||||
@@ -122,13 +121,14 @@ describe('ol.events', function() {
|
|||||||
|
|
||||||
describe('listenOnce()', function() {
|
describe('listenOnce()', function() {
|
||||||
it('creates a one-off listener', function() {
|
it('creates a one-off listener', function() {
|
||||||
|
const target = new EventTarget();
|
||||||
const listener = sinon.spy();
|
const listener = sinon.spy();
|
||||||
const key = listenOnce(target, 'foo', listener);
|
const key = listenOnce(target, 'foo', listener);
|
||||||
expect(add.callCount).to.be(1);
|
|
||||||
expect(key.callOnce).to.be(true);
|
expect(key.callOnce).to.be(true);
|
||||||
key.boundListener();
|
target.dispatchEvent('foo');
|
||||||
|
expect(listener.callCount).to.be(1);
|
||||||
|
target.dispatchEvent('foo');
|
||||||
expect(listener.callCount).to.be(1);
|
expect(listener.callCount).to.be(1);
|
||||||
expect(remove.callCount).to.be(1);
|
|
||||||
});
|
});
|
||||||
it('does not add the same listener twice', function() {
|
it('does not add the same listener twice', function() {
|
||||||
const listener = function() {};
|
const listener = function() {};
|
||||||
|
|||||||
Reference in New Issue
Block a user