Last active
August 29, 2015 14:05
-
-
Save you21979/01ab8cfdd851f322ecf3 to your computer and use it in GitHub Desktop.
プロミスのパイプラインを動的に拡張するための仕組み
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addtioalTask(pipeline){ | |
pipeline.push(function(p){ return p.then(function(v){console.log(v);return v}) }); | |
} | |
var pipeline = []; | |
addtionalTask(pipeline); | |
pipeline.push(function(p){ return p.then(function(v){return v}) }); | |
pipeline.reduce(function(p, f){ return f(p) }, promiseAync()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util'); | |
var event = require('events'); | |
var Promise = require('bluebird'); | |
var PromiseTask = module.exports = function(){ | |
event.EventEmitter.call(this); | |
this.pipeline = []; | |
} | |
util.inherits(PromiseTask, event.EventEmitter); | |
PromiseTask.prototype.addThen = function(f){ | |
this.pipeline.push(function(p){ return p.then(f) }); | |
} | |
PromiseTask.prototype.addWatch = function(name){ | |
if(!name) name = ''; | |
var self = this; | |
this.addThen(function(v){self.emit('watch', v, name); return v}); | |
} | |
PromiseTask.prototype.add = function(f, name){ | |
this.addThen(f); | |
this.addWatch(name); | |
} | |
PromiseTask.prototype.run = function(promise){ | |
if(!(promise instanceof Array)){ | |
promise = [promise]; | |
} | |
var self = this; | |
this.pipeline.push(function(p){ return p.then(function(result){ self.emit('complete', result) }) }); | |
this.pipeline.push(function(p){ return p.catch(function(err){ self.emit('error', err) }) }); | |
this.pipeline.reduce(function(p, f){ return f(p) }, Promise.all(promise)); | |
} |
promiseタスクの方は流れてるデータを動的にウォッチできるけどどうだろうなぁ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
妄想してみたけどあまり必要ないのかもしれない
ボトムアップ的なアプローチの利点が消える