@instantdb/core 0.22.88-experimental.drewh-ssr.20251523170.1 → 0.22.88-experimental.split-store.20252005043.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/__tests__/src/Reactor.test.js +18 -11
  2. package/__tests__/src/{datalog.test.js → datalog.test.ts} +17 -5
  3. package/__tests__/src/{instaml.test.js → instaml.test.ts} +183 -119
  4. package/__tests__/src/instaql.bench.ts +34 -0
  5. package/__tests__/src/{instaql.test.js → instaql.test.ts} +342 -455
  6. package/__tests__/src/instaqlInference.test.js +13 -9
  7. package/__tests__/src/{store.test.js → store.test.ts} +215 -212
  8. package/dist/commonjs/Reactor.d.ts +24 -19
  9. package/dist/commonjs/Reactor.d.ts.map +1 -1
  10. package/dist/commonjs/Reactor.js +112 -106
  11. package/dist/commonjs/Reactor.js.map +1 -1
  12. package/dist/commonjs/SyncTable.d.ts +4 -1
  13. package/dist/commonjs/SyncTable.d.ts.map +1 -1
  14. package/dist/commonjs/SyncTable.js +35 -37
  15. package/dist/commonjs/SyncTable.js.map +1 -1
  16. package/dist/commonjs/index.d.ts +1 -5
  17. package/dist/commonjs/index.d.ts.map +1 -1
  18. package/dist/commonjs/index.js +1 -7
  19. package/dist/commonjs/index.js.map +1 -1
  20. package/dist/commonjs/instaml.d.ts +17 -4
  21. package/dist/commonjs/instaml.d.ts.map +1 -1
  22. package/dist/commonjs/instaml.js +115 -82
  23. package/dist/commonjs/instaml.js.map +1 -1
  24. package/dist/commonjs/instaql.d.ts +4 -3
  25. package/dist/commonjs/instaql.d.ts.map +1 -1
  26. package/dist/commonjs/instaql.js +65 -63
  27. package/dist/commonjs/instaql.js.map +1 -1
  28. package/dist/commonjs/reactorTypes.d.ts +29 -0
  29. package/dist/commonjs/reactorTypes.d.ts.map +1 -0
  30. package/dist/commonjs/reactorTypes.js +3 -0
  31. package/dist/commonjs/reactorTypes.js.map +1 -0
  32. package/dist/commonjs/store.d.ts +67 -25
  33. package/dist/commonjs/store.d.ts.map +1 -1
  34. package/dist/commonjs/store.js +177 -81
  35. package/dist/commonjs/store.js.map +1 -1
  36. package/dist/esm/Reactor.d.ts +24 -19
  37. package/dist/esm/Reactor.d.ts.map +1 -1
  38. package/dist/esm/Reactor.js +113 -107
  39. package/dist/esm/Reactor.js.map +1 -1
  40. package/dist/esm/SyncTable.d.ts +4 -1
  41. package/dist/esm/SyncTable.d.ts.map +1 -1
  42. package/dist/esm/SyncTable.js +35 -37
  43. package/dist/esm/SyncTable.js.map +1 -1
  44. package/dist/esm/index.d.ts +1 -5
  45. package/dist/esm/index.d.ts.map +1 -1
  46. package/dist/esm/index.js +2 -5
  47. package/dist/esm/index.js.map +1 -1
  48. package/dist/esm/instaml.d.ts +17 -4
  49. package/dist/esm/instaml.d.ts.map +1 -1
  50. package/dist/esm/instaml.js +112 -77
  51. package/dist/esm/instaml.js.map +1 -1
  52. package/dist/esm/instaql.d.ts +4 -3
  53. package/dist/esm/instaql.d.ts.map +1 -1
  54. package/dist/esm/instaql.js +65 -63
  55. package/dist/esm/instaql.js.map +1 -1
  56. package/dist/esm/reactorTypes.d.ts +29 -0
  57. package/dist/esm/reactorTypes.d.ts.map +1 -0
  58. package/dist/esm/reactorTypes.js +2 -0
  59. package/dist/esm/reactorTypes.js.map +1 -0
  60. package/dist/esm/store.d.ts +67 -25
  61. package/dist/esm/store.d.ts.map +1 -1
  62. package/dist/esm/store.js +174 -81
  63. package/dist/esm/store.js.map +1 -1
  64. package/dist/standalone/index.js +1851 -1961
  65. package/dist/standalone/index.umd.cjs +3 -3
  66. package/package.json +2 -2
  67. package/src/Reactor.js +155 -143
  68. package/src/SyncTable.ts +85 -45
  69. package/src/index.ts +0 -9
  70. package/src/{instaml.js → instaml.ts} +201 -96
  71. package/src/instaql.ts +88 -62
  72. package/src/reactorTypes.ts +32 -0
  73. package/src/store.ts +257 -101
  74. package/__tests__/src/instaql.bench.js +0 -29
  75. package/__tests__/src/serializeSchema.test.ts +0 -123
  76. package/dist/commonjs/createRouteHandler.d.ts +0 -8
  77. package/dist/commonjs/createRouteHandler.d.ts.map +0 -1
  78. package/dist/commonjs/createRouteHandler.js +0 -57
  79. package/dist/commonjs/createRouteHandler.js.map +0 -1
  80. package/dist/commonjs/framework.d.ts +0 -77
  81. package/dist/commonjs/framework.d.ts.map +0 -1
  82. package/dist/commonjs/framework.js +0 -199
  83. package/dist/commonjs/framework.js.map +0 -1
  84. package/dist/commonjs/parseSchemaFromJSON.d.ts +0 -3
  85. package/dist/commonjs/parseSchemaFromJSON.d.ts.map +0 -1
  86. package/dist/commonjs/parseSchemaFromJSON.js +0 -148
  87. package/dist/commonjs/parseSchemaFromJSON.js.map +0 -1
  88. package/dist/esm/createRouteHandler.d.ts +0 -8
  89. package/dist/esm/createRouteHandler.d.ts.map +0 -1
  90. package/dist/esm/createRouteHandler.js +0 -53
  91. package/dist/esm/createRouteHandler.js.map +0 -1
  92. package/dist/esm/framework.d.ts +0 -77
  93. package/dist/esm/framework.d.ts.map +0 -1
  94. package/dist/esm/framework.js +0 -159
  95. package/dist/esm/framework.js.map +0 -1
  96. package/dist/esm/parseSchemaFromJSON.d.ts +0 -3
  97. package/dist/esm/parseSchemaFromJSON.d.ts.map +0 -1
  98. package/dist/esm/parseSchemaFromJSON.js +0 -144
  99. package/dist/esm/parseSchemaFromJSON.js.map +0 -1
  100. package/src/createRouteHandler.ts +0 -44
  101. package/src/framework.ts +0 -281
  102. package/src/parseSchemaFromJSON.ts +0 -176
