Waterfall + parallel

This commit is contained in:
Kevin van Zonneveld 2015-11-27 11:08:44 +01:00 committed by Harry Hedger
parent 741377a8c8
commit 5c226f4e6c
2 changed files with 37 additions and 74 deletions

View file

@ -12,11 +12,6 @@ export default class Transloadit {
use(Plugin, opts) {
// Instantiate
var plugin = new Plugin(this, opts);
// Save in plugin container
// if (!this.plugins[plugin.type]) {
// this.plugins[plugin.type] = [];
// }
this.plugins[plugin.type] = this.plugins[plugin.type] || [];
this.plugins[plugin.type].push(plugin);
@ -30,78 +25,41 @@ export default class Transloadit {
return this;
}
run() {
// Walk over plugins in the order as defined by this.types.
// var files = []
// for (var j in this.types) {
// var type = this.types[j];
// // Walk over all plugins of this type, passing & modifying the files array as we go
// for (var i in this.plugins[type]) {
// var plugin = this.plugins[type][i];
// console.log('--> Now running ' + plugin.type + ' plugin ' + plugin.name + ': ');
// files = plugin.run(files);
// console.dir(files);
// console.log('');
// }
// }
// console.log(this.plugins);
// for (let plugin in this.plugins) {
// console.log(this.plugins[plugin]);
// this.plugins[plugin].run().then(function (text) {
// console.log(text);
// })
// }
// array of promises from all plugins
// Promise.all(plugins).then(function(files) {
// console.log(files);
// });
// Walk over plugins in the order as defined by this.types.
// plugins.push(plugin.run.bind(plugin));
var pluginTypePacks = [];
function dummy(cb) {
cb(null, 'smth');
}
// pluginTypePacks.push(async.value([]));
pluginTypePacks.push(dummy);
for (let j in this.types) {
const type = this.types[j];
const pluginPack = [];
for (let i in this.plugins[type]) {
const plugin = this.plugins[type][i];
pluginPack.push(plugin.run.bind(plugin));
}
// console.log(pluginPack);
// async.parallel(pluginPack, function (err, files) {
// // console.log('parallel done');
// console.log(files);
// // done(files);
// });
const pluginTypePackExecuter = function (files, done) {
async.parallel(pluginPack, function (err, files) {
// console.log('parallel done');
// console.log(files);
done(files);
});
};
pluginTypePacks.push(pluginTypePackExecuter);
}
async.waterfall(pluginTypePacks, function (result) {
// console.log(result);
runType(type, files, cb) {
console.dir({
method: 'Transloadit.runType',
type : type,
files : files,
cb : cb
});
// console.log(plugins);
const methods = [];
for (let p in this.plugins[type]) {
const plugin = this.plugins[type][p];
methods.push(plugin.run.bind(plugin, files));
}
async.parallel(methods, cb);
}
// core.run is the final step and retuns the results (vs every other method, returning `this`)
// for chainability
// return files;
run() {
console.dir({
method: 'Transloadit.run'
});
var typeMethods = [];
typeMethods.push(async.constant([]));
for (let t in this.types) {
const type = this.types[t];
typeMethods.push(this.runType.bind(this, type));
}
async.waterfall(typeMethods, function (err, finalFiles) {
console.dir({
err :err,
finalFiles:finalFiles
})
});
}
}

View file

@ -49,6 +49,11 @@ export default class DragDrop extends TransloaditPlugin {
}
run(files, done) {
console.dir({
method: "DragDrop.run",
files : files,
done : done
});
console.log('DragDrop running!');
// console.log(files);
this.listenForEvents();