REFACTOR: Use the session to track assetVersion/requiresRefresh

This saves us having to put the data on `Discourse`
This commit is contained in:
Robin Ward 2020-08-06 15:08:34 -04:00
parent af63871d4d
commit 7cd11bad0c
6 changed files with 19 additions and 39 deletions

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;
}
});
}

View File

@ -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 =>

View File

@ -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));
}

View File

@ -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",