diff --git a/.travis.yml b/.travis.yml index 5a3a171..b275b2f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: node_js node_js: - 0.11 - - 0.10 - - 0.8 # whitelist branches: only: diff --git a/lib/event-pipe.coffee b/lib/event-pipe.coffee index 6119b73..c52e5cf 100644 --- a/lib/event-pipe.coffee +++ b/lib/event-pipe.coffee @@ -1,18 +1,15 @@ -events = require "events" +{EventEmitter} = require "events" +{isArray, isFunction} = require 'util' -isArray = (arg)-> - Object.prototype.toString.call(arg) is '[object Array]' - -class EventPipe extends events.EventEmitter +class EventPipe extends EventEmitter constructor: (cbs...) -> @list = [] - + @_setContainer() @reset() - return if cbs.length is 0 - @add cbs... + @add cbs... if cbs.length - getContainer : -> - container = (args...) => + _setContainer: -> + @container = (args...) => if @stopped @emit 'stopped', @container return @@ -21,7 +18,7 @@ class EventPipe extends events.EventEmitter @emit 'drain', @container return - [type, cb, lazy] = @list[@pointer] + [type, cbs, lazy] = @list[@pointer] # lazy mode if lazy @@ -43,58 +40,56 @@ class EventPipe extends events.EventEmitter @pointer++ - # seq to next cb - if type is 'seq' - cb.apply @container, args - # par to next cbs - else if type is 'par' - @flag = cb.length; - cb1.apply @container, args for cb1 in cb - return + switch type + # seq to next cb + when 'seq' then cbs.apply @container, args + # par to next cbs + when 'par' + @flag = cbs.length + cb.apply @container, args for cb in cbs - container.__stop = => + @container.stop = => @stop() - @container = container + + @container add: (cbs...) -> for cb in cbs - if typeof cb is 'function' + if isFunction cb @seq cb else if isArray cb @par cb... - @ + this lazy: (cbs...) -> for cb in cbs - if typeof cb is 'function' + if isFunction cb @lseq cb else if isArray cb @lpar cb... - @ + this seq: (cbs...) -> @list.push ['seq', cb] for cb in cbs - @ + this lseq: (cbs...) -> - @list.push ['seq', cb, true] for cb in cbs - @ + @list.push ['seq', cb, yes] for cb in cbs + this par: (cbs...) -> @list.push ['par', cbs] - @ + this lpar: (cbs...) -> - @list.push ['par', cbs, true] - @ + @list.push ['par', cbs, yes] + this run: (args...) -> - @container(args...) - @ + @container args... + this stop: -> - @stopped = true + @stopped = yes @emit 'stop' - @ - reset : -> + this + reset: -> @flag = @pointer = 0 @parArgs = [] - @getContainer() - @ + this module.exports = (args...) -> new EventPipe args... - diff --git a/out/release/lib/event-pipe.js b/out/release/lib/event-pipe.js index c2594e6..4e9a1ff 100644 --- a/out/release/lib/event-pipe.js +++ b/out/release/lib/event-pipe.js @@ -1,171 +1,176 @@ -// Generated by CoffeeScript 1.6.3 -var EventPipe, events, isArray, - __hasProp = {}.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, - __slice = [].slice; - -events = require("events"); - -isArray = function(arg) { - return Object.prototype.toString.call(arg) === '[object Array]'; -}; - -EventPipe = (function(_super) { - __extends(EventPipe, _super); - - function EventPipe() { - var cbs; - cbs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - this.list = []; - this.reset(); - if (cbs.length === 0) { - return; - } - this.add.apply(this, cbs); - } - - EventPipe.prototype.getContainer = function() { - var container, - _this = this; - container = function() { - var args, cb, cb1, err, lazy, type, _i, _len, _ref, _ref1; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - if (_this.stopped) { - _this.emit('stopped', _this.container); - return; - } - if (_this.pointer >= _this.list.length) { - _this.emit('drain', _this.container); - return; +// Generated by CoffeeScript 1.9.3 +(function() { + var EventEmitter, EventPipe, isArray, isFunction, ref, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty, + slice = [].slice; + + EventEmitter = require("events").EventEmitter; + + ref = require('util'), isArray = ref.isArray, isFunction = ref.isFunction; + + EventPipe = (function(superClass) { + extend(EventPipe, superClass); + + function EventPipe() { + var cbs; + cbs = 1 <= arguments.length ? slice.call(arguments, 0) : []; + this.list = []; + this._setContainer(); + this.reset(); + if (cbs.length) { + this.add.apply(this, cbs); } - _ref = _this.list[_this.pointer], type = _ref[0], cb = _ref[1], lazy = _ref[2]; - if (lazy) { - _ref1 = args, err = _ref1[0], args = 2 <= _ref1.length ? __slice.call(_ref1, 1) : []; - if (err) { - _this.emit('error', err); - _this.stop(); - return; + } + + EventPipe.prototype._setContainer = function() { + this.container = (function(_this) { + return function() { + var args, cb, cbs, err, i, lazy, len, ref1, ref2, results, type; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; + if (_this.stopped) { + _this.emit('stopped', _this.container); + return; + } + if (_this.pointer >= _this.list.length) { + _this.emit('drain', _this.container); + return; + } + ref1 = _this.list[_this.pointer], type = ref1[0], cbs = ref1[1], lazy = ref1[2]; + if (lazy) { + ref2 = args, err = ref2[0], args = 2 <= ref2.length ? slice.call(ref2, 1) : []; + if (err) { + _this.emit('error', err); + _this.stop(); + return; + } + } + if (_this.flag-- > 0) { + _this.parArgs.push(args); + } + if (_this.flag === 0 && _this.parArgs.length !== 0) { + args = _this.parArgs; + _this.parArgs = []; + } + if (_this.flag > 0) { + return; + } + _this.pointer++; + switch (type) { + case 'seq': + return cbs.apply(_this.container, args); + case 'par': + _this.flag = cbs.length; + results = []; + for (i = 0, len = cbs.length; i < len; i++) { + cb = cbs[i]; + results.push(cb.apply(_this.container, args)); + } + return results; + } + }; + })(this); + this.container.stop = (function(_this) { + return function() { + return _this.stop(); + }; + })(this); + return this.container; + }; + + EventPipe.prototype.add = function() { + var cb, cbs, i, len; + cbs = 1 <= arguments.length ? slice.call(arguments, 0) : []; + for (i = 0, len = cbs.length; i < len; i++) { + cb = cbs[i]; + if (isFunction(cb)) { + this.seq(cb); + } else if (isArray(cb)) { + this.par.apply(this, cb); } } - if (_this.flag-- > 0) { - _this.parArgs.push(args); - } - if (_this.flag === 0 && _this.parArgs.length !== 0) { - args = _this.parArgs; - _this.parArgs = []; - } - if (_this.flag > 0) { - return; - } - _this.pointer++; - if (type === 'seq') { - cb.apply(_this.container, args); - } else if (type === 'par') { - _this.flag = cb.length; - for (_i = 0, _len = cb.length; _i < _len; _i++) { - cb1 = cb[_i]; - cb1.apply(_this.container, args); + return this; + }; + + EventPipe.prototype.lazy = function() { + var cb, cbs, i, len; + cbs = 1 <= arguments.length ? slice.call(arguments, 0) : []; + for (i = 0, len = cbs.length; i < len; i++) { + cb = cbs[i]; + if (isFunction(cb)) { + this.lseq(cb); + } else if (isArray(cb)) { + this.lpar.apply(this, cb); } } + return this; }; - container.__stop = function() { - return _this.stop(); - }; - return this.container = container; - }; - EventPipe.prototype.add = function() { - var cb, cbs, _i, _len; - cbs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - for (_i = 0, _len = cbs.length; _i < _len; _i++) { - cb = cbs[_i]; - if (typeof cb === 'function') { - this.seq(cb); - } else if (isArray(cb)) { - this.par.apply(this, cb); + EventPipe.prototype.seq = function() { + var cb, cbs, i, len; + cbs = 1 <= arguments.length ? slice.call(arguments, 0) : []; + for (i = 0, len = cbs.length; i < len; i++) { + cb = cbs[i]; + this.list.push(['seq', cb]); } - } - return this; - }; + return this; + }; - EventPipe.prototype.lazy = function() { - var cb, cbs, _i, _len; - cbs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - for (_i = 0, _len = cbs.length; _i < _len; _i++) { - cb = cbs[_i]; - if (typeof cb === 'function') { - this.lseq(cb); - } else if (isArray(cb)) { - this.lpar.apply(this, cb); + EventPipe.prototype.lseq = function() { + var cb, cbs, i, len; + cbs = 1 <= arguments.length ? slice.call(arguments, 0) : []; + for (i = 0, len = cbs.length; i < len; i++) { + cb = cbs[i]; + this.list.push(['seq', cb, true]); } - } - return this; - }; - - EventPipe.prototype.seq = function() { - var cb, cbs, _i, _len; - cbs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - for (_i = 0, _len = cbs.length; _i < _len; _i++) { - cb = cbs[_i]; - this.list.push(['seq', cb]); - } - return this; - }; + return this; + }; - EventPipe.prototype.lseq = function() { - var cb, cbs, _i, _len; - cbs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - for (_i = 0, _len = cbs.length; _i < _len; _i++) { - cb = cbs[_i]; - this.list.push(['seq', cb, true]); - } - return this; - }; + EventPipe.prototype.par = function() { + var cbs; + cbs = 1 <= arguments.length ? slice.call(arguments, 0) : []; + this.list.push(['par', cbs]); + return this; + }; - EventPipe.prototype.par = function() { - var cbs; - cbs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - this.list.push(['par', cbs]); - return this; - }; + EventPipe.prototype.lpar = function() { + var cbs; + cbs = 1 <= arguments.length ? slice.call(arguments, 0) : []; + this.list.push(['par', cbs, true]); + return this; + }; - EventPipe.prototype.lpar = function() { - var cbs; - cbs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - this.list.push(['par', cbs, true]); - return this; - }; + EventPipe.prototype.run = function() { + var args; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; + this.container.apply(this, args); + return this; + }; - EventPipe.prototype.run = function() { - var args; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - this.container.apply(this, args); - return this; - }; + EventPipe.prototype.stop = function() { + this.stopped = true; + this.emit('stop'); + return this; + }; - EventPipe.prototype.stop = function() { - this.stopped = true; - this.emit('stop'); - return this; - }; + EventPipe.prototype.reset = function() { + this.flag = this.pointer = 0; + this.parArgs = []; + return this; + }; - EventPipe.prototype.reset = function() { - this.flag = this.pointer = 0; - this.parArgs = []; - this.getContainer(); - return this; - }; + return EventPipe; - return EventPipe; + })(EventEmitter); -})(events.EventEmitter); + module.exports = function() { + var args; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; + return (function(func, args, ctor) { + ctor.prototype = func.prototype; + var child = new ctor, result = func.apply(child, args); + return Object(result) === result ? result : child; + })(EventPipe, args, function(){}); + }; -module.exports = function() { - var args; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - return (function(func, args, ctor) { - ctor.prototype = func.prototype; - var child = new ctor, result = func.apply(child, args); - return Object(result) === result ? result : child; - })(EventPipe, args, function(){}); -}; +}).call(this); diff --git a/package.json b/package.json index 3906d39..b75ba44 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "email": "qingdu@alibaba-inc.com" }, "engines": { - "node": ">=0.8.0" + "node": ">=0.11.0" }, "homepage": "http://github.com/q3boy/event-pipe", "repository": { @@ -30,4 +30,4 @@ "postpublish": "make clean", "test": "make test-cov" } -} \ No newline at end of file +} diff --git a/package.yaml b/package.yaml index 1fc0259..64309ee 100644 --- a/package.yaml +++ b/package.yaml @@ -6,7 +6,7 @@ author: name: 清笃 email: qingdu@alibaba-inc.com engines: - node: '>=0.8.0' + node: '>=0.11.0' homepage: http://github.com/q3boy/event-pipe repository: type: git diff --git a/tests/test-event-pipe.coffee b/tests/test-event-pipe.coffee index 72cb7bd..003589b 100644 --- a/tests/test-event-pipe.coffee +++ b/tests/test-event-pipe.coffee @@ -117,7 +117,7 @@ describe 'Event Pipe Test', -> flag = 0 stop = false ep -> - @__stop() + @stop() process.nextTick (-> flag++ @()