Skip to content

Commit

Permalink
@uppy/core: remove reason (#5159)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed May 23, 2024
1 parent 67af5e9 commit f72138f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 76 deletions.
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 @@ -838,11 +838,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 @@ -1137,7 +1135,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 @@ -1197,7 +1195,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 @@ -1207,8 +1205,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 @@ -1305,21 +1303,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 @@ -1818,12 +1813,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
14 changes: 3 additions & 11 deletions packages/@uppy/transloadit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,20 +441,14 @@ export default class Transloadit<
// TODO: this should not live inside a `file-removed` event but somewhere more deterministic.
// Such as inside the function where the assembly has succeeded or cancelled.
// For the use case of cancelling the assembly when needed, we should try to do that with just `cancel-all`.
const fileRemovedHandler = (
fileRemoved: UppyFile<M, B>,
reason?: string,
) => {
const fileRemovedHandler = (fileRemoved: UppyFile<M, B>) => {
// If the assembly has successfully completed, we do not need these checks.
// Otherwise we may cancel an assembly after it already succeeded
if (assembly.status?.ok === 'ASSEMBLY_COMPLETED') {
this.uppy.off('file-removed', fileRemovedHandler)
return
}
if (reason === 'cancel-all') {
assembly.close()
this.uppy.off('file-removed', fileRemovedHandler)
} else if (fileRemoved.id in updatedFiles) {
if (fileRemoved.id in updatedFiles) {
delete updatedFiles[fileRemoved.id]
const nbOfRemainingFiles = Object.keys(updatedFiles).length

Expand Down Expand Up @@ -667,10 +661,8 @@ export default class Transloadit<
/**
* When all files are removed, cancel in-progress Assemblies.
*/
#onCancelAll = async ({ reason }: { reason?: string } = {}) => {
#onCancelAll = async () => {
try {
if (reason !== 'user') return

const { uploadsAssemblies } = this.getPluginState()
const assemblyIDs = Object.values(uploadsAssemblies).flat(1)
const assemblies = assemblyIDs.map((assemblyID) =>
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

0 comments on commit f72138f

Please sign in to comment.