Преглед на файлове

@uppy/status-bar: listen to `upload` event instead of button click (#4563)

This ensures that the internal state of the status bar will be correct
when the upload is started programmatically rather than using the
Dashboard UI.
Antoine du Hamel преди 1 година
родител
ревизия
10d0d0d0c6
променени са 1 файла, в които са добавени 21 реда и са изтрити 16 реда
  1. 21 16
      packages/@uppy/status-bar/src/StatusBar.jsx

+ 21 - 16
packages/@uppy/status-bar/src/StatusBar.jsx

@@ -130,22 +130,6 @@ export default class StatusBar extends UIPlugin {
   }
 
   startUpload = () => {
-    const { recoveredState } = this.uppy.getState()
-
-    this.#previousSpeed = null
-    this.#previousETA = null
-    if (recoveredState) {
-      this.#previousUploadedBytes = Object.values(recoveredState.files)
-        .reduce((pv, { progress }) => pv + progress.bytesUploaded, 0)
-
-      // We don't set `#lastUpdateTime` at this point because the upload won't
-      // actually resume until the user asks for it.
-
-      this.uppy.emit('restore-confirmed')
-      return undefined
-    }
-    this.#lastUpdateTime = performance.now()
-    this.#previousUploadedBytes = 0
     return this.uppy.upload().catch(() => {
       // Error logged in Core
     })
@@ -245,14 +229,35 @@ export default class StatusBar extends UIPlugin {
     }
   }
 
+  #onUploadStart = () => {
+    const { recoveredState } = this.uppy.getState()
+
+    this.#previousSpeed = null
+    this.#previousETA = null
+    if (recoveredState) {
+      this.#previousUploadedBytes = Object.values(recoveredState.files)
+        .reduce((pv, { progress }) => pv + progress.bytesUploaded, 0)
+
+      // We don't set `#lastUpdateTime` at this point because the upload won't
+      // actually resume until the user asks for it.
+
+      this.uppy.emit('restore-confirmed')
+      return
+    }
+    this.#lastUpdateTime = performance.now()
+    this.#previousUploadedBytes = 0
+  }
+
   install () {
     const { target } = this.opts
     if (target) {
       this.mount(target, this)
     }
+    this.uppy.on('upload', this.#onUploadStart)
   }
 
   uninstall () {
     this.unmount()
+    this.uppy.off('upload', this.#onUploadStart)
   }
 }