Przeglądaj źródła

core: move upload promise sequencing into a helper

Renée Kooi 7 lat temu
rodzic
commit
37c699160b
2 zmienionych plików z 16 dodań i 9 usunięć
  1. 4 9
      src/core/Core.js
  2. 12 0
      src/core/Utils.js

+ 4 - 9
src/core/Core.js

@@ -575,8 +575,6 @@ class Uppy {
   }
   }
 
 
   upload () {
   upload () {
-    let promise = Promise.resolve()
-
     this.emit('core:upload')
     this.emit('core:upload')
 
 
     const waitingFileIDs = Object.keys(this.state.files)
     const waitingFileIDs = Object.keys(this.state.files)
@@ -597,13 +595,10 @@ class Uppy {
       return file.id
       return file.id
     }
     }
 
 
-    ;[].concat(
-      this.preProcessors,
-      this.uploaders,
-      this.postProcessors
-    ).forEach((fn) => {
-      promise = promise.then(() => fn(waitingFileIDs))
-    })
+    const promise = Utils.runPromiseSequence(
+      [...this.preProcessors, ...this.uploaders, ...this.postProcessors],
+      waitingFileIDs
+    )
 
 
     // Not returning the `catch`ed promise, because we still want to return a rejected
     // Not returning the `catch`ed promise, because we still want to return a rejected
     // promise from this method if the upload failed.
     // promise from this method if the upload failed.

+ 12 - 0
src/core/Utils.js

@@ -122,6 +122,17 @@ function extend (...objs) {
   return Object.assign.apply(this, [{}].concat(objs))
   return Object.assign.apply(this, [{}].concat(objs))
 }
 }
 
 
+/**
+ * Runs an array of promise-returning functions in sequence.
+ */
+function runPromiseSequence (functions, ...args) {
+  let promise = Promise.resolve()
+  functions.forEach((func) => {
+    promise = promise.then(() => func(...args))
+  })
+  return promise
+}
+
 /**
 /**
  * Takes function or class, returns its name.
  * Takes function or class, returns its name.
  * Because IE doesn’t support `constructor.name`.
  * Because IE doesn’t support `constructor.name`.
@@ -395,6 +406,7 @@ module.exports = {
   // $,
   // $,
   // $$,
   // $$,
   extend,
   extend,
+  runPromiseSequence,
   supportsMediaRecorder,
   supportsMediaRecorder,
   isTouchDevice,
   isTouchDevice,
   getFileNameAndExtension,
   getFileNameAndExtension,