소스 검색

Attempting to make plugins run in a waterfall of parallels

Artur Paikin 9 년 전
부모
커밋
09ec916d9c
5개의 변경된 파일88개의 추가작업 그리고 26개의 파일을 삭제
  1. 2 2
      examples/playground/src/js/app.js
  2. 3 0
      package.json
  3. 66 15
      src/core/Transloadit.js
  4. 5 1
      src/plugins/DragDrop.js
  5. 12 8
      src/plugins/Tus10.js

+ 2 - 2
examples/playground/src/js/app.js

@@ -7,8 +7,8 @@ const files = transloadit
   .use(Tus10, {endpoint: 'http://master.tus.io:8080'})
   .run();
 
-console.log('--> Finished transloadit. Final result: ');
-console.dir(files);
+// console.log('--> Finished transloadit. Final result: ');
+// console.dir(files);
 
 // var Transloadit = require('./src/core/Transloadit.js');
 // var DragDrop = require('./src/plugins/DragDrop.js');

+ 3 - 0
package.json

@@ -45,5 +45,8 @@
     "phantomjs": "^1.9.18",
     "watchify": "^3.6.1",
     "zuul": "^3.7.2"
+  },
+  "dependencies": {
+    "async": "^1.5.0"
   }
 }

+ 66 - 15
src/core/Transloadit.js

@@ -1,4 +1,6 @@
-export default class {
+import async from 'async';
+
+export default class Transloadit {
   constructor(opts) {
     // Dictates in what order different plugin types are ran:
     this.types = [ 'presetter', 'selecter', 'uploader' ];
@@ -12,9 +14,10 @@ export default class {
     var plugin = new Plugin(this, opts);
 
     // Save in plugin container
-    if (!this.plugins[plugin.type]) {
-      this.plugins[plugin.type] = [];
-    }
+    // if (!this.plugins[plugin.type]) {
+    //   this.plugins[plugin.type] = [];
+    // }
+    this.plugins[plugin.type] = this.plugins[plugin.type] || [];
     this.plugins[plugin.type].push(plugin);
 
     return this;
@@ -29,21 +32,69 @@ export default class {
 
   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('');
+    // 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 pluginTypePack = [];
+    // function dummy(cb) {
+    //   cb(null, 'smth');
+    // }
+    // pluginTypePack.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);
+      const pluginTypePackExecuter = function (done) {
+        async.parallel(pluginPack, function (err, files) {
+          // console.log('parallel done');
+          console.log(files);
+          done(files);
+        });
+      };
+      pluginTypePack.push(pluginTypePackExecuter);
     }
+    // console.log(pluginTypePack);
+
+    async.waterfall(pluginTypePack, function (result) {
+      // console.log(result);
+    });
+
+    // console.log(plugins);
+
 
     // core.run is the final step and retuns the results (vs every other method, returning `this`)
     // for chainability
-    return files;
+    // return files;
   }
 }

+ 5 - 1
src/plugins/DragDrop.js

@@ -48,11 +48,15 @@ export default class DragDrop extends TransloaditPlugin {
     return files;
   }
 
-  run(files) {
+  run(done) {
+    console.log('DragDrop running!');
+    // console.log(files);
     this.listenForEvents();
     this.core.setProgress(this, 0);
     var selected = [ {name: 'lolcat.jpeg'} ];
     this.core.setProgress(this, 100);
     // return selected;
+    done(null, 'done with DragDrop');
+    // return Promise.resolve('done with DragDrop');
   }
 }

+ 12 - 8
src/plugins/Tus10.js

@@ -6,17 +6,21 @@ export default class Tus10 extends TransloaditPlugin {
     this.type = 'uploader';
   }
 
-  run(files) {
+  run(done) {
+    // console.log(files);
     this.core.setProgress(this, 0);
     var uploaded = [];
-    for (var i in files) {
-      var file = files[i];
-      this.core.setProgress(this, (i * 1) + 1);
-      uploaded[i]     = file;
-      uploaded[i].url = this.opts.endpoint + '/uploaded/' + file.name;
-    }
+    // for (var i in files) {
+    //   var file = files[i];
+    //   this.core.setProgress(this, (i * 1) + 1);
+    //   uploaded[i]     = file;
+    //   uploaded[i].url = this.opts.endpoint + '/uploaded/' + file.name;
+    // }
     this.core.setProgress(this, 100);
 
-    return uploaded;
+    done(null, 'done with Tus');
+    // return Promise.resolve('done with Tus');
+
+    // return uploaded;
   }
 }