From e5f852e7c0623d29ebad6219b08496c36811b16e Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Sat, 6 Jan 2024 14:33:49 +0100 Subject: [PATCH] Sanity-check decaffeinate app.collections.Docs --- assets/javascripts/collections/docs.js | 226 ++++++++++++------------- 1 file changed, 104 insertions(+), 122 deletions(-) diff --git a/assets/javascripts/collections/docs.js b/assets/javascripts/collections/docs.js index 7b8ed4a9..25d8c007 100644 --- a/assets/javascripts/collections/docs.js +++ b/assets/javascripts/collections/docs.js @@ -1,142 +1,124 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -/* - * decaffeinate suggestions: - * DS101: Remove unnecessary use of Array.from - * DS102: Remove unnecessary code created because of implicit returns - * DS202: Simplify dynamic range loops - * DS206: Consider reworking classes to avoid initClass - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md - */ -(function () { - let NORMALIZE_VERSION_RGX = undefined; - let NORMALIZE_VERSION_SUB = undefined; - let CONCURRENCY = undefined; - app.collections.Docs = class Docs extends app.Collection { - static initClass() { - this.model = "Doc"; +app.collections.Docs = class Docs extends app.Collection { + static model = "Doc"; + static NORMALIZE_VERSION_RGX = /\.(\d)$/; + static NORMALIZE_VERSION_SUB = ".0$1"; - NORMALIZE_VERSION_RGX = /\.(\d)$/; - NORMALIZE_VERSION_SUB = ".0$1"; + // Load models concurrently. + // It's not pretty but I didn't want to import a promise library only for this. + static CONCURRENCY = 3; - // Load models concurrently. - // It's not pretty but I didn't want to import a promise library only for this. - CONCURRENCY = 3; - } - - findBySlug(slug) { - return ( - this.findBy("slug", slug) || this.findBy("slug_without_version", slug) - ); - } - sort() { - return this.models.sort(function (a, b) { - if (a.name === b.name) { - if ( - !a.version || - a.version.replace(NORMALIZE_VERSION_RGX, NORMALIZE_VERSION_SUB) > - b.version.replace(NORMALIZE_VERSION_RGX, NORMALIZE_VERSION_SUB) - ) { - return -1; - } else { - return 1; - } - } else if (a.name.toLowerCase() > b.name.toLowerCase()) { - return 1; - } else { + findBySlug(slug) { + return ( + this.findBy("slug", slug) || this.findBy("slug_without_version", slug) + ); + } + sort() { + return this.models.sort(function (a, b) { + if (a.name === b.name) { + if ( + !a.version || + a.version.replace( + Docs.NORMALIZE_VERSION_RGX, + Docs.NORMALIZE_VERSION_SUB, + ) > + b.version.replace( + Docs.NORMALIZE_VERSION_RGX, + Docs.NORMALIZE_VERSION_SUB, + ) + ) { return -1; + } else { + return 1; } - }); - } - load(onComplete, onError, options) { - let i = 0; - - var next = () => { - if (i < this.models.length) { - this.models[i].load(next, fail, options); - } else if (i === this.models.length + CONCURRENCY - 1) { - onComplete(); - } - i++; - }; - - var fail = function (...args) { - if (onError) { - onError(...Array.from(args || [])); - onError = null; - } - next(); - }; + } else if (a.name.toLowerCase() > b.name.toLowerCase()) { + return 1; + } else { + return -1; + } + }); + } + load(onComplete, onError, options) { + let i = 0; - for ( - let j = 0, end = CONCURRENCY, asc = 0 <= end; - asc ? j < end : j > end; - asc ? j++ : j-- - ) { - next(); + var next = () => { + if (i < this.models.length) { + this.models[i].load(next, fail, options); + } else if (i === this.models.length + Docs.CONCURRENCY - 1) { + onComplete(); } - } + i++; + }; - clearCache() { - for (var doc of Array.from(this.models)) { - doc.clearCache(); + var fail = function (...args) { + if (onError) { + onError(args); + onError = null; } - } + next(); + }; - uninstall(callback) { - let i = 0; - var next = () => { - if (i < this.models.length) { - this.models[i++].uninstall(next, next); - } else { - callback(); - } - }; + for (let j = 0, end = Docs.CONCURRENCY; j < end; j++) { next(); } + } - getInstallStatuses(callback) { - app.db.versions(this.models, function (statuses) { - if (statuses) { - for (var key in statuses) { - var value = statuses[key]; - statuses[key] = { installed: !!value, mtime: value }; - } - } - callback(statuses); - }); + clearCache() { + for (var doc of this.models) { + doc.clearCache(); } + } - checkForUpdates(callback) { - this.getInstallStatuses((statuses) => { - let i = 0; - if (statuses) { - for (var slug in statuses) { - var status = statuses[slug]; - if (this.findBy("slug", slug).isOutdated(status)) { - i += 1; - } - } - } - callback(i); - }); - } + uninstall(callback) { + let i = 0; + var next = () => { + if (i < this.models.length) { + this.models[i++].uninstall(next, next); + } else { + callback(); + } + }; + next(); + } - updateInBackground() { - this.getInstallStatuses((statuses) => { - if (!statuses) { - return; + getInstallStatuses(callback) { + app.db.versions(this.models, function (statuses) { + if (statuses) { + for (var key in statuses) { + var value = statuses[key]; + statuses[key] = { installed: !!value, mtime: value }; } + } + callback(statuses); + }); + } + + checkForUpdates(callback) { + this.getInstallStatuses((statuses) => { + let i = 0; + if (statuses) { for (var slug in statuses) { var status = statuses[slug]; - var doc = this.findBy("slug", slug); - if (doc.isOutdated(status)) { - doc.install($.noop, $.noop); + if (this.findBy("slug", slug).isOutdated(status)) { + i += 1; } } - }); - } - }; - app.collections.Docs.initClass(); - return app.collections.Docs; -})(); + } + callback(i); + }); + } + + updateInBackground() { + this.getInstallStatuses((statuses) => { + if (!statuses) { + return; + } + for (var slug in statuses) { + var status = statuses[slug]; + var doc = this.findBy("slug", slug); + if (doc.isOutdated(status)) { + doc.install($.noop, $.noop); + } + } + }); + } +};