forked from Hubs-Foundation/hubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions.js
106 lines (83 loc) · 3.01 KB
/
subscriptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import nextTick from "./utils/next-tick.js";
const INIT_TIMEOUT_MS = 5000;
// Manages web push subscriptions
//
function urlBase64ToUint8Array(base64String) {
const padding = "=".repeat((4 - (base64String.length % 4)) % 4);
const base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/");
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
export default class Subscriptions {
constructor(hubId) {
this.hubId = hubId;
}
setHubChannel = hubChannel => {
this.hubChannel = hubChannel;
};
setRegistration = registration => {
this.registration = registration;
};
setRegistrationFailed = () => {
this.registration = null;
};
setVapidPublicKey = vapidPublicKey => {
this.vapidPublicKey = vapidPublicKey;
};
setSubscribed = isSubscribed => {
this._isSubscribed = isSubscribed;
};
isSubscribed = () => {
return this._isSubscribed;
};
getCurrentEndpoint = async () => {
if (!navigator.serviceWorker) return null;
const startedAt = performance.now();
// registration becomes null if failed, non null if registered
while (this.registration === undefined && performance.now() - startedAt < INIT_TIMEOUT_MS) await nextTick();
if (performance.now() - startedAt >= INIT_TIMEOUT_MS) console.warn("Service worker registration timed out.");
if (!this.registration || !this.registration.pushManager) return null;
while (this.vapidPublicKey === undefined) await nextTick();
if (this.vapidPublicKey === null) return null;
try {
const convertedVapidKey = urlBase64ToUint8Array(this.vapidPublicKey);
if (
(await this.registration.pushManager.permissionState({
userVisibleOnly: true,
applicationServerKey: convertedVapidKey
})) !== "granted"
)
return null;
} catch (e) {
return null; // Chrome can throw here complaining about userVisible if push is not right
}
const sub = await this.registration.pushManager.getSubscription();
if (!sub) return null;
return sub.endpoint;
};
toggle = async () => {
if (!this.registration) return;
if (this._isSubscribed) {
const pushSubscription = await this.registration.pushManager.getSubscription();
const res = await this.hubChannel.unsubscribe(pushSubscription);
if (res && res.has_remaining_subscriptions === false) {
pushSubscription.unsubscribe();
}
} else {
let pushSubscription = await this.registration.pushManager.getSubscription();
if (!pushSubscription) {
const convertedVapidKey = urlBase64ToUint8Array(this.vapidPublicKey);
pushSubscription = await this.registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: convertedVapidKey
});
}
this.hubChannel.subscribe(pushSubscription);
}
this._isSubscribed = !this._isSubscribed;
};
}