This repository was archived by the owner on Apr 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathsw.ts
More file actions
74 lines (64 loc) · 2.37 KB
/
Copy pathsw.ts
File metadata and controls
74 lines (64 loc) · 2.37 KB
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
self.addEventListener("periodicsync", (event) => {
if (event.tag === "check-inflight-payments") {
event.waitUntil(checkPaymentsInFlight());
}
});
async function checkPaymentsInFlight() {
console.log('checkPaymentsInFlight');
const db = await openDatabase();
const transaction = db.transaction('wallet_store', 'readonly');
const store = transaction.objectStore('wallet_store');
// Get keys prefixed with "payment_outbound"
const keys = await getAllKeysWithPrefix(store, 'payment_outbound');
for (let key of keys) {
const payment = await get(store, key);
console.log(payment.status);
if (payment && payment.status === "InFlight") {
showNotification();
break;
}
}
transaction.commit();
}
function openDatabase(): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const request = indexedDB.open('wallet');
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
function getAllKeysWithPrefix(store: IDBObjectStore, prefix: string): Promise<string[]> {
return new Promise((resolve, reject) => {
const keys: string[] = [];
const cursorRequest = store.openKeyCursor();
cursorRequest.onsuccess = function(event) {
const cursor = (event.target as IDBRequest).result as IDBCursor;
if (cursor) {
if (cursor.key.toString().startsWith(prefix)) {
keys.push(cursor.key.toString());
}
cursor.continue();
} else {
resolve(keys);
}
};
cursorRequest.onerror = function() {
reject(cursorRequest.error);
};
});
}
function get(store: IDBObjectStore, key: string): Promise<any> {
return new Promise((resolve, reject) => {
const request = store.get(key);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
function showNotification() {
// todo make pretty
self.registration.showNotification('Payment Alert', {
body: 'There are payments with status InFlight.',
// icon: '/path/to/icon.png', // You can specify an icon if you have one
// badge: '/path/to/badge.png' // You can specify a badge if you have one
});
}