Skip to content

Instantly share code, notes, and snippets.

@richardwillars
Created November 23, 2017 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardwillars/7eb86220330068dd184670c690bb4c4b to your computer and use it in GitHub Desktop.
Save richardwillars/7eb86220330068dd184670c690bb4c4b to your computer and use it in GitHub Desktop.
Sequential thumbnail generation
constructor(opts) {
...
this.queue = []
this.queueProcessing = false
...
}
actions () {
...
this.on('core:file-added', (file) => {
this.addToPreviewQueue(file)
})
...
}
addToPreviewQueue (file) {
this.queue.push(file)
if (this.queueProcessing === false) {
this.processPreviewQueue()
}
}
generatePreview (file) {
return new Promise((resolve, reject) => {
if (Utils.isPreviewSupported(file.type) && !file.isRemote) {
Utils.createThumbnail(file, 200).then((thumbnail) => {
this.setPreviewURL(file.id, thumbnail)
resolve()
}).catch(function (err) {
console.warn(err.stack || err.message)
reject(err)
})
} else {
reject(new Error('no preview'))
}
})
}
processPreviewQueue () {
if (this.queue.length > 0) {
this.queueProcessing = true
const currentFile = this.queue.shift()
this.generatePreview(currentFile).catch(() => {
}).then(() => {
if (this.queue.length > 0) {
this.processPreviewQueue()
} else {
this.queueProcessing = false
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment