[FIX] base,bus: Notify bundle change on version change

Before this commit: a notification asking to reload the current window
appeared as soon as the server detected a change in one of the assets
bundles, even on first load.

To fix this problem and make the feature more meaningful, it has been
decided to only notify the client when the server version (not the
bundle version) is outdated (i.e. on database upgrades, when the changes
in the code are actually relevant).

closes odoo/odoo#82032

X-original-commit: a3b5a9d715be6a93c7f2859074b916f3249f97c3
Signed-off-by: Antony Lesuisse <al@odoo.com>
Signed-off-by: Julien Mougenot (jum) <jum@odoo.com>
This commit is contained in:
Julien Mougenot
2022-01-04 10:26:10 +00:00
parent 66f976f776
commit 65d70acdbf
4 changed files with 7 additions and 18 deletions
@@ -2,24 +2,17 @@
import { browser } from "@web/core/browser/browser"; import { browser } from "@web/core/browser/browser";
import { registry } from "@web/core/registry"; import { registry } from "@web/core/registry";
import { session } from "@web/session";
export const assetsWatchdogService = { export const assetsWatchdogService = {
dependencies: ["notification"], dependencies: ["notification"],
start(env, { notification }) { start(env, { notification }) {
const assets = {};
let isNotificationDisplayed = false; let isNotificationDisplayed = false;
let bundleNotifTimerID = null; let bundleNotifTimerID = null;
env.bus.on("WEB_CLIENT_READY", null, async () => { env.bus.on("WEB_CLIENT_READY", null, async () => {
const legacyEnv = owl.Component.env; const legacyEnv = owl.Component.env;
document.querySelectorAll("*[data-asset-bundle]").forEach((el) => {
assets[el.getAttribute("data-asset-bundle")] = el.getAttribute(
"data-asset-version"
);
});
legacyEnv.services.bus_service.onNotification(this, onNotification); legacyEnv.services.bus_service.onNotification(this, onNotification);
legacyEnv.services.bus_service.startPolling(); legacyEnv.services.bus_service.startPolling();
}); });
@@ -78,7 +71,7 @@ export const assetsWatchdogService = {
function onNotification(notifications) { function onNotification(notifications) {
for (const { payload, type } of notifications) { for (const { payload, type } of notifications) {
if (type === 'bundle_changed') { if (type === 'bundle_changed') {
if (payload.name in assets && payload.version !== assets[payload.name]) { if (payload.server_version !== session.server_version) {
displayBundleChangedNotification(); displayBundleChangedNotification();
break; break;
} }
@@ -48,10 +48,7 @@ QUnit.module("Bus Assets WatchDog", (hooks) => {
return [{ return [{
message: { message: {
type: 'bundle_changed', type: 'bundle_changed',
payload: { payload: { server_version: "NEW_MAJOR_VERSION" },
name: 'web.assets_backend',
version: 'newHash',
},
}, },
}]; }];
} }
+2 -3
View File
@@ -47,6 +47,5 @@ class BusWebTests(odoo.tests.HttpCase):
) )
for (channel, message) in sendones: for (channel, message) in sendones:
self.assertEqual(channel, 'broadcast') self.assertEqual(channel, 'broadcast')
self.assertEqual(len(message), 2) self.assertEqual(len(message), 1)
self.assertIn(message.get('name'), bundle_xml_ids) self.assertTrue(isinstance(message.get('server_version'), str))
self.assertTrue(isinstance(message.get('name'), str))
+2 -2
View File
@@ -26,6 +26,7 @@ from odoo.modules.module import get_resource_path
from odoo.tools import func, misc, transpile_javascript, is_odoo_module, SourceMapGenerator, profiler from odoo.tools import func, misc, transpile_javascript, is_odoo_module, SourceMapGenerator, profiler
from odoo.tools.misc import file_open, html_escape as escape from odoo.tools.misc import file_open, html_escape as escape
from odoo.tools.pycompat import to_text from odoo.tools.pycompat import to_text
from odoo.release import version as server_version
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@@ -343,8 +344,7 @@ class AssetsBundle(object):
# to invite the user to refresh their browser # to invite the user to refresh their browser
if self.env and 'bus.bus' in self.env and self.name in self.TRACKED_BUNDLES: if self.env and 'bus.bus' in self.env and self.name in self.TRACKED_BUNDLES:
self.env['bus.bus']._sendone('broadcast', 'bundle_changed', { self.env['bus.bus']._sendone('broadcast', 'bundle_changed', {
'name': self.name, 'server_version': server_version
'version': self.version,
}) })
_logger.debug('Asset Changed: bundle: %s -- version: %s', self.name, self.version) _logger.debug('Asset Changed: bundle: %s -- version: %s', self.name, self.version)