Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 | 1x 1x 1x 1x 1x 1x 93x 93x 64x 64x 93x 89x 89x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 80x 80x 93x 93x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 1x 68x 69x 69x 155x 153x 153x 155x 69x 69x 2x 2x 2x 2x 2x 2x 69x 93x 84x 1x 1x 1x 84x 6x 6x 6x 17x 1x 1x 1x 1x 16x 16x 16x 17x 1x 1x 1x 1x 15x 15x 14x 1x 1x 1x 1x 1x 1x 17x 6x 6x 84x 93x 93x 46x 46x 46x 46x 93x 93x 93x 93x 8x 8x 8x 93x 8x 8x 8x 1x 1x 7x 8x 8x 1x 8x 7x 7x 8x 8x 93x 5x 5x 5x 1x 1x 4x 4x 4x 5x 5x 1x 5x 4x 4x 5x 5x 93x 8x 8x 8x 8x 8x 8x 8x 1x 1x 7x 8x 8x 1x 8x 7x 7x 8x 8x 93x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 5x 5x 5x 5x 6x 6x 3x 3x 3x 3x 6x 6x 93x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 1x 3x 3x 3x 4x 4x 93x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 5x 5x 5x 7x 7x 1x 7x 6x 6x 7x 7x 93x 20x 20x 20x 1x 1x 19x 19x 19x 20x 20x 5x 20x 15x 15x 20x 20x 93x 63x 63x 63x 2x 2x 61x 63x 63x 26x 63x 37x 37x 63x 63x 93x 9x 9x 9x 1x 1x 8x 9x 9x 4x 5x 5x 5x 9x 9x 93x 6x 6x 6x 1x 1x 5x 6x 6x 3x 3x 3x 3x 6x 6x 93x 1x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 10x 10x 26x 26x 26x 26x 26x 52x 22x 22x 22x 22x 30x 30x 30x 30x 30x 207x 112x 5x 5x 5x 5x 4x 5x 5x 1x 5x 112x 112x 30x 30x 4x 4x 4x 3x 3x 3x 3x 4x 1x 1x 4x 30x 30x 52x 26x 26x 8x 8x 3x 3x 3x 3x 2x 1x 1x 1x 1x 3x 3x 1x 3x 3x 8x 8x 26x 35x 35x 35x 24x 24x 24x 24x 22x 24x 24x 2x 24x 24x 35x 35x 26x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 3x 3x 1x 1x 1x 1x 3x 3x 3x 26x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 1x 2x 2x 2x 26x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 3x 3x 1x 1x 3x 3x 3x 26x 1x 1x 1x 26x 3x 3x 2x 2x 3x 26x 34x 29x 29x 59x 29x 6x 29x 23x 23x 29x 29x 29x 29x 29x 5x 34x 26x 8x 8x 26x 26x 26x 26x | import { isArray, isDefined, isFunction } from '@beerush/utils'; import { DB_SYNC_DELAY, IndexedStore } from './db.js'; import { count as countRecord, create, createRecord, DEFAULT_FIND_LIMIT, find, read, remove, update, } from './helper.js'; import { anchor, captureStack, derive, microtask, type StateUnsubscribe } from '@anchorlib/core'; import { type FilterFn, IDBStatus, type ReactiveTable, type Rec, type Row, type RowList, type RowListState, type RowState, } from './types.js'; /** * IndexedDB Table is a promise-first API for IndexedDB. * It provides a simple way to interact with IndexedDB. * As it is promise-first, users doesn't need to wait for the database to be ready before using it. * It also provides a simple way to subscribe to events. * * @template T - The base record type that extends Rec * @template R - The row type that extends Row<T>, defaults to Row<T> */ export class IndexedTable<T extends Rec, R extends Row<T> = Row<T>> extends IndexedStore { /** * Queue of operations to be executed once the database is ready */ protected queues = new Set<() => void>(); /** * Gets a read-only object store transaction */ get reader(): IDBObjectStore { return this.instance?.transaction(this.name).objectStore(this.name) as IDBObjectStore; } /** * Gets a read-write object store transaction */ get writer(): IDBObjectStore { return this.instance?.transaction(this.name, 'readwrite').objectStore(this.name) as IDBObjectStore; } /** * Creates a new IndexedTable instance * @param name - The name of the object store * @param version - The version of the database (default: 1) * @param indexes - Array of index names to create (default: undefined) * @param remIndexes - Array of index names to remove (default: undefined) * @param dbName - The name of the database (default: name) * @param seeds - Array of seed data to insert into the table for the first time (default: undefined) */ constructor( protected name: string, protected version = 1, protected indexes?: Array<keyof R>, protected remIndexes?: Array<keyof R>, dbName = name, protected seeds?: R[] ) { super(dbName, version); this.init(); if (dbName === name) { this.open(); } } /** * Handles database upgrade events * @param event - The IDBVersionChangeEvent */ protected upgrade(event: IDBVersionChangeEvent): void { const indexes = [...(this.indexes || [])]; if (!indexes.includes('created_at')) { indexes.push('created_at'); } if (!indexes.includes('updated_at')) { indexes.push('updated_at'); } const db = (event.target as IDBOpenDBRequest)?.result as IDBDatabase; const transaction = (event.target as IDBOpenDBRequest).transaction as IDBTransaction; const store = db.objectStoreNames.contains(this.name) ? transaction.objectStore(this.name) : db.createObjectStore(this.name, { keyPath: 'id' }); if (indexes.length) { for (const name of indexes as string[]) { if (!store.indexNames.contains(name)) { store.createIndex(name, name); } } } if (this.remIndexes?.length) { for (const name of this.remIndexes as string[]) { if (store.indexNames.contains(name)) { store.deleteIndex(name); } } } } /** * Sets up the table by seeding initial data if the table is empty. * This method is called during initialization to populate the table * with seed data if no records exist. * @returns {Promise<void>} A promise that resolves when setup is complete */ protected async setup(): Promise<void> { if (isDefined(this.seeds) && !isArray(this.seeds)) { const error = new Error('Seed Error: Invalid seed data'); captureStack.error.argument('The given data is not a valid seed data.', error); } if (isArray(this.seeds)) { const total = await countRecord(this.reader); if (total <= 0) { for (const seed of this.seeds) { if (typeof seed !== 'object' || seed === null) { const error = new Error(`Seed Error: Invalid seed data`); captureStack.error.external('Invalid seed data: Object is not JSON serializable.', error as Error); continue; } let data: string; try { data = JSON.stringify(seed, null, 2); } catch (err) { const error = new Error(`Seed Error: ${(err as Error).message}`); captureStack.error.external('Invalid seed data: Object is not JSON serializable.', error as Error); continue; } try { await create(this.writer, seed); } catch (err) { const error = new Error(`Seed Error: ${(err as Error).message}`); captureStack.error.external( `Unable to seed table "${this.name}@${this.version}" with:\n\n${data}`, error as Error ); } } } } } /** * Finalizes initialization and executes queued operations */ protected finalize() { for (const submit of this.queues) { if (isFunction(submit)) { submit(); } } this.queues.clear(); this.publish({ type: this.status }); } /** * Sets the seed data for the table. * This method allows updating the seed data that will be used to populate * the table when it's first created and empty. * * @param seeds - Array of seed data to insert into the table for the first time */ public seed(seeds: R[]): this { this.seeds = seeds; return this; } /** * Counts the number of records matching the filter criteria * @param filter - The filter criteria (IDBKeyRange or FilterFn) * @returns Promise resolving to the count of records */ public count(filter?: IDBKeyRange | FilterFn<R>): Promise<number> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } countRecord(this.reader, filter).then(resolve).catch(reject); }; if (this.status === IDBStatus.Init) { this.queues.add(submit); } else { submit(); } }); } /** * Counts the number of records by index matching the filter criteria * @param index - The index name to count on * @param filter - The filter criteria (IDBKeyRange or FilterFn) * @returns Promise resolving to the count of records */ public countByIndex(index: keyof R, filter?: IDBKeyRange | FilterFn<R>): Promise<number> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } countRecord(this.reader.index(index as string), filter) .then(resolve) .catch(reject); }; if (this.status === IDBStatus.Init) { this.queues.add(submit); } else { submit(); } }); } /** * Finds records matching the filter criteria * @param filter - The filter criteria (IDBKeyRange or FilterFn) * @param limit - Maximum number of records to return (default: DEFAULT_FIND_LIMIT) * @param direction - Cursor direction (default: undefined) * @returns Promise resolving to array of records */ public find( filter?: IDBKeyRange | FilterFn<R>, limit = DEFAULT_FIND_LIMIT, direction?: IDBCursorDirection ): Promise<R[]> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } find<R>(this.reader, filter, limit, direction).then(resolve).catch(reject); }; if (this.status === IDBStatus.Init) { this.queues.add(submit); } else { submit(); } }); } /** * Lists records matching the filter criteria with pagination support. * Returns both the records and the total count of matching records. * * @param filter - The filter criteria (IDBKeyRange or FilterFn) * @param limit - Maximum number of records to return (default: DEFAULT_FIND_LIMIT) * @param direction - Cursor direction for traversal (default: undefined) * @returns Promise resolving to RowList containing records and count */ public list( filter?: IDBKeyRange | FilterFn<R>, limit = DEFAULT_FIND_LIMIT, direction?: IDBCursorDirection ): Promise<RowList<T>> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } Promise.all([find(this.reader, filter, limit, direction), countRecord(this.reader, filter)]) .then(([rows, count]) => { resolve({ rows, count }); }) .catch(reject); }; if (this.status === IDBStatus.Init) { this.queues.add(submit); } else { submit(); } }); } /** * Lists records by index matching the filter criteria with pagination support. * Returns both the records and the total count of matching records. * * @param index - The index name to search on * @param filter - The filter criteria (IDBKeyRange or FilterFn) * @param limit - Maximum number of records to return (default: DEFAULT_FIND_LIMIT) * @param direction - Cursor direction for traversal (default: undefined) * @returns Promise resolving to RowList containing records and count */ public listByIndex( index: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit = DEFAULT_FIND_LIMIT, direction?: IDBCursorDirection ): Promise<RowList<T>> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } Promise.all([ find(this.reader.index(index as string), filter, limit, direction), countRecord(this.reader.index(index as string), filter), ]) .then(([rows, count]) => { resolve({ rows, count }); }) .catch(reject); }; if (this.status === IDBStatus.Init) { this.queues.add(submit); } else { submit(); } }); } /** * Finds records by index matching the filter criteria * @param index - The index name to search on * @param filter - The filter criteria (IDBKeyRange or FilterFn) * @param limit - Maximum number of records to return (default: DEFAULT_FIND_LIMIT) * @param direction - Cursor direction (default: undefined) * @returns Promise resolving to array of records */ public findByIndex( index: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit = DEFAULT_FIND_LIMIT, direction?: IDBCursorDirection ): Promise<R[]> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject('Table is closed.'); } find<R>(this.reader.index(index as string), filter, limit, direction) .then(resolve) .catch(reject); }; if (this.status === IDBStatus.Init) { this.queues.add(submit); } else { submit(); } }); } /** * Reads a single record by ID * @param id - The record ID * @returns Promise resolving to the record or undefined */ public read(id: string): Promise<R | undefined> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } read<R>(this.reader, id) .then(resolve as never) .catch(reject); }; if (this.status === IDBStatus.Init) { return this.queues.add(submit); } else { submit(); } }); } /** * Creates a new record * @param payload - The record data to create * @returns Promise resolving to the created record */ public create(payload: T): Promise<R> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } create<T, R>(this.writer, payload).then(resolve).catch(reject); }; if (this.status === IDBStatus.Init) { return this.queues.add(submit); } else { submit(); } }); } /** * Updates an existing record * @param id - The record ID to update * @param payload - The partial data to update * @returns Promise resolving to the updated record */ public update(id: string, payload: Partial<T>): Promise<R> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } update<T, R>(this.writer, id, payload).then(resolve).catch(reject); }; if (this.status === IDBStatus.Init) { return this.queues.add(submit); } else { submit(); } }); } /** * Deletes a record by ID * @param id - The record ID to delete * @returns Promise resolving to true when deletion is complete */ public delete(id: string): Promise<true> { return new Promise((resolve, reject) => { const submit = () => { if (this.status === IDBStatus.Closed) { return reject(new Error('Table is closed.')); } remove(this.writer, id).then(resolve).catch(reject); }; if (this.status === IDBStatus.Init) { return this.queues.add(submit); } else { submit(); } }); } } const TABLE_STORES = new Map<string, ReactiveTable<Rec>>(); /** * Creates a reactive table instance that provides state management for IndexedDB records. * This function wraps an IndexedTable with reactive state management capabilities, * allowing for automatic synchronization between memory and database states. * * @template T - The base record type that extends Rec * @template R - The row type that extends Row<T>, defaults to Row<T> * @param name - The name of the IndexedDB object store * @param version - The version of the database schema * @param indexes - An array of index names to create in the object store * @param remIndexes - An array of index names to remove from the object store * @param dbName - The name of the database * @param seeds - An array of seed data to populate the object store * @returns A reactive table interface with methods for managing records */ export function createTable<T extends Rec, R extends Row<T> = Row<T>>( name: string, version = 1, indexes?: (keyof R)[], remIndexes?: (keyof R)[], dbName = name, seeds?: R[] ): ReactiveTable<T, R> { const key = `${dbName}://${name}@${version}`; if (TABLE_STORES.has(key)) { return TABLE_STORES.get(key) as ReactiveTable<T, R>; } const table = new IndexedTable<T, R>(name, version, indexes, remIndexes, dbName, seeds); /** * Map storing row states by record ID */ const rowMaps = new Map<string, RowState<R>>(); /** * Map tracking usage count of each row for reference counting */ const rowUsages = new Map<string, number>(); /** * WeakMap storing unsubscribe functions for row state subscriptions */ const rowSubscriptions = new WeakMap<RowState<R>, StateUnsubscribe>(); /** * Ensures a row state exists for the given ID, creating it if necessary. * Implements reference counting to manage row lifecycle. * * @param id - The record ID * @param data - Initial data for the row (default: empty object) * @param status - Initial status for the row (default: 'init') * @returns The row state for the given ID */ const ensureRow = (id: string, data = {}, status = 'init') => { if (rowMaps.has(id)) { const usage = rowUsages.get(id) as number; rowUsages.set(id, usage + 1); return rowMaps.get(id) as RowState<R>; } const state = anchor.raw({ data, status }) as RowState<R>; const [schedule] = microtask(DB_SYNC_DELAY); rowMaps.set(id, state); rowUsages.set(id, 1); const stateUnsubscribe = derive(state, (snapshot, event) => { if (event.type !== 'init' && event.keys.includes('data')) { schedule(() => { state.status = 'pending'; table .update(id, snapshot.data) .then(() => { state.status = 'ready'; }) .catch((error) => { anchor.assign(state, { status: 'error', error }); }); }); } }); const unsubscribe = () => { let usage = rowUsages.get(id) as number; usage -= 1; if (usage <= 0) { stateUnsubscribe(); rowMaps.delete(id); rowUsages.delete(id); rowSubscriptions.delete(state); } else { rowUsages.set(id, usage); } }; rowSubscriptions.set(state, unsubscribe); return state; }; const tableRef = { /** * Gets a row state by ID, initializing it with data from the database if needed. * @param id - The record ID * @returns The row state */ get(id: string): RowState<R> { const state = ensureRow(id); if (state.status === 'init') { state.status = 'pending'; table .read(id) .then((data) => { if (data) { anchor.assign(state, { data, status: 'ready' }); } else { anchor.assign(state, { status: 'error', error: new Error('Not found.') }); } }) .catch((error) => { anchor.assign(state, { status: 'error', error }); }); } return state; }, /** * Adds a new record to the table. * @param payload - The record data to add * @returns The row state for the new record */ add(payload: T): RowState<R> { const row = createRecord<T>(payload); const state = ensureRow(row.id, row); if (state.status === 'init') { state.status = 'pending'; table .create(row) .then(() => { anchor.assign(state, { status: 'ready' }); }) .catch((error) => { anchor.assign(state, { status: 'error', error }); }); } return state; }, /** * Lists records matching filter criteria. * @param filter - The filter criteria (IDBKeyRange or FilterFn) * @param limit - Maximum number of records to return (default: DEFAULT_FIND_LIMIT) * @param direction - Cursor direction (default: undefined) * @returns A state object containing the list of records */ list(filter?: IDBKeyRange | FilterFn<R>, limit = DEFAULT_FIND_LIMIT, direction?: IDBCursorDirection) { const state = anchor.raw<RowListState<R>>({ data: [], count: 0, status: 'pending', }); table .list(filter, limit, direction) .then((res) => { const data = res.rows.map((rec) => ensureRow(rec.id, rec, 'ready').data); anchor.assign(state, { data, count: res.count, status: 'ready', }); }) .catch((error) => { anchor.assign(state, { status: 'error', error, }); }); return state; }, /** * Lists records by index matching filter criteria. * @param name - The index name to search on * @param filter - The filter criteria (IDBKeyRange or FilterFn) * @param limit - Maximum number of records to return * @param direction - Cursor direction * @returns A state object containing the list of records */ listByIndex( name: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection ): RowListState<R> { const state = anchor.raw<RowListState<R>>({ data: [], count: 0, status: 'pending', }); table .listByIndex(name, filter, limit, direction) .then((res) => { const data = res.rows.map((rec) => ensureRow(rec.id, rec, 'ready').data); anchor.assign(state, { data, count: res.count, status: 'ready', }); }) .catch((error) => { anchor.assign(state, { status: 'error', error }); }); return state; }, /** * Removes a record by ID. * @param id - The record ID to remove * @returns The row state for the removed record */ remove(id: string): RowState<R> { const state = ensureRow(id); state.status = 'pending'; state.data.deleted_at = new Date(); table .delete(id) .then(() => { anchor.assign(state, { status: 'removed' }); rowMaps.delete(id); rowUsages.set(id, 1); rowSubscriptions.get(state)?.(); }) .catch((error) => { delete state.data.deleted_at; anchor.assign(state, { status: 'error', error }); }); return state; }, /** * Sets the seed data for the reactive table. * This method allows updating the seed data that will be used to populate * the table when it's first created and empty. * * @param seeds - Array of seed data to insert into the table for the first time * @returns The reactive table instance for method chaining */ seed(seeds: R[]) { table.seed(seeds); return this; }, /** * Decrements the reference count for a row and cleans up if no longer used. * @param id - The record ID to leave */ leave(id: string) { const state = rowMaps.get(id); if (state) { rowSubscriptions.get(state)?.(); } }, /** * Convert the state into a promise that resolves when the state is ready. * @param {T} state * @returns {Promise<T>} */ async promise<T extends RowState<R> | RowListState<R>>(state: T): Promise<T> { if (state.status === 'pending') { return await new Promise<T>((resolve, reject) => { const unsubscribe = derive(state, (snapshot, event) => { if (event.type !== 'init' && !event.keys.includes('data')) { if (state.error) { reject(state.error); } else { resolve(state); } unsubscribe(); } }); }); } return state; }, /** * Gets the underlying table store. * @returns {IndexedTable<T, R>} */ store() { return table; }, } as ReactiveTable<T, R>; TABLE_STORES.set(key, tableRef); return tableRef; } |