Merge pull request #2031 from jareguo/master

event listener should not add more than once when using once
This commit is contained in:
Jare Guo
2017-10-09 21:08:13 -05:00
committed by GitHub
4 changed files with 75 additions and 55 deletions
@@ -48,43 +48,6 @@ test('emit', function () {
cb1.once('callback1 should be invoked by fire event');
});
test('once', function () {
var target = new cc.EventTarget();
var fireEvent = new cc.Event('fire');
var cb1 = new Callback();
// once
target.once('fire', cb1.enable());
target.dispatchEvent(fireEvent);
cb1.once('should be invoked if registered by once')
.disable('should only invoke once 1');
target.dispatchEvent(fireEvent);
// once + on
var cb2 = new Callback().enable();
target.once('fire', cb1.enable());
target.on('fire', cb2);
target.dispatchEvent(fireEvent);
cb1.once('should be invoked if registered by once and before other callback');
cb2.once('should still be invoked if previous event was removed');
cb1.disable('should only invoke once 2');
target.dispatchEvent(fireEvent);
cb2.once('should not remove common event');
// on + once
target.once('fire', cb1.enable());
target.dispatchEvent(fireEvent);
cb2.once();
cb1.once('should be invoked if registered by once and after other callback')
.disable('should only invoke once 3');
target.dispatchEvent(fireEvent);
cb2.once();
});
test('test useCapture in on/off', function () {
var target = new cc.EventTarget();
var event = new cc.Event('fire');
+22 -16
View File
@@ -162,7 +162,7 @@ proto._addEventFlag = function (type, listeners, useCapture) {
cache[type] |= flag;
};
proto._removeEventFlag = function (type, listeners, useCapture) {
proto._purgeEventFlag = function (type, listeners, useCapture) {
var cache = this._hasListenerCache;
if (!cache || listeners.has(type)) {
@@ -175,7 +175,7 @@ proto._removeEventFlag = function (type, listeners, useCapture) {
if (cache[type] === 0) {
delete cache[type];
}
}
};
proto._resetFlagForTarget = function (target, listeners, useCapture) {
var cache = this._hasListenerCache;
@@ -206,13 +206,8 @@ proto.hasEventListener = function (type, checkCapture) {
var cache = this._hasListenerCache;
if (!cache) return false;
var flag = cache[type];
if (checkCapture && (flag & CAPTURING_FLAG))
return true;
if (!checkCapture && (flag & BUBBLING_FLAG))
return true;
return false;
var flag = checkCapture ? CAPTURING_FLAG : BUBBLING_FLAG;
return (cache[type] & flag) > 0;
};
@@ -322,7 +317,7 @@ proto.off = function (type, callback, target, useCapture) {
fastRemove(target.__eventTargets, this);
}
this._removeEventFlag(type, listeners, useCapture);
this._purgeEventFlag(type, listeners, useCapture);
}
}
@@ -376,12 +371,23 @@ proto.targetOff = function (target) {
* }, node);
*/
proto.once = function (type, callback, target, useCapture) {
var self = this;
var cb = function (event) {
self.off(type, cb, target, useCapture);
callback.call(this, event);
};
this.on(type, cb, target, useCapture);
var eventType_hasOnceListener = '__ONCE_FLAG:' + type;
var listeners = useCapture ? this._capturingListeners : this._bubblingListeners;
var hasOnceListener = listeners && listeners.has(eventType_hasOnceListener, callback, target);
if (!hasOnceListener) {
var self = this;
var onceWrapper = function (event) {
self.off(type, onceWrapper, target, useCapture);
listeners.remove(eventType_hasOnceListener, callback, target);
callback.call(this, event);
};
this.on(type, onceWrapper, target, useCapture);
if (!listeners) {
// obtain new created listeners
listeners = useCapture ? this._capturingListeners : this._bubblingListeners;
}
listeners.add(eventType_hasOnceListener, callback, target);
}
};
/**
@@ -71,8 +71,7 @@ if (_IEFilter) {
var byteMapping = {};
for (var i = 0; i < 256; i++) {
for (var j = 0; j < 256; j++) {
byteMapping[ String.fromCharCode(i + j * 256) ] =
String.fromCharCode(i) + String.fromCharCode(j);
byteMapping[ String.fromCharCode(i + j * 256) ] = String.fromCharCode(i, j);
}
}
var rawBytes = IEBinaryToArray_ByteStr(binary);
+52
View File
@@ -0,0 +1,52 @@
module('EventTarget');
test('once', function () {
var target = new cc.EventTarget();
var fireEvent = new cc.Event('fire');
var cb1 = new Callback();
// once
target.once('fire', cb1.enable());
target.dispatchEvent(fireEvent);
cb1.once('should be invoked if registered by once')
.disable('should only invoke once 1');
target.dispatchEvent(fireEvent);
// once + on
var cb2 = new Callback().enable();
target.once('fire', cb1.enable());
target.on('fire', cb2);
target.dispatchEvent(fireEvent);
cb1.once('should be invoked if registered by once and before other callback');
cb2.once('should still be invoked if previous event was removed');
cb1.disable('should only invoke once 2');
target.dispatchEvent(fireEvent);
cb2.once('should not remove common event');
// on + once
target.once('fire', cb1.enable());
target.dispatchEvent(fireEvent);
cb2.once();
cb1.once('should be invoked if registered by once and after other callback')
.disable('should only invoke once 3');
target.dispatchEvent(fireEvent);
cb2.once();
});
test('call "once" twice', function () {
var target = new cc.EventTarget();
var cb1 = new Callback().enable();
var ctx = {};
target.once('fire', cb1, ctx, true);
target.once('fire', cb1, ctx, true);
target.emit('fire');
cb1.once('should be invoked only once')
.disable('should only invoke once');
target.emit('fire');
});