From 7cd11bad0c3d730783378af8618d75228122e933 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 6 Aug 2020 15:08:34 -0400 Subject: [PATCH] REFACTOR: Use the session to track assetVersion/requiresRefresh This saves us having to put the data on `Discourse` --- app/assets/javascripts/discourse/app/app.js | 25 +------------------ .../app/controllers/preferences/interface.js | 5 ++-- .../app/initializers/asset-version.js | 20 +++++++++------ .../subscribe-user-notifications.js | 3 --- .../javascripts/discourse/app/lib/url.js | 3 ++- .../pre-initializers/discourse-bootstrap.js | 2 +- 6 files changed, 19 insertions(+), 39 deletions(-) diff --git a/app/assets/javascripts/discourse/app/app.js b/app/assets/javascripts/discourse/app/app.js index 9693598a636..a22244c00e0 100644 --- a/app/assets/javascripts/discourse/app/app.js +++ b/app/assets/javascripts/discourse/app/app.js @@ -1,8 +1,6 @@ /*global Mousetrap:true*/ import Application from "@ember/application"; -import { computed } from "@ember/object"; import { buildResolver } from "discourse-common/resolver"; -import discourseComputed from "discourse-common/utils/decorators"; const _pluginCallbacks = []; @@ -62,30 +60,9 @@ const Discourse = Application.extend({ }); }, - @discourseComputed("currentAssetVersion", "desiredAssetVersion") - requiresRefresh(currentAssetVersion, desiredAssetVersion) { - return desiredAssetVersion && currentAssetVersion !== desiredAssetVersion; - }, - _registerPluginCode(version, code) { _pluginCallbacks.push({ version, code }); - }, - - assetVersion: computed({ - get() { - return this.currentAssetVersion; - }, - set(key, val) { - if (val) { - if (this.currentAssetVersion) { - this.set("desiredAssetVersion", val); - } else { - this.set("currentAssetVersion", val); - } - } - return this.currentAssetVersion; - } - }) + } }); export default Discourse; diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/interface.js b/app/assets/javascripts/discourse/app/controllers/preferences/interface.js index 5d41ab7b128..8b63f5b5fe0 100644 --- a/app/assets/javascripts/discourse/app/controllers/preferences/interface.js +++ b/app/assets/javascripts/discourse/app/controllers/preferences/interface.js @@ -205,7 +205,7 @@ export default Controller.extend({ if (this.isiPad) { if (safariHacksDisabled() !== this.disableSafariHacks) { - Discourse.set("assetVersion", "forceRefresh"); + this.session.requiresRefresh = true; } localStorage.setItem( "safari-hacks-disabled", @@ -233,8 +233,7 @@ export default Controller.extend({ }); // Force refresh when leaving this screen - Discourse.set("assetVersion", "forceRefresh"); - + this.session.requiresRefresh = true; this.set("textSize", newSize); } } diff --git a/app/assets/javascripts/discourse/app/initializers/asset-version.js b/app/assets/javascripts/discourse/app/initializers/asset-version.js index fad499c8bbb..ab1a85fec9b 100644 --- a/app/assets/javascripts/discourse/app/initializers/asset-version.js +++ b/app/assets/javascripts/discourse/app/initializers/asset-version.js @@ -6,26 +6,32 @@ export default { after: "message-bus", initialize(container) { - let timeoutIsSet = false; + let timeout; const messageBus = container.lookup("message-bus:main"); if (!messageBus) { return; } - messageBus.subscribe("/global/asset-version", function(version) { - Discourse.set("assetVersion", version); + let session = container.lookup("session:main"); + messageBus.subscribe("/refresh_client", () => { + session.requiresRefresh = true; + }); - if (!timeoutIsSet && Discourse.get("requiresRefresh")) { + messageBus.subscribe("/global/asset-version", function(version) { + if (session.assetVersion !== version) { + session.requiresRefresh = true; + } + + if (!timeout && session.requiresRefresh) { // Since we can do this transparently for people browsing the forum - // hold back the message 24 hours. - later(() => { + // hold back the message 24 hours. + timeout = later(() => { bootbox.confirm(I18n.t("assets_changed_confirm"), function(result) { if (result) { document.location.reload(); } }); }, 1000 * 60 * 24 * 60); - timeoutIsSet = true; } }); } diff --git a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js index af11b97543e..53c63766bdc 100644 --- a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js +++ b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js @@ -126,9 +126,6 @@ export default { bus.subscribe("/client_settings", data => set(siteSettings, data.name, data.value) ); - bus.subscribe("/refresh_client", data => - Discourse.set("assetVersion", data) - ); if (!isTesting()) { bus.subscribe(alertChannel(user), data => diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.js index b5b582fa5ca..60211f9805e 100644 --- a/app/assets/javascripts/discourse/app/lib/url.js +++ b/app/assets/javascripts/discourse/app/lib/url.js @@ -6,6 +6,7 @@ import LockOn from "discourse/lib/lock-on"; import { defaultHomepage } from "discourse/lib/utilities"; import User from "discourse/models/user"; import { default as getURL, withoutPrefix } from "discourse-common/lib/get-url"; +import Session from "discourse/models/session"; const rewrites = []; const TOPIC_REGEXP = /\/t\/([^\/]+)\/(\d+)\/?(\d+)?/; @@ -211,7 +212,7 @@ const DiscourseURL = EmberObject.extend({ return; } - if (Discourse.get("requiresRefresh")) { + if (Session.currentProp("requiresRefresh")) { return redirectTo(getURL(path)); } diff --git a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js index d8eb01dadc6..adc5c25c0df 100644 --- a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js +++ b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js @@ -70,7 +70,7 @@ export default { window.Logster = window.Logster || {}; window.Logster.enabled = setupData.enableJsErrorReporting === "true"; - app.set("assetVersion", setupData.assetVersion); + Session.currentProp("assetVersion", setupData.assetVersion); Session.currentProp( "disableCustomCSS",