瀏覽代碼

Second crack at reimplementing async.waterfall and async.parallel in Promises

Thanks to:
http://trevorburnham.com/presentations/flow-control-with-promises/#/16
http://bluebirdjs.com/docs/coming-from-other-libraries.html#async.waterf
all
and personally Roman Liutikov
@roman01la
Artur Paikin 9 年之前
父節點
當前提交
a1e33441af
共有 4 個文件被更改,包括 39 次插入25 次删除
  1. 2 1
      package.json
  2. 25 13
      src/core/Transloadit.js
  3. 9 8
      src/plugins/DragDrop.js
  4. 3 3
      src/plugins/Tus10.js

+ 2 - 1
package.json

@@ -47,6 +47,7 @@
     "zuul": "^3.7.2"
   },
   "dependencies": {
-    "async": "^1.5.0"
+    "async": "^1.5.0",
+    "tus-js-client": "^1.1.2"
   }
 }

+ 25 - 13
src/core/Transloadit.js

@@ -1,4 +1,10 @@
-import async from 'async';
+function promiseWaterfall([resolvedPromise, ...tasks]) {
+    const finalTaskPromise = tasks.reduce(function (prevTaskPromise, task) {
+        return prevTaskPromise.then(task);
+    }, resolvedPromise(1));  // initial value
+
+    return finalTaskPromise;
+}
 
 export default class {
   constructor(opts) {
@@ -25,23 +31,25 @@ export default class {
   }
 
   // Runs all plugins of the same type in parallel
-  runType(type, files, cb) {
+  runType(type, files) {
     console.dir({
       method: 'Transloadit.runType',
       type  : type,
-      files : files,
-      cb    : cb
+      files : files
+      // cb    : cb
     });
 
     const methods = [];
     for (let p in this.plugins[type]) {
       const plugin = this.plugins[type][p];
-      methods.push(plugin.run.bind(plugin, files));
+      methods.push(plugin.run.call(plugin, files));
     }
 
+    return Promise.all(methods);
+
     // const methods = this.plugins[type].map(plugin => plugin.run.bind(plugin, files));
 
-    async.parallel(methods, cb);
+    // async.parallel(methods, cb);
   }
 
   // Runs a waterfall of runType plugin packs, like so:
@@ -52,7 +60,7 @@ export default class {
     });
 
     var typeMethods = [];
-    typeMethods.push(async.constant([]));
+    // typeMethods.push(async.constant([]));
 
     // for (let t in this.types) {
     //   const type = this.types[t];
@@ -65,11 +73,15 @@ export default class {
       }
     });
 
-    async.waterfall(typeMethods, function (err, finalFiles) {
-      console.dir({
-        err       : err ,
-        finalFiles: finalFiles
-      });
-    });
+    promiseWaterfall(typeMethods)
+      .then((result) => console.log(result))
+      .catch((error) => console.error(error));
+
+    // async.waterfall(typeMethods, function (err, finalFiles) {
+    //   console.dir({
+    //     err       : err ,
+    //     finalFiles: finalFiles
+    //   });
+    // });
   }
 }

+ 9 - 8
src/plugins/DragDrop.js

@@ -1,6 +1,7 @@
 import Utils from '../core/Utils';
 import TransloaditPlugin from './TransloaditPlugin';
 // import Tus from 'tus-js-client';
+// console.log('pizza', Tus);
 
 export default class DragDrop extends TransloaditPlugin {
   constructor(core, opts) {
@@ -128,7 +129,7 @@ export default class DragDrop extends TransloaditPlugin {
     request.send(data);
 
     // Create a new tus upload
-    // const upload = new Tus.Upload(files, {
+    // const upload = new Tus.Upload(data, {
     //   endpoint: 'http://master.tus.io:8080',
     //   onError: function(error) {
     //     console.log('Failed because: ' + error);
@@ -141,16 +142,16 @@ export default class DragDrop extends TransloaditPlugin {
     //     console.log('Download %s from %s', upload.file.name, upload.url);
     //   }
     // });
-
-    // Start the upload
+    //
+    // // Start the upload
     // upload.start();
   }
 
-  run(files, done) {
+  run(files) {
     console.dir({
       method: 'DragDrop.run',
-      files : files,
-      done  : done
+      files : files
+      // done  : done
     });
 
     console.log('DragDrop running!');
@@ -160,7 +161,7 @@ export default class DragDrop extends TransloaditPlugin {
     var selected = [ {name: 'lolcat.jpeg'} ];
     this.core.setProgress(this, 100);
     // return selected;
-    done(null, 'done with DragDrop');
-    // return Promise.resolve('done with DragDrop');
+    // done(null, 'done with DragDrop');
+    return Promise.resolve(files);
   }
 }

+ 3 - 3
src/plugins/Tus10.js

@@ -6,7 +6,7 @@ export default class Tus10 extends TransloaditPlugin {
     this.type = 'uploader';
   }
 
-  run(files, done) {
+  run(files) {
     // console.log(files);
     this.core.setProgress(this, 0);
     var uploaded = [];
@@ -18,8 +18,8 @@ export default class Tus10 extends TransloaditPlugin {
     // }
     this.core.setProgress(this, 100);
 
-    done(null, 'done with Tus');
-    // return Promise.resolve('done with Tus');
+    // done(null, 'done with Tus');
+    return Promise.resolve(files);
 
     // return uploaded;
   }