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

proposal: Cancel assemblies optional #3575

Merged
merged 16 commits into from
May 5, 2022
9 changes: 5 additions & 4 deletions packages/@uppy/core/src/Uppy.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,8 @@ class Uppy {
return this.#runUpload(uploadID)
}

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

const { files } = this.getState()

Expand Down Expand Up @@ -838,6 +838,7 @@ class Uppy {
return this.#runUpload(uploadID)
}

// todo what is the point of the reset method when we have cancelAll or vice versa?
mifi marked this conversation as resolved.
Show resolved Hide resolved
reset () {
this.cancelAll()
}
Expand Down Expand Up @@ -1234,10 +1235,10 @@ class Uppy {
/**
* Uninstall all plugins and close down this Uppy instance.
*/
close () {
close ({ reason } = {}) {
this.log(`Closing Uppy instance ${this.opts.id}: removing all files and uninstalling plugins`)

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

this.#storeUnsubscribe()

Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/react/src/useUppy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function useUppy (factory) {

useEffect(() => {
return () => {
uppy.current.close()
uppy.current.close({ reason: 'unmount' })
}
}, [])

Expand Down
19 changes: 9 additions & 10 deletions packages/@uppy/transloadit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,18 @@ module.exports = class Transloadit extends BasePlugin {
/**
* When all files are removed, cancel in-progress Assemblies.
*/
#onCancelAll = () => {
const { uploadsAssemblies } = this.getPluginState()

const assemblyIDs = Object.values(uploadsAssemblies).flat(1)
#onCancelAll = async ({ reason } = {}) => {
try {
if (reason !== 'user') return

const cancelPromises = assemblyIDs.map((assemblyID) => {
const assembly = this.getAssembly(assemblyID)
return this.#cancelAssembly(assembly)
})
const { uploadsAssemblies } = this.getPluginState()
const assemblyIDs = Object.values(uploadsAssemblies).flat(1)
const assemblies = assemblyIDs.map((assemblyID) => this.getAssembly(assemblyID))

Promise.all(cancelPromises).catch((err) => {
await Promise.all(assemblies.map((assembly) => this.#cancelAssembly(assembly)))
} catch (err) {
this.uppy.log(err)
})
}
}

/**
Expand Down
10 changes: 4 additions & 6 deletions website/src/docs/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,6 @@ Retry an upload (after an error, for example).

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

### `uppy.cancelAll()`

Cancel all uploads, reset progress and remove all files.

### `uppy.setState(patch)`

Update Uppy’s internal state. Usually, this method is called internally, but in some cases it might be useful to alter something directly, especially when implementing your own plugins.
Expand Down Expand Up @@ -673,14 +669,16 @@ uppy.getPlugin('Dashboard').setOptions({
})
```

### `uppy.reset()`
### `uppy.reset()` (alias `uppy.cancelAll()`)

Stop all uploads in progress and clear file selection, set progress to 0. More or less, it returns things to the way they were before any user input.

### `uppy.close()`
### `uppy.close({ reason = 'user' })`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also be added for uppy.cancelAll?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch


Uninstall all plugins and close down this Uppy instance. Also runs `uppy.reset()` before uninstalling.

* `reason`: If set to the string `user`, it will also cancel any running Transloadit assemblies. Set to `null` to disable this behavior.
mifi marked this conversation as resolved.
Show resolved Hide resolved

### `uppy.logout()`

Calls `provider.logout()` on each remote provider plugin (Google Drive, Instagram, etc). Useful, for example, after your users log out of their account in your app — this will clean things up with Uppy cloud providers as well, for extra security.
Expand Down