diff --git a/web_src/js/features/eventsource.sharedworker.js b/web_src/js/features/eventsource.sharedworker.js index 824ccfea79f84..a3cc352375ba9 100644 --- a/web_src/js/features/eventsource.sharedworker.js +++ b/web_src/js/features/eventsource.sharedworker.js @@ -93,10 +93,18 @@ self.addEventListener('connect', (e) => { } } // Create a new Source - source = new Source(url); - source.register(port); - sourcesByUrl[url] = source; - sourcesByPort[port] = source; + try { + source = new Source(url); + source.register(port); + sourcesByUrl[url] = source; + sourcesByPort[port] = source; + } catch (error) { + port.postMessage({ + type: 'error', + message: `unable to create Source: ${error}`, + }); + port.close(); + } } else if (event.data.type === 'listen') { const source = sourcesByPort[port]; source.listen(event.data.eventType); diff --git a/web_src/js/features/notification.js b/web_src/js/features/notification.js index 36df196cac2d8..84c5eb56e8a1d 100644 --- a/web_src/js/features/notification.js +++ b/web_src/js/features/notification.js @@ -49,11 +49,28 @@ export function initNotificationCount() { return; } + const poller = () => { + const pollerFn = (timeout, lastCount) => { + if (timeout <= 0) { + return; + } + setTimeout(() => { + const _promise = updateNotificationCountWithCallback(pollerFn, timeout, lastCount); + }, timeout); + }; + + pollerFn(notificationSettings.MinTimeout, notificationCount.text()); + }; + if (notificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) { // Try to connect to the event source via the shared worker first const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker'); - worker.addEventListener('error', (event) => { - console.error(event); + worker.addEventListener('error', (error) => { + if (error.message && error.message === 'ReferenceError: EventSource is not defined') { + poller(); + return; + } + console.error(error); }); worker.port.addEventListener('messageerror', () => { console.error('Unable to deserialize message'); @@ -70,6 +87,10 @@ export function initNotificationCount() { if (event.data.type === 'notification-count') { const _promise = receiveUpdateCount(event.data); } else if (event.data.type === 'error') { + if (event.data.message === 'unable to create Source: ReferenceError: EventSource is not defined') { + poller(); + return; + } console.error(event.data); } else if (event.data.type === 'logout') { if (event.data.data !== 'here') { @@ -87,8 +108,11 @@ export function initNotificationCount() { worker.port.close(); } }); - worker.port.addEventListener('error', (e) => { - console.error(e); + worker.port.addEventListener('error', (error) => { + if (error.message && error.message === 'unable to create Source: ReferenceError: EventSource is not defined') { + return; + } + console.error(error); }); worker.port.start(); window.addEventListener('beforeunload', () => { @@ -97,7 +121,6 @@ export function initNotificationCount() { }); worker.port.close(); }); - return; } @@ -105,13 +128,7 @@ export function initNotificationCount() { return; } - const fn = (timeout, lastCount) => { - setTimeout(() => { - const _promise = updateNotificationCountWithCallback(fn, timeout, lastCount); - }, timeout); - }; - - fn(notificationSettings.MinTimeout, notificationCount.text()); + poller(); } async function updateNotificationCountWithCallback(callback, timeout, lastCount) { diff --git a/web_src/js/features/stopwatch.js b/web_src/js/features/stopwatch.js index d63da4155af27..16c8cf77a1dbf 100644 --- a/web_src/js/features/stopwatch.js +++ b/web_src/js/features/stopwatch.js @@ -26,11 +26,34 @@ export function initStopwatch() { $(this).parent().trigger('submit'); }); + const poller = () => { + const fn = (timeout) => { + if (timeout <= 0) { + return; + } + setTimeout(() => { + const _promise = updateStopwatchWithCallback(fn, timeout); + }, timeout); + }; + + fn(notificationSettings.MinTimeout); + + const currSeconds = $('.stopwatch-time').data('seconds'); + if (currSeconds) { + updateTimeInterval = updateStopwatchTime(currSeconds); + } + }; + + if (notificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) { // Try to connect to the event source via the shared worker first const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker'); - worker.addEventListener('error', (event) => { - console.error(event); + worker.addEventListener('error', (error) => { + if (error.message && error.message === 'ReferenceError: EventSource is not defined') { + poller(); + return; + } + console.error(error); }); worker.port.addEventListener('messageerror', () => { console.error('Unable to deserialize message'); @@ -47,6 +70,10 @@ export function initStopwatch() { if (event.data.type === 'stopwatches') { updateStopwatchData(JSON.parse(event.data.data)); } else if (event.data.type === 'error') { + if (event.data.message === 'unable to create Source: ReferenceError: EventSource is not defined') { + poller(); + return; + } console.error(event.data); } else if (event.data.type === 'logout') { if (event.data.data !== 'here') { @@ -64,8 +91,11 @@ export function initStopwatch() { worker.port.close(); } }); - worker.port.addEventListener('error', (e) => { - console.error(e); + worker.port.addEventListener('error', (error) => { + if (error.message && error.message === 'ReferenceError: EventSource is not defined') { + return; + } + console.error(error); }); worker.port.start(); window.addEventListener('beforeunload', () => { @@ -82,18 +112,7 @@ export function initStopwatch() { return; } - const fn = (timeout) => { - setTimeout(() => { - const _promise = updateStopwatchWithCallback(fn, timeout); - }, timeout); - }; - - fn(notificationSettings.MinTimeout); - - const currSeconds = $('.stopwatch-time').data('seconds'); - if (currSeconds) { - updateTimeInterval = updateStopwatchTime(currSeconds); - } + poller(); } async function updateStopwatchWithCallback(callback, timeout) {