From da659752e27e8cf334f12273ffd9df33a80adee6 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 8 Mar 2017 01:52:22 +0800 Subject: [PATCH] minimize code --- cocos2d/core/component-scheduler.js | 155 ++++++++++------------------ 1 file changed, 53 insertions(+), 102 deletions(-) diff --git a/cocos2d/core/component-scheduler.js b/cocos2d/core/component-scheduler.js index 32d24d279e..44221f90a2 100644 --- a/cocos2d/core/component-scheduler.js +++ b/cocos2d/core/component-scheduler.js @@ -86,11 +86,12 @@ function sortedIndex (array, comp) { var LifeCycleInvoker = cc.Class({ __ctor__ (invokeFunc) { // components which priority === 0 (default) - this.compsZero = new JsArray.MutableForwardIterator([]); + var Iterator = JsArray.MutableForwardIterator; + this._zero = new Iterator([]); // components which priority > 0 - this.compsPos = new JsArray.MutableForwardIterator([]); + this._pos = new Iterator([]); // components which priority < 0 - this.compsNeg = new JsArray.MutableForwardIterator([]); + this._neg = new Iterator([]); this._invoke = invokeFunc; }, @@ -122,45 +123,29 @@ var OneOffInvoker = cc.Class({ extends: LifeCycleInvoker, add (comp) { var order = comp.constructor._executionOrder; - if (order === 0) { - this.compsZero.array.push(comp); - } - else if (order < 0) { - this.compsNeg.array.push(comp); - } - else { - this.compsPos.array.push(comp); - } + (order === 0 ? this._zero : (order < 0 ? this._neg : this._pos)).array.push(comp); }, remove (comp) { var order = comp.constructor._executionOrder; - if (order === 0) { - this.compsZero.fastRemove(comp); - } - else if (order < 0) { - this.compsNeg.fastRemove(comp); - } - else { - this.compsPos.fastRemove(comp); - } + (order === 0 ? this._zero : (order < 0 ? this._neg : this._pos)).fastRemove(comp); }, cancelInactive () { - stableRemoveInactive(this.compsZero); - stableRemoveInactive(this.compsNeg); - stableRemoveInactive(this.compsPos); + stableRemoveInactive(this._zero); + stableRemoveInactive(this._neg); + stableRemoveInactive(this._pos); }, invoke () { - var compsNeg = this.compsNeg; + var compsNeg = this._neg; if (compsNeg.array.length > 0) { this.sort(compsNeg); this._invoke(compsNeg); compsNeg.array.length = 0; } - this._invoke(this.compsZero); - this.compsZero.array.length = 0; + this._invoke(this._zero); + this._zero.array.length = 0; - var compsPos = this.compsPos; + var compsPos = this._pos; if (compsPos.array.length > 0) { this.sort(compsPos); this._invoke(compsPos); @@ -175,10 +160,10 @@ var ReusableInvoker = cc.Class({ add (comp) { var order = comp.constructor._executionOrder; if (order === 0) { - this.compsZero.array.push(comp); + this._zero.array.push(comp); } else { - var array = order < 0 ? this.compsNeg.array : this.compsPos.array; + var array = order < 0 ? this._neg.array : this._pos.array; var i = sortedIndex(array, comp); if (i < 0) { array.splice(~i, 0, comp); @@ -191,10 +176,10 @@ var ReusableInvoker = cc.Class({ remove (comp) { var order = comp.constructor._executionOrder; if (order === 0) { - this.compsZero.fastRemove(comp); + this._zero.fastRemove(comp); } else { - var iterator = order < 0 ? this.compsNeg : this.compsPos; + var iterator = order < 0 ? this._neg : this._pos; var i = sortedIndex(iterator.array, comp); if (i >= 0) { iterator.removeAt(i); @@ -202,14 +187,14 @@ var ReusableInvoker = cc.Class({ } }, invoke (dt) { - if (this.compsNeg.array.length > 0) { - this._invoke(this.compsNeg, dt); + if (this._neg.array.length > 0) { + this._invoke(this._neg, dt); } - this._invoke(this.compsZero, dt); + this._invoke(this._zero, dt); - if (this.compsPos.array.length > 0) { - this._invoke(this.compsPos, dt); + if (this._pos.array.length > 0) { + this._invoke(this._pos, dt); } }, }); @@ -218,17 +203,17 @@ var ReusableInvoker = cc.Class({ var UnsortedInvoker = cc.Class({ extends: LifeCycleInvoker, add (comp) { - this.compsZero.array.push(comp); + this._zero.array.push(comp); }, remove (comp) { - this.compsZero.fastRemove(comp); + this._zero.fastRemove(comp); }, cancelInactive () { - stableRemoveInactive(this.compsZero); + stableRemoveInactive(this._zero); }, invoke () { - this._invoke(this.compsZero); - this.compsZero.array.length = 0; + this._invoke(this._zero); + this._zero.array.length = 0; }, }); @@ -239,38 +224,32 @@ function enableInEditor (comp) { } } -// function getInvokeImpl (code, param) { -// // function (it) { -// // var a = it.array; -// // for (it.i = 0; it.i < a.length; ++it.i) { -// // let comp = a[it.i]; -// // // ... -// // } -// // } -// return Misc.cleanEval_1P('(function(it){' + -// 'var a=it.array;' + -// 'for(it.i=0;it.i