docs.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. 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 (
  26. this.findBy("slug", slug) || this.findBy("slug_without_version", slug)
  27. );
  28. }
  29. sort() {
  30. return this.models.sort(function (a, b) {
  31. if (a.name === b.name) {
  32. if (
  33. !a.version ||
  34. a.version.replace(NORMALIZE_VERSION_RGX, NORMALIZE_VERSION_SUB) >
  35. b.version.replace(NORMALIZE_VERSION_RGX, NORMALIZE_VERSION_SUB)
  36. ) {
  37. return -1;
  38. } else {
  39. return 1;
  40. }
  41. } else if (a.name.toLowerCase() > b.name.toLowerCase()) {
  42. return 1;
  43. } else {
  44. return -1;
  45. }
  46. });
  47. }
  48. load(onComplete, onError, options) {
  49. let i = 0;
  50. var next = () => {
  51. if (i < this.models.length) {
  52. this.models[i].load(next, fail, options);
  53. } else if (i === this.models.length + CONCURRENCY - 1) {
  54. onComplete();
  55. }
  56. i++;
  57. };
  58. var fail = function (...args) {
  59. if (onError) {
  60. onError(...Array.from(args || []));
  61. onError = null;
  62. }
  63. next();
  64. };
  65. for (
  66. let j = 0, end = CONCURRENCY, asc = 0 <= end;
  67. asc ? j < end : j > end;
  68. asc ? j++ : j--
  69. ) {
  70. next();
  71. }
  72. }
  73. clearCache() {
  74. for (var doc of Array.from(this.models)) {
  75. doc.clearCache();
  76. }
  77. }
  78. uninstall(callback) {
  79. let i = 0;
  80. var next = () => {
  81. if (i < this.models.length) {
  82. this.models[i++].uninstall(next, next);
  83. } else {
  84. callback();
  85. }
  86. };
  87. next();
  88. }
  89. getInstallStatuses(callback) {
  90. app.db.versions(this.models, function (statuses) {
  91. if (statuses) {
  92. for (var key in statuses) {
  93. var value = statuses[key];
  94. statuses[key] = { installed: !!value, mtime: value };
  95. }
  96. }
  97. callback(statuses);
  98. });
  99. }
  100. checkForUpdates(callback) {
  101. this.getInstallStatuses((statuses) => {
  102. let i = 0;
  103. if (statuses) {
  104. for (var slug in statuses) {
  105. var status = statuses[slug];
  106. if (this.findBy("slug", slug).isOutdated(status)) {
  107. i += 1;
  108. }
  109. }
  110. }
  111. callback(i);
  112. });
  113. }
  114. updateInBackground() {
  115. this.getInstallStatuses((statuses) => {
  116. if (!statuses) {
  117. return;
  118. }
  119. for (var slug in statuses) {
  120. var status = statuses[slug];
  121. var doc = this.findBy("slug", slug);
  122. if (doc.isOutdated(status)) {
  123. doc.install($.noop, $.noop);
  124. }
  125. }
  126. });
  127. }
  128. };
  129. app.collections.Docs.initClass();
  130. return app.collections.Docs;
  131. })();