/
wrap-idb-value.ts
378 lines (346 loc) · 12 KB
/
wrap-idb-value.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import {
IDBPCursor,
IDBPCursorWithValue,
IDBPDatabase,
IDBPIndex,
IDBPObjectStore,
IDBPTransaction,
} from './entry';
import { Constructor, Func, instanceOfAny } from './util';
import {
LOCAL_CHANGES_STORE,
IGNORED_STORES,
VERSION_ATTRIBUTE,
CHANGE_EVENT_NAME,
BROADCAST_CHANNEL_NAME,
} from './constants';
let idbProxyableTypes: Constructor[];
let cursorAdvanceMethods: Func[];
// This is a function to prevent it throwing up in node environments.
function getIdbProxyableTypes(): Constructor[] {
return (
idbProxyableTypes ||
(idbProxyableTypes = [
IDBDatabase,
IDBObjectStore,
IDBIndex,
IDBCursor,
IDBTransaction,
])
);
}
// This is a function to prevent it throwing up in node environments.
function getCursorAdvanceMethods(): Func[] {
return (
cursorAdvanceMethods ||
(cursorAdvanceMethods = [
IDBCursor.prototype.advance,
IDBCursor.prototype.continue,
IDBCursor.prototype.continuePrimaryKey,
])
);
}
const writeMethods = [
IDBObjectStore.prototype.add,
IDBObjectStore.prototype.put,
IDBObjectStore.prototype.delete,
IDBObjectStore.prototype.clear,
];
export class UpdateEvent extends Event {
constructor(readonly impactedStores: string[]) {
super(CHANGE_EVENT_NAME);
}
}
let channel: BroadcastChannel;
if (typeof BroadcastChannel === 'function') {
channel = new BroadcastChannel(BROADCAST_CHANNEL_NAME);
channel.onmessage = (evt) => {
const impactedStores = evt.data as string[];
dispatchEvent(new UpdateEvent(impactedStores));
};
}
const cursorRequestMap: WeakMap<
IDBPCursor,
IDBRequest<IDBCursor>
> = new WeakMap();
const transactionDoneMap: WeakMap<
IDBTransaction,
Promise<void>
> = new WeakMap();
const transactionStoreNamesMap: WeakMap<IDBTransaction, string[]> =
new WeakMap();
const transformCache = new WeakMap();
export const reverseTransformCache = new WeakMap();
function promisifyRequest<T>(request: IDBRequest<T>): Promise<T> {
const promise = new Promise<T>((resolve, reject) => {
const unlisten = () => {
request.removeEventListener('success', success);
request.removeEventListener('error', error);
};
const success = () => {
resolve(wrap(request.result as any) as any);
unlisten();
};
const error = () => {
reject(request.error);
unlisten();
};
request.addEventListener('success', success);
request.addEventListener('error', error);
});
promise
.then((value) => {
// Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
// (see wrapFunction).
if (value instanceof IDBCursor) {
cursorRequestMap.set(
value as unknown as IDBPCursor,
request as unknown as IDBRequest<IDBCursor>,
);
}
// Catching to avoid "Uncaught Promise exceptions"
})
.catch(() => {});
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
// is because we create many promises from a single IDBRequest.
reverseTransformCache.set(promise, request);
return promise;
}
function cacheDonePromiseForTransaction(tx: IDBTransaction): void {
// Early bail if we've already created a done promise for this transaction.
if (transactionDoneMap.has(tx)) return;
const done = new Promise<void>((resolve, reject) => {
const unlisten = () => {
tx.removeEventListener('complete', complete);
tx.removeEventListener('error', error);
tx.removeEventListener('abort', error);
};
const complete = () => {
resolve();
unlisten();
};
const error = () => {
reject(tx.error || new DOMException('AbortError', 'AbortError'));
unlisten();
};
tx.addEventListener('complete', complete);
tx.addEventListener('error', error);
tx.addEventListener('abort', error);
});
// Cache it for later retrieval.
transactionDoneMap.set(tx, done);
}
let idbProxyTraps: ProxyHandler<any> = {
get(target, prop, receiver) {
if (target instanceof IDBTransaction) {
// Special handling for transaction.done.
if (prop === 'done') return transactionDoneMap.get(target);
// Polyfill for objectStoreNames because of Edge.
if (prop === 'objectStoreNames') {
return target.objectStoreNames || transactionStoreNamesMap.get(target);
}
// Make tx.store return the only store in the transaction, or undefined if there are many.
if (prop === 'store') {
const storeNames = receiver.objectStoreNames;
if (storeNames.length === 1) {
return receiver.objectStore(storeNames[0]);
} else if (
storeNames.length === 2 &&
storeNames.contains(LOCAL_CHANGES_STORE)
) {
for (let storeName of storeNames) {
if (storeName !== LOCAL_CHANGES_STORE) {
return receiver.objectStore(storeName);
}
}
}
return undefined;
}
}
// Else transform whatever we get back.
return wrap(target[prop]);
},
set(target, prop, value) {
target[prop] = value;
return true;
},
has(target, prop) {
if (
target instanceof IDBTransaction &&
(prop === 'done' || prop === 'store')
) {
return true;
}
return prop in target;
},
};
export function replaceTraps(
callback: (currentTraps: ProxyHandler<any>) => ProxyHandler<any>,
): void {
idbProxyTraps = callback(idbProxyTraps);
}
function wrapFunction<T extends Func>(func: T): Function {
// Due to expected object equality (which is enforced by the caching in `wrap`), we
// only create one new func per func.
// Edge doesn't support objectStoreNames (booo), so we polyfill it here.
if (
func === IDBDatabase.prototype.transaction &&
!('objectStoreNames' in IDBTransaction.prototype)
) {
return function (
this: IDBPDatabase,
storeNames: string | string[],
...args: any[]
) {
const tx = func.call(unwrap(this), storeNames, ...args);
transactionStoreNamesMap.set(
tx,
(storeNames as any).sort ? (storeNames as any[]).sort() : [storeNames],
);
return wrap(tx);
};
}
// Cursor methods are special, as the behaviour is a little more different to standard IDB. In
// IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
// cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
// with real promises, so each advance methods returns a new promise for the cursor object, or
// undefined if the end of the cursor has been reached.
if (getCursorAdvanceMethods().includes(func)) {
return function (this: IDBPCursor, ...args: Parameters<T>) {
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
// the original object.
func.apply(unwrap(this), args);
return wrap(cursorRequestMap.get(this)!);
};
}
return function (this: any, ...args: Parameters<T>) {
if (
func === IDBDatabase.prototype.transaction &&
args[1] === 'readwrite' &&
args[0] !== LOCAL_CHANGES_STORE
) {
if (!Array.isArray(args[0])) {
args[0] = [args[0]];
}
// transform `db.transaction("my-store", "readwrite")` into `db.transaction(["my-store", "_local_changes"], "readwrite")`
args[0].push(LOCAL_CHANGES_STORE);
// @ts-ignore
const transaction = wrap(
func.apply(unwrap(this), args),
) as IDBPTransaction;
transaction.done.then(() => {
const impactedStores = Array.from(transaction.objectStoreNames);
// notify LiveQueries in the same browser tab
dispatchEvent(new UpdateEvent(impactedStores));
// notify other browser tabs
channel?.postMessage(impactedStores);
});
return transaction;
}
// track any update into the _local_changes store
if (writeMethods.includes(func)) {
const storeName = this.name;
// updates from the server are not tracked, i.e. `store.add(value, key, true)` or `store.delete(key, true)`
const isUpdateIgnored =
args.length === 3 ||
(args.length === 2 && func === IDBObjectStore.prototype.delete);
if (!IGNORED_STORES.includes(storeName) && !isUpdateIgnored) {
const store = this.transaction.objectStore(
LOCAL_CHANGES_STORE,
) as IDBObjectStore;
const change: any = {
operation: func.name,
storeName,
};
switch (func) {
case IDBObjectStore.prototype.clear:
change.operation = 'delete';
this.getAllKeys().then((keys: IDBValidKey[]) => {
keys.forEach((key) => {
change.key = key;
store.add(change);
});
});
break;
case IDBObjectStore.prototype.delete:
change.key = args[0];
store.add(change);
break;
default:
// add or put
const value = args[0];
const key = args[1] || value[this.keyPath];
if (typeof value === 'object') {
value[VERSION_ATTRIBUTE] = (value[VERSION_ATTRIBUTE] || 0) + 1;
}
change.key = key;
change.value = value; // store the full entity
store.add(change);
break;
}
}
}
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
// the original object.
return wrap(func.apply(unwrap(this), args));
};
}
function transformCachableValue(value: any): any {
if (typeof value === 'function') return wrapFunction(value);
// This doesn't return, it just creates a 'done' promise for the transaction,
// which is later returned for transaction.done (see idbObjectHandler).
if (value instanceof IDBTransaction) cacheDonePromiseForTransaction(value);
if (instanceOfAny(value, getIdbProxyableTypes()))
return new Proxy(value, idbProxyTraps);
// Return the same value back if we're not going to transform it.
return value;
}
/**
* Enhance an IDB object with helpers.
*
* @param value The thing to enhance.
*/
export function wrap(value: IDBDatabase): IDBPDatabase;
export function wrap(value: IDBIndex): IDBPIndex;
export function wrap(value: IDBObjectStore): IDBPObjectStore;
export function wrap(value: IDBTransaction): IDBPTransaction;
export function wrap(
value: IDBOpenDBRequest,
): Promise<IDBPDatabase | undefined>;
export function wrap<T>(value: IDBRequest<T>): Promise<T>;
export function wrap(value: any): any {
// We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
// IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
if (value instanceof IDBRequest) return promisifyRequest(value);
// If we've already transformed this value before, reuse the transformed value.
// This is faster, but it also provides object equality.
if (transformCache.has(value)) return transformCache.get(value);
const newValue = transformCachableValue(value);
// Not all types are transformed.
// These may be primitive types, so they can't be WeakMap keys.
if (newValue !== value) {
transformCache.set(value, newValue);
reverseTransformCache.set(newValue, value);
}
return newValue;
}
/**
* Revert an enhanced IDB object to a plain old miserable IDB one.
*
* Will also revert a promise back to an IDBRequest.
*
* @param value The enhanced object to revert.
*/
interface Unwrap {
(value: IDBPCursorWithValue<any, any, any, any, any>): IDBCursorWithValue;
(value: IDBPCursor<any, any, any, any, any>): IDBCursor;
(value: IDBPDatabase): IDBDatabase;
(value: IDBPIndex<any, any, any, any, any>): IDBIndex;
(value: IDBPObjectStore<any, any, any, any>): IDBObjectStore;
(value: IDBPTransaction<any, any, any>): IDBTransaction;
<T extends any>(value: Promise<IDBPDatabase<T>>): IDBOpenDBRequest;
(value: Promise<IDBPDatabase>): IDBOpenDBRequest;
<T>(value: Promise<T>): IDBRequest<T>;
}
export const unwrap: Unwrap = (value: any): any =>
reverseTransformCache.get(value);