forked from forgejo/forgejo
Move EventSource to SharedWorker (#12095)
Move EventSource to use a SharedWorker. This prevents issues with HTTP/1.1 open browser connections from preventing gitea from opening multiple tabs. Also allow setting EVENT_SOURCE_UPDATE_TIME to disable EventSource updating Fix #11978 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
cd994f6354
commit
ae56411e9f
9 changed files with 220 additions and 36 deletions
|
@ -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');
|
||||
} else {
|
||||
notificationCount.removeClass('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 {
|
||||
return;
|
||||
}
|
||||
});
|
||||
worker.port.addEventListener('error', (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
worker.port.start();
|
||||
window.addEventListener('beforeunload', () => {
|
||||
worker.port.postMessage({
|
||||
type: 'close',
|
||||
});
|
||||
worker.port.close();
|
||||
});
|
||||
|
||||
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;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (NotificationSettings.MinTimeout <= 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue