Skip to content
This repository has been archived by the owner on Nov 19, 2018. It is now read-only.

Commit

Permalink
fix(callback-proxy): callback values not passed through to Fine Uploader
Browse files Browse the repository at this point in the history
fixes #5

Also:
feat(callback-names): onUpload and onUploadChunk now accept promises
re: FineUploader/fine-uploader#1917
  • Loading branch information
rnicholus authored Sep 28, 2017
1 parent 92a5695 commit 31df221
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fine-uploader-wrappers",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"description": "Fine Uploader core ES6 class wrappers that provide additional features.",
"author": {
Expand Down
2 changes: 2 additions & 0 deletions src/callback-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const thenable = [
'onPasteReceived',
'onSubmit',
'onSubmitDelete',
'onUpload',
'onUploadChunk',
'onValidate',
'onValidateBatch'
]
Expand Down
24 changes: 19 additions & 5 deletions src/callback-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ const executeThenableCallbacks = ({ registeredCallbacks, originalCallbackArgumen
return Promise.resolve()
}

const executeThenableCallback = ({ registeredCallbacks, originalCallbackArguments} ) => {

const getResultToPass = ({ newResult, previousResult }) => {
if (newResult !== null && (typeof newResult) === 'object') {
return objectAssign({}, previousResult || {}, newResult)
}
else {
return newResult || previousResult
}
}

const executeThenableCallback = ({ registeredCallbacks, originalCallbackArguments, previousResult } ) => {
return new Promise((resolve, reject) => {
const callback = registeredCallbacks.pop()

Expand All @@ -80,12 +90,14 @@ const executeThenableCallback = ({ registeredCallbacks, originalCallbackArgument
if (returnValue && returnValue.then) {
returnValue
.then(result => {
const resultToPass = getResultToPass({ newResult: result, previousResult })

if (registeredCallbacks.length) {
executeThenableCallback({ registeredCallbacks, originalCallbackArguments })
executeThenableCallback({ registeredCallbacks, originalCallbackArguments, previousResult: resultToPass })
.then(resolve, reject)
}
else {
resolve(result)
resolve(resultToPass)
}
})
.catch(error => reject(error))
Expand All @@ -94,12 +106,14 @@ const executeThenableCallback = ({ registeredCallbacks, originalCallbackArgument
reject()
}
else {
const resultToPass = getResultToPass({ newResult: returnValue, previousResult })

if (registeredCallbacks.length) {
executeThenableCallback({ registeredCallbacks, originalCallbackArguments })
executeThenableCallback({ registeredCallbacks, originalCallbackArguments, previousResult: resultToPass })
.then(resolve, reject)
}
else {
resolve()
resolve(resultToPass)
}
}
})
Expand Down
98 changes: 98 additions & 0 deletions src/test/unit/base-wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,104 @@ describe('Fine Uploader wrapper classes', () => {
}, 100)
})

it('passes aggregated resolved Promise objects from all callbacks to Fine Uploader - all callbacks return promises and objects', done => {
let onSubmitResult

const wrapper = new FineUploaderTraditional({
options: {
autoUpload: false,
callbacks: {
onSubmit: function() {
return Promise.resolve({ callbackOne: 'hi' })
}
}
}
})

wrapper.on('submit', () => {
return Promise.resolve({ callbackTwo: 'ho' })
})

wrapper.methods._onSubmitCallbackSuccess = (id, name, result) => {
onSubmitResult = result
}

wrapper.methods.addFiles(sampleBlobWrapper)

setTimeout(() => {
expect(onSubmitResult).toEqual({
callbackOne: 'hi',
callbackTwo: 'ho'
})

done()
}, 100)
})

it('passes aggregated resolved Promise objects from all callbacks to Fine Uploader - all callbacks return promises, only some return objects', done => {
let onSubmitResult

const wrapper = new FineUploaderTraditional({
options: {
autoUpload: false,
callbacks: {
onSubmit: function() {
return Promise.resolve()
}
}
}
})

wrapper.on('submit', () => {
return Promise.resolve({ callbackTwo: 'ho' })
})

wrapper.methods._onSubmitCallbackSuccess = (id, name, result) => {
onSubmitResult = result
}

wrapper.methods.addFiles(sampleBlobWrapper)

setTimeout(() => {
expect(onSubmitResult).toEqual({
callbackTwo: 'ho'
})

done()
}, 100)
})

it('passes aggregated resolved Promise objects from all callbacks to Fine Uploader - only some callbacks return promises and objects', done => {
let onSubmitResult

const wrapper = new FineUploaderTraditional({
options: {
autoUpload: false,
callbacks: {
onSubmit: function() {}
}
}
})

wrapper.on('submit', () => {
return Promise.resolve({ callbackTwo: 'ho' })
})

wrapper.methods._onSubmitCallbackSuccess = (id, name, result) => {
onSubmitResult = result
}

wrapper.methods.addFiles(sampleBlobWrapper)

setTimeout(() => {
expect(onSubmitResult).toEqual({
callbackTwo: 'ho'
})

done()
}, 100)
})

it('allows callback handlers to be removed', done => {
const handler = () => callbacksHit++
let callbacksHit = 0
Expand Down

0 comments on commit 31df221

Please sign in to comment.