MCPcopy Create free account
hub / github.com/instantdb/instant / initSubscription

Method initSubscription

client/packages/core/src/SyncTable.ts:549–591  ·  view source on GitHub ↗
(
    query: any,
    hash: string,
    cb?: SyncTableCallback<any, any, any>,
  )

Source from the content-addressed store, hash-verified

547 }
548
549 private async initSubscription(
550 query: any,
551 hash: string,
552 cb?: SyncTableCallback<any, any, any>,
553 ) {
554 // Wait for storage to load so that we know if we already have an existing subscription
555 await this.subs.waitForKeyToLoad(hash);
556 const existingSub = this.subs.currentValue[hash];
557
558 if (existingSub && existingSub.state && existingSub.state.txId) {
559 this.sendResync(existingSub, existingSub.state, existingSub.state.txId);
560
561 if (existingSub.values?.entities && cb) {
562 cb({
563 type: CallbackEventType.LoadFromStorage,
564 data: subData(existingSub, existingSub.values?.entities),
565 });
566 }
567
568 return;
569 }
570
571 const table = Object.keys(query)[0];
572 const orderBy = query[table]?.$?.order || { serverCreatedAt: 'asc' };
573 const [orderField, orderDirection] = Object.entries(orderBy)[0] as [
574 string,
575 'asc' | 'desc',
576 ];
577
578 this.subs.updateInPlace((prev) => {
579 prev[hash] = {
580 query,
581 hash: hash,
582 table,
583 orderDirection,
584 orderField,
585 createdAt: Date.now(),
586 updatedAt: Date.now(),
587 };
588 });
589
590 this.sendStart(query);
591 }
592
593 public async flushPending() {
594 for (const hash of Object.keys(this.callbacks)) {

Callers 3

subscribeMethod · 0.95
flushPendingMethod · 0.95
onResyncErrorMethod · 0.95

Calls 5

sendResyncMethod · 0.95
sendStartMethod · 0.95
subDataFunction · 0.85
waitForKeyToLoadMethod · 0.80
updateInPlaceMethod · 0.80

Tested by

no test coverage detected