Created
November 23, 2017 09:40
-
-
Save richardwillars/7eb86220330068dd184670c690bb4c4b to your computer and use it in GitHub Desktop.
Sequential thumbnail generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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