Skip to content

Commit

Permalink
@uppy/core: pass file to events consistently (#5136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon authored May 29, 2024
1 parent a030861 commit 453eb0f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
25 changes: 15 additions & 10 deletions docs/uppy-core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1236,7 +1241,7 @@ the `retry-all` event instead.
**Parameters**
- `fileID` - ID of the file that is being retried.
- `file` (`UppyFile<M,B>`)
**Example**
Expand Down Expand Up @@ -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**
Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/companion-client/src/RequestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 6 additions & 6 deletions packages/@uppy/core/src/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand All @@ -65,17 +65,17 @@ 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)
}
})
}

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()
}
})
Expand Down
25 changes: 10 additions & 15 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,29 +266,26 @@ 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 },
response?:
| 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>[],
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 453eb0f

Please sign in to comment.