diff options
author | zeripath <art27@cantab.net> | 2020-05-07 22:49:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-07 22:49:00 +0100 |
commit | 791353c03ba81d1c67393a04256a77293307ecad (patch) | |
tree | b0771f7e1683db318c5e5606a312319578392dcd /web_src/js/features/notification.js | |
parent | 486e4c8087746ca91c05a693cadd563ac061a913 (diff) | |
download | gitea-791353c03ba81d1c67393a04256a77293307ecad.tar.gz gitea-791353c03ba81d1c67393a04256a77293307ecad.zip |
Add EventSource support (#11235)
If the browser supports EventSource switch to use this instead of
polling notifications.
Signed-off-by: Andrew Thornton art27@cantab.net
Diffstat (limited to 'web_src/js/features/notification.js')
-rw-r--r-- | web_src/js/features/notification.js | 59 |
1 files changed, 48 insertions, 11 deletions
diff --git a/web_src/js/features/notification.js b/web_src/js/features/notification.js index 3f2af4de91..8b843e9806 100644 --- a/web_src/js/features/notification.js +++ b/web_src/js/features/notification.js @@ -19,21 +19,53 @@ export function initNotificationsTable() { } export function initNotificationCount() { - if (NotificationSettings.MinTimeout <= 0) { + const notificationCount = $('.notification_count'); + + if (!notificationCount.length) { return; } - const notificationCount = $('.notification_count'); - - if (notificationCount.length > 0) { - const fn = (timeout, lastCount) => { - setTimeout(async () => { - await updateNotificationCountWithCallback(fn, timeout, lastCount); - }, timeout); - }; + 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'); + } else { + notificationCount.removeClass('hidden'); + } + + 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; + }); + return; + } - fn(NotificationSettings.MinTimeout, notificationCount.text()); + if (NotificationSettings.MinTimeout <= 0) { + return; } + + const fn = (timeout, lastCount) => { + setTimeout(async () => { + await updateNotificationCountWithCallback(fn, timeout, lastCount); + }, timeout); + }; + + fn(NotificationSettings.MinTimeout, notificationCount.text()); } async function updateNotificationCountWithCallback(callback, timeout, lastCount) { @@ -54,9 +86,14 @@ async function updateNotificationCountWithCallback(callback, timeout, lastCount) } callback(timeout, newCount); + if (needsUpdate) { + await updateNotificationTable(); + } +} +async function updateNotificationTable() { const notificationDiv = $('#notification_div'); - if (notificationDiv.length > 0 && needsUpdate) { + if (notificationDiv.length > 0) { const data = await $.ajax({ type: 'GET', url: `${AppSubUrl}/notifications?${notificationDiv.data('params')}`, |