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

@uppy/core: pass file to events consistently (#5136)

Merlijn Vos преди 10 месеца
родител
ревизия
453eb0f9b5
променени са 4 файла, в които са добавени 33 реда и са изтрити 33 реда
  1. 15 10
      docs/uppy-core.mdx
  2. 2 2
      packages/@uppy/companion-client/src/RequestClient.ts
  3. 6 6
      packages/@uppy/core/src/EventManager.ts
  4. 10 15
      packages/@uppy/core/src/Uppy.ts

+ 15 - 10
docs/uppy-core.mdx

@@ -1036,14 +1036,10 @@ uppy.on('file-removed', (file) => {
 
 Fired when the upload starts.
 
-```js
-uppy.on('upload', (data) => {
-	// data object consists of `id` with upload ID and `fileIDs` array
-	// with file IDs in current upload
-	// data: { id, fileIDs }
-	console.log(`Starting upload ${id} for files ${fileIDs}`);
-});
-```
+**Parameters**
+
+- `uploadID` (`string)`
+- `files` (`UppyFile<M,B>`)
 
 #### `preprocess-progress`
 
@@ -1097,6 +1093,15 @@ uppy.on('upload-progress', (file, progress) => {
 });
 ```
 
+#### `upload-pause`
+
+Fired when an individual upload is (un)paused.
+
+**Parameters**
+
+- `file` (`UppyFile<M,B>`)
+- `isPaused` (`boolean`)
+
 #### `postprocess-progress`
 
 Progress of the post-processors.
@@ -1236,7 +1241,7 @@ the `retry-all` event instead.
 
 **Parameters**
 
-- `fileID` - ID of the file that is being retried.
+- `file` (`UppyFile<M,B>`)
 
 **Example**
 
@@ -1272,7 +1277,7 @@ Fired when all failed uploads are retried
 
 **Parameters**
 
-- `fileIDs` - Arrays of IDs of the files being retried.
+- `files` (`UppyFile<M,B>[]`)
 
 **Example**
 

+ 2 - 2
packages/@uppy/companion-client/src/RequestClient.ts

@@ -582,10 +582,10 @@ export default class RequestClient<M extends Meta, B extends Body> {
         }
 
         const onFilePausedChange = (
-          targetFileId: string | undefined,
+          targetFile: UppyFile<M, B> | undefined,
           newPausedState: boolean,
         ) => {
-          if (targetFileId !== file.id) return
+          if (targetFile?.id !== file.id) return
           pause(newPausedState)
         }
 

+ 6 - 6
packages/@uppy/core/src/EventManager.ts

@@ -48,8 +48,8 @@ export default class EventManager<M extends Meta, B extends Body> {
     fileID: UppyFile<M, B>['id'],
     cb: (isPaused: boolean) => void,
   ): void {
-    this.on('upload-pause', (targetFileID, isPaused) => {
-      if (fileID === targetFileID) {
+    this.on('upload-pause', (file, isPaused) => {
+      if (fileID === file?.id) {
         cb(isPaused)
       }
     })
@@ -65,8 +65,8 @@ export default class EventManager<M extends Meta, B extends Body> {
   }
 
   onPause(fileID: UppyFile<M, B>['id'], cb: (isPaused: boolean) => void): void {
-    this.on('upload-pause', (targetFileID, isPaused) => {
-      if (fileID === targetFileID) {
+    this.on('upload-pause', (file, isPaused) => {
+      if (fileID === file?.id) {
         // const isPaused = this.#uppy.pauseResume(fileID)
         cb(isPaused)
       }
@@ -74,8 +74,8 @@ export default class EventManager<M extends Meta, B extends Body> {
   }
 
   onRetry(fileID: UppyFile<M, B>['id'], cb: () => void): void {
-    this.on('upload-retry', (targetFileID) => {
-      if (fileID === targetFileID) {
+    this.on('upload-retry', (file) => {
+      if (fileID === file?.id) {
         cb()
       }
     })

+ 10 - 15
packages/@uppy/core/src/Uppy.ts

@@ -266,13 +266,13 @@ export interface _UppyEventMap<M extends Meta, B extends Body> {
   'restore-canceled': () => void
   'restriction-failed': (file: UppyFile<M, B> | undefined, error: Error) => void
   'resume-all': () => void
-  'retry-all': (fileIDs: string[]) => void
+  'retry-all': (files: UppyFile<M, B>[]) => void
   'state-update': (
     prevState: State<M, B>,
     nextState: State<M, B>,
     patch?: Partial<State<M, B>>,
   ) => void
-  upload: (data: { id: string; fileIDs: string[] }) => void
+  upload: (uploadID: string, files: UppyFile<M, B>[]) => void
   'upload-error': (
     file: UppyFile<M, B> | undefined,
     error: { name: string; message: string; details?: string },
@@ -280,15 +280,12 @@ export interface _UppyEventMap<M extends Meta, B extends Body> {
       | Omit<NonNullable<UppyFile<M, B>['response']>, 'uploadURL'>
       | undefined,
   ) => void
-  'upload-pause': (
-    fileID: UppyFile<M, B>['id'] | undefined,
-    isPaused: boolean,
-  ) => void
+  'upload-pause': (file: UppyFile<M, B> | undefined, isPaused: boolean) => void
   'upload-progress': (
     file: UppyFile<M, B> | undefined,
     progress: FileProgressStarted,
   ) => void
-  'upload-retry': (fileID: string) => void
+  'upload-retry': (file: UppyFile<M, B>) => void
   'upload-stalled': (
     error: { message: string; details?: string },
     files: UppyFile<M, B>[],
@@ -1217,14 +1214,15 @@ export class Uppy<M extends Meta, B extends Body> {
       return undefined
     }
 
-    const wasPaused = this.getFile(fileID).isPaused || false
+    const file = this.getFile(fileID)
+    const wasPaused = file.isPaused || false
     const isPaused = !wasPaused
 
     this.setFileState(fileID, {
       isPaused,
     })
 
-    this.emit('upload-pause', fileID, isPaused)
+    this.emit('upload-pause', file, isPaused)
 
     return isPaused
   }
@@ -1288,7 +1286,7 @@ export class Uppy<M extends Meta, B extends Body> {
       error: null,
     })
 
-    this.emit('retry-all', filesToRetry)
+    this.emit('retry-all', Object.values(updatedFiles))
 
     if (filesToRetry.length === 0) {
       return Promise.resolve({
@@ -1323,7 +1321,7 @@ export class Uppy<M extends Meta, B extends Body> {
       isPaused: false,
     })
 
-    this.emit('upload-retry', fileID)
+    this.emit('upload-retry', this.getFile(fileID))
 
     const uploadID = this.#createUpload([fileID], {
       forceAllowNewUpload: true, // create new upload even if allowNewUpload: false
@@ -1952,10 +1950,7 @@ export class Uppy<M extends Meta, B extends Body> {
 
     const uploadID = nanoid()
 
-    this.emit('upload', {
-      id: uploadID,
-      fileIDs,
-    })
+    this.emit('upload', uploadID, this.getFilesByIds(fileIDs))
 
     this.setState({
       allowNewUpload: