docs.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS202: Simplify dynamic range loops
  8. * DS206: Consider reworking classes to avoid initClass
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  10. */
  11. (function() {
  12. let NORMALIZE_VERSION_RGX = undefined;
  13. let NORMALIZE_VERSION_SUB = undefined;
  14. let CONCURRENCY = undefined;
  15. const Cls = (app.collections.Docs = class Docs extends app.Collection {
  16. static initClass() {
  17. this.model = 'Doc';
  18. NORMALIZE_VERSION_RGX = /\.(\d)$/;
  19. NORMALIZE_VERSION_SUB = '.0$1';
  20. // Load models concurrently.
  21. // It's not pretty but I didn't want to import a promise library only for this.
  22. CONCURRENCY = 3;
  23. }
  24. findBySlug(slug) {
  25. return this.findBy('slug', slug) || this.findBy('slug_without_version', slug);
  26. }
  27. sort() {
  28. return this.models.sort(function(a, b) {
  29. if (a.name === b.name) {
  30. if (!a.version || (a.version.replace(NORMALIZE_VERSION_RGX, NORMALIZE_VERSION_SUB) > b.version.replace(NORMALIZE_VERSION_RGX, NORMALIZE_VERSION_SUB))) {
  31. return -1;
  32. } else {
  33. return 1;
  34. }
  35. } else if (a.name.toLowerCase() > b.name.toLowerCase()) {
  36. return 1;
  37. } else {
  38. return -1;
  39. }
  40. });
  41. }
  42. load(onComplete, onError, options) {
  43. let i = 0;
  44. var next = () => {
  45. if (i < this.models.length) {
  46. this.models[i].load(next, fail, options);
  47. } else if (i === ((this.models.length + CONCURRENCY) - 1)) {
  48. onComplete();
  49. }
  50. i++;
  51. };
  52. var fail = function(...args) {
  53. if (onError) {
  54. onError(...Array.from(args || []));
  55. onError = null;
  56. }
  57. next();
  58. };
  59. for (let j = 0, end = CONCURRENCY, asc = 0 <= end; asc ? j < end : j > end; asc ? j++ : j--) { next(); }
  60. }
  61. clearCache() {
  62. for (var doc of Array.from(this.models)) { doc.clearCache(); }
  63. }
  64. uninstall(callback) {
  65. let i = 0;
  66. var next = () => {
  67. if (i < this.models.length) {
  68. this.models[i++].uninstall(next, next);
  69. } else {
  70. callback();
  71. }
  72. };
  73. next();
  74. }
  75. getInstallStatuses(callback) {
  76. app.db.versions(this.models, function(statuses) {
  77. if (statuses) {
  78. for (var key in statuses) {
  79. var value = statuses[key];
  80. statuses[key] = {installed: !!value, mtime: value};
  81. }
  82. }
  83. callback(statuses);
  84. });
  85. }
  86. checkForUpdates(callback) {
  87. this.getInstallStatuses(statuses => {
  88. let i = 0;
  89. if (statuses) {
  90. for (var slug in statuses) { var status = statuses[slug]; if (this.findBy('slug', slug).isOutdated(status)) { i += 1; } }
  91. }
  92. callback(i);
  93. });
  94. }
  95. updateInBackground() {
  96. this.getInstallStatuses(statuses => {
  97. if (!statuses) { return; }
  98. for (var slug in statuses) {
  99. var status = statuses[slug];
  100. var doc = this.findBy('slug', slug);
  101. if (doc.isOutdated(status)) { doc.install($.noop, $.noop); }
  102. }
  103. });
  104. }
  105. });
  106. Cls.initClass();
  107. return Cls;
  108. })();