From 5c226f4e6c754f432d4fee5241161fe2c730737b Mon Sep 17 00:00:00 2001 From: Kevin van Zonneveld Date: Fri, 27 Nov 2015 11:08:44 +0100 Subject: [PATCH] Waterfall + parallel --- src/core/Transloadit.js | 106 ++++++++++++---------------------------- src/plugins/DragDrop.js | 5 ++ 2 files changed, 37 insertions(+), 74 deletions(-) diff --git a/src/core/Transloadit.js b/src/core/Transloadit.js index da3b822e7..42a88f6d6 100644 --- a/src/core/Transloadit.js +++ b/src/core/Transloadit.js @@ -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 + }) + }); } } diff --git a/src/plugins/DragDrop.js b/src/plugins/DragDrop.js index a9f1acf7f..f6da4bfa0 100644 --- a/src/plugins/DragDrop.js +++ b/src/plugins/DragDrop.js @@ -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();