-
Notifications
You must be signed in to change notification settings - Fork 570
/
IndexedDbStorage.ts
85 lines (66 loc) · 2.32 KB
/
IndexedDbStorage.ts
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
import { Storage } from "./Storage";
const STORE_NAME = "IdentityIds";
export class IndexedDbStorage implements Storage {
constructor(private readonly dbName: string = "aws:cognito-identity-ids") {}
getItem(key: string): Promise<string | null> {
return this.withObjectStore("readonly", (store) => {
const req = store.get(key);
return new Promise<string | null>((resolve) => {
req.onerror = () => resolve(null);
req.onsuccess = () => resolve(req.result ? req.result.value : null);
});
}).catch(() => null);
}
removeItem(key: string): Promise<void> {
return this.withObjectStore("readwrite", (store) => {
const req = store.delete(key);
return new Promise<void>((resolve, reject) => {
req.onerror = () => reject(req.error);
req.onsuccess = () => resolve();
});
});
}
setItem(id: string, value: string): Promise<void> {
return this.withObjectStore("readwrite", (store) => {
const req = store.put({ id, value });
return new Promise<void>((resolve, reject) => {
req.onerror = () => reject(req.error);
req.onsuccess = () => resolve();
});
});
}
private getDb(): Promise<IDBDatabase> {
const openDbRequest = self.indexedDB.open(this.dbName, 1);
return new Promise((resolve, reject) => {
openDbRequest.onsuccess = () => {
resolve(openDbRequest.result);
};
openDbRequest.onerror = () => {
reject(openDbRequest.error);
};
openDbRequest.onblocked = () => {
reject(new Error("Unable to access DB"));
};
openDbRequest.onupgradeneeded = () => {
const db = openDbRequest.result;
db.onerror = () => {
reject(new Error("Failed to create object store"));
};
db.createObjectStore(STORE_NAME, { keyPath: "id" });
};
});
}
private withObjectStore<R>(mode: IDBTransactionMode, action: (store: IDBObjectStore) => Promise<R>): Promise<R> {
return this.getDb().then((db) => {
const tx = db.transaction(STORE_NAME, mode);
tx.oncomplete = () => db.close();
return new Promise<R>((resolve, reject) => {
tx.onerror = () => reject(tx.error);
resolve(action(tx.objectStore(STORE_NAME)));
}).catch((err) => {
db.close();
throw err;
});
});
}
}