aboutsummaryrefslogtreecommitdiffstats
path: root/web_src/js/features/notification.js
diff options
context:
space:
mode:
Diffstat (limited to 'web_src/js/features/notification.js')
-rw-r--r--web_src/js/features/notification.js97
1 files changed, 68 insertions, 29 deletions
diff --git a/web_src/js/features/notification.js b/web_src/js/features/notification.js
index 0ea7f93c8d..2b7fc45237 100644
--- a/web_src/js/features/notification.js
+++ b/web_src/js/features/notification.js
@@ -18,7 +18,25 @@ export function initNotificationsTable() {
});
}
-export function initNotificationCount() {
+async function receiveUpdateCount(event) {
+ try {
+ const data = JSON.parse(event.data);
+
+ const notificationCount = document.querySelector('.notification_count');
+ if (data.Count > 0) {
+ notificationCount.classList.remove('hidden');
+ } else {
+ notificationCount.classList.add('hidden');
+ }
+
+ notificationCount.text(`${data.Count}`);
+ await updateNotificationTable();
+ } catch (error) {
+ console.error(error, event);
+ }
+}
+
+export async function initNotificationCount() {
const notificationCount = $('.notification_count');
if (!notificationCount.length) {
@@ -26,36 +44,57 @@ export function initNotificationCount() {
}
if (NotificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource) {
- // Try to connect to the event source first
- const source = new EventSource(`${AppSubUrl}/user/events`);
- source.addEventListener('notification-count', async (e) => {
- try {
- const data = JSON.parse(e.data);
-
- const notificationCount = $('.notification_count');
- if (data.Count === 0) {
- notificationCount.addClass('hidden');
+ // Try to connect to the event source via the shared worker first
+ if (window.SharedWorker) {
+ const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
+ worker.addEventListener('error', (event) => {
+ console.error(event);
+ });
+ worker.port.onmessageerror = () => {
+ console.error('Unable to deserialize message');
+ };
+ worker.port.postMessage({
+ type: 'start',
+ url: `${window.location.origin}${AppSubUrl}/user/events`,
+ });
+ worker.port.addEventListener('message', (e) => {
+ if (!e.data || !e.data.type) {
+ console.error(e);
+ return;
+ }
+ if (event.data.type === 'notification-count') {
+ receiveUpdateCount(e.data);
+ return;
+ } else if (event.data.type === 'error') {
+ console.error(e.data);
+ return;
+ } else if (event.data.type === 'logout') {
+ if (e.data !== 'here') {
+ return;
+ }
+ worker.port.postMessage({
+ type: 'close',
+ });
+ worker.port.close();
+ window.location.href = AppSubUrl;
+ return;
} else {
- notificationCount.removeClass('hidden');
+ return;
}
-
- notificationCount.text(`${data.Count}`);
- await updateNotificationTable();
- } catch (error) {
- console.error(error);
- }
- });
- source.addEventListener('logout', async (e) => {
- if (e.data !== 'here') {
- return;
- }
- source.close();
- window.location.href = AppSubUrl;
- });
- window.addEventListener('beforeunload', () => {
- source.close();
- });
- return;
+ });
+ worker.port.addEventListener('error', (e) => {
+ console.error(e);
+ });
+ worker.port.start();
+ window.addEventListener('beforeunload', () => {
+ worker.port.postMessage({
+ type: 'close',
+ });
+ worker.port.close();
+ });
+
+ return;
+ }
}
if (NotificationSettings.MinTimeout <= 0) {