docs.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. class app.collections.Docs extends app.Collection
  2. @model: 'Doc'
  3. findBySlug: (slug) ->
  4. @findBy('slug', slug) or @findBy('slug_without_version', slug)
  5. NORMALIZE_VERSION_RGX = /\.(\d)$/
  6. NORMALIZE_VERSION_SUB = '.0$1'
  7. sort: ->
  8. @models.sort (a, b) ->
  9. if a.name is b.name
  10. if not a.version or a.version.replace(NORMALIZE_VERSION_RGX, NORMALIZE_VERSION_SUB) > b.version.replace(NORMALIZE_VERSION_RGX, NORMALIZE_VERSION_SUB)
  11. -1
  12. else
  13. 1
  14. else if a.name.toLowerCase() > b.name.toLowerCase()
  15. 1
  16. else
  17. -1
  18. # Load models concurrently.
  19. # It's not pretty but I didn't want to import a promise library only for this.
  20. CONCURRENCY = 3
  21. load: (onComplete, onError, options) ->
  22. i = 0
  23. next = =>
  24. if i < @models.length
  25. @models[i].load(next, fail, options)
  26. else if i is @models.length + CONCURRENCY - 1
  27. onComplete()
  28. i++
  29. return
  30. fail = (args...) ->
  31. if onError
  32. onError(args...)
  33. onError = null
  34. next()
  35. return
  36. next() for [0...CONCURRENCY]
  37. return
  38. clearCache: ->
  39. doc.clearCache() for doc in @models
  40. return
  41. uninstall: (callback) ->
  42. i = 0
  43. next = =>
  44. if i < @models.length
  45. @models[i++].uninstall(next, next)
  46. else
  47. callback()
  48. return
  49. next()
  50. return
  51. getInstallStatuses: (callback) ->
  52. app.db.versions @models, (statuses) ->
  53. if statuses
  54. for key, value of statuses
  55. statuses[key] = installed: !!value, mtime: value
  56. callback(statuses)
  57. return
  58. return
  59. checkForUpdates: (callback) ->
  60. @getInstallStatuses (statuses) =>
  61. i = 0
  62. if statuses
  63. i += 1 for slug, status of statuses when @findBy('slug', slug).isOutdated(status)
  64. callback(i)
  65. return
  66. return
  67. updateInBackground: ->
  68. @getInstallStatuses (statuses) =>
  69. return unless statuses
  70. for slug, status of statuses
  71. doc = @findBy 'slug', slug
  72. doc.install($.noop, $.noop) if doc.isOutdated(status)
  73. return
  74. return