forked from adolfdaniel/widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.js
99 lines (83 loc) · 2.7 KB
/
counter.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
const COUNTER_STORAGE = 'counter-db';
const COUNTER_KEY = 'counter';
const COUNTER_NAME = 'widget-clicks';
let db = null;
const COUNT_TYPE = {
DEFAULT: 'default',
INSTALL: 'install',
CLICK: 'click',
ACTIVATE: 'activate',
SW_ACTIVATE: 'swActivate',
};
const openDatabase = async () => {
if (db) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
// Let us open our training sample database.
const DBOpenRequest = indexedDB.open(COUNTER_STORAGE, 2);
// Register two event handlers to act on the database being opened successfully, or not.
DBOpenRequest.onerror = (e) => {
reject(new Error('Error loading database.', e));
};
DBOpenRequest.onupgradeneeded = (event) => {
const db = DBOpenRequest.result;
if (event.oldVersion < 1) {
db.createObjectStore(COUNTER_STORAGE, { keyPath: COUNTER_KEY });
}
if (event.oldVersion < 2) {
const objectStore = DBOpenRequest.transaction.objectStore(COUNTER_STORAGE);
for (const type of Object.values(COUNT_TYPE)) {
objectStore.createIndex(type, type, { unique: false });
}
}
};
DBOpenRequest.onsuccess = () => {
db = DBOpenRequest.result;
resolve();
};
});
};
const getObjectStore = async () => {
await openDatabase();
// open a read/write db transaction
const transaction = db.transaction([COUNTER_STORAGE], 'readwrite');
// report on the error of opening the transaction
transaction.onerror = (event) => {
console.error('Transaction failed', event);
}
// create an object store on the transaction
return transaction.objectStore(COUNTER_STORAGE);
};
const getCount = async (tag, type) => {
type = type || COUNT_TYPE.DEFAULT;
const objectStore = await getObjectStore();
return new Promise((resolve, reject) => {
const objectStoreRequest = objectStore.get(COUNTER_NAME + tag);
objectStoreRequest.onerror = (event) => {
// report the error of the request
console.error('Object Store Request failed', event);
reject();
};
objectStoreRequest.onsuccess = () => {
const { result } = objectStoreRequest;
const count = result ? result[type] : 0;
resolve(count || 0);
};
});
};
const putCounts = async (tag, counts) => {
const key = COUNTER_NAME + tag;
const objectStore = await getObjectStore();
return new Promise((resolve, reject) => {
const objectStoreRequest = objectStore.put({ [COUNTER_KEY]: key, ...counts });
objectStoreRequest.onerror = (event) => {
// report the error of the request
console.error('Object Store Request failed', event);
reject();
};
objectStoreRequest.onsuccess = () => {
resolve();
};
});
};