-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
566 lines (466 loc) · 13.7 KB
/
index.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import { useState, useEffect, useCallback, useRef, useMemo } from 'react';
import Dexie from 'dexie';
import 'dexie-observable';
import { nanoid } from 'nanoid';
import isEqual from 'lodash.isequal';
const emptyObj = {};
let db;
let monitor = false;
class TransactionMonitor {
constructor() {
this.active = new Map();
this.fulfilled = new Map();
this.maxActive = 0;
this.elapsed = [];
}
getStats() {
const active = this.active.size;
const fulfilled = this.fulfilled.size;
const avg = (this.elapsed.reduce((acc, val) => acc + val, 0) / this.elapsed.length).toFixed(2);
const avgLast10 = (
this.elapsed.slice(0, 10).reduce((acc, val) => acc + val, 0) / this.elapsed.length
).toFixed(2);
return {
maxActive: this.maxActive,
active,
fulfilled,
avg,
avgLast10,
};
}
start() {
const id = nanoid();
const startTime = Date.now();
this.active.set(id, { id, startTime, open: true });
const active = this.active.size;
if (active > this.maxActive) this.maxActive = active;
return id;
}
end(id) {
const tx = this.active.get(id);
const elapsed = Date.now() - tx.startTime;
this.elapsed.push(elapsed);
this.active.delete(id);
this.fulfilled.set(id, { id, elapsed, open: false });
}
}
class DBDispatcher {
constructor() {
this.subscribers = new Map();
this.subscriptions = new Map();
this.engaged = false;
}
getStats() {
const subscribers = this.subscribers.size;
const subscriptions = this.subscriptions.size;
return {
subscriptions,
tables: [...this.subscriptions.keys()],
subscribers,
};
}
dispatchChanges(changes) {
const changedTables = new Set(changes.map((change) => change.table));
for (const key of changedTables) {
(this.subscriptions.get(key) || []).forEach((subscriber) => {
subscriber.cb(key);
});
}
}
subscribe(subscriberId, key, cb) {
if (!this.engaged) {
this.engaged = true;
db.on('changes', (changes) => this.dispatchChanges(changes));
}
if (!this.subscribers.has(subscriberId)) {
this.subscribers.set(subscriberId, key);
this.subscriptions.set(key, [
...(this.subscriptions.get(key) || []),
{ id: subscriberId, cb },
]);
}
}
unsubscribe(subscriberId) {
const key = this.subscribers.get(subscriberId);
if (key) {
const subscriptions = this.subscriptions.get(key).filter((s) => s.id !== subscriberId);
if (subscriptions.length === 0) this.subscriptions.delete(key);
else this.subscriptions.set(key, subscriptions);
}
this.subscribers.delete(subscriberId);
}
}
const dbDispatcher = new DBDispatcher();
const trxMonitor = new TransactionMonitor();
function executeTransaction(key, query, cb, cbError) {
let txId = monitor ? trxMonitor.start() : null;
db.transaction('rw?', db.table(key), (tx) => {
return query(tx.table(key));
})
.then((data) => {
if (txId) trxMonitor.end(txId);
return cb(data);
})
.catch((error) => {
if (txId) trxMonitor.end(txId);
if (cbError) cbError(error);
else throw new Error(`useDexie: Transaction ${key}: ${error}`);
});
}
function transaction(key, query, cb, cbError) {
if (db.isOpen()) executeTransaction(key, query, cb, cbError);
else {
db.open().then(() => executeTransaction(key, query, cb, cbError));
}
}
function composeWhere(query, where, join, forceWhere) {
for (const { field, operator, value = '', param, filter, or, and } of where) {
const joiner = !join || forceWhere ? 'where' : join;
const Value = (() => {
if (Array.isArray(value)) return JSON.stringify(value);
if (typeof value === 'boolean') return value;
if (!isNaN(value)) return Number(value);
const multiple = value.split(',');
if (multiple.length === 1) return `"${value}"`;
else {
const values = multiple.map((m) => {
if (typeof value === 'boolean') return value;
if (!isNaN(value)) return Number(value);
return `"${value}"`;
});
return values.join(',');
}
})();
let missingWhere = joiner === 'where' && !field;
if (!missingWhere) {
forceWhere = false;
if (['or', 'where'].includes(joiner)) {
//console.log(`query.${joiner}('${field}').${operator}(${Value})`);
query = new Function('query', `return query.${joiner}('${field}').${operator}(${Value})`)(
query
);
} else {
//console.log(`query.and(${filter.toString()})`, param);
query = new Function('query,param', `return query.and(${filter.toString()})`)(query, param);
}
}
if (or) query = composeWhere(query, or, 'or', missingWhere);
if (and) query = composeWhere(query, and, 'and', missingWhere);
}
return query;
}
function composeQuery(dbTable, query = emptyObj) {
const {
where,
orderBy,
reverse,
offset,
limit,
filter,
count,
primaryKeys,
erase,
toArray = true,
} = query;
const canOrderBy = orderBy
? dbTable.schema.indexes.findIndex((i) => i.keyPath === orderBy) > -1 && !where
: false;
if (where !== undefined && where.length > 0) dbTable = composeWhere(dbTable, where);
if (canOrderBy) dbTable = dbTable.orderBy(orderBy);
if (filter !== undefined) dbTable = dbTable.filter(filter);
if (reverse !== undefined) dbTable = dbTable.reverse();
if (offset !== undefined) dbTable = dbTable.offset(offset);
if (limit !== undefined) dbTable = dbTable.limit(limit);
if (count === true) dbTable = dbTable.count();
if (toArray && !count && !primaryKeys && !erase) dbTable = dbTable.toArray();
if (primaryKeys) dbTable = dbTable.primaryKeys();
if (erase === true) dbTable = dbTable.delete();
return dbTable;
}
function useSubscribeToDBChanges(key, cb) {
const callerIdRef = useRef(nanoid());
const subscribe = useCallback((key, cb) => {
if (key) dbDispatcher.subscribe(callerIdRef.current, key, cb);
}, []);
useEffect(() => {
if (key) dbDispatcher.subscribe(callerIdRef.current, key, cb);
// eslint-disable-next-line react-hooks/exhaustive-deps
return () => dbDispatcher.unsubscribe(callerIdRef.current);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return subscribe;
}
function useExecuteQuery(key, options) {
const [rawData, setRawData] = useState();
const dataRef = useRef();
const optionsRef = useRef();
const subscribeToChanges = useSubscribeToDBChanges();
const execute = useCallback(() => {
if (optionsRef.current === null) return;
transaction(
key,
(dbTable) => composeQuery(dbTable, optionsRef.current),
(data) => {
if (!isEqual(dataRef.current, data)) {
dataRef.current = data;
setRawData(data);
}
}
);
}, [key]);
useEffect(() => {
if (!isEqual(optionsRef.current, options)) {
optionsRef.current = options;
execute();
}
}, [execute, options]);
useEffect(() => {
subscribeToChanges(key, () => execute());
}, [execute, key, subscribeToChanges]);
return rawData;
}
const toObj = (idField = 'id') => (data) =>
data.reduce((rv, x) => {
rv[x[idField]] = x;
return rv;
}, {});
const toMap = (idField = 'id') => (data) =>
data.reduce((rv, x) => {
rv.set(x[idField], x);
return rv;
}, new Map());
const toSet = (idField = 'id') => (data) =>
data.reduce((rv, x) => {
rv.add(x[idField]);
return rv;
}, new Set());
function useDataType(TypeFunc, Table, params) {
let options = emptyObj;
let idField = 'id';
let cb;
params.forEach((param) => {
if (typeof param === 'object') options = param;
else if (typeof param === 'string') idField = param;
else if (typeof param === 'function') cb = param;
});
const data = useDexieTable(Table, options, TypeFunc(idField));
return cb && data ? cb(data) : data;
}
export function useDexie(name, ...params) {
const [database, setDatabase] = useState();
useEffect(() => {
if (database) return;
let version = 1;
let schema;
let cb;
for (const param of params) {
if (typeof param === 'object') schema = param;
else if (typeof param === 'number') version = param;
else if (typeof param === 'function') cb = param;
}
// Populate default DB
db = new Dexie(name);
if (typeof schema === 'object') {
db.version(version).stores(schema);
}
setDatabase(db);
cb && cb(db);
//return () => db && db.close();
}, []);
return database;
}
export const useDexieMonitor = (freq) => {
const [data, setData] = useState({});
const intervalRef = useRef();
useEffect(() => {
if (!freq) {
if (intervalRef.current) clearInterval(intervalRef.current);
return null;
}
monitor = true;
intervalRef.current = setInterval(() => {
const dispatch = dbDispatcher.getStats();
const tx = trxMonitor.getStats();
setData({ ...dispatch, ...tx });
}, freq);
return () => {
if (intervalRef.current) clearInterval(intervalRef.current);
};
}, [freq]);
return data;
};
export const useDexieTable = (Table, ...params) => {
const options = useMemo(() => (typeof params[0] === 'object' ? params[0] : emptyObj), [params]);
const cb = useMemo(() => (typeof params[0] === 'object' ? params[1] : params[0]), [params]);
const data = useExecuteQuery(Table, options);
if (!data) return;
return cb ? cb(data) : data;
};
export const useDexieObj = (t, ...p) => useDataType(toObj, t, p);
export const useDexieMap = (t, ...p) => useDataType(toMap, t, p);
export const useDexieSet = (t, ...p) => useDataType(toSet, t, p);
export function useDexieGetTable(Table, opts) {
const [key, setKey] = useState(Table);
const [options, setOptions] = useState(opts);
const cbRef = useRef();
const data = useExecuteQuery(key, options);
const func = useCallback(
(...params) => {
let id, opts, cb;
params.forEach((param) => {
if (typeof param === 'string') id = param;
else if (typeof param === 'object') opts = param;
else if (typeof param === 'function') cb = param;
});
cbRef.current = cb;
if ((id && !isEqual(id, key)) || (opts && !isEqual(opts, options))) {
if (id) setKey(id);
if (opts) setOptions(opts);
return;
}
if (data && cb) return cb(data);
return data;
},
[key, options, data]
);
useEffect(() => {
if (data && cbRef.current) {
cbRef.current(data);
cbRef.current = undefined;
}
}, [data]);
return func;
}
export function useDexieGetItem(Table, itemID, idField = 'id') {
const idMap = useRef(new Set(itemID ? [itemID] : []));
const [valuesMap, setValuesMap] = useState(new Map());
const fetchValues = useCallback(
async (cb) => {
transaction(
Table,
(table) =>
table
.where(idField)
.anyOf([...idMap.current])
.toArray(),
(data) => {
const newValuesMap = new Map(data.map((item) => [item[idField], item]));
cb && cb(newValuesMap);
setValuesMap(newValuesMap);
}
);
},
[idField, Table]
);
useSubscribeToDBChanges(Table, () => fetchValues());
const getItem = useCallback(
(id, cb) => {
if (!idMap.current.has(id)) {
idMap.current.add(id);
fetchValues((values) => {
if (cb) return cb(values.get(id));
});
} else {
if (cb) return cb(valuesMap.get(id));
return valuesMap.get(id);
}
},
[fetchValues, valuesMap]
);
useEffect(() => {
if (itemID && !idMap.current.has(itemID)) idMap.current.add(itemID);
if (idMap.current.size > 0) fetchValues();
}, [fetchValues, itemID]);
return itemID !== undefined ? valuesMap.get(itemID) : getItem;
}
export function useDexieGetItemKey(Table) {
const getKeys = useDexieGetTable(Table);
const cb = useCallback(
(query, cb) => {
getKeys({ ...query, primaryKeys: true, limit: 1 }, (keys) => {
cb && cb(keys[0]);
});
},
[getKeys]
);
return cb;
}
export function useDexieDeleteItem(Table) {
const cb = useCallback(
(key, cb) => {
transaction(
Table,
(dbTable) => dbTable.delete(key),
(data) => cb && cb(data)
);
},
[Table]
);
return cb;
}
export function useDexieDeleteByQuery(Table) {
const cb = useCallback(
(query, cb) => {
transaction(
Table,
(dbTable) => composeQuery(dbTable, { ...query, erase: true }),
(data) => cb && cb(data)
);
},
[Table]
);
return cb;
}
export function useDexiePutItem(Table) {
const cb = useCallback(
(item, cb) => {
transaction(
Table,
(dbTable) => dbTable.put(item),
(data) => cb && cb(data)
);
},
[Table]
);
return cb;
}
export function useDexiePutItems(Table) {
const cb = useCallback(
(items, cb) => {
transaction(
Table,
(dbTable) => dbTable.bulkPut(items),
(data) => cb && cb(data)
);
},
[Table]
);
return cb;
}
export function useDexieUpdateItem(Table) {
const getKey = useDexieGetItemKey(Table);
const getData = useDexieGetTable(Table);
const cb = useCallback(
(query, cbOrItem) => {
getKey(query, (key) => {
getData({ ...query, limit: 1 }, (data) => {
const item = data[0];
if (!item) return;
const newItem = typeof cbOrItem === 'function' ? cbOrItem(item) : cbOrItem;
transaction(
Table,
(dbTable) => {
dbTable.put(newItem, key);
return dbTable;
},
(data) => {
return data;
}
);
});
});
},
[Table, getData, getKey]
);
return cb;
}