From c37fc094d9f4d6953e9e550beae01c16b179903e Mon Sep 17 00:00:00 2001 From: switer Date: Tue, 2 Dec 2014 15:55:10 +0800 Subject: [PATCH] buid v0.1.2 --- dist/chain.browser.js | 47 +++++++++++++++++++++++++++++++++---------- dist/chain.cmd.js | 47 +++++++++++++++++++++++++++++++++---------- 2 files changed, 72 insertions(+), 22 deletions(-) diff --git a/dist/chain.browser.js b/dist/chain.browser.js index c646cda..9ab6fdc 100644 --- a/dist/chain.browser.js +++ b/dist/chain.browser.js @@ -102,28 +102,37 @@ function pushNode( /*handler1, handler2, ..., handlerN*/ ) { return node } +function noop () {} +function setAlltoNoop (obj, methods) { + utils.each(methods, function (method) { + obj[method] = noop + }) +} + utils.merge(Chain.prototype, { /** * Define a chain node **/ then: function() { + if (this._destroy) return pushNode.apply(this, arguments) return this }, some: function() { + if (this._destroy) return var node = pushNode.apply(this, arguments) if (node.items.length) node.type = 'some' return this }, /** - * @RuntimeMethod can be call in runtime + * @RuntimeMethod only be called in runtime * Check current node states and execulate nextNode **/ next: function() { + if (this._end || this._destroy) return var that = this var args = utils.slice(arguments) var node - if (this._end) return if (this.__id) { node = this._nodes.get(this.__id) @@ -160,6 +169,8 @@ utils.merge(Chain.prototype, { utils.merge(chainDummy, that) chainDummy.__id = node.id chainDummy.__index = index + chainDummy.__callee = item + chainDummy.__arguments = xArgs chainDummy.__proto__ = that.__proto__ xArgs.unshift(chainDummy) @@ -167,11 +178,20 @@ utils.merge(Chain.prototype, { }) return this }, + /** - * @RuntimeMethod can be call in runtime - * comment + * @RuntimeMethod only be called in runtime + * Run current step once again + **/ + retry: function () { + if (this._end || this._destroy) return + this.__callee.apply(this._context, this.__arguments) + }, + /** + * @RuntimeMethod only be called in runtime **/ wait: function(time) { + if (this._destroy) return var that = this var args = utils.slice(arguments) args.shift() @@ -180,11 +200,11 @@ utils.merge(Chain.prototype, { }, time) }, /** - * @RuntimeMethod can be call in runtime - * - * Save/Update/Get data in current chain instance + * @RuntimeMethod only be called in runtime + * Save, Update, Get data in current chain instance */ data: function(key, data) { + if (this._destroy) return // set data if (key && data) { this._data[key] = data @@ -196,19 +216,20 @@ utils.merge(Chain.prototype, { else return util.merge({}, this._data) }, start: function() { - if (this._end) return + if (this._end || this._destroy) return this._running = true - this.next() + this.next.apply(this, arguments) return this }, end: function() { - if (this._end) return + if (this._end || this._destroy) return this._end = true - utils.batch(this._context, this._finals, this) + utils.batch.apply(utils, [this._context, this._finals, this].concat(utils.slice(arguments)) ) return this }, final: function (handler) { + if (this._destroy) return this._finals.push(handler) return this }, @@ -216,12 +237,16 @@ utils.merge(Chain.prototype, { * @RuntimeMethod can be call in runtime **/ destroy: function() { + this._destroy = true this._context = null this._data = null this._nodes = null + this._finals = null + setAlltoNoop(this, ['then', 'some', 'next', 'wait', 'data', 'start', 'end', 'final', 'destroy']) return this }, context: function(ctx) { + if (this._destroy) return this._context = ctx return this } diff --git a/dist/chain.cmd.js b/dist/chain.cmd.js index aae07dc..bcfd8b6 100644 --- a/dist/chain.cmd.js +++ b/dist/chain.cmd.js @@ -102,28 +102,37 @@ function pushNode( /*handler1, handler2, ..., handlerN*/ ) { return node } +function noop () {} +function setAlltoNoop (obj, methods) { + utils.each(methods, function (method) { + obj[method] = noop + }) +} + utils.merge(Chain.prototype, { /** * Define a chain node **/ then: function() { + if (this._destroy) return pushNode.apply(this, arguments) return this }, some: function() { + if (this._destroy) return var node = pushNode.apply(this, arguments) if (node.items.length) node.type = 'some' return this }, /** - * @RuntimeMethod can be call in runtime + * @RuntimeMethod only be called in runtime * Check current node states and execulate nextNode **/ next: function() { + if (this._end || this._destroy) return var that = this var args = utils.slice(arguments) var node - if (this._end) return if (this.__id) { node = this._nodes.get(this.__id) @@ -160,6 +169,8 @@ utils.merge(Chain.prototype, { utils.merge(chainDummy, that) chainDummy.__id = node.id chainDummy.__index = index + chainDummy.__callee = item + chainDummy.__arguments = xArgs chainDummy.__proto__ = that.__proto__ xArgs.unshift(chainDummy) @@ -167,11 +178,20 @@ utils.merge(Chain.prototype, { }) return this }, + /** - * @RuntimeMethod can be call in runtime - * comment + * @RuntimeMethod only be called in runtime + * Run current step once again + **/ + retry: function () { + if (this._end || this._destroy) return + this.__callee.apply(this._context, this.__arguments) + }, + /** + * @RuntimeMethod only be called in runtime **/ wait: function(time) { + if (this._destroy) return var that = this var args = utils.slice(arguments) args.shift() @@ -180,11 +200,11 @@ utils.merge(Chain.prototype, { }, time) }, /** - * @RuntimeMethod can be call in runtime - * - * Save/Update/Get data in current chain instance + * @RuntimeMethod only be called in runtime + * Save, Update, Get data in current chain instance */ data: function(key, data) { + if (this._destroy) return // set data if (key && data) { this._data[key] = data @@ -196,19 +216,20 @@ utils.merge(Chain.prototype, { else return util.merge({}, this._data) }, start: function() { - if (this._end) return + if (this._end || this._destroy) return this._running = true - this.next() + this.next.apply(this, arguments) return this }, end: function() { - if (this._end) return + if (this._end || this._destroy) return this._end = true - utils.batch(this._context, this._finals, this) + utils.batch.apply(utils, [this._context, this._finals, this].concat(utils.slice(arguments)) ) return this }, final: function (handler) { + if (this._destroy) return this._finals.push(handler) return this }, @@ -216,12 +237,16 @@ utils.merge(Chain.prototype, { * @RuntimeMethod can be call in runtime **/ destroy: function() { + this._destroy = true this._context = null this._data = null this._nodes = null + this._finals = null + setAlltoNoop(this, ['then', 'some', 'next', 'wait', 'data', 'start', 'end', 'final', 'destroy']) return this }, context: function(ctx) { + if (this._destroy) return this._context = ctx return this }