Use private fields with `#`
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 19 Feb 2024 11:02:15 +0000 (12:02 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Fri, 23 Feb 2024 13:41:41 +0000 (14:41 +0100)
ts/WoltLabSuite/Core/Notification/ServiceWorker.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js

index e3bc116d3cdaa10f8e2b9ce7c33992f5b4fdac97..ab1ff32f87e04046e1ee2c2f628e818c1a0f743f 100644 (file)
@@ -11,32 +11,30 @@ import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
 let _serviceWorker: ServiceWorker | null = null;
 
 class ServiceWorker {
-  private readonly publicKey: string;
-  private readonly serviceWorkerJsUrl: string;
-  private readonly registerUrl: string;
-  private readonly serviceWorkerRegistration: Promise<ServiceWorkerRegistration>;
+  readonly #publicKey: string;
+  readonly #serviceWorkerJsUrl: string;
+  readonly #registerUrl: string;
+  readonly #serviceWorkerRegistration: Promise<ServiceWorkerRegistration>;
 
   constructor(publicKey: string, serviceWorkerJsUrl: string, registerUrl: string) {
-    this.publicKey = publicKey;
-    this.serviceWorkerJsUrl = serviceWorkerJsUrl;
-    this.registerUrl = registerUrl;
-    void window.navigator.serviceWorker.register(this.serviceWorkerJsUrl, {
+    this.#publicKey = publicKey;
+    this.#serviceWorkerJsUrl = serviceWorkerJsUrl;
+    this.#registerUrl = registerUrl;
+    void window.navigator.serviceWorker.register(this.#serviceWorkerJsUrl, {
       scope: "/",
     });
-    this.serviceWorkerRegistration = window.navigator.serviceWorker.ready;
+    this.#serviceWorkerRegistration = window.navigator.serviceWorker.ready;
   }
 
   async register(): Promise<void> {
-    const currentSubscription = await (await this.serviceWorkerRegistration).pushManager.getSubscription();
+    const currentSubscription = await(await this.#serviceWorkerRegistration).pushManager.getSubscription();
     if (currentSubscription && this.#compareApplicationServerKey(currentSubscription)) {
       return;
     }
     await this.unsubscribe(currentSubscription);
-    const subscription = await (
-      await this.serviceWorkerRegistration
-    ).pushManager.subscribe({
+    const subscription = await(await this.#serviceWorkerRegistration).pushManager.subscribe({
       userVisibleOnly: true,
-      applicationServerKey: this.#urlBase64ToUint8Array(this.publicKey),
+      applicationServerKey: this.#urlBase64ToUint8Array(this.#publicKey),
     });
     if (!subscription) {
       // subscription failed
@@ -57,7 +55,7 @@ class ServiceWorker {
     base64 = base64.replace(/\+/g, "-").replace(/\//g, "_");
     base64 = base64.replace(/=+$/, "");
 
-    return base64 === this.publicKey;
+    return base64 === this.#publicKey;
   }
 
   async #sendRequest(subscription: PushSubscription, remove: boolean = false): Promise<void> {
@@ -67,7 +65,7 @@ class ServiceWorker {
     // @see https://w3c.github.io/push-api/#dom-pushmanager-supportedcontentencodings
     const contentEncoding = (PushManager.supportedContentEncodings || ["aes128gcm"])[0];
     try {
-      await prepareRequest(this.registerUrl)
+      await prepareRequest(this.#registerUrl)
         .post({
           remove: remove,
           endpoint: subscription.endpoint,
index dc8c905250f561ad9f090c225f93f42aca98a538..2b4864d5c917197d7903082a81c2eacccd48e227 100644 (file)
@@ -11,28 +11,28 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi
     exports.registerServiceWorker = exports.setup = void 0;
     let _serviceWorker = null;
     class ServiceWorker {
-        publicKey;
-        serviceWorkerJsUrl;
-        registerUrl;
-        serviceWorkerRegistration;
+        #publicKey;
+        #serviceWorkerJsUrl;
+        #registerUrl;
+        #serviceWorkerRegistration;
         constructor(publicKey, serviceWorkerJsUrl, registerUrl) {
-            this.publicKey = publicKey;
-            this.serviceWorkerJsUrl = serviceWorkerJsUrl;
-            this.registerUrl = registerUrl;
-            void window.navigator.serviceWorker.register(this.serviceWorkerJsUrl, {
+            this.#publicKey = publicKey;
+            this.#serviceWorkerJsUrl = serviceWorkerJsUrl;
+            this.#registerUrl = registerUrl;
+            void window.navigator.serviceWorker.register(this.#serviceWorkerJsUrl, {
                 scope: "/",
             });
-            this.serviceWorkerRegistration = window.navigator.serviceWorker.ready;
+            this.#serviceWorkerRegistration = window.navigator.serviceWorker.ready;
         }
         async register() {
-            const currentSubscription = await (await this.serviceWorkerRegistration).pushManager.getSubscription();
+            const currentSubscription = await (await this.#serviceWorkerRegistration).pushManager.getSubscription();
             if (currentSubscription && this.#compareApplicationServerKey(currentSubscription)) {
                 return;
             }
             await this.unsubscribe(currentSubscription);
-            const subscription = await (await this.serviceWorkerRegistration).pushManager.subscribe({
+            const subscription = await (await this.#serviceWorkerRegistration).pushManager.subscribe({
                 userVisibleOnly: true,
-                applicationServerKey: this.#urlBase64ToUint8Array(this.publicKey),
+                applicationServerKey: this.#urlBase64ToUint8Array(this.#publicKey),
             });
             if (!subscription) {
                 // subscription failed
@@ -50,7 +50,7 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi
             let base64 = window.btoa(String.fromCharCode(...new Uint8Array(subscription.options.applicationServerKey)));
             base64 = base64.replace(/\+/g, "-").replace(/\//g, "_");
             base64 = base64.replace(/=+$/, "");
-            return base64 === this.publicKey;
+            return base64 === this.#publicKey;
         }
         async #sendRequest(subscription, remove = false) {
             const key = subscription.getKey("p256dh");
@@ -59,7 +59,7 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi
             // @see https://w3c.github.io/push-api/#dom-pushmanager-supportedcontentencodings
             const contentEncoding = (PushManager.supportedContentEncodings || ["aes128gcm"])[0];
             try {
-                await (0, Backend_1.prepareRequest)(this.registerUrl)
+                await (0, Backend_1.prepareRequest)(this.#registerUrl)
                     .post({
                     remove: remove,
                     endpoint: subscription.endpoint,