docs.coffee 1.8 KB

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