Allow out-of-band polling of notifications
authorAlexander Ebert <ebert@woltlab.com>
Tue, 12 Apr 2022 16:59:52 +0000 (18:59 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 12 Apr 2022 16:59:52 +0000 (18:59 +0200)
ts/WoltLabSuite/Core/Notification/Handler.ts

index 27135b79e4173c9de1fe488f4ad06722f277abbe..733aa033153b897bb6180049a9e295c2e594eaef 100644 (file)
@@ -126,13 +126,13 @@ class NotificationHandler {
   private prepareNextRequest(): void {
     this.resetTimer();
 
-    this.requestTimer = window.setTimeout(this.dispatchRequest.bind(this), this.getNextDelay() * 60_000);
+    this.requestTimer = window.setTimeout(() => this.dispatchRequest(), this.getNextDelay() * 60_000);
   }
 
   /**
    * Requests new data from the server.
    */
-  private dispatchRequest(): void {
+  dispatchRequest(): void {
     const parameters: ArbitraryObject = {};
 
     EventHandler.fire("com.woltlab.wcf.notification", "beforePoll", parameters);
@@ -154,23 +154,23 @@ class NotificationHandler {
     // abort and re-schedule periodic request
     this.prepareNextRequest();
 
-    let pollData;
-    let keepAliveData;
+    let pollData: unknown;
+    let keepAliveData: unknown;
     let abort = false;
     try {
       pollData = window.localStorage.getItem(Core.getStoragePrefix() + "notification");
       keepAliveData = window.localStorage.getItem(Core.getStoragePrefix() + "keepAliveData");
 
-      pollData = JSON.parse(pollData);
-      keepAliveData = JSON.parse(keepAliveData);
+      pollData = JSON.parse(pollData as string);
+      keepAliveData = JSON.parse(keepAliveData as string);
     } catch (e) {
       abort = true;
     }
 
     if (!abort) {
       EventHandler.fire("com.woltlab.wcf.notification", "onStorage", {
-        pollData: pollData,
-        keepAliveData: keepAliveData,
+        pollData,
+        keepAliveData,
       });
     }
   }
@@ -268,3 +268,7 @@ export function setup(options: NotificationHandlerOptions): void {
 export function enableNotifications(): void {
   notificationHandler!.enableNotifications();
 }
+
+export function poll(): void {
+  notificationHandler?.dispatchRequest();
+}