Guest User

Untitled

a guest
Nov 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. constructor(opts) {
  2. ...
  3. this.queue = []
  4. this.queueProcessing = false
  5. ...
  6. }
  7.  
  8. actions () {
  9. ...
  10. this.on('core:file-added', (file) => {
  11. this.addToPreviewQueue(file)
  12. })
  13. ...
  14. }
  15.  
  16. addToPreviewQueue (file) {
  17. this.queue.push(file)
  18. if (this.queueProcessing === false) {
  19. this.processPreviewQueue()
  20. }
  21. }
  22.  
  23. generatePreview (file) {
  24. return new Promise((resolve, reject) => {
  25. if (Utils.isPreviewSupported(file.type) && !file.isRemote) {
  26. Utils.createThumbnail(file, 200).then((thumbnail) => {
  27. this.setPreviewURL(file.id, thumbnail)
  28. resolve()
  29. }).catch(function (err) {
  30. console.warn(err.stack || err.message)
  31. reject(err)
  32. })
  33. } else {
  34. reject(new Error('no preview'))
  35. }
  36. })
  37. }
  38.  
  39. processPreviewQueue () {
  40. if (this.queue.length > 0) {
  41. this.queueProcessing = true
  42. const currentFile = this.queue.shift()
  43. this.generatePreview(currentFile).catch(() => {
  44. }).then(() => {
  45. if (this.queue.length > 0) {
  46. this.processPreviewQueue()
  47. } else {
  48. this.queueProcessing = false
  49. }
  50. })
  51. }
  52. }
Add Comment
Please, Sign In to add comment