Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/core: remove reason #5159

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions docs/uppy-core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,7 @@ Retry an upload (after an error, for example).

Retry all uploads (after an error, for example).

#### `cancelAll({ reason: 'user' })`

| Argument | Type | Description |
| -------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `reason` | `string` | The reason for canceling. Plugins can use this to provide different cleanup behavior (Transloadit plugin cancels an Assembly if user clicked on the “cancel” button). Possible values are: `user` (default) - The user has pressed “cancel”; `unmount` - The Uppy instance has been closed programmatically |
#### `cancelAll()`

Cancel all uploads, reset progress and remove all files.

Expand Down Expand Up @@ -874,11 +870,7 @@ uppy.getPlugin('Dashboard').setOptions({
});
```

#### `close({ reason: 'user' })`

| Argument | Type | Description |
| -------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `reason` | `string` | The reason for canceling. Plugins can use this to provide different cleanup behavior (Transloadit plugin cancels an Assembly if user clicked on the “cancel” button). Possible values are: `user` (default) - The user has pressed “cancel”; `unmount` - The Uppy instance has been closed programmatically |
#### `destroy()`

Uninstall all plugins and close down this Uppy instance. Also runs
`uppy.cancelAll()` before uninstalling.
Expand Down Expand Up @@ -1024,25 +1016,19 @@ Fired each time a file is removed.
**Parameters**

- `file` - The [Uppy file](#working-with-uppy-files) that was removed.
- `reason` - A string explaining why the file was removed. See
[#2301](https://github.com/transloadit/uppy/issues/2301#issue-628931176) for
details. Current reasons are: `removed-by-user` and `cancel-all`.

**Example**

```js
uppy.on('file-removed', (file, reason) => {
uppy.on('file-removed', (file) => {
console.log('Removed file', file);
});
```

```js
uppy.on('file-removed', (file, reason) => {
uppy.on('file-removed', (file) => {
removeFileFromUploadingCounterUI(file);

if (reason === 'removed-by-user') {
sendDeleteRequestForFile(file);
}
sendDeleteRequestForFile(file);
});
```

Expand Down Expand Up @@ -1322,10 +1308,6 @@ Fired when “info” message should be hidden in the UI. See

#### `cancel-all`

| Argument | Type | Description |
| -------- | -------- | ----------------------------------- |
| `reason` | `string` | See [uppy.cancelAll](####cancelAll) |

Fired when `cancelAll()` is called, all uploads are canceled, files removed and
progress is reset.

Expand Down
8 changes: 3 additions & 5 deletions packages/@uppy/aws-s3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,9 @@ export default class AwsS3Multipart<
resolve(`upload ${removed} was removed`)
})

eventManager.onCancelAll(file.id, (options) => {
if (options?.reason === 'user') {
upload.abort()
this.resetUploaderReferences(file.id, { abort: true })
}
eventManager.onCancelAll(file.id, () => {
upload.abort()
this.resetUploaderReferences(file.id, { abort: true })
resolve(`upload ${file.id} was canceled`)
})

Expand Down
6 changes: 2 additions & 4 deletions packages/@uppy/companion-client/src/RequestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,8 @@ export default class RequestClient<M extends Meta, B extends Body> {
resolve()
}

const onCancelAll = ({ reason }: { reason?: string }) => {
if (reason === 'user') {
socketSend('cancel')
}
const onCancelAll = () => {
socketSend('cancel')
socketAbortController?.abort?.()
this.uppy.log(`upload ${file.id} was canceled`, 'info')
resolve()
Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/core/src/Uppy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ describe('src/Core', () => {
core.cancelAll()

expect(coreCancelEventMock).toHaveBeenCalledWith(
{ reason: 'user' },
undefined,
undefined,
undefined,
undefined,
Expand Down Expand Up @@ -555,7 +555,7 @@ describe('src/Core', () => {
core.destroy()

expect(coreCancelEventMock).toHaveBeenCalledWith(
{ reason: 'user' },
undefined,
undefined,
undefined,
undefined,
Expand Down
39 changes: 17 additions & 22 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ type Processor = (
uploadID: string,
) => Promise<unknown> | void

type FileRemoveReason = 'user' | 'cancel-all' | 'unmount'

type LogLevel = 'info' | 'warning' | 'error' | 'success'

export type UnknownPlugin<
Expand Down Expand Up @@ -228,15 +226,15 @@ export type NonNullableUppyOptions<M extends Meta, B extends Body> = Required<

export interface _UppyEventMap<M extends Meta, B extends Body> {
'back-online': () => void
'cancel-all': (reason: { reason?: FileRemoveReason }) => void
'cancel-all': () => void
complete: (result: UploadResult<M, B>) => void
error: (
error: { name: string; message: string; details?: string },
file?: UppyFile<M, B>,
response?: UppyFile<M, B>['response'],
) => void
'file-added': (file: UppyFile<M, B>) => void
'file-removed': (file: UppyFile<M, B>, reason?: FileRemoveReason) => void
'file-removed': (file: UppyFile<M, B>) => void
'files-added': (files: UppyFile<M, B>[]) => void
'info-hidden': () => void
'info-visible': () => void
Expand Down Expand Up @@ -1115,7 +1113,7 @@ export class Uppy<M extends Meta, B extends Body> {
}
}

removeFiles(fileIDs: string[], reason?: FileRemoveReason): void {
removeFiles(fileIDs: string[]): void {
const { files, currentUploads } = this.getState()
const updatedFiles = { ...files }
const updatedUploads = { ...currentUploads }
Expand Down Expand Up @@ -1175,7 +1173,7 @@ export class Uppy<M extends Meta, B extends Body> {

const removedFileIDs = Object.keys(removedFiles)
removedFileIDs.forEach((fileID) => {
this.emit('file-removed', removedFiles[fileID], reason)
this.emit('file-removed', removedFiles[fileID])
})

if (removedFileIDs.length > 5) {
Expand All @@ -1185,8 +1183,8 @@ export class Uppy<M extends Meta, B extends Body> {
}
}

removeFile(fileID: string, reason?: FileRemoveReason): void {
this.removeFiles([fileID], reason)
removeFile(fileID: string): void {
this.removeFiles([fileID])
}

pauseResume(fileID: string): boolean | undefined {
Expand Down Expand Up @@ -1283,21 +1281,18 @@ export class Uppy<M extends Meta, B extends Body> {
return this.#runUpload(uploadID)
}

cancelAll({ reason = 'user' }: { reason?: FileRemoveReason } = {}): void {
this.emit('cancel-all', { reason })

// Only remove existing uploads if user is canceling
if (reason === 'user') {
const { files } = this.getState()
cancelAll(): void {
this.emit('cancel-all')

const fileIDs = Object.keys(files)
if (fileIDs.length) {
this.removeFiles(fileIDs, 'cancel-all')
}
const { files } = this.getState()

this.setState(defaultUploadState)
// todo should we call this.emit('reset-progress') like we do for resetProgress?
const fileIDs = Object.keys(files)
if (fileIDs.length) {
this.removeFiles(fileIDs)
}

this.setState(defaultUploadState)
// todo should we call this.emit('reset-progress') like we do for resetProgress?
}

retryUpload(fileID: string): Promise<UploadResult<M, B> | undefined> {
Expand Down Expand Up @@ -1796,12 +1791,12 @@ export class Uppy<M extends Meta, B extends Body> {
/**
* Uninstall all plugins and close down this Uppy instance.
*/
destroy({ reason }: { reason?: FileRemoveReason } | undefined = {}): void {
destroy(): void {
this.log(
`Closing Uppy instance ${this.opts.id}: removing all files and uninstalling plugins`,
)

this.cancelAll({ reason })
this.cancelAll()

this.#storeUnsubscribe()

Expand Down
8 changes: 3 additions & 5 deletions packages/@uppy/tus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,9 @@ export default class Tus<M extends Meta, B extends Body> extends BasePlugin<
upload.abort()
})

eventManager.onCancelAll(file.id, ({ reason } = {}) => {
if (reason === 'user') {
queuedRequest.abort()
this.resetUploaderReferences(file.id, { abort: !!upload.url })
}
eventManager.onCancelAll(file.id, () => {
queuedRequest.abort()
this.resetUploaderReferences(file.id, { abort: !!upload.url })
resolve(`upload ${file.id} was canceled`)
})

Expand Down
6 changes: 2 additions & 4 deletions packages/@uppy/xhr-upload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,8 @@ export default class XHRUpload<
})

events.onFileRemove(file.id, () => controller.abort())
events.onCancelAll(file.id, ({ reason }) => {
if (reason === 'user') {
controller.abort()
}
events.onCancelAll(file.id, () => {
controller.abort()
})

try {
Expand Down