@instantdb/core 0.22.88 → 0.22.89-experimental.drewh-fix-export.20277749804.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.
- package/__tests__/src/Reactor.test.js +18 -11
- package/__tests__/src/{datalog.test.js → datalog.test.ts} +17 -5
- package/__tests__/src/{instaml.test.js → instaml.test.ts} +183 -119
- package/__tests__/src/instaql.bench.ts +34 -0
- package/__tests__/src/{instaql.test.js → instaql.test.ts} +342 -455
- package/__tests__/src/instaqlInference.test.js +13 -9
- package/__tests__/src/{store.test.js → store.test.ts} +215 -212
- package/dist/commonjs/Reactor.d.ts +23 -6
- package/dist/commonjs/Reactor.d.ts.map +1 -1
- package/dist/commonjs/Reactor.js +110 -45
- package/dist/commonjs/Reactor.js.map +1 -1
- package/dist/commonjs/SyncTable.d.ts +4 -1
- package/dist/commonjs/SyncTable.d.ts.map +1 -1
- package/dist/commonjs/SyncTable.js +35 -37
- package/dist/commonjs/SyncTable.js.map +1 -1
- package/dist/commonjs/instaml.d.ts +17 -4
- package/dist/commonjs/instaml.d.ts.map +1 -1
- package/dist/commonjs/instaml.js +115 -82
- package/dist/commonjs/instaml.js.map +1 -1
- package/dist/commonjs/instaql.d.ts +4 -3
- package/dist/commonjs/instaql.d.ts.map +1 -1
- package/dist/commonjs/instaql.js +65 -63
- package/dist/commonjs/instaql.js.map +1 -1
- package/dist/commonjs/reactorTypes.d.ts +29 -0
- package/dist/commonjs/reactorTypes.d.ts.map +1 -0
- package/dist/commonjs/reactorTypes.js +3 -0
- package/dist/commonjs/reactorTypes.js.map +1 -0
- package/dist/commonjs/store.d.ts +67 -25
- package/dist/commonjs/store.d.ts.map +1 -1
- package/dist/commonjs/store.js +177 -81
- package/dist/commonjs/store.js.map +1 -1
- package/dist/esm/Reactor.d.ts +23 -6
- package/dist/esm/Reactor.d.ts.map +1 -1
- package/dist/esm/Reactor.js +111 -46
- package/dist/esm/Reactor.js.map +1 -1
- package/dist/esm/SyncTable.d.ts +4 -1
- package/dist/esm/SyncTable.d.ts.map +1 -1
- package/dist/esm/SyncTable.js +35 -37
- package/dist/esm/SyncTable.js.map +1 -1
- package/dist/esm/instaml.d.ts +17 -4
- package/dist/esm/instaml.d.ts.map +1 -1
- package/dist/esm/instaml.js +112 -77
- package/dist/esm/instaml.js.map +1 -1
- package/dist/esm/instaql.d.ts +4 -3
- package/dist/esm/instaql.d.ts.map +1 -1
- package/dist/esm/instaql.js +65 -63
- package/dist/esm/instaql.js.map +1 -1
- package/dist/esm/reactorTypes.d.ts +29 -0
- package/dist/esm/reactorTypes.d.ts.map +1 -0
- package/dist/esm/reactorTypes.js +2 -0
- package/dist/esm/reactorTypes.js.map +1 -0
- package/dist/esm/store.d.ts +67 -25
- package/dist/esm/store.d.ts.map +1 -1
- package/dist/esm/store.js +174 -81
- package/dist/esm/store.js.map +1 -1
- package/dist/standalone/index.js +1605 -1415
- package/dist/standalone/index.umd.cjs +3 -3
- package/package.json +2 -2
- package/src/Reactor.js +152 -75
- package/src/SyncTable.ts +85 -45
- package/src/{instaml.js → instaml.ts} +201 -96
- package/src/instaql.ts +88 -62
- package/src/reactorTypes.ts +32 -0
- package/src/store.ts +257 -101
- package/__tests__/src/instaql.bench.js +0 -29
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export default class Reactor<RoomSchema extends import("./presence.ts").RoomSchemaShape = {}> {
|
|
5
5
|
constructor(config: any, Storage: typeof IndexedDBStorage | undefined, NetworkListener: typeof WindowNetworkListener | undefined, versions: any, EventSourceConstructor: any);
|
|
6
|
-
|
|
6
|
+
/** @type {s.AttrsStore | undefined} */
|
|
7
|
+
attrs: s.AttrsStore | undefined;
|
|
7
8
|
_isOnline: boolean;
|
|
8
9
|
_isShutdown: boolean;
|
|
9
10
|
status: string;
|
|
10
|
-
/** @type {PersistedObject} */
|
|
11
|
-
querySubs: PersistedObject<
|
|
11
|
+
/** @type {PersistedObject<string, QuerySub, QuerySubInStorage>} */
|
|
12
|
+
querySubs: PersistedObject<string, QuerySub, QuerySubInStorage>;
|
|
12
13
|
/** @type {PersistedObject} */
|
|
13
14
|
kv: PersistedObject<any, any, any>;
|
|
14
15
|
/** @type {SyncTable} */
|
|
@@ -78,6 +79,7 @@ export default class Reactor<RoomSchema extends import("./presence.ts").RoomSche
|
|
|
78
79
|
queryCacheLimit: any;
|
|
79
80
|
versions: any;
|
|
80
81
|
_beforeUnload(): void;
|
|
82
|
+
ensureAttrs(): s.AttrsStore;
|
|
81
83
|
updateSchema(schema: any): void;
|
|
82
84
|
_reactorStats(): {
|
|
83
85
|
inFlightMutationCount: number;
|
|
@@ -139,12 +141,24 @@ export default class Reactor<RoomSchema extends import("./presence.ts").RoomSche
|
|
|
139
141
|
_unsubQuery(q: any, hash: any, cb: any): void;
|
|
140
142
|
_hasQueryListeners(hash: any): boolean;
|
|
141
143
|
_cleanupQuery(q: any, hash: any): void;
|
|
142
|
-
|
|
144
|
+
/**
|
|
145
|
+
*
|
|
146
|
+
* @param {s.AttrsStore} attrs
|
|
147
|
+
* @param {any} muts
|
|
148
|
+
* @param {number} [processedTxId]
|
|
149
|
+
*/
|
|
150
|
+
_rewriteMutations(attrs: s.AttrsStore, muts: any, processedTxId?: number): any;
|
|
143
151
|
_rewriteMutationsSorted(attrs: any, muts: any): any[];
|
|
144
|
-
|
|
152
|
+
/**
|
|
153
|
+
* @returns {s.AttrsStore}
|
|
154
|
+
*/
|
|
155
|
+
optimisticAttrs(): s.AttrsStore;
|
|
145
156
|
/** Runs instaql on a query and a store */
|
|
146
157
|
dataForQuery(hash: any): any;
|
|
147
|
-
_applyOptimisticUpdates(store: any, mutations: any, processedTxId: any):
|
|
158
|
+
_applyOptimisticUpdates(store: any, attrsStore: any, mutations: any, processedTxId: any): {
|
|
159
|
+
store: any;
|
|
160
|
+
attrsStore: any;
|
|
161
|
+
};
|
|
148
162
|
/** Re-run instaql and call all callbacks with new data */
|
|
149
163
|
notifyOne: (hash: any) => void;
|
|
150
164
|
notifyOneQueryOnce: (hash: any) => void;
|
|
@@ -338,6 +352,9 @@ export type Logger = import("./utils/log.ts").Logger;
|
|
|
338
352
|
export type Connection = import("./Connection.ts").Connection<any>;
|
|
339
353
|
export type TransportType = import("./Connection.ts").TransportType;
|
|
340
354
|
export type EventSourceConstructor = import("./Connection.ts").EventSourceConstructor;
|
|
355
|
+
export type QuerySub = import("./reactorTypes.ts").QuerySub;
|
|
356
|
+
export type QuerySubInStorage = import("./reactorTypes.ts").QuerySubInStorage;
|
|
357
|
+
import * as s from './store.ts';
|
|
341
358
|
import { PersistedObject } from './utils/PersistedObject.ts';
|
|
342
359
|
import { SyncTable } from './SyncTable.ts';
|
|
343
360
|
import { Deferred } from './utils/Deferred.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Reactor.d.ts","sourceRoot":"","sources":["../../src/Reactor.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Reactor.d.ts","sourceRoot":"","sources":["../../src/Reactor.js"],"names":[],"mappings":"AAyMA;;GAEG;AACH,6BAFwD,UAAU,SAArD,OAAQ,eAAe,EAAE,eAAgB;IAmEpD,8KAiHC;IAjLD,uCAAuC;IACvC,OADW,CAAC,CAAC,UAAU,GAAG,SAAS,CAC7B;IACN,mBAAiB;IACjB,qBAAoB;IACpB,eAA2B;IAE3B,mEAAmE;IACnE,WADW,eAAe,CAAC,MAAM,EAAE,QAAQ,EAAE,iBAAiB,CAAC,CACrD;IAEV,8BAA8B;IAC9B,mCAAG;IAEH,wBAAwB;IACxB,YADW,SAAS,CACT;IAEX,wEAAwE;IACxE,UADW,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;QAAE,CAAC,EAAE,GAAG,CAAC;QAAC,EAAE,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,GAAG,CAAA;KAAE,CAAC,CAAC,CACtD;IACd,gFAAgF;IAChF,eADW,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;QAAE,CAAC,EA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AG,GAAG,IAAI,GAAG,SAAS,cA4BhC;IAED,gCAiBC;IAMD;;;;;;;;;;;aAUC;IAGD,oEAsBC;IAED,8CAMC;IAED,2CAGC;IAED,iCAEC;IAED,0DAKC;IAGD,8EAuBC;IAED,uCAIC;IAED,oDAaC;IAED,mDAyBC;IAED,gDAWC;IAKD;;;;;aAeC;IAED,uEAQC;IAED,6DAoBC;IAED,4DAWC;IAKD,oFAWC;IAED,8DAWC;IAKD,+CAaC;IAED,wCAWC;CACF;qBA93Ea,OAAO,gBAAgB,EAAE,MAAM;;4BAE/B,OAAO,iBAAiB,EAAE,aAAa;qCACvC,OAAO,iBAAiB,EAAE,sBAAsB;uBAChD,OAAO,mBAAmB,EAAE,QAAQ;gCACpC,OAAO,mBAAmB,EAAE,iBAAiB;mBAnCxC,YAAY;gCASC,4BAA4B;0BAmBlC,gBAAgB;yBApBjB,qBAAqB;yBAJrB,cAAc;4BACX,iBAAiB;6BAHhB,uBAAuB;kCAClB,4BAA4B"}
|
package/dist/commonjs/Reactor.js
CHANGED
|
@@ -87,6 +87,8 @@ const SyncTable_ts_1 = require("./SyncTable.js");
|
|
|
87
87
|
/** @typedef {import('./Connection.ts').Connection} Connection */
|
|
88
88
|
/** @typedef {import('./Connection.ts').TransportType} TransportType */
|
|
89
89
|
/** @typedef {import('./Connection.ts').EventSourceConstructor} EventSourceConstructor */
|
|
90
|
+
/** @typedef {import('./reactorTypes.ts').QuerySub} QuerySub */
|
|
91
|
+
/** @typedef {import('./reactorTypes.ts').QuerySubInStorage} QuerySubInStorage */
|
|
90
92
|
const STATUS = {
|
|
91
93
|
CONNECTING: 'connecting',
|
|
92
94
|
OPENED: 'opened',
|
|
@@ -139,20 +141,37 @@ const ignoreLogging = {
|
|
|
139
141
|
'refresh-presence': true,
|
|
140
142
|
'patch-presence': true,
|
|
141
143
|
};
|
|
144
|
+
/**
|
|
145
|
+
* @param {QuerySubInStorage} x
|
|
146
|
+
* @param {boolean | null} useDateObjects
|
|
147
|
+
* @returns {QuerySub}
|
|
148
|
+
*/
|
|
142
149
|
function querySubFromStorage(x, useDateObjects) {
|
|
143
150
|
var _a;
|
|
144
151
|
const v = typeof x === 'string' ? JSON.parse(x) : x;
|
|
145
152
|
if ((_a = v === null || v === void 0 ? void 0 : v.result) === null || _a === void 0 ? void 0 : _a.store) {
|
|
146
|
-
const
|
|
147
|
-
|
|
153
|
+
const attrsStore = s.attrsStoreFromJSON(v.result.attrsStore, v.result.store);
|
|
154
|
+
if (attrsStore) {
|
|
155
|
+
const storeJSON = v.result.store;
|
|
156
|
+
v.result.store = s.fromJSON(attrsStore, Object.assign(Object.assign({}, storeJSON), { useDateObjects: useDateObjects }));
|
|
157
|
+
v.result.attrsStore = attrsStore;
|
|
158
|
+
}
|
|
148
159
|
}
|
|
149
160
|
return v;
|
|
150
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
*
|
|
164
|
+
* @param {string} _key
|
|
165
|
+
* @param {QuerySub} sub
|
|
166
|
+
* @returns QuerySubInStorage
|
|
167
|
+
*/
|
|
151
168
|
function querySubToStorage(_key, sub) {
|
|
152
|
-
|
|
153
|
-
const jsonSub =
|
|
154
|
-
if (
|
|
155
|
-
|
|
169
|
+
const { result } = sub, rest = __rest(sub, ["result"]);
|
|
170
|
+
const jsonSub = /** @type {import('./reactorTypes.ts').QuerySubInStorage} */ (rest);
|
|
171
|
+
if (result) {
|
|
172
|
+
/** @type {import('./reactorTypes.ts').QuerySubResultInStorage} */
|
|
173
|
+
const jsonResult = Object.assign(Object.assign({}, result), { store: s.toJSON(result.store), attrsStore: result.attrsStore.toJSON() });
|
|
174
|
+
jsonSub.result = jsonResult;
|
|
156
175
|
}
|
|
157
176
|
return jsonSub;
|
|
158
177
|
}
|
|
@@ -297,7 +316,7 @@ class Reactor {
|
|
|
297
316
|
}
|
|
298
317
|
try {
|
|
299
318
|
const txSteps = instaml.transform({
|
|
300
|
-
|
|
319
|
+
attrsStore: this.optimisticAttrs(),
|
|
301
320
|
schema: this.config.schema,
|
|
302
321
|
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; }),
|
|
303
322
|
useDateObjects: this.config.useDateObjects,
|
|
@@ -463,7 +482,9 @@ class Reactor {
|
|
|
463
482
|
this._initStorage(Storage);
|
|
464
483
|
this._syncTable = new SyncTable_ts_1.SyncTable(this._trySendAuthed.bind(this), new Storage(this.config.appId, 'syncSubs'), {
|
|
465
484
|
useDateObjects: this.config.useDateObjects,
|
|
466
|
-
}, this._log, (triples) =>
|
|
485
|
+
}, this._log, (triples) => {
|
|
486
|
+
return s.createStore(this.ensureAttrs(), triples, this.config.enableCardinalityInference, this.config.useDateObjects);
|
|
487
|
+
}, () => this.ensureAttrs());
|
|
467
488
|
this._oauthCallbackResponse = this._oauthLoginInit();
|
|
468
489
|
// kick off a request to cache it
|
|
469
490
|
this.getCurrentUser();
|
|
@@ -493,6 +514,12 @@ class Reactor {
|
|
|
493
514
|
addEventListener('beforeunload', this._beforeUnload);
|
|
494
515
|
}
|
|
495
516
|
}
|
|
517
|
+
ensureAttrs() {
|
|
518
|
+
if (!this.attrs) {
|
|
519
|
+
throw new Error('attrs have not loaded.');
|
|
520
|
+
}
|
|
521
|
+
return this.attrs;
|
|
522
|
+
}
|
|
496
523
|
updateSchema(schema) {
|
|
497
524
|
this.config = Object.assign(Object.assign({}, this.config), { schema: schema, cardinalityInference: Boolean(schema) });
|
|
498
525
|
this._linkIndex = schema ? (0, linkIndex_ts_1.createLinkIndex)(this.config.schema) : null;
|
|
@@ -516,7 +543,7 @@ class Reactor {
|
|
|
516
543
|
serialize: querySubToStorage,
|
|
517
544
|
parse: (_key, x) => querySubFromStorage(x, this.config.useDateObjects),
|
|
518
545
|
// objectSize
|
|
519
|
-
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; },
|
|
546
|
+
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; },
|
|
520
547
|
logger: this._log,
|
|
521
548
|
preloadEntryCount: 10,
|
|
522
549
|
gc: {
|
|
@@ -650,7 +677,8 @@ class Reactor {
|
|
|
650
677
|
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'];
|
|
651
678
|
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'];
|
|
652
679
|
const triples = (0, instaqlResult_js_1.extractTriples)(result);
|
|
653
|
-
const
|
|
680
|
+
const attrsStore = this.ensureAttrs();
|
|
681
|
+
const store = s.createStore(attrsStore, triples, enableCardinalityInference, this.config.useDateObjects);
|
|
654
682
|
this.querySubs.updateInPlace((prev) => {
|
|
655
683
|
if (!prev[hash]) {
|
|
656
684
|
this._log.info('Missing value in querySubs', { hash, q });
|
|
@@ -658,6 +686,7 @@ class Reactor {
|
|
|
658
686
|
}
|
|
659
687
|
prev[hash].result = {
|
|
660
688
|
store,
|
|
689
|
+
attrsStore,
|
|
661
690
|
pageInfo,
|
|
662
691
|
aggregate,
|
|
663
692
|
processedTxId: msg['processed-tx-id'],
|
|
@@ -692,7 +721,7 @@ class Reactor {
|
|
|
692
721
|
this._setAttrs(attrs);
|
|
693
722
|
}
|
|
694
723
|
this._cleanupPendingMutationsTimeout();
|
|
695
|
-
const rewrittenMutations = this._rewriteMutations(this.
|
|
724
|
+
const rewrittenMutations = this._rewriteMutations(this.ensureAttrs(), this._pendingMutations(), processedTxId);
|
|
696
725
|
if (rewrittenMutations !== this._pendingMutations()) {
|
|
697
726
|
// We know we've changed the mutations to fix the attr ids and removed
|
|
698
727
|
// processed attrs, so we'll persist those changes to prevent optimisticAttrs
|
|
@@ -708,19 +737,33 @@ class Reactor {
|
|
|
708
737
|
const result = x['instaql-result'];
|
|
709
738
|
const hash = (0, weakHash_ts_1.default)(q);
|
|
710
739
|
const triples = (0, instaqlResult_js_1.extractTriples)(result);
|
|
711
|
-
const
|
|
712
|
-
const
|
|
740
|
+
const attrsStore = this.ensureAttrs();
|
|
741
|
+
const store = s.createStore(attrsStore, triples, enableCardinalityInference, this.config.useDateObjects);
|
|
742
|
+
const { store: newStore, attrsStore: newAttrsStore } = this._applyOptimisticUpdates(store, attrsStore, mutations, processedTxId);
|
|
713
743
|
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'];
|
|
714
744
|
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'];
|
|
715
|
-
return {
|
|
745
|
+
return {
|
|
746
|
+
q,
|
|
747
|
+
hash,
|
|
748
|
+
store: newStore,
|
|
749
|
+
attrsStore: newAttrsStore,
|
|
750
|
+
pageInfo,
|
|
751
|
+
aggregate,
|
|
752
|
+
};
|
|
716
753
|
});
|
|
717
|
-
updates.forEach(({ hash, q, store, pageInfo, aggregate }) => {
|
|
754
|
+
updates.forEach(({ hash, q, store, attrsStore, pageInfo, aggregate }) => {
|
|
718
755
|
this.querySubs.updateInPlace((prev) => {
|
|
719
756
|
if (!prev[hash]) {
|
|
720
757
|
this._log.error('Missing value in querySubs', { hash, q });
|
|
721
758
|
return;
|
|
722
759
|
}
|
|
723
|
-
prev[hash].result = {
|
|
760
|
+
prev[hash].result = {
|
|
761
|
+
store,
|
|
762
|
+
attrsStore,
|
|
763
|
+
pageInfo,
|
|
764
|
+
aggregate,
|
|
765
|
+
processedTxId,
|
|
766
|
+
};
|
|
724
767
|
});
|
|
725
768
|
});
|
|
726
769
|
this._cleanupPendingMutationsQueries();
|
|
@@ -732,7 +775,7 @@ class Reactor {
|
|
|
732
775
|
case 'transact-ok': {
|
|
733
776
|
const { 'client-event-id': eventId, 'tx-id': txId } = msg;
|
|
734
777
|
this._inFlightMutationEventIds.delete(eventId);
|
|
735
|
-
const muts = this._rewriteMutations(this.
|
|
778
|
+
const muts = this._rewriteMutations(this.ensureAttrs(), this._pendingMutations());
|
|
736
779
|
const prevMutation = muts.get(eventId);
|
|
737
780
|
if (!prevMutation) {
|
|
738
781
|
break;
|
|
@@ -741,11 +784,17 @@ class Reactor {
|
|
|
741
784
|
this._updatePendingMutations((prev) => {
|
|
742
785
|
prev.set(eventId, Object.assign(Object.assign({}, prev.get(eventId)), { 'tx-id': txId, confirmed: Date.now() }));
|
|
743
786
|
});
|
|
744
|
-
const newAttrs =
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
787
|
+
const newAttrs = [];
|
|
788
|
+
for (const step of prevMutation['tx-steps']) {
|
|
789
|
+
if (step[0] === 'add-attr') {
|
|
790
|
+
const attr = step[1];
|
|
791
|
+
newAttrs.push(attr);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
if (newAttrs.length) {
|
|
795
|
+
const existingAttrs = Object.values(this.ensureAttrs().attrs);
|
|
796
|
+
this._setAttrs([...existingAttrs, ...newAttrs]);
|
|
797
|
+
}
|
|
749
798
|
this._finishTransaction('synced', eventId);
|
|
750
799
|
this._cleanupPendingMutationsTimeout();
|
|
751
800
|
break;
|
|
@@ -906,10 +955,10 @@ class Reactor {
|
|
|
906
955
|
this._completeQueryOnce(q, hash, r.dfd);
|
|
907
956
|
}
|
|
908
957
|
_setAttrs(attrs) {
|
|
909
|
-
this.attrs = attrs.reduce((acc, attr) => {
|
|
958
|
+
this.attrs = new s.AttrsStoreClass(attrs.reduce((acc, attr) => {
|
|
910
959
|
acc[attr.id] = attr;
|
|
911
960
|
return acc;
|
|
912
|
-
}, {});
|
|
961
|
+
}, {}), this._linkIndex);
|
|
913
962
|
this.notifyAttrsSubs();
|
|
914
963
|
}
|
|
915
964
|
_startQuerySub(q, hash) {
|
|
@@ -1015,6 +1064,12 @@ class Reactor {
|
|
|
1015
1064
|
// We remove `add-attr` commands for attrs that already exist.
|
|
1016
1065
|
// We update `add-triple` and `retract-triple` commands to use the
|
|
1017
1066
|
// server attr-ids.
|
|
1067
|
+
/**
|
|
1068
|
+
*
|
|
1069
|
+
* @param {s.AttrsStore} attrs
|
|
1070
|
+
* @param {any} muts
|
|
1071
|
+
* @param {number} [processedTxId]
|
|
1072
|
+
*/
|
|
1018
1073
|
_rewriteMutations(attrs, muts, processedTxId) {
|
|
1019
1074
|
if (!attrs)
|
|
1020
1075
|
return muts;
|
|
@@ -1022,12 +1077,12 @@ class Reactor {
|
|
|
1022
1077
|
return new Map();
|
|
1023
1078
|
const findExistingAttr = (attr) => {
|
|
1024
1079
|
const [_, etype, label] = attr['forward-identity'];
|
|
1025
|
-
const existing =
|
|
1080
|
+
const existing = s.getAttrByFwdIdentName(attrs, etype, label);
|
|
1026
1081
|
return existing;
|
|
1027
1082
|
};
|
|
1028
1083
|
const findReverseAttr = (attr) => {
|
|
1029
1084
|
const [_, etype, label] = attr['forward-identity'];
|
|
1030
|
-
const revAttr =
|
|
1085
|
+
const revAttr = s.getAttrByReverseIdentName(attrs, etype, label);
|
|
1031
1086
|
return revAttr;
|
|
1032
1087
|
};
|
|
1033
1088
|
const mapping = { attrIdMap: {}, refSwapAttrIds: new Set() };
|
|
@@ -1090,8 +1145,11 @@ class Reactor {
|
|
|
1090
1145
|
}
|
|
1091
1146
|
// ---------------------------
|
|
1092
1147
|
// Transact
|
|
1148
|
+
/**
|
|
1149
|
+
* @returns {s.AttrsStore}
|
|
1150
|
+
*/
|
|
1093
1151
|
optimisticAttrs() {
|
|
1094
|
-
var _a;
|
|
1152
|
+
var _a, _b;
|
|
1095
1153
|
const pendingMutationSteps = [...this._pendingMutations().values()] // hack due to Map()
|
|
1096
1154
|
.flatMap((x) => x['tx-steps']);
|
|
1097
1155
|
const deletedAttrIds = new Set(pendingMutationSteps
|
|
@@ -1104,17 +1162,22 @@ class Reactor {
|
|
|
1104
1162
|
}
|
|
1105
1163
|
else if (_action === 'update-attr' &&
|
|
1106
1164
|
attr.id &&
|
|
1107
|
-
((_a = this.attrs) === null || _a === void 0 ? void 0 : _a
|
|
1108
|
-
const fullAttr = Object.assign(Object.assign({}, this.attrs
|
|
1165
|
+
((_a = this.attrs) === null || _a === void 0 ? void 0 : _a.getAttr(attr.id))) {
|
|
1166
|
+
const fullAttr = Object.assign(Object.assign({}, this.attrs.getAttr(attr.id)), attr);
|
|
1109
1167
|
pendingAttrs.push(fullAttr);
|
|
1110
1168
|
}
|
|
1111
1169
|
}
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
const
|
|
1117
|
-
|
|
1170
|
+
if (!deletedAttrIds.size && !pendingAttrs.length) {
|
|
1171
|
+
return this.attrs || new s.AttrsStoreClass({}, this._linkIndex);
|
|
1172
|
+
}
|
|
1173
|
+
const attrs = Object.assign({}, (((_b = this.attrs) === null || _b === void 0 ? void 0 : _b.attrs) || {}));
|
|
1174
|
+
for (const attr of pendingAttrs) {
|
|
1175
|
+
attrs[attr.id] = attr;
|
|
1176
|
+
}
|
|
1177
|
+
for (const id of deletedAttrIds) {
|
|
1178
|
+
delete attrs[id];
|
|
1179
|
+
}
|
|
1180
|
+
return new s.AttrsStoreClass(attrs, this._linkIndex);
|
|
1118
1181
|
}
|
|
1119
1182
|
/** Runs instaql on a query and a store */
|
|
1120
1183
|
dataForQuery(hash) {
|
|
@@ -1139,19 +1202,21 @@ class Reactor {
|
|
|
1139
1202
|
pendingMutationsVersion === cached.pendingMutationsVersion) {
|
|
1140
1203
|
return cached;
|
|
1141
1204
|
}
|
|
1142
|
-
const { store, pageInfo, aggregate, processedTxId } = result;
|
|
1143
|
-
const mutations = this._rewriteMutationsSorted(
|
|
1144
|
-
const newStore = this._applyOptimisticUpdates(store, mutations, processedTxId);
|
|
1145
|
-
const resp = (0, instaql_ts_1.default)({ store: newStore, pageInfo, aggregate }, q);
|
|
1205
|
+
const { store, attrsStore, pageInfo, aggregate, processedTxId } = result;
|
|
1206
|
+
const mutations = this._rewriteMutationsSorted(attrsStore, pendingMutations);
|
|
1207
|
+
const { store: newStore, attrsStore: newAttrsStore } = this._applyOptimisticUpdates(store, attrsStore, mutations, processedTxId);
|
|
1208
|
+
const resp = (0, instaql_ts_1.default)({ store: newStore, attrsStore: newAttrsStore, pageInfo, aggregate }, q);
|
|
1146
1209
|
return { data: resp, querySubVersion, pendingMutationsVersion };
|
|
1147
1210
|
}
|
|
1148
|
-
_applyOptimisticUpdates(store, mutations, processedTxId) {
|
|
1211
|
+
_applyOptimisticUpdates(store, attrsStore, mutations, processedTxId) {
|
|
1149
1212
|
for (const [_, mut] of mutations) {
|
|
1150
1213
|
if (!mut['tx-id'] || (processedTxId && mut['tx-id'] > processedTxId)) {
|
|
1151
|
-
|
|
1214
|
+
const result = s.transact(store, attrsStore, mut['tx-steps']);
|
|
1215
|
+
store = result.store;
|
|
1216
|
+
attrsStore = result.attrsStore;
|
|
1152
1217
|
}
|
|
1153
1218
|
}
|
|
1154
|
-
return store;
|
|
1219
|
+
return { store, attrsStore };
|
|
1155
1220
|
}
|
|
1156
1221
|
/** Re-compute all subscriptions */
|
|
1157
1222
|
notifyAll() {
|
|
@@ -1231,7 +1296,7 @@ class Reactor {
|
|
|
1231
1296
|
.forEach(({ eventId, q }) => {
|
|
1232
1297
|
this._trySendAuthed(eventId, { op: 'add-query', q });
|
|
1233
1298
|
});
|
|
1234
|
-
const muts = this._rewriteMutationsSorted(this.
|
|
1299
|
+
const muts = this._rewriteMutationsSorted(this.ensureAttrs(), this._pendingMutations());
|
|
1235
1300
|
muts.forEach(([eventId, mut]) => {
|
|
1236
1301
|
if (!mut['tx-id']) {
|
|
1237
1302
|
this._sendMutation(eventId, mut);
|
|
@@ -1518,7 +1583,7 @@ class Reactor {
|
|
|
1518
1583
|
subscribeAttrs(cb) {
|
|
1519
1584
|
this.attrsCbs.push(cb);
|
|
1520
1585
|
if (this.attrs) {
|
|
1521
|
-
cb(this.attrs);
|
|
1586
|
+
cb(this.attrs.attrs);
|
|
1522
1587
|
}
|
|
1523
1588
|
return () => {
|
|
1524
1589
|
this.attrsCbs = this.attrsCbs.filter((x) => x !== cb);
|
|
@@ -1534,7 +1599,7 @@ class Reactor {
|
|
|
1534
1599
|
if (!this.attrs)
|
|
1535
1600
|
return;
|
|
1536
1601
|
const oas = this.optimisticAttrs();
|
|
1537
|
-
this.attrsCbs.forEach((cb) => cb(oas));
|
|
1602
|
+
this.attrsCbs.forEach((cb) => cb(oas.attrs));
|
|
1538
1603
|
}
|
|
1539
1604
|
notifyConnectionStatusSubs(status) {
|
|
1540
1605
|
this.connectionStatusCbs.forEach((cb) => cb(status));
|