瀏覽代碼

Merge pull request #817 from loopbio/tus-limit-parallel-uploads

tus: respect `limit` option for upload parameter requests
Artur Paikin 7 年之前
父節點
當前提交
d33e2ec992
共有 2 個文件被更改,包括 29 次插入16 次删除
  1. 25 16
      src/plugins/Tus.js
  2. 4 0
      website/src/docs/tus.md

+ 25 - 16
src/plugins/Tus.js

@@ -4,7 +4,8 @@ const UppySocket = require('../core/UppySocket')
 const {
 const {
   emitSocketProgress,
   emitSocketProgress,
   getSocketHost,
   getSocketHost,
-  settle
+  settle,
+  limitPromises
 } = require('../core/Utils')
 } = require('../core/Utils')
 require('whatwg-fetch')
 require('whatwg-fetch')
 
 
@@ -61,12 +62,20 @@ module.exports = class Tus extends Plugin {
       resume: true,
       resume: true,
       autoRetry: true,
       autoRetry: true,
       useFastRemoteRetry: true,
       useFastRemoteRetry: true,
+      limit: 0,
       retryDelays: [0, 1000, 3000, 5000]
       retryDelays: [0, 1000, 3000, 5000]
     }
     }
 
 
     // merge default options with the ones set by user
     // merge default options with the ones set by user
     this.opts = Object.assign({}, defaultOptions, opts)
     this.opts = Object.assign({}, defaultOptions, opts)
 
 
+    // Simultaneous upload limiting is shared across all uploads with this plugin.
+    if (typeof this.opts.limit === 'number' && this.opts.limit !== 0) {
+      this.limitUploads = limitPromises(this.opts.limit)
+    } else {
+      this.limitUploads = (fn) => fn
+    }
+
     this.uploaders = Object.create(null)
     this.uploaders = Object.create(null)
     this.uploaderEvents = Object.create(null)
     this.uploaderEvents = Object.create(null)
     this.uploaderSockets = Object.create(null)
     this.uploaderSockets = Object.create(null)
@@ -194,9 +203,6 @@ module.exports = class Tus extends Plugin {
       if (!file.isPaused) {
       if (!file.isPaused) {
         upload.start()
         upload.start()
       }
       }
-      if (!file.isRestored) {
-        this.uppy.emit('upload-started', file, upload)
-      }
     })
     })
   }
   }
 
 
@@ -218,8 +224,6 @@ module.exports = class Tus extends Plugin {
           .catch(reject)
           .catch(reject)
       }
       }
 
 
-      this.uppy.emit('upload-started', file)
-
       fetch(file.remote.url, {
       fetch(file.remote.url, {
         method: 'post',
         method: 'post',
         credentials: 'include',
         credentials: 'include',
@@ -407,23 +411,28 @@ module.exports = class Tus extends Plugin {
   }
   }
 
 
   uploadFiles (files) {
   uploadFiles (files) {
-    const promises = files.map((file, index) => {
-      const current = parseInt(index, 10) + 1
+    const actions = files.map((file, i) => {
+      const current = parseInt(i, 10) + 1
       const total = files.length
       const total = files.length
 
 
       if (file.error) {
       if (file.error) {
-        return Promise.reject(new Error(file.error))
-      }
-
-      this.uppy.log(`uploading ${current} of ${total}`)
-
-      if (file.isRemote) {
-        return this.uploadRemote(file, current, total)
+        return () => Promise.reject(new Error(file.error))
+      } else if (file.isRemote) {
+        // We emit upload-started here, so that it's also emitted for files
+        // that have to wait due to the `limit` option.
+        this.uppy.emit('upload-started', file)
+        return this.uploadRemote.bind(this, file, current, total)
       } else {
       } else {
-        return this.upload(file, current, total)
+        this.uppy.emit('upload-started', file)
+        return this.upload.bind(this, file, current, total)
       }
       }
     })
     })
 
 
+    const promises = actions.map((action) => {
+      const limitedAction = this.limitUploads(action)
+      return limitedAction()
+    })
+
     return settle(promises)
     return settle(promises)
   }
   }
 
 

+ 4 - 0
website/src/docs/tus.md

@@ -36,4 +36,8 @@ URL to upload to, where your tus.io server is running.
 
 
 Whether to auto-retry the upload when the user's internet connection is back online after an outage.
 Whether to auto-retry the upload when the user's internet connection is back online after an outage.
 
 
+### `limit: 0`
+
+Limit the amount of uploads going on at the same time. Passing `0` means no limit.
+
 [tus-js-client]: https://github.com/tus/tus-js-client
 [tus-js-client]: https://github.com/tus/tus-js-client