-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.worker.ts
434 lines (415 loc) · 12.3 KB
/
table.worker.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
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
import type { TableWorkerRequestMessage, TableRow, TableWorkerRequestMessageActionType, DerefRow, DoneHandler, StarterPackage } from '@type';
self.onmessage = function requestHandler(e: MessageEvent) {
const eventData = e.data as TableWorkerRequestMessage;
if (eventData.dataBaseName === undefined) {
return;
}
switch (eventData.type) {
case 'stream':
stream(eventData);
break;
case 'startingPackage':
if (eventData.scope === undefined) {
return postMessage({
type: 'error',
data: 'undefined action',
});
}
const starterPackage: StarterPackage = {
startingCount: undefined,
startingColumns: undefined,
starterRows: undefined,
};
const starterHandler = {
set(target: StarterPackage, prop: keyof StarterPackage, value: number | string[] | DerefRow[]) {
// @ts-expect-error the values will match the prop
target[prop] = value;
if (target['starterRows'] && target['startingColumns'] && target['startingCount']) {
postMessage({
type: 'startingPackage',
data: target,
});
}
return true;
},
};
const proxy = new Proxy(starterPackage, starterHandler);
getCount(eventData.dataBaseName, eventData.dbVersion, eventData.storeName, (count: number | undefined) => {
if (count !== undefined) {
proxy.startingCount = count;
}
});
getColumns(eventData.dataBaseName, eventData.dbVersion, eventData.storeName, (cols: string[] | undefined) => {
if (cols !== undefined) {
proxy.startingColumns = cols;
}
});
getStartingRows(eventData.dataBaseName, eventData.dbVersion, eventData.storeName, eventData.scope, (rows: DerefRow[] | undefined) => {
if (rows !== undefined) {
proxy.starterRows = rows;
}
});
break;
case 'columns':
getColumns(eventData.dataBaseName, eventData.dbVersion, eventData.storeName);
break;
case 'count':
getCount(eventData.dataBaseName, eventData.dbVersion, eventData.storeName);
break;
case 'startingRows':
if (eventData.scope === undefined) {
return postMessage({
type: 'error',
data: 'undefined action',
});
}
getStartingRows(eventData.dataBaseName, eventData.dbVersion, eventData.storeName, eventData.scope);
break;
default:
postMessage({ type: 'error', data: 'unknown request type' });
break;
}
};
function getStartingRows(
dataBaseName: string,
dbVersion: number,
storeName: string,
scope: number,
callback?: (rows: DerefRow[] | undefined) => void | undefined
) {
const dbRequest = indexedDB.open(dataBaseName, dbVersion);
dbRequest.onblocked = function startingDBblocked() {
// console.error('starting Rows db blocked');
if (callback !== undefined) {
callback(undefined);
} else {
postMessage({
type: 'error',
data: 'Database access was blocked',
});
}
};
dbRequest.onerror = function startingDBerror() {
// console.error('starting Rows db error');
if (callback !== undefined) {
callback(undefined);
} else {
postMessage({
type: 'error',
data: 'Database access failed',
});
}
};
dbRequest.onsuccess = function startingDBsuccess() {
const db = dbRequest.result;
if (db.objectStoreNames.contains(storeName)) {
const transaction = db.transaction(storeName, 'readonly');
const oStore = transaction.objectStore(storeName);
let received: number = 0;
let counter: number = 0;
const done: DoneHandler = {
data: [],
add: { row: 0 },
};
const doneListener = {
set(target: DoneHandler, prop: keyof DoneHandler, value: boolean | DerefRow) {
if (prop === 'add' && typeof value === 'object') {
target['data'].push(value);
received += 1;
}
if (received === scope) {
if (callback !== undefined) {
callback(target['data']);
} else {
postMessage({ type: 'startingRows', data: target['data'] });
}
}
return true;
},
};
const doneHandler = new Proxy(done, doneListener);
const cursorRequest = oStore.openCursor(null, 'nextunique');
cursorRequest.onsuccess = function startingCursorSuccess() {
const cursor: IDBCursorWithValue | null = cursorRequest.result;
if (cursor) {
if (counter < scope) {
fillReferences(db, cursor.value, undefined, undefined, doneHandler);
counter += 1;
cursor.continue();
}
}
};
} else {
if (callback !== undefined) {
console.error('oStore does not exist on db');
callback(undefined);
}
}
};
}
function getColumns(dataBaseName: string, dbVersion: number, storeName: string, callback?: (cols: string[] | undefined) => void) {
const dbRequest = indexedDB.open(dataBaseName, dbVersion);
dbRequest.onsuccess = function getColumnDBsuccess() {
const db = dbRequest.result;
if (!db.objectStoreNames.contains(storeName)) {
if (callback !== undefined) {
callback(undefined);
} else {
postMessage({
type: 'error',
data: 'unknown Object Store',
});
}
} else {
const columnTransaction = db.transaction(storeName, 'readonly', { durability: 'strict' });
const oStore = columnTransaction.objectStore(storeName);
const cursorRequest = oStore.openCursor(null);
cursorRequest.onsuccess = function getColumnsCursorSuccess() {
let cursor = cursorRequest.result ?? false;
if (cursor) {
if (callback !== undefined) {
callback(Object.keys(cursor.value));
} else {
postMessage({
type: 'columns',
data: Object.keys(cursor.value),
});
}
cursor = false;
}
};
cursorRequest.onerror = function getColumnsCursorError() {
if (callback !== undefined) {
callback(undefined);
} else {
postMessage({
type: 'error',
data: 'Database cursor request failed, while getting column names',
});
}
};
}
dbRequest.onerror = function getColumnDBerror() {
if (callback !== undefined) {
callback(undefined);
} else {
postMessage({
type: 'error',
data: 'Database access failed, while getting column names',
});
}
};
};
}
function getCount(dataBaseName: string, dbVersion: number, storeName: string, callback?: (count: number | undefined) => void) {
const dbRequest = indexedDB.open(dataBaseName, dbVersion);
dbRequest.onsuccess = function getCountDBrequest() {
const db = dbRequest.result;
if (!db.objectStoreNames.contains(storeName)) {
if (callback !== undefined) {
callback(undefined);
} else {
postMessage({
type: 'error',
data: 'unknown object store',
});
}
} else {
const countTransaction = db.transaction(storeName, 'readonly', { durability: 'strict' });
const oStore = countTransaction.objectStore(storeName);
const countRequest = oStore.count();
countRequest.onsuccess = function getCountCursorSuccess() {
if (callback !== undefined) {
callback(countRequest.result);
} else {
postMessage({ type: 'count', data: countRequest.result });
}
};
countRequest.onerror = function getCountCursorError() {
if (callback !== undefined) {
callback(undefined);
} else {
postMessage({
type: 'error',
data: 'Failed to get table entries',
});
}
};
}
};
dbRequest.onerror = function getCountDBError() {
if (callback !== undefined) {
callback(undefined);
} else {
postMessage({
type: 'error',
data: 'Database access Failed, while getting table entries',
});
}
};
}
function fillReferences(
dataBase: IDBDatabase,
row: TableRow,
callback?: () => void,
actionType?: TableWorkerRequestMessageActionType,
doneHandler?: DoneHandler
): void {
const copy = structuredClone(row);
const targetCounter = new WeakMap<TableRow, number>();
targetCounter.set(row, 0);
type CounterType = {
count: number;
};
const counter: CounterType = {
count: 0,
};
// this handler acts as a sort of event listener
const handler = {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/set#parameters
set(target: CounterType, prop: keyof CounterType, value: number) {
target[prop] = value;
const currentTargetCount = targetCounter.get(row);
if (currentTargetCount !== undefined) {
if (value === currentTargetCount) {
// if we are on the last result finally send the back the response
// this is a very neat way of handling things
if (actionType !== undefined) {
postStream(copy, actionType, row.row, callback);
}
if (doneHandler !== undefined) {
doneHandler.add = copy;
}
}
}
// every time we increase the counter we check if it was the last request
return true;
},
};
const proxy = new Proxy(counter, handler);
for (const [key, value] of Object.entries(row)) {
if (value instanceof ArrayBuffer) {
//in row $copy.row : $key is of type ArrayBuffer
// increase $targetCunt by $increment
const byteLength = new DataView(value).byteLength;
const elementCount = byteLength / 2;
const targetCount = targetCounter.get(row);
if (targetCount !== undefined) {
targetCounter.set(row, targetCount + elementCount);
}
// replace the ArrayBuffer with an regular array in our copy
copy[key] = new Array(elementCount);
for (let i = 0; i < byteLength; i += 2) {
const location = new DataView(value).getInt16(i);
// the ArrayBuffer has stored $location at $i
// query the object store $key at $location
if (dataBase.objectStoreNames.contains(key)) {
const transaction = dataBase.transaction(key, 'readonly');
const oStore = transaction.objectStore(key);
const only = IDBKeyRange.only(location);
const request = oStore.get(only);
request.onsuccess = function fillReferencesRequestSuccess() {
// insert the requested item into our copy
// @ts-expect-error we know we are writing to an array
copy[key][i / 2] = request.result;
// increase the counter
proxy.count += 1;
};
request.onerror = function fillReferencesRequestError() {
console.error('referencing error');
proxy.count += 1;
};
}
}
}
}
const targeted = targetCounter.get(row);
if (targeted !== undefined) {
if (targeted === 0) {
if (actionType !== undefined) {
postStream(copy, actionType, copy.row, callback);
}
if (doneHandler !== undefined) {
doneHandler.add = copy;
}
}
}
}
function postStream(oStoreItem: DerefRow, actionType: TableWorkerRequestMessageActionType, position: number, callback?: () => void): void {
if (Object.keys(oStoreItem).includes('row') && typeof oStoreItem.row === 'number') {
// what if there is a row property
postMessage({
type: 'stream',
action: actionType,
data: oStoreItem,
index: oStoreItem.row,
});
} else {
// if there is now row prop take the one from the request add add it
oStoreItem.row = position;
postMessage({
type: 'stream',
action: actionType,
data: oStoreItem,
index: position,
});
}
if (callback !== undefined) {
callback();
}
}
function stream(eventData: TableWorkerRequestMessage) {
const dbRequest = indexedDB.open(eventData.dataBaseName, eventData.dbVersion);
dbRequest.onsuccess = function streamDBsuccess() {
const streamDB = dbRequest.result;
if (streamDB.objectStoreNames.contains(eventData.storeName)) {
const transaction = streamDB.transaction(eventData.storeName, 'readwrite');
const oStore = transaction.objectStore(eventData.storeName);
if (eventData.action === undefined) {
transaction.abort();
return postMessage({
type: 'error',
data: 'undefined action',
});
}
const only = IDBKeyRange.only(eventData.action.pos);
const req = oStore.get(only);
req.onsuccess = function streamRequestSuccess() {
if (eventData.action === undefined) {
transaction.abort();
return postMessage({
type: 'error',
data: 'undefined action',
});
}
if (req.result) {
const row = req.result as TableRow;
fillReferences(
streamDB,
row,
() => {
// transaction.commit();
},
eventData.action.type
);
}
};
req.onerror = function streamRequestError() {
transaction.abort();
if (eventData.action === undefined) {
return postMessage({
type: 'error',
data: 'undefined action',
});
}
postMessage({
type: 'error',
action: eventData.action.type,
data: 'undefined action',
index: eventData.action.pos,
});
};
}
};
dbRequest.onerror = function streamDBerror() {};
}