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

Method onSyncUpdateTriples

client/packages/core/src/SyncTable.ts:726–853  ·  view source on GitHub ↗
(msg: SyncUpdateTriplesMsg)

Source from the content-addressed store, hash-verified

724 }
725
726 public onSyncUpdateTriples(msg: SyncUpdateTriplesMsg) {
727 const subscriptionId = msg['subscription-id'];
728 const hash = this.idToHash[subscriptionId];
729 if (!hash) {
730 this.log.error('Missing hash for subscription', msg);
731 return;
732 }
733
734 const sub = this.subs.currentValue[hash];
735 if (!sub) {
736 this.log.error('Missing sub for hash', hash, msg);
737 return;
738 }
739
740 const state = sub.state;
741 if (!state) {
742 this.log.error('Missing state for sub', sub, msg);
743 return;
744 }
745
746 for (const tx of msg.txes) {
747 if (state.txId && state.txId >= tx['tx-id']) {
748 continue;
749 }
750 state.txId = tx['tx-id'];
751 const idxesToDelete: number[] = [];
752 // Note: this won't work as well when links are involved
753 const byEid: {
754 [eid: string]: SyncUpdateTriplesMsg['txes'][number]['changes'];
755 } = {};
756 for (const change of tx.changes) {
757 const eidChanges = byEid[change.triple[0]] ?? [];
758 byEid[change.triple[0]] = eidChanges;
759 eidChanges.push(change);
760 }
761
762 const values: SubValues = sub.values ?? {
763 entities: [],
764 attrsStore: this.getAttrs(),
765 };
766 const entities = values.entities;
767 sub.values = values;
768
769 const updated: SyncTransaction<any, any, any>['updated'] = [];
770 // Update the existing stores, if we already know about this entity
771 eidLoop: for (const [eid, changes] of Object.entries(byEid)) {
772 for (let i = 0; i < entities.length; i++) {
773 const ent = entities[i];
774 if (s.hasEntity(ent.store, eid)) {
775 applyChangesToStore(ent.store, values.attrsStore, changes);
776 const entity = queryEntity(sub, ent.store, values.attrsStore);
777 const changedFields = changedFieldsOfChanges(
778 ent.store,
779 values.attrsStore,
780 changes,
781 )[eid];
782 if (entity) {
783 updated.push({

Callers 1

_handleReceiveMethod · 0.80

Calls 11

notifyCbsMethod · 0.95
applyChangesToStoreFunction · 0.85
queryEntityFunction · 0.85
changedFieldsOfChangesFunction · 0.85
getServerCreatedAtFunction · 0.85
orderFieldTypeMutativeFunction · 0.85
sortEntitiesInPlaceFunction · 0.85
subDataFunction · 0.85
pushMethod · 0.80
updateInPlaceMethod · 0.80
errorMethod · 0.45

Tested by

no test coverage detected