Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ts/WoltLabSuite/Core/Notification/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as Ajax from "../Ajax";
import { AjaxCallbackSetup } from "../Ajax/Data";
import * as Core from "../Core";
import * as EventHandler from "../Event/Handler";
import { serviceWorkerSupported } from "./ServiceWorker";
import { serviceWorkerSupported, updateNotificationLastReadTime } from "./ServiceWorker";
import { updateCounter } from "WoltLabSuite/Core/Ui/User/Menu/Manager";

interface NotificationHandlerOptions {
Expand Down Expand Up @@ -206,6 +206,9 @@ class NotificationHandler {

this.lastRequestTimestamp = data.returnValues.lastRequestTimestamp;

// Update the last read time for the service worker
updateNotificationLastReadTime(this.lastRequestTimestamp);

EventHandler.fire("com.woltlab.wcf.notification", "afterPoll", pollData);

this.showNotification(pollData);
Expand Down Expand Up @@ -264,6 +267,10 @@ class NotificationHandler {
silent: !window.ENABLE_DEBUG_MODE,
};
}

updateLastRequestTimestamp(timestamp: number): void {
this.lastRequestTimestamp = Math.max(timestamp, this.lastRequestTimestamp);
}
}

let notificationHandler: NotificationHandler;
Expand All @@ -281,6 +288,10 @@ export function enableNotifications(): void {
notificationHandler!.enableNotifications();
}

export function updateLastRequestTimestamp(timestamp: number): void {
notificationHandler?.updateLastRequestTimestamp(timestamp);
}

export function poll(): void {
notificationHandler?.dispatchRequest();
}
10 changes: 10 additions & 0 deletions ts/WoltLabSuite/Core/Notification/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { updateLastRequestTimestamp } from "WoltLabSuite/Core/Notification/Handler";

let _serviceWorker: ServiceWorker | null = null;

Expand All @@ -24,6 +25,15 @@ class ServiceWorker {
scope: "/",
});
this.#serviceWorkerRegistration = window.navigator.serviceWorker.ready;

window.navigator.serviceWorker.addEventListener("message", (event) => {
// Validate that this is a message from our service worker
if (!(event.source instanceof window.ServiceWorker) || event.source.scriptURL !== this.#serviceWorkerJsUrl) {
return;
}

updateLastRequestTimestamp(event.data.time);
});
}

async register(): Promise<void> {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.