@@ -21,7 +21,7 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  // @ts-check
22
22
  import weakHash from "./utils/weakHash.js";
23
23
  import instaql from "./instaql.js";
24
- import * as instaml from './instaml.js';
24
+ import * as instaml from "./instaml.js";
25
25
  import * as s from "./store.js";
26
26
  import uuid from "./utils/uuid.js";
27
27
  import IndexedDBStorage from "./IndexedDBStorage.js";
@@ -49,6 +49,8 @@ import { SyncTable } from "./SyncTable.js";
49
49
  /** @typedef {import('./Connection.ts').Connection} Connection */
50
50
  /** @typedef {import('./Connection.ts').TransportType} TransportType */
51
51
  /** @typedef {import('./Connection.ts').EventSourceConstructor} EventSourceConstructor */
52
+ /** @typedef {import('./reactorTypes.ts').QuerySub} QuerySub */
53
+ /** @typedef {import('./reactorTypes.ts').QuerySubInStorage} QuerySubInStorage */
52
54
  const STATUS = {
53
55
  CONNECTING: 'connecting',
54
56
  OPENED: 'opened',
@@ -101,20 +103,37 @@ const ignoreLogging = {
101
103
  'refresh-presence': true,
102
104
  'patch-presence': true,
103
105
  };
106
+ /**
107
+ * @param {QuerySubInStorage} x
108
+ * @param {boolean | null} useDateObjects
109
+ * @returns {QuerySub}
110
+ */
104
111
  function querySubFromStorage(x, useDateObjects) {
105
112
  var _a;
106
113
  const v = typeof x === 'string' ? JSON.parse(x) : x;
107
114
  if ((_a = v === null || v === void 0 ? void 0 : v.result) === null || _a === void 0 ? void 0 : _a.store) {
108
- const storeJSON = v.result.store;
109
- v.result.store = s.fromJSON(Object.assign(Object.assign({}, storeJSON), { useDateObjects: useDateObjects }));
115
+ const attrsStore = s.attrsStoreFromJSON(v.result.attrsStore, v.result.store);
116
+ if (attrsStore) {
117
+ const storeJSON = v.result.store;
118
+ v.result.store = s.fromJSON(attrsStore, Object.assign(Object.assign({}, storeJSON), { useDateObjects: useDateObjects }));
119
+ v.result.attrsStore = attrsStore;
120
+ }
110
121
  }
111
122
  return v;
112
123
  }
124
+ /**
125
+ *
126
+ * @param {string} _key
127
+ * @param {QuerySub} sub
128
+ * @returns QuerySubInStorage
129
+ */
113
130
  function querySubToStorage(_key, sub) {
114
- var _a;
115
- const jsonSub = Object.assign({}, sub);
116
- if ((_a = sub.result) === null || _a === void 0 ? void 0 : _a.store) {
117
- jsonSub.result = Object.assign(Object.assign({}, sub.result), { store: s.toJSON(sub.result.store) });
131
+ const { result } = sub, rest = __rest(sub, ["result"]);
132
+ const jsonSub = /** @type {import('./reactorTypes.ts').QuerySubInStorage} */ (rest);
133
+ if (result) {
134
+ /** @type {import('./reactorTypes.ts').QuerySubResultInStorage} */
135
+ const jsonResult = Object.assign(Object.assign({}, result), { store: s.toJSON(result.store), attrsStore: result.attrsStore.toJSON() });
136
+ jsonSub.result = jsonResult;
118
137
  }
119
138
  return jsonSub;
120
139
  }
@@ -259,7 +278,7 @@ export default class Reactor {
259
278
  }
260
279
  try {
261
280
  const txSteps = instaml.transform({
262
- attrs: this.optimisticAttrs(),
281
+ attrsStore: this.optimisticAttrs(),
263
282
  schema: this.config.schema,
264
283
  stores: Object.values(this.querySubs.currentValue).map((sub) => { var _a; return (_a = sub === null || sub === void 0 ? void 0 : sub.result) === null || _a === void 0 ? void 0 : _a.store; }),
265
284
  useDateObjects: this.config.useDateObjects,
@@ -425,16 +444,12 @@ export default class Reactor {
425
444
  this._initStorage(Storage);
426
445
  this._syncTable = new SyncTable(this._trySendAuthed.bind(this), new Storage(this.config.appId, 'syncSubs'), {
427
446
  useDateObjects: this.config.useDateObjects,
428
- }, this._log, (triples) => s.createStore(this.attrs, triples, this.config.enableCardinalityInference, this._linkIndex, this.config.useDateObjects));
447
+ }, this._log, (triples) => {
448
+ return s.createStore(this.ensureAttrs(), triples, this.config.enableCardinalityInference, this.config.useDateObjects);
449
+ }, () => this.ensureAttrs());
429
450
  this._oauthCallbackResponse = this._oauthLoginInit();
430
451
  // kick off a request to cache it
431
- this.getCurrentUser().then((userInfo) => {
432
- this.syncUserToEndpoint(userInfo.user);
433
- });
434
- setInterval(() => __awaiter(this, void 0, void 0, function* () {
435
- const currentUser = yield this.getCurrentUser();
436
- this.syncUserToEndpoint(currentUser.user);
437
- }), 1000 * 60 * 20);
452
+ this.getCurrentUser();
438
453
  NetworkListener.getIsOnline().then((isOnline) => {
439
454
  this._isOnline = isOnline;
440
455
  this._startSocket();
@@ -461,6 +476,12 @@ export default class Reactor {
461
476
  addEventListener('beforeunload', this._beforeUnload);
462
477
  }
463
478
  }
479
+ ensureAttrs() {
480
+ if (!this.attrs) {
481
+ throw new Error('attrs have not loaded.');
482
+ }
483
+ return this.attrs;
484
+ }
464
485
  updateSchema(schema) {
465
486
  this.config = Object.assign(Object.assign({}, this.config), { schema: schema, cardinalityInference: Boolean(schema) });
466
487
  this._linkIndex = schema ? createLinkIndex(this.config.schema) : null;
@@ -484,7 +505,7 @@ export default class Reactor {
484
505
  serialize: querySubToStorage,
485
506
  parse: (_key, x) => querySubFromStorage(x, this.config.useDateObjects),
486
507
  // objectSize
487
- objectSize: (x) => { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = x.result) === null || _a === void 0 ? void 0 : _a.store) === null || _b === void 0 ? void 0 : _b.triples) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0; },
508
+ objectSize: (x) => { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = x === null || x === void 0 ? void 0 : x.result) === null || _a === void 0 ? void 0 : _a.store) === null || _b === void 0 ? void 0 : _b.triples) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0; },
488
509
  logger: this._log,
489
510
  preloadEntryCount: 10,
490
511
  gc: {
@@ -580,35 +601,6 @@ export default class Reactor {
580
601
  }
581
602
  }
582
603
  }
583
- /**
584
- * Does the same thing as add-query-ok
585
- * but called as a result of receiving query info from ssr
586
- * @param {any} q
587
- * @param {{ triples: any; pageInfo: any; }} result
588
- * @param {boolean} enableCardinalityInference
589
- */
590
- _addQueryData(q, result, enableCardinalityInference) {
591
- if (!this.attrs) {
592
- throw new Error('Attrs in reactor have not been set');
593
- }
594
- const queryHash = weakHash(q);
595
- const store = s.createStore(this.attrs, result.triples, enableCardinalityInference, this._linkIndex, this.config.useDateObjects);
596
- this.querySubs.updateInPlace((prev) => {
597
- prev[queryHash] = {
598
- result: {
599
- store,
600
- pageInfo: result.pageInfo,
601
- processedTxId: undefined,
602
- isExternal: true,
603
- },
604
- q,
605
- };
606
- });
607
- this._cleanupPendingMutationsQueries();
608
- this.notifyOne(queryHash);
609
- this.notifyOneQueryOnce(queryHash);
610
- this._cleanupPendingMutationsTimeout();
611
- }
612
604
  _handleReceive(connId, msg) {
613
605
  var _a, _b, _c, _d, _e, _f;
614
606
  // opt-out, enabled by default if schema
@@ -647,7 +639,8 @@ export default class Reactor {
647
639
  const pageInfo = (_d = (_c = result === null || result === void 0 ? void 0 : result[0]) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d['page-info'];
648
640
  const aggregate = (_f = (_e = result === null || result === void 0 ? void 0 : result[0]) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f['aggregate'];
649
641
  const triples = extractTriples(result);
650
- const store = s.createStore(this.attrs, triples, enableCardinalityInference, this._linkIndex, this.config.useDateObjects);
642
+ const attrsStore = this.ensureAttrs();
643
+ const store = s.createStore(attrsStore, triples, enableCardinalityInference, this.config.useDateObjects);
651
644
  this.querySubs.updateInPlace((prev) => {
652
645
  if (!prev[hash]) {
653
646
  this._log.info('Missing value in querySubs', { hash, q });
@@ -655,6 +648,7 @@ export default class Reactor {
655
648
  }
656
649
  prev[hash].result = {
657
650
  store,
651
+ attrsStore,
658
652
  pageInfo,
659
653
  aggregate,
660
654
  processedTxId: msg['processed-tx-id'],
@@ -689,7 +683,7 @@ export default class Reactor {
689
683
  this._setAttrs(attrs);
690
684
  }
691
685
  this._cleanupPendingMutationsTimeout();
692
- const rewrittenMutations = this._rewriteMutations(this.attrs, this._pendingMutations(), processedTxId);
686
+ const rewrittenMutations = this._rewriteMutations(this.ensureAttrs(), this._pendingMutations(), processedTxId);
693
687
  if (rewrittenMutations !== this._pendingMutations()) {
694
688
  // We know we've changed the mutations to fix the attr ids and removed
695
689
  // processed attrs, so we'll persist those changes to prevent optimisticAttrs
@@ -705,19 +699,33 @@ export default class Reactor {
705
699
  const result = x['instaql-result'];
706
700
  const hash = weakHash(q);
707
701
  const triples = extractTriples(result);
708
- const store = s.createStore(this.attrs, triples, enableCardinalityInference, this._linkIndex, this.config.useDateObjects);
709
- const newStore = this._applyOptimisticUpdates(store, mutations, processedTxId);
702
+ const attrsStore = this.ensureAttrs();
703
+ const store = s.createStore(attrsStore, triples, enableCardinalityInference, this.config.useDateObjects);
704
+ const { store: newStore, attrsStore: newAttrsStore } = this._applyOptimisticUpdates(store, attrsStore, mutations, processedTxId);
710
705
  const pageInfo = (_b = (_a = result === null || result === void 0 ? void 0 : result[0]) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b['page-info'];
711
706
  const aggregate = (_d = (_c = result === null || result === void 0 ? void 0 : result[0]) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d['aggregate'];
712
- return { q, hash, store: newStore, pageInfo, aggregate };
707
+ return {
708
+ q,
709
+ hash,
710
+ store: newStore,
711
+ attrsStore: newAttrsStore,
712
+ pageInfo,
713
+ aggregate,
714
+ };
713
715
  });
714
- updates.forEach(({ hash, q, store, pageInfo, aggregate }) => {
716
+ updates.forEach(({ hash, q, store, attrsStore, pageInfo, aggregate }) => {
715
717
  this.querySubs.updateInPlace((prev) => {
716
718
  if (!prev[hash]) {
717
719
  this._log.error('Missing value in querySubs', { hash, q });
718
720
  return;
719
721
  }
720
- prev[hash].result = { store, pageInfo, aggregate, processedTxId };
722
+ prev[hash].result = {
723
+ store,
724
+ attrsStore,
725
+ pageInfo,
726
+ aggregate,
727
+ processedTxId,
728
+ };
721
729
  });
722
730
  });
723
731
  this._cleanupPendingMutationsQueries();
@@ -729,7 +737,7 @@ export default class Reactor {
729
737
  case 'transact-ok': {
730
738
  const { 'client-event-id': eventId, 'tx-id': txId } = msg;
731
739
  this._inFlightMutationEventIds.delete(eventId);
732
- const muts = this._rewriteMutations(this.attrs, this._pendingMutations());
740
+ const muts = this._rewriteMutations(this.ensureAttrs(), this._pendingMutations());
733
741
  const prevMutation = muts.get(eventId);
734
742
  if (!prevMutation) {
735
743
  break;
@@ -738,11 +746,17 @@ export default class Reactor {
738
746
  this._updatePendingMutations((prev) => {
739
747
  prev.set(eventId, Object.assign(Object.assign({}, prev.get(eventId)), { 'tx-id': txId, confirmed: Date.now() }));
740
748
  });
741
- const newAttrs = prevMutation['tx-steps']
742
- .filter(([action, ..._args]) => action === 'add-attr')
743
- .map(([_action, attr]) => attr)
744
- .concat(Object.values(this.attrs));
745
- this._setAttrs(newAttrs);
749
+ const newAttrs = [];
750
+ for (const step of prevMutation['tx-steps']) {
751
+ if (step[0] === 'add-attr') {
752
+ const attr = step[1];
753
+ newAttrs.push(attr);
754
+ }
755
+ }
756
+ if (newAttrs.length) {
757
+ const existingAttrs = Object.values(this.ensureAttrs().attrs);
758
+ this._setAttrs([...existingAttrs, ...newAttrs]);
759
+ }
746
760
  this._finishTransaction('synced', eventId);
747
761
  this._cleanupPendingMutationsTimeout();
748
762
  break;
@@ -903,10 +917,10 @@ export default class Reactor {
903
917
  this._completeQueryOnce(q, hash, r.dfd);
904
918
  }
905
919
  _setAttrs(attrs) {
906
- this.attrs = attrs.reduce((acc, attr) => {
920
+ this.attrs = new s.AttrsStoreClass(attrs.reduce((acc, attr) => {
907
921
  acc[attr.id] = attr;
908
922
  return acc;
909
- }, {});
923
+ }, {}), this._linkIndex);
910
924
  this.notifyAttrsSubs();
911
925
  }
912
926
  _startQuerySub(q, hash) {
@@ -1012,6 +1026,12 @@ export default class Reactor {
1012
1026
  // We remove `add-attr` commands for attrs that already exist.
1013
1027
  // We update `add-triple` and `retract-triple` commands to use the
1014
1028
  // server attr-ids.
1029
+ /**
1030
+ *
1031
+ * @param {s.AttrsStore} attrs
1032
+ * @param {any} muts
1033
+ * @param {number} [processedTxId]
1034
+ */
1015
1035
  _rewriteMutations(attrs, muts, processedTxId) {
1016
1036
  if (!attrs)
1017
1037
  return muts;
@@ -1019,12 +1039,12 @@ export default class Reactor {
1019
1039
  return new Map();
1020
1040
  const findExistingAttr = (attr) => {
1021
1041
  const [_, etype, label] = attr['forward-identity'];
1022
- const existing = instaml.getAttrByFwdIdentName(attrs, etype, label);
1042
+ const existing = s.getAttrByFwdIdentName(attrs, etype, label);
1023
1043
  return existing;
1024
1044
  };
1025
1045
  const findReverseAttr = (attr) => {
1026
1046
  const [_, etype, label] = attr['forward-identity'];
1027
- const revAttr = instaml.getAttrByReverseIdentName(attrs, etype, label);
1047
+ const revAttr = s.getAttrByReverseIdentName(attrs, etype, label);
1028
1048
  return revAttr;
1029
1049
  };
1030
1050
  const mapping = { attrIdMap: {}, refSwapAttrIds: new Set() };
@@ -1087,8 +1107,11 @@ export default class Reactor {
1087
1107
  }
1088
1108
  // ---------------------------
1089
1109
  // Transact
1110
+ /**
1111
+ * @returns {s.AttrsStore}
1112
+ */
1090
1113
  optimisticAttrs() {
1091
- var _a;
1114
+ var _a, _b;
1092
1115
  const pendingMutationSteps = [...this._pendingMutations().values()] // hack due to Map()
1093
1116
  .flatMap((x) => x['tx-steps']);
1094
1117
  const deletedAttrIds = new Set(pendingMutationSteps
@@ -1101,20 +1124,25 @@ export default class Reactor {
1101
1124
  }
1102
1125
  else if (_action === 'update-attr' &&
1103
1126
  attr.id &&
1104
- ((_a = this.attrs) === null || _a === void 0 ? void 0 : _a[attr.id])) {
1105
- const fullAttr = Object.assign(Object.assign({}, this.attrs[attr.id]), attr);
1127
+ ((_a = this.attrs) === null || _a === void 0 ? void 0 : _a.getAttr(attr.id))) {
1128
+ const fullAttr = Object.assign(Object.assign({}, this.attrs.getAttr(attr.id)), attr);
1106
1129
  pendingAttrs.push(fullAttr);
1107
1130
  }
1108
1131
  }
1109
- const attrsWithoutDeleted = [
1110
- ...Object.values(this.attrs || {}),
1111
- ...pendingAttrs,
1112
- ].filter((a) => !deletedAttrIds.has(a.id));
1113
- const attrsRecord = Object.fromEntries(attrsWithoutDeleted.map((a) => [a.id, a]));
1114
- return attrsRecord;
1132
+ if (!deletedAttrIds.size && !pendingAttrs.length) {
1133
+ return this.attrs || new s.AttrsStoreClass({}, this._linkIndex);
1134
+ }
1135
+ const attrs = Object.assign({}, (((_b = this.attrs) === null || _b === void 0 ? void 0 : _b.attrs) || {}));
1136
+ for (const attr of pendingAttrs) {
1137
+ attrs[attr.id] = attr;
1138
+ }
1139
+ for (const id of deletedAttrIds) {
1140
+ delete attrs[id];
1141
+ }
1142
+ return new s.AttrsStoreClass(attrs, this._linkIndex);
1115
1143
  }
1116
1144
  /** Runs instaql on a query and a store */
1117
- dataForQuery(hash, applyOptimistic = true) {
1145
+ dataForQuery(hash) {
1118
1146
  const errorMessage = this._errorMessage;
1119
1147
  if (errorMessage) {
1120
1148
  return { error: errorMessage };
@@ -1136,22 +1164,21 @@ export default class Reactor {
1136
1164
  pendingMutationsVersion === cached.pendingMutationsVersion) {
1137
1165
  return cached;
1138
1166
  }
1139
- let store = result.store;
1140
- const { pageInfo, aggregate, processedTxId } = result;
1141
- const mutations = this._rewriteMutationsSorted(store.attrs, pendingMutations);
1142
- if (applyOptimistic) {
1143
- store = this._applyOptimisticUpdates(store, mutations, processedTxId);
1144
- }
1145
- const resp = instaql({ store: store, pageInfo, aggregate }, q);
1167
+ const { store, attrsStore, pageInfo, aggregate, processedTxId } = result;
1168
+ const mutations = this._rewriteMutationsSorted(attrsStore, pendingMutations);
1169
+ const { store: newStore, attrsStore: newAttrsStore } = this._applyOptimisticUpdates(store, attrsStore, mutations, processedTxId);
1170
+ const resp = instaql({ store: newStore, attrsStore: newAttrsStore, pageInfo, aggregate }, q);
1146
1171
  return { data: resp, querySubVersion, pendingMutationsVersion };
1147
1172
  }
1148
- _applyOptimisticUpdates(store, mutations, processedTxId) {
1173
+ _applyOptimisticUpdates(store, attrsStore, mutations, processedTxId) {
1149
1174
  for (const [_, mut] of mutations) {
1150
1175
  if (!mut['tx-id'] || (processedTxId && mut['tx-id'] > processedTxId)) {
1151
- store = s.transact(store, mut['tx-steps']);
1176
+ const result = s.transact(store, attrsStore, mut['tx-steps']);
1177
+ store = result.store;
1178
+ attrsStore = result.attrsStore;
1152
1179
  }
1153
1180
  }
1154
- return store;
1181
+ return { store, attrsStore };
1155
1182
  }
1156
1183
  /** Re-compute all subscriptions */
1157
1184
  notifyAll() {
@@ -1231,7 +1258,7 @@ export default class Reactor {
1231
1258
  .forEach(({ eventId, q }) => {
1232
1259
  this._trySendAuthed(eventId, { op: 'add-query', q });
1233
1260
  });
1234
- const muts = this._rewriteMutationsSorted(this.attrs, this._pendingMutations());
1261
+ const muts = this._rewriteMutationsSorted(this.ensureAttrs(), this._pendingMutations());
1235
1262
  muts.forEach(([eventId, mut]) => {
1236
1263
  if (!mut['tx-id']) {
1237
1264
  this._sendMutation(eventId, mut);
@@ -1518,7 +1545,7 @@ export default class Reactor {
1518
1545
  subscribeAttrs(cb) {
1519
1546
  this.attrsCbs.push(cb);
1520
1547
  if (this.attrs) {
1521
- cb(this.attrs);
1548
+ cb(this.attrs.attrs);
1522
1549
  }
1523
1550
  return () => {
1524
1551
  this.attrsCbs = this.attrsCbs.filter((x) => x !== cb);
@@ -1534,7 +1561,7 @@ export default class Reactor {
1534
1561
  if (!this.attrs)
1535
1562
  return;
1536
1563
  const oas = this.optimisticAttrs();
1537
- this.attrsCbs.forEach((cb) => cb(oas));
1564
+ this.attrsCbs.forEach((cb) => cb(oas.attrs));
1538
1565
  }
1539
1566
  notifyConnectionStatusSubs(status) {
1540
1567
  this.connectionStatusCbs.forEach((cb) => cb(status));
@@ -1606,28 +1633,7 @@ export default class Reactor {
1606
1633
  }
1607
1634
  });
1608
1635
  }
1609
- syncUserToEndpoint(user) {
1610
- return __awaiter(this, void 0, void 0, function* () {
1611
- if (this.config.cookieEndpoint) {
1612
- try {
1613
- fetch(this.config.cookieEndpoint + '/sync-auth', {
1614
- method: 'POST',
1615
- body: JSON.stringify({
1616
- user: user,
1617
- }),
1618
- headers: {
1619
- 'Content-Type': 'application/json',
1620
- },
1621
- });
1622
- }
1623
- catch (error) {
1624
- console.error('Error syncing user with external endpoint', error);
1625
- }
1626
- }
1627
- });
1628
- }
1629
1636
  updateUser(newUser) {
1630
- this.syncUserToEndpoint(newUser);
1631
1637
  const newV = { error: undefined, user: newUser };
1632
1638
  this._currentUserCached = Object.assign({ isLoading: false }, newV);
1633
1639
  this._dataForQueryCache = {};