mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-23 02:08:30 +00:00
Add addResultData() instead of returning result data from upload hooks
This commit is contained in:
parent
8092230c6c
commit
a169c2ed52
5 changed files with 48 additions and 48 deletions
3
package-lock.json
generated
3
package-lock.json
generated
|
|
@ -8521,7 +8521,8 @@
|
|||
"is-plain-obj": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
|
||||
"integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4="
|
||||
"integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4=",
|
||||
"dev": true
|
||||
},
|
||||
"is-port-reachable": {
|
||||
"version": "1.0.0",
|
||||
|
|
|
|||
|
|
@ -104,7 +104,6 @@
|
|||
"drag-drop": "2.13.2",
|
||||
"es6-promise": "^4.2.2",
|
||||
"get-form-data": "^2.0.0",
|
||||
"is-plain-obj": "^1.1.0",
|
||||
"lodash.throttle": "4.1.1",
|
||||
"mime-match": "^1.0.2",
|
||||
"namespace-emitter": "^2.0.0",
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ const Utils = require('../core/Utils')
|
|||
const Translator = require('../core/Translator')
|
||||
const ee = require('namespace-emitter')
|
||||
const cuid = require('cuid')
|
||||
const isPlainObject = require('is-plain-obj')
|
||||
const throttle = require('lodash.throttle')
|
||||
const prettyBytes = require('prettier-bytes')
|
||||
const match = require('mime-match')
|
||||
|
|
@ -968,7 +967,8 @@ class Uppy {
|
|||
currentUploads: Object.assign({}, this.getState().currentUploads, {
|
||||
[uploadID]: {
|
||||
fileIDs: fileIDs,
|
||||
step: 0
|
||||
step: 0,
|
||||
result: {}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
@ -976,6 +976,28 @@ class Uppy {
|
|||
return uploadID
|
||||
}
|
||||
|
||||
_getUpload (uploadID) {
|
||||
return this.getState().currentUploads[uploadID]
|
||||
}
|
||||
|
||||
/**
|
||||
* Add data to an upload's result object.
|
||||
*
|
||||
* @param {string} uploadID The ID of the upload.
|
||||
* @param {object} data Data properties to add to the result object.
|
||||
*/
|
||||
addResultData (uploadID, data) {
|
||||
const currentUploads = this.getState().currentUploads
|
||||
const currentUpload = Object.assign({}, currentUploads[uploadID], {
|
||||
result: Object.assign({}, currentUploads[uploadID].result, data)
|
||||
})
|
||||
this.setState({
|
||||
currentUploads: Object.assign({}, currentUploads, {
|
||||
[uploadID]: currentUpload
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an upload, eg. if it has been canceled or completed.
|
||||
*
|
||||
|
|
@ -1000,8 +1022,6 @@ class Uppy {
|
|||
const fileIDs = uploadData.fileIDs
|
||||
const restoreStep = uploadData.step
|
||||
|
||||
const resultData = {}
|
||||
|
||||
const steps = [
|
||||
...this.preProcessors,
|
||||
...this.uploaders,
|
||||
|
|
@ -1028,9 +1048,6 @@ class Uppy {
|
|||
// Otherwise when more metadata may be added to the upload this would keep getting more parameters
|
||||
return fn(fileIDs, uploadID)
|
||||
}).then((result) => {
|
||||
if (result && isPlainObject(result)) {
|
||||
Object.assign(resultData, result)
|
||||
}
|
||||
return null
|
||||
})
|
||||
})
|
||||
|
|
@ -1047,15 +1064,17 @@ class Uppy {
|
|||
const files = fileIDs.map((fileID) => this.getFile(fileID))
|
||||
const successful = files.filter((file) => file && !file.error)
|
||||
const failed = files.filter((file) => file && file.error)
|
||||
Object.assign(resultData, { successful, failed })
|
||||
this.addResultData(uploadID, { successful, failed })
|
||||
|
||||
this.emit('complete', resultData)
|
||||
const { currentUploads } = this.getState()
|
||||
const result = currentUploads[uploadID].result
|
||||
this.emit('complete', result)
|
||||
// Compatibility with pre-0.21
|
||||
this.emit('success', fileIDs)
|
||||
|
||||
this._removeUpload(uploadID)
|
||||
|
||||
return resultData
|
||||
return result
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -273,9 +273,15 @@ describe('src/Core', () => {
|
|||
describe('upload hooks', () => {
|
||||
it('should add data returned from upload hooks to the .upload() result', () => {
|
||||
const core = new Core()
|
||||
core.addPreProcessor(() => Promise.resolve({ pre: 'ok' }))
|
||||
core.addPostProcessor(() => Promise.resolve({ post: 'ok' }))
|
||||
core.addUploader(() => Promise.resolve({ upload: 'ok' }))
|
||||
core.addPreProcessor((fileIDs, uploadID) => {
|
||||
core.addResultData(uploadID, { pre: 'ok' })
|
||||
})
|
||||
core.addPostProcessor((fileIDs, uploadID) => {
|
||||
core.addResultData(uploadID, { post: 'ok' })
|
||||
})
|
||||
core.addUploader((fileIDs, uploadID) => {
|
||||
core.addResultData(uploadID, { upload: 'ok' })
|
||||
})
|
||||
core.run()
|
||||
return core.upload().then((result) => {
|
||||
expect(result.pre).toBe('ok')
|
||||
|
|
@ -1143,39 +1149,11 @@ describe('src/Core', () => {
|
|||
const currentUploadsState = {}
|
||||
currentUploadsState[uploadId] = {
|
||||
fileIDs: Object.keys(core.state.files),
|
||||
step: 0
|
||||
step: 0,
|
||||
result: {}
|
||||
}
|
||||
expect(core.state.currentUploads).toEqual(currentUploadsState)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('removeUpload', () => {
|
||||
it('should remove all files from the specified upload', () => {
|
||||
// this uploader will run once the upload has started
|
||||
const uploader = () => {
|
||||
return Promise.resolve().then(() => {
|
||||
const uploadId = Object.keys(core.state.currentUploads)[0]
|
||||
expect(typeof core.state.currentUploads[uploadId]).toEqual('object')
|
||||
expect(core.state.currentUploads[uploadId].fileIDs.length).toEqual(1)
|
||||
core._removeUpload(uploadId)
|
||||
expect(typeof core.state.currentUploads[uploadId]).toEqual('undefined')
|
||||
})
|
||||
}
|
||||
|
||||
const core = new Core()
|
||||
core.run()
|
||||
core.addUploader(uploader)
|
||||
return core
|
||||
.addFile({
|
||||
source: 'jest',
|
||||
name: 'foo.jpg',
|
||||
type: 'image/jpeg',
|
||||
data: utils.dataURItoFile(sampleImageDataURI, {})
|
||||
})
|
||||
.then(() => {
|
||||
return core.upload(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -610,13 +610,15 @@ module.exports = class Transloadit extends Plugin {
|
|||
socket.close()
|
||||
})
|
||||
const assemblies = assemblyIDs.map((id) => this.getAssembly(id))
|
||||
return Promise.resolve({ transloadit: assemblies })
|
||||
this.uppy.addResultData(uploadID, { transloadit: assemblies })
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
// If no assemblies were created for this upload, we also do not have to wait.
|
||||
// There's also no sockets or anything to close, so just return immediately.
|
||||
if (assemblyIDs.length === 0) {
|
||||
return Promise.resolve({ transloadit: [] })
|
||||
this.uppy.addResultData(uploadID, { transloadit: [] })
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
let finishedAssemblies = 0
|
||||
|
|
@ -689,7 +691,8 @@ module.exports = class Transloadit extends Plugin {
|
|||
// We're done, these listeners can be removed
|
||||
removeListeners()
|
||||
const assemblies = assemblyIDs.map((id) => this.getAssembly(id))
|
||||
resolve({ transloadit: assemblies })
|
||||
this.uppy.addResultData(uploadID, { transloadit: assemblies })
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue