Skip to content

Instantly share code, notes, and snippets.

@you21979
Last active August 29, 2015 14:05
Show Gist options
  • Save you21979/01ab8cfdd851f322ecf3 to your computer and use it in GitHub Desktop.
Save you21979/01ab8cfdd851f322ecf3 to your computer and use it in GitHub Desktop.
プロミスのパイプラインを動的に拡張するための仕組み
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());
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));
}
@you21979
Copy link
Author

妄想してみたけどあまり必要ないのかもしれない
ボトムアップ的なアプローチの利点が消える

@you21979
Copy link
Author

promiseタスクの方は流れてるデータを動的にウォッチできるけどどうだろうなぁ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment