Browse Source

Turn Tus upload into a proper promise

Artur Paikin 9 years ago
parent
commit
9f4e412762
1 changed files with 45 additions and 21 deletions
  1. 45 21
      src/plugins/Tus10.js

+ 45 - 21
src/plugins/Tus10.js

@@ -35,29 +35,53 @@ export default class Tus10 extends Plugin {
     })
 
     // Create a new tus upload
-    const upload = new tus.Upload(file, {
-      endpoint: this.opts.endpoint,
-      onError: error => {
-        return Promise.reject('Failed because: ' + error)
-      },
-      onProgress: (bytesUploaded, bytesTotal) => {
-        let percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
-        percentage = Math.round(percentage)
-        // self.setProgress(percentage, current, total)
+    return new Promise((resolve, reject) => {
+      const upload = new tus.Upload(file, {
+        endpoint: this.opts.endpoint,
+        onError: error => {
+          reject('Failed because: ' + error)
+        },
+        onProgress: (bytesUploaded, bytesTotal) => {
+          let percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
+          percentage = Math.round(percentage)
 
-        // Dispatch progress event
-        this.core.emitter.emit('progress', {
-          plugin: this,
-          percentage: percentage
-        })
-      },
-      onSuccess: () => {
-        this.core.log(`Download ${upload.file.name} from ${upload.url}`)
-        return Promise.resolve(upload)
-      }
+          // Dispatch progress event
+          this.core.emitter.emit('progress', {
+            plugin: this,
+            percentage: percentage
+          })
+        },
+        onSuccess: () => {
+          this.core.log(`Download ${upload.file.name} from ${upload.url}`)
+          resolve(upload)
+        }
+      })
+      upload.start()
     })
-    // Start the upload
-    return upload.start()
+
+    // const upload = new tus.Upload(file, {
+    //   endpoint: this.opts.endpoint,
+    //   onError: error => {
+    //     return Promise.reject('Failed because: ' + error)
+    //   },
+    //   onProgress: (bytesUploaded, bytesTotal) => {
+    //     let percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
+    //     percentage = Math.round(percentage)
+    //     // self.setProgress(percentage, current, total)
+    //
+    //     // Dispatch progress event
+    //     this.core.emitter.emit('progress', {
+    //       plugin: this,
+    //       percentage: percentage
+    //     })
+    //   },
+    //   onSuccess: () => {
+    //     this.core.log(`Download ${upload.file.name} from ${upload.url}`)
+    //     return Promise.resolve(upload)
+    //   }
+    // })
+    // // Start the upload
+    // return upload.start()
   }
 
 /**