From 806c0ec2d469dbb8379f6cc04f6aea307b9a8a6c Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 10 Oct 2017 01:08:04 +0800 Subject: [PATCH 1/2] event listener should not add more than once when using once --- cocos2d/ToMerge/qunit/test-event-system.js | 37 --------------- cocos2d/core/event/event-target.js | 38 +++++++++------- test/qunit/unit-es5/test-event-system.js | 52 ++++++++++++++++++++++ 3 files changed, 74 insertions(+), 53 deletions(-) create mode 100644 test/qunit/unit-es5/test-event-system.js diff --git a/cocos2d/ToMerge/qunit/test-event-system.js b/cocos2d/ToMerge/qunit/test-event-system.js index aa1dd99a41..d81bbe4fe0 100644 --- a/cocos2d/ToMerge/qunit/test-event-system.js +++ b/cocos2d/ToMerge/qunit/test-event-system.js @@ -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'); diff --git a/cocos2d/core/event/event-target.js b/cocos2d/core/event/event-target.js index e3b742882c..9475583110 100644 --- a/cocos2d/core/event/event-target.js +++ b/cocos2d/core/event/event-target.js @@ -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); + } }; /** diff --git a/test/qunit/unit-es5/test-event-system.js b/test/qunit/unit-es5/test-event-system.js new file mode 100644 index 0000000000..af13041ef2 --- /dev/null +++ b/test/qunit/unit-es5/test-event-system.js @@ -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'); +}); From 5dea89a6272de07410d9d014968965a15df054da Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 10 Oct 2017 09:40:33 +0800 Subject: [PATCH 2/2] small optimization --- cocos2d/core/load-pipeline/binary-downloader.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cocos2d/core/load-pipeline/binary-downloader.js b/cocos2d/core/load-pipeline/binary-downloader.js index 3272675ebf..287e434528 100644 --- a/cocos2d/core/load-pipeline/binary-downloader.js +++ b/cocos2d/core/load-pipeline/binary-downloader.js @@ -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);