diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 1ee9f7c159..3f706b3ee1 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -346,11 +346,11 @@ var Node = cc.Class({ */ _addComponentAt: CC_EDITOR && function (comp, index) { if (this._objFlags & Destroying) { - return cc.error('isDestroying');; + return cc.error('isDestroying'); + } + if ( !(comp instanceof cc.Component) ) { + return cc.error("_addComponentAt: The component to add must be a constructor"); } - // if (typeof comp !== 'function') { - // return cc.error("_addComponentAt: The component to add must be a constructor"); - // } if (index > this._components.length) { return cc.error("_addComponentAt: Index out of range"); } diff --git a/cocos2d/core/event/event-listeners.js b/cocos2d/core/event/event-listeners.js index fa33d92d20..399e247b69 100644 --- a/cocos2d/core/event/event-listeners.js +++ b/cocos2d/core/event/event-listeners.js @@ -8,46 +8,56 @@ function EventListeners () { JS.extend(EventListeners, CallbacksHandler); EventListeners.prototype.invoke = function (event) { - var list = this._callbackTable[event.type], - i, endIndex, offset, valid, + var list = this._callbackTable[event.type], + i, endIndex, expectedLastFunc, callingFunc, target, hasTarget; - if (list && list.length > 0) { + if (list) { if (list.length === 1) { list[0].call(event.currentTarget, event); return; } endIndex = list.length - 1; + expectedLastFunc = list[endIndex]; for (i = 0; i <= endIndex;) { callingFunc = list[i]; target = list[i+1]; - if (target && typeof target === 'object') { - hasTarget = true; + hasTarget = target && typeof target === 'object'; + var increment; + if (hasTarget) { callingFunc.call(target, event); + increment = 2; } else { - hasTarget = false; callingFunc.call(event.currentTarget, event); + increment = 1; } + if (event._propagationImmediateStopped || i === endIndex) { break; } // 为了不每次触发消息时都创建一份回调数组的拷贝,这里需要对消息的反注册做检查和限制 // check last one to see if any one removed - offset = endIndex - (list.length-1); - valid = true; - if (offset <= 2) { // 只删一个item - if (list[i] !== callingFunc) { // 如果删了后面的回调,索引不变 - i -= offset; + if (list[endIndex] !== expectedLastFunc) { // 如果变短 + var removedCount = 1; + var secToLast = list[endIndex - removedCount]; + if (secToLast && typeof secToLast === 'object') { + // is target + removedCount = 2; + secToLast = list[endIndex - removedCount]; + } + if (secToLast === expectedLastFunc) { // 只支持删一个 + endIndex -= removedCount; + if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 + continue; + } + } + else { + // 只允许在一个回调里面移除一个回调。如果要移除很多,只能用 event.stopPropagationImmediate() + return cc.error('Call event.stopPropagationImmediate() when you remove more than one callbacks in a event callback.'); } - endIndex -= offset; } - else if (offset !== 0) { - // 只允许在一个回调里面移除一个回调。如果要移除很多,只能用 event.stopPropagationImmediate() - cc.error('Call event.stopPropagationImmediate() when you remove more than one callbacks in a event callback.'); - return; - } - // Increment i - hasTarget ? i += 2 : ++i; + + i += increment; } } }; diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index 6bfa11c310..335b809745 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -165,18 +165,47 @@ if (CC_TEST) { * @param {any} [p5] */ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { - var list = this._callbackTable[key], i, l, target; + var list = this._callbackTable[key]; if (list) { - for (i = 0, l = list.length; i < l;) { - target = list[i+1]; - if (target && typeof target === 'object') { + var endIndex = list.length - 1; + var expectedLastFunc = list[endIndex]; + for (var i = 0; i <= endIndex;) { + var callingFunc = list[i]; + var target = list[i + 1]; + var hasTarget = target && typeof target === 'object'; + var increment; + if (hasTarget) { list[i].call(target, p1, p2, p3, p4, p5); - i += 2; + increment = 2; } else { - list[i](p1, p2, p3, p4, p5); - ++i; + callingFunc(p1, p2, p3, p4, p5); + increment = 1; } + + // 为了不每次触发消息时都创建一份回调数组的拷贝,这里需要对消息的反注册做检查和限制 + // check last one to see if any one removed + if (list[endIndex] !== expectedLastFunc && i !== endIndex) { // 如果变短 + var removedCount = 1; + var secToLast = list[endIndex - removedCount]; + if (secToLast && typeof secToLast === 'object') { + // is target + removedCount = 2; + secToLast = list[endIndex - removedCount]; + } + if (secToLast === expectedLastFunc) { // 只支持删一个 + endIndex -= removedCount; + if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 + continue; + } + } + else { + // 只允许在一个回调里面移除一个回调。如果要移除很多,只能用 event.stopPropagationImmediate() + return cc.error('Call event.stopPropagationImmediate() when you remove more than one callbacks in a event callback.'); + } + } + + i += increment; } } }; diff --git a/test/qunit/unit/test-callbacks-invoker.js b/test/qunit/unit/test-callbacks-invoker.js index ea095a07a9..431455d972 100644 --- a/test/qunit/unit/test-callbacks-invoker.js +++ b/test/qunit/unit/test-callbacks-invoker.js @@ -1,4 +1,6 @@ -test('CallbacksInvoker', function () { +module('CallbacksInvoker'); + +test('test', function () { var ci = new cc._Test.CallbacksInvoker(); var cb1 = new Callback(); @@ -41,6 +43,52 @@ test('CallbacksInvoker', function () { ci.invoke('a'); }); +test('remove self during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback(function () { + ci.remove('eve', cb1); + }).enable(); + var cb2 = new Callback().enable(); + + ci.add('eve', cb1); + ci.add('eve', cb2); + ci.invoke('eve'); + cb2.once('it should be called correctly if previous callback deregistered itself'); + + cb1.disable('should not call after removed'); + ci.invoke('eve'); +}); + +test('remove previous during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback().enable(); + var cb2 = new Callback(function () { + ci.remove('eve', cb1); + }).enable(); + + ci.add('eve', cb1); + ci.add('eve', cb2); + ci.invoke('eve'); + + strictEqual(ci.has('eve', cb1), false, 'previous callback should be removed'); + strictEqual(ci.has('eve', cb2), true, 'self callback should not be removed'); +}); + +test('remove last during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback().enable(); + var cb2 = new Callback(function () { + ci.remove('eve', cb2); + }).enable(); + + ci.add('eve', cb1); + ci.add('eve', cb2); + ci.invoke('eve'); + + strictEqual(ci.has('eve', cb1), true, 'previous callback should not be removed'); + strictEqual(ci.has('eve', cb2), false, 'self callback should be removed'); +}); + test('CallbacksInvoker support target', function () { var ci = new cc._Test.CallbacksInvoker(); var cb1 = function () { diff --git a/test/qunit/unit/test-spriteRenderer.js b/test/qunit/unit/test-spriteRenderer.js index 7b4e5da381..96da914761 100644 --- a/test/qunit/unit/test-spriteRenderer.js +++ b/test/qunit/unit/test-spriteRenderer.js @@ -11,18 +11,11 @@ asyncTest('basic test', function () { deepEqual(render._sgNode.color, Color.RED, 'color set success'); - var texture = new cc.Texture2D(); - texture.url = url; - cc.loader.load(texture.url, function (err) { - texture.handleLoadedTexture(cc.path.extname(url) === '.png'); - }); - cc.textureCache.cacheImage(url, texture); - var newSprite = new cc.SpriteFrame(); - newSprite.setTexture(texture); + newSprite.initWithTexture(url, cc.rect(0, 0, 10, 10)); render.sprite = newSprite; - - render.sprite.on('load', function () { + var tex = newSprite.getTexture(); + tex.once('load', function () { strictEqual(render._sgNode._scale9Image.texture.url, url, 'texture set success'); start(); });