nowity 1.6.0 → 1.7.0
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/README.md +58 -0
- package/dist/api/contract.d.ts +11 -0
- package/dist/api/contract.d.ts.map +1 -1
- package/dist/api/contract.js +5 -1
- package/dist/client/confirmed-store.d.ts +2 -11
- package/dist/client/confirmed-store.d.ts.map +1 -1
- package/dist/client/confirmed-store.js +5 -0
- package/dist/client/nowity-client.d.ts +85 -0
- package/dist/client/nowity-client.d.ts.map +1 -1
- package/dist/client/nowity-client.js +213 -14
- package/dist/client/overlay.d.ts +2 -0
- package/dist/client/overlay.d.ts.map +1 -1
- package/dist/client/overlay.js +4 -0
- package/dist/client.d.ts +2 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -1
- package/dist/core/derived.d.ts +96 -0
- package/dist/core/derived.d.ts.map +1 -0
- package/dist/core/derived.js +127 -0
- package/dist/core/protocol.d.ts +23 -1
- package/dist/core/protocol.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/server/api/bridge.d.ts +8 -0
- package/dist/server/api/bridge.d.ts.map +1 -1
- package/dist/server/api/bridge.js +20 -1
- package/dist/server/api/configure.d.ts.map +1 -1
- package/dist/server/api/configure.js +1 -0
- package/dist/server/api/derived.d.ts +143 -0
- package/dist/server/api/derived.d.ts.map +1 -0
- package/dist/server/api/derived.js +377 -0
- package/dist/server/api/execution.d.ts +6 -0
- package/dist/server/api/execution.d.ts.map +1 -1
- package/dist/server/api/execution.js +1 -0
- package/dist/server/api/read-keys.d.ts +20 -0
- package/dist/server/api/read-keys.d.ts.map +1 -1
- package/dist/server/api/read-keys.js +103 -0
- package/dist/server/api/runtime.d.ts +10 -0
- package/dist/server/api/runtime.d.ts.map +1 -1
- package/dist/server/api/runtime.js +1 -0
- package/dist/server/api/tracked-client.d.ts.map +1 -1
- package/dist/server/api/tracked-client.js +9 -0
- package/dist/server/evaluate.d.ts +2 -0
- package/dist/server/evaluate.d.ts.map +1 -1
- package/dist/server/evaluate.js +1 -0
- package/dist/server/mutator.d.ts +2 -0
- package/dist/server/mutator.d.ts.map +1 -1
- package/dist/server/query.d.ts +6 -0
- package/dist/server/query.d.ts.map +1 -1
- package/dist/server/sync-engine.d.ts +21 -0
- package/dist/server/sync-engine.d.ts.map +1 -1
- package/dist/server/sync-engine.js +74 -1
- package/package.json +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NowityClient = exports.StrictOptimisticError = void 0;
|
|
3
|
+
exports.NowityClient = exports.ConnectionLostError = exports.StrictOptimisticError = void 0;
|
|
4
4
|
exports.subscriptionKey = subscriptionKey;
|
|
5
5
|
const mobx_1 = require("mobx");
|
|
6
|
+
const derived_1 = require("../core/derived");
|
|
6
7
|
const protocol_1 = require("../core/protocol");
|
|
7
8
|
const confirmed_store_1 = require("./confirmed-store");
|
|
8
9
|
const live_query_1 = require("./live-query");
|
|
@@ -20,6 +21,19 @@ class StrictOptimisticError extends Error {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
exports.StrictOptimisticError = StrictOptimisticError;
|
|
24
|
+
/**
|
|
25
|
+
* A mutation that was waiting when the connection dropped, in `onDisconnect:
|
|
26
|
+
* "drop"` mode. It may or may not have reached the server; the data that
|
|
27
|
+
* arrives after the reconnect says which, and nothing resends it.
|
|
28
|
+
*/
|
|
29
|
+
class ConnectionLostError extends Error {
|
|
30
|
+
constructor(mutation) {
|
|
31
|
+
super(`The connection dropped before "${mutation}" was confirmed. It was not resent.`);
|
|
32
|
+
this.mutation = mutation;
|
|
33
|
+
this.name = "ConnectionLostError";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.ConnectionLostError = ConnectionLostError;
|
|
23
37
|
/** Shared empty result, so a query with no local inserts allocates nothing. */
|
|
24
38
|
const EMPTY_IDS = [];
|
|
25
39
|
/**
|
|
@@ -59,6 +73,12 @@ class NowityClient {
|
|
|
59
73
|
* a row under "undefined" and turns its first patch into a fragment.
|
|
60
74
|
*/
|
|
61
75
|
this.sentIdFields = new Map();
|
|
76
|
+
/** Derived mutators per subscription, by mutation name. */
|
|
77
|
+
this.derivedBySub = new Map();
|
|
78
|
+
/** Derived mutators the server's data contradicted; never used again here. */
|
|
79
|
+
this.derivedFallback = new Set();
|
|
80
|
+
/** Mutations the server declared `dedupe`. */
|
|
81
|
+
this.dedupe = new Set();
|
|
62
82
|
this.pending = [];
|
|
63
83
|
/**
|
|
64
84
|
* Who is waiting for each mutation's answer. Separate from {@link pending}:
|
|
@@ -89,6 +109,8 @@ class NowityClient {
|
|
|
89
109
|
this.reconnectTimer = null;
|
|
90
110
|
this.intentionalClose = false;
|
|
91
111
|
this.connected = false;
|
|
112
|
+
/** The current connection has been welcomed (its numbering is known). */
|
|
113
|
+
this.welcomed = false;
|
|
92
114
|
this.everWelcomed = false;
|
|
93
115
|
/** What this connection says it is looking at; resent after every reconnect. */
|
|
94
116
|
this.context = null;
|
|
@@ -96,12 +118,32 @@ class NowityClient {
|
|
|
96
118
|
this.rebase = (0, mobx_1.action)(() => {
|
|
97
119
|
this.pendingBox.set(this.pending.length > 0);
|
|
98
120
|
const overlay = new overlay_1.Overlay(this.confirmed, (entity) => this.idFieldOf(entity));
|
|
99
|
-
for (const mutation of this.pending) {
|
|
121
|
+
for (const mutation of this.optimisticOn ? this.pending : []) {
|
|
100
122
|
const def = this.mutators.get(mutation.name);
|
|
101
|
-
if (!def?.local)
|
|
102
|
-
continue;
|
|
103
123
|
try {
|
|
104
|
-
def
|
|
124
|
+
if (def?.local) {
|
|
125
|
+
def.local(mutation.args, overlay.tx());
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const derived = this.derivedOf(mutation.name);
|
|
129
|
+
if (!derived)
|
|
130
|
+
continue;
|
|
131
|
+
(0, derived_1.applyDerived)(derived, mutation.args, {
|
|
132
|
+
get: (entity, id) => overlay.get(entity, id),
|
|
133
|
+
stage: (entity, id, row) => overlay.write(entity, id, row),
|
|
134
|
+
// Per query instance: a row may change only if every
|
|
135
|
+
// subscription holding it was sent this mutator.
|
|
136
|
+
allows: (entity, id) => {
|
|
137
|
+
const holders = this.confirmed.holders(entity, id);
|
|
138
|
+
if (holders.size === 0)
|
|
139
|
+
return false;
|
|
140
|
+
for (const subId of holders) {
|
|
141
|
+
if (!this.derivedBySub.get(subId)?.has(mutation.name))
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
},
|
|
146
|
+
});
|
|
105
147
|
}
|
|
106
148
|
catch (err) {
|
|
107
149
|
this.onError(err);
|
|
@@ -116,6 +158,7 @@ class NowityClient {
|
|
|
116
158
|
this.onError = opts.onError ?? ((err) => console.error("[nowity/client]", err));
|
|
117
159
|
this.context = opts.context ?? null;
|
|
118
160
|
this.strictOptimistic = opts.strictOptimistic ?? strictFromEnv();
|
|
161
|
+
this.optimisticOn = opts.optimistic ?? true;
|
|
119
162
|
for (const mutator of opts.mutators ?? []) {
|
|
120
163
|
if (mutator.local === undefined && !mutator.pessimistic?.trim()) {
|
|
121
164
|
throw new Error(`Client mutator "${mutator.name}" has neither local() nor a pessimistic reason.`);
|
|
@@ -186,7 +229,10 @@ class NowityClient {
|
|
|
186
229
|
}
|
|
187
230
|
handleClose() {
|
|
188
231
|
this.connected = false;
|
|
232
|
+
this.welcomed = false;
|
|
189
233
|
(0, mobx_1.runInAction)(() => this.status.set(this.intentionalClose ? "idle" : "disconnected"));
|
|
234
|
+
if (this.opts.onDisconnect === "drop")
|
|
235
|
+
this.dropPending();
|
|
190
236
|
if (this.intentionalClose)
|
|
191
237
|
return;
|
|
192
238
|
// Not "wrong", but possibly no longer current — the UI can dim it.
|
|
@@ -210,6 +256,21 @@ class NowityClient {
|
|
|
210
256
|
send(message) {
|
|
211
257
|
this.opts.transport.send(message);
|
|
212
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Forget every unconfirmed mutation: the connection they were bound to is
|
|
261
|
+
* gone. Their effect leaves the overlay now; whatever of it the server did
|
|
262
|
+
* apply comes back with the data after the reconnect.
|
|
263
|
+
*/
|
|
264
|
+
dropPending() {
|
|
265
|
+
const callers = [...this.callers];
|
|
266
|
+
this.callers.clear();
|
|
267
|
+
(0, mobx_1.runInAction)(() => {
|
|
268
|
+
this.pending = [];
|
|
269
|
+
this.rebase();
|
|
270
|
+
});
|
|
271
|
+
for (const [, caller] of callers)
|
|
272
|
+
caller.reject(new ConnectionLostError(caller.name));
|
|
273
|
+
}
|
|
213
274
|
// --------------------------------------------------------------- subscriptions
|
|
214
275
|
/**
|
|
215
276
|
* Seed the store with data rendered on the server (`readInitialData`).
|
|
@@ -274,6 +335,7 @@ class NowityClient {
|
|
|
274
335
|
if (!sub)
|
|
275
336
|
return;
|
|
276
337
|
this.subs.delete(subId);
|
|
338
|
+
this.derivedBySub.delete(subId);
|
|
277
339
|
this.send({ t: "unsubscribe", subId });
|
|
278
340
|
this.resolveResync(subId);
|
|
279
341
|
(0, mobx_1.runInAction)(() => {
|
|
@@ -308,13 +370,53 @@ class NowityClient {
|
|
|
308
370
|
this.onError(error);
|
|
309
371
|
return Promise.reject(error);
|
|
310
372
|
}
|
|
373
|
+
if (this.opts.onDisconnect === "drop" && this.everWelcomed && !this.welcomed) {
|
|
374
|
+
// Bound to a connection there is none of — or one that has not said
|
|
375
|
+
// yet where the numbering stands, which would number this call
|
|
376
|
+
// after mutations the server never received. Queued, it would go
|
|
377
|
+
// out after the reconnect: the resend this mode exists not to do.
|
|
378
|
+
return Promise.reject(new ConnectionLostError(name));
|
|
379
|
+
}
|
|
380
|
+
const dedupeKey = this.dedupe.has(name) ? subscriptionKey(name, args) : undefined;
|
|
381
|
+
if (dedupeKey !== undefined) {
|
|
382
|
+
for (const caller of this.callers.values()) {
|
|
383
|
+
if (caller.dedupeKey === dedupeKey && caller.promise)
|
|
384
|
+
return caller.promise;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
311
387
|
const mutationId = ++this.lastMutationId;
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
this.send({ t: "mutate", mutationId, name, args });
|
|
388
|
+
const caller = { resolve: () => { }, reject: () => { }, name, dedupeKey };
|
|
389
|
+
const promise = new Promise((resolve, reject) => {
|
|
390
|
+
caller.resolve = resolve;
|
|
391
|
+
caller.reject = reject;
|
|
317
392
|
});
|
|
393
|
+
caller.promise = promise;
|
|
394
|
+
this.callers.set(mutationId, caller);
|
|
395
|
+
this.pending.push({ mutationId, name, args });
|
|
396
|
+
(0, mobx_1.runInAction)(() => this.rebase());
|
|
397
|
+
this.send({ t: "mutate", mutationId, name, args });
|
|
398
|
+
return promise;
|
|
399
|
+
}
|
|
400
|
+
/** See {@link NowityClientOptions.optimistic}. Takes effect immediately. */
|
|
401
|
+
get optimistic() {
|
|
402
|
+
return this.optimisticOn;
|
|
403
|
+
}
|
|
404
|
+
setOptimistic(on) {
|
|
405
|
+
if (this.optimisticOn === on)
|
|
406
|
+
return;
|
|
407
|
+
this.optimisticOn = on;
|
|
408
|
+
(0, mobx_1.runInAction)(() => this.rebase());
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Whether something called here is not confirmed yet — observable, for a
|
|
412
|
+
* UI that marks an action as still in flight.
|
|
413
|
+
*/
|
|
414
|
+
get hasPendingMutations() {
|
|
415
|
+
return this.pendingBox.get();
|
|
416
|
+
}
|
|
417
|
+
/** The derived mutators a subscription holds, for diagnostics and tests. */
|
|
418
|
+
derivedMutatorsOf(subId) {
|
|
419
|
+
return [...(this.derivedBySub.get(subId)?.values() ?? [])];
|
|
318
420
|
}
|
|
319
421
|
// -------------------------------------------------------------------- messages
|
|
320
422
|
handleMessage(message) {
|
|
@@ -338,10 +440,17 @@ class NowityClient {
|
|
|
338
440
|
for (const patch of message.patches) {
|
|
339
441
|
this.applyPatch(patch);
|
|
340
442
|
}
|
|
443
|
+
this.applyMutators(message.mutators);
|
|
341
444
|
this.ackThrough(message.lastMutationId);
|
|
342
445
|
this.rebase();
|
|
343
446
|
});
|
|
344
447
|
break;
|
|
448
|
+
case "mutators":
|
|
449
|
+
(0, mobx_1.runInAction)(() => {
|
|
450
|
+
this.applyMutators(message.subs);
|
|
451
|
+
this.rebase();
|
|
452
|
+
});
|
|
453
|
+
break;
|
|
345
454
|
case "mutation-result":
|
|
346
455
|
this.handleMutationResult(message.mutationId, message.ok, message.error, message.value);
|
|
347
456
|
break;
|
|
@@ -356,6 +465,8 @@ class NowityClient {
|
|
|
356
465
|
}
|
|
357
466
|
handleWelcome(message) {
|
|
358
467
|
const serverLastMutationId = message.lastMutationId;
|
|
468
|
+
this.welcomed = true;
|
|
469
|
+
this.dedupe = new Set((message.api?.contracts ?? []).flatMap((contract) => contract.endpoints.filter((e) => e.dedupe).map((e) => `${contract.name}.${e.method}`)));
|
|
359
470
|
if (message.api?.entities) {
|
|
360
471
|
this.serverEntities = message.api.entities;
|
|
361
472
|
}
|
|
@@ -367,8 +478,14 @@ class NowityClient {
|
|
|
367
478
|
(0, mobx_1.runInAction)(() => {
|
|
368
479
|
// Mutation ids must keep climbing across reconnects, otherwise the
|
|
369
480
|
// server sees a gap (or a replay) and rejects the numbering.
|
|
370
|
-
|
|
371
|
-
|
|
481
|
+
// Dropping mode has nothing queued to keep numbering for, and its
|
|
482
|
+
// last numbers may never have reached the server: continue from
|
|
483
|
+
// the server's, or the next mutation would leave a gap.
|
|
484
|
+
this.lastMutationId =
|
|
485
|
+
this.opts.onDisconnect === "drop"
|
|
486
|
+
? serverLastMutationId
|
|
487
|
+
: Math.max(this.lastMutationId, serverLastMutationId);
|
|
488
|
+
this.ackThrough(serverLastMutationId, false);
|
|
372
489
|
this.rebase();
|
|
373
490
|
});
|
|
374
491
|
// A mutation already out of the queue is not re-sent, so its result —
|
|
@@ -434,8 +551,22 @@ class NowityClient {
|
|
|
434
551
|
sub.filter.set(snapshot.filter);
|
|
435
552
|
sub.status.set("ready");
|
|
436
553
|
sub.error.set(null);
|
|
554
|
+
this.setDerived(snapshot.subId, snapshot.mutators ?? []);
|
|
437
555
|
this.resolveResync(snapshot.subId);
|
|
438
556
|
}
|
|
557
|
+
applyMutators(subs) {
|
|
558
|
+
for (const { subId, mutators } of subs ?? []) {
|
|
559
|
+
if (this.subs.has(subId))
|
|
560
|
+
this.setDerived(subId, mutators);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
setDerived(subId, mutators) {
|
|
564
|
+
if (mutators.length === 0) {
|
|
565
|
+
this.derivedBySub.delete(subId);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
this.derivedBySub.set(subId, new Map(mutators.map((m) => [m.name, m])));
|
|
569
|
+
}
|
|
439
570
|
applyPatch(patch) {
|
|
440
571
|
const sub = this.subs.get(patch.subId);
|
|
441
572
|
if (!sub)
|
|
@@ -467,6 +598,7 @@ class NowityClient {
|
|
|
467
598
|
// Never wait for the server to say which rows to delete — drop the whole
|
|
468
599
|
// slice of the store the subscription was responsible for.
|
|
469
600
|
this.confirmed.releaseSub(subId);
|
|
601
|
+
this.derivedBySub.delete(subId);
|
|
470
602
|
sub.ids.clear();
|
|
471
603
|
sub.status.set("revoked");
|
|
472
604
|
sub.error.set(reason);
|
|
@@ -513,8 +645,10 @@ class NowityClient {
|
|
|
513
645
|
return;
|
|
514
646
|
const deferred = this.deferredAck;
|
|
515
647
|
this.deferredAck = -1;
|
|
648
|
+
// Not checked: after an outage an acknowledged number may belong to a
|
|
649
|
+
// mutation the server refused, its result lost with the old socket.
|
|
516
650
|
if (deferred >= 0)
|
|
517
|
-
this.ackThrough(deferred);
|
|
651
|
+
this.ackThrough(deferred, false);
|
|
518
652
|
}
|
|
519
653
|
/**
|
|
520
654
|
* Forget mutations whose effect is already contained in confirmed data.
|
|
@@ -524,14 +658,79 @@ class NowityClient {
|
|
|
524
658
|
* would make the UI fall back to the pre-mutation value until the snapshot
|
|
525
659
|
* arrives — the same flicker the whole rebase mechanism exists to prevent.
|
|
526
660
|
*/
|
|
527
|
-
ackThrough(lastMutationId) {
|
|
661
|
+
ackThrough(lastMutationId, check = true) {
|
|
528
662
|
if (this.resyncPending.size > 0) {
|
|
529
663
|
this.deferredAck = Math.max(this.deferredAck, lastMutationId);
|
|
530
664
|
return;
|
|
531
665
|
}
|
|
532
666
|
if (this.pending.length === 0)
|
|
533
667
|
return;
|
|
668
|
+
const acked = this.pending.filter((mutation) => mutation.mutationId <= lastMutationId);
|
|
669
|
+
if (acked.length === 0)
|
|
670
|
+
return;
|
|
534
671
|
this.pending = this.pending.filter((mutation) => mutation.mutationId > lastMutationId);
|
|
672
|
+
if (check)
|
|
673
|
+
this.checkDerived(acked);
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Compare what derived mutators predicted with what the server confirmed.
|
|
677
|
+
*
|
|
678
|
+
* The confirmed store already holds data that includes these mutations, so
|
|
679
|
+
* every value a derived mutator replaced must be there — unless a later
|
|
680
|
+
* mutation, still pending, writes the same field. A difference means the
|
|
681
|
+
* fingerprint was wrong (a value it took for a constant was computed, a
|
|
682
|
+
* write it took for unconditional was not), or that someone else wrote the
|
|
683
|
+
* same field in between: either way this client stops predicting it.
|
|
684
|
+
*/
|
|
685
|
+
checkDerived(acked) {
|
|
686
|
+
if (!this.optimisticOn)
|
|
687
|
+
return;
|
|
688
|
+
const promised = new Map();
|
|
689
|
+
for (const mutation of acked) {
|
|
690
|
+
const derived = this.derivedOf(mutation.name);
|
|
691
|
+
if (!derived)
|
|
692
|
+
continue;
|
|
693
|
+
for (const p of (0, derived_1.derivedPredictions)(derived, mutation.args)) {
|
|
694
|
+
promised.set(`${p.entity}\u0000${p.id}\u0000${p.field}`, { name: mutation.name, ...p });
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
if (promised.size === 0)
|
|
698
|
+
return;
|
|
699
|
+
for (const mutation of this.pending) {
|
|
700
|
+
const derived = this.derivedOf(mutation.name);
|
|
701
|
+
if (!derived)
|
|
702
|
+
continue;
|
|
703
|
+
for (const p of (0, derived_1.derivedPredictions)(derived, mutation.args)) {
|
|
704
|
+
promised.delete(`${p.entity}\u0000${p.id}\u0000${p.field}`);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
const contradicted = new Set();
|
|
708
|
+
for (const p of promised.values()) {
|
|
709
|
+
const row = this.confirmed.get(p.entity, p.id);
|
|
710
|
+
if (!row || !(p.field in row))
|
|
711
|
+
continue;
|
|
712
|
+
if (!(0, derived_1.sameValue)(row[p.field], p.value))
|
|
713
|
+
contradicted.add(p.name);
|
|
714
|
+
}
|
|
715
|
+
for (const name of contradicted) {
|
|
716
|
+
this.derivedFallback.add(name);
|
|
717
|
+
this.opts.onDerivedMismatch?.(name);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* The derived mutator for a mutation, unless a hand-written one exists —
|
|
722
|
+
* `local()` and a declared `pessimistic` reason both win — or the server's
|
|
723
|
+
* data has contradicted it.
|
|
724
|
+
*/
|
|
725
|
+
derivedOf(name) {
|
|
726
|
+
if (this.mutators.has(name) || this.derivedFallback.has(name))
|
|
727
|
+
return undefined;
|
|
728
|
+
for (const bySub of this.derivedBySub.values()) {
|
|
729
|
+
const derived = bySub.get(name);
|
|
730
|
+
if (derived)
|
|
731
|
+
return derived;
|
|
732
|
+
}
|
|
733
|
+
return undefined;
|
|
535
734
|
}
|
|
536
735
|
// --------------------------------------------------------------- MobX identity
|
|
537
736
|
/** Observable identity map of an entity: id → live object. */
|
package/dist/client/overlay.d.ts
CHANGED
|
@@ -89,6 +89,8 @@ export declare class Overlay {
|
|
|
89
89
|
entities(): string[];
|
|
90
90
|
/** Confirmed rows with pending changes applied. */
|
|
91
91
|
merged(entity: string): Map<EntityId, Row>;
|
|
92
|
+
/** Stage a whole row (or its removal) — the write a derived mutator makes. */
|
|
93
|
+
write(entity: string, id: EntityId, row: Row | null): void;
|
|
92
94
|
private stage;
|
|
93
95
|
}
|
|
94
96
|
//# sourceMappingURL=overlay.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"overlay.d.ts","sourceRoot":"","sources":["../../src/client/overlay.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD;;;;;;;;;GASG;AACH,MAAM,WAAW,aAAa;CAAG;AAEjC;;;;;;;GAOG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI,CAAC,CAAC;AAE3C,sDAAsD;AACtD,MAAM,WAAW,QAAQ,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG;IACzC,oFAAoF;IACpF,GAAG,CAAC,EAAE,EAAE,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;IACxC,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IAClB,8EAA8E;IAC9E,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC;IACrB,MAAM,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAAC;CAC9B;AAED,gDAAgD;AAChD,MAAM,WAAW,OAAO;IACpB,wEAAwE;IACxE,EAAE,CAAC,CAAC,SAAS,MAAM,aAAa,EAAE,MAAM,EAAE,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;IAC/E,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IACrD,+CAA+C;IAC/C,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,GAAG,GAAG,SAAS,CAAC;IACnD,6C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C,kDAAkD;IAClD,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,IAAI,CAAC;CAC9C;AAED;;;;;;;;GAQG;AACH,qBAAa,OAAO;IAIZ,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAJ9B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgD;gBAGnD,SAAS,EAAE,cAAc,EACzB,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM;IAG1D,EAAE,IAAI,OAAO;IAkBb,OAAO,CAAC,QAAQ;IAShB;;;;;OAKG;IACH,OAAO,CAAC,KAAK;IA2Bb,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,GAAG,GAAG,SAAS;IAMlD;;;;;;;OAOG;IACH,QAAQ,IAAI,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC;IAYnC,yEAAyE;IACzE,QAAQ,IAAI,MAAM,EAAE;IAIpB,mDAAmD;IACnD,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC;IAY1C,8EAA8E;IAC9E,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI;IAI1D,OAAO,CAAC,KAAK;CAQhB"}
|
package/dist/client/overlay.js
CHANGED
|
@@ -121,6 +121,10 @@ class Overlay {
|
|
|
121
121
|
}
|
|
122
122
|
return out;
|
|
123
123
|
}
|
|
124
|
+
/** Stage a whole row (or its removal) — the write a derived mutator makes. */
|
|
125
|
+
write(entity, id, row) {
|
|
126
|
+
this.stage(entity, id, row === null ? null : { ...row });
|
|
127
|
+
}
|
|
124
128
|
stage(entity, id, row) {
|
|
125
129
|
let staged = this.changes.get(entity);
|
|
126
130
|
if (!staged) {
|
package/dist/client.d.ts
CHANGED
|
@@ -37,11 +37,12 @@ export type { QueryEntry, QueryError, QuerySource } from "./client/query-source"
|
|
|
37
37
|
* bundle, so an application that wants the change on screen before the server
|
|
38
38
|
* has answered states its effect here.
|
|
39
39
|
*/
|
|
40
|
-
export { StrictOptimisticError, type ClientMutatorDef, type OptimisticMutatorDef, type PessimisticMutatorDef, } from "./client/nowity-client";
|
|
40
|
+
export { ConnectionLostError, StrictOptimisticError, type ClientMutatorDef, type OptimisticMutatorDef, type PessimisticMutatorDef, } from "./client/nowity-client";
|
|
41
41
|
export type { Draft, EntityTx, LocalEntities, LocalTx } from "./client/overlay";
|
|
42
42
|
export { WebSocketTransport, type WebSocketTransportOptions } from "./client/ws-transport";
|
|
43
43
|
export { configureNowityClient, type NowityClientContainerConfig } from "./ironbean/client";
|
|
44
44
|
export { NowityClientToken } from "./ironbean/tokens";
|
|
45
45
|
export type { EntityId, Row } from "./core/types";
|
|
46
46
|
export type { HydrationPayload } from "./core/protocol";
|
|
47
|
+
export type { DerivedMutator, DerivedValue, DerivedWrite } from "./core/derived";
|
|
47
48
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EACH,GAAG,EACH,QAAQ,EACR,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,cAAc,EACd,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,SAAS,EACT,UAAU,EACV,aAAa,EACb,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,YAAY,GACpB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,QA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M,uBAAuB,CAAC;AAC3F,OAAO,EAAE,qBAAqB,EAAE,KAAK,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AAC5F,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAGtD,YAAY,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACxD,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/client.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
* load this module at all.
|
|
22
22
|
*/
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.NowityClientToken = exports.configureNowityClient = exports.WebSocketTransport = exports.StrictOptimisticError = exports.currentConnection = exports.activeConnection = exports.NowityConnection = exports.Restricted = exports.useQuerySource = exports.SourceScope = exports.NowityProvider = exports.useService = exports.DEFAULT_PAGE_SIZE = exports.LiveList = exports.List = exports.GridList = exports.useList = exports.useQuerySuspense = exports.observer = exports.isRejected = exports.isForbidden = exports.Rejected = exports.Forbidden = exports.buildManifest = exports.countQuery = exports.pageQuery = exports.endpointName = exports.resetContracts = exports.resolveContracts = exports.contractOf = exports.knownContracts = exports.mutation = exports.list = exports.query = exports.contract = exports.api = void 0;
|
|
24
|
+
exports.NowityClientToken = exports.configureNowityClient = exports.WebSocketTransport = exports.StrictOptimisticError = exports.ConnectionLostError = exports.currentConnection = exports.activeConnection = exports.NowityConnection = exports.Restricted = exports.useQuerySource = exports.SourceScope = exports.NowityProvider = exports.useService = exports.DEFAULT_PAGE_SIZE = exports.LiveList = exports.List = exports.GridList = exports.useList = exports.useQuerySuspense = exports.observer = exports.isRejected = exports.isForbidden = exports.Rejected = exports.Forbidden = exports.buildManifest = exports.countQuery = exports.pageQuery = exports.endpointName = exports.resetContracts = exports.resolveContracts = exports.contractOf = exports.knownContracts = exports.mutation = exports.list = exports.query = exports.contract = exports.api = void 0;
|
|
25
25
|
// ------------------------------------------------------------------- contracts
|
|
26
26
|
var contract_1 = require("./api/contract");
|
|
27
27
|
Object.defineProperty(exports, "api", { enumerable: true, get: function () { return contract_1.api; } });
|
|
@@ -79,6 +79,7 @@ Object.defineProperty(exports, "currentConnection", { enumerable: true, get: fun
|
|
|
79
79
|
* has answered states its effect here.
|
|
80
80
|
*/
|
|
81
81
|
var nowity_client_1 = require("./client/nowity-client");
|
|
82
|
+
Object.defineProperty(exports, "ConnectionLostError", { enumerable: true, get: function () { return nowity_client_1.ConnectionLostError; } });
|
|
82
83
|
Object.defineProperty(exports, "StrictOptimisticError", { enumerable: true, get: function () { return nowity_client_1.StrictOptimisticError; } });
|
|
83
84
|
var ws_transport_1 = require("./client/ws-transport");
|
|
84
85
|
Object.defineProperty(exports, "WebSocketTransport", { enumerable: true, get: function () { return ws_transport_1.WebSocketTransport; } });
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client mutators the server derived from what a mutation wrote.
|
|
3
|
+
*
|
|
4
|
+
* A hand-written `local()` says what a mutation does to the rows a browser
|
|
5
|
+
* holds. For the simplest writes that is a restatement of the server code —
|
|
6
|
+
* `update Document set title = $2 where id = $1` — and a restatement is
|
|
7
|
+
* somewhere for the two to drift. So the server watches what each mutation
|
|
8
|
+
* writes (§ "Návrh mutátorů přes get"), and where every written value is one
|
|
9
|
+
* of the mutation's own arguments, a constant, or an increment, it describes
|
|
10
|
+
* the write as data and sends it with the live queries it can affect.
|
|
11
|
+
*
|
|
12
|
+
* Only the description travels, never code: a template naming argument
|
|
13
|
+
* positions. The browser fills them with the arguments of each call.
|
|
14
|
+
*
|
|
15
|
+
* Shared by both halves, so nothing here may touch `pg`, `mobx` or Node.
|
|
16
|
+
*/
|
|
17
|
+
import type { EntityId, Row } from "./types";
|
|
18
|
+
/** Where a value of a derived write comes from. */
|
|
19
|
+
export type DerivedValue =
|
|
20
|
+
/** A leaf of the mutation's (positional) arguments, by path. */
|
|
21
|
+
{
|
|
22
|
+
arg: Array<string | number>;
|
|
23
|
+
}
|
|
24
|
+
/** The same value on every call. */
|
|
25
|
+
| {
|
|
26
|
+
const: string | number | boolean | null;
|
|
27
|
+
};
|
|
28
|
+
/** One row-level write of a derived mutator. */
|
|
29
|
+
export interface DerivedWrite {
|
|
30
|
+
/** Entity the row is filed under in the client store (the Prisma model). */
|
|
31
|
+
entity: string;
|
|
32
|
+
op: "update" | "delete";
|
|
33
|
+
/** The row's identity. */
|
|
34
|
+
id: DerivedValue;
|
|
35
|
+
/** Fields replaced by a value. */
|
|
36
|
+
set?: Record<string, DerivedValue>;
|
|
37
|
+
/** Numeric fields moved by an amount (a decrement is a negative amount). */
|
|
38
|
+
inc?: Record<string, DerivedValue>;
|
|
39
|
+
}
|
|
40
|
+
/** What the server sends for one mutation: its writes, as templates. */
|
|
41
|
+
export interface DerivedMutator {
|
|
42
|
+
/** Wire name of the mutation, `contract.method`. */
|
|
43
|
+
name: string;
|
|
44
|
+
writes: DerivedWrite[];
|
|
45
|
+
}
|
|
46
|
+
/** The value a template stands for in one call, or `undefined` if the path is not there. */
|
|
47
|
+
export declare function resolveDerived(value: DerivedValue, args: unknown): unknown;
|
|
48
|
+
/**
|
|
49
|
+
* A value as it appears in a row the server sent: dates as ISO strings, the
|
|
50
|
+
* rest as JSON has it. Arguments arrive as JSON already; this is for the rare
|
|
51
|
+
* caller that passes a `Date` directly.
|
|
52
|
+
*/
|
|
53
|
+
export declare function canonical(value: unknown): unknown;
|
|
54
|
+
/** The part of the overlay a derived mutator writes through. */
|
|
55
|
+
export interface DerivedTarget {
|
|
56
|
+
get(entity: string, id: EntityId): Row | undefined;
|
|
57
|
+
stage(entity: string, id: EntityId, row: Row | null): void;
|
|
58
|
+
/**
|
|
59
|
+
* May this mutator touch this row?
|
|
60
|
+
*
|
|
61
|
+
* Rows are shared between live queries in the identity map, so a write is
|
|
62
|
+
* only safe when every query holding the row was told the mutator applies
|
|
63
|
+
* to it — a filtered list that was not told would show a row that no
|
|
64
|
+
* longer belongs to it.
|
|
65
|
+
*/
|
|
66
|
+
allows(entity: string, id: EntityId): boolean;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Apply a derived mutator to the overlay.
|
|
70
|
+
*
|
|
71
|
+
* A row that is not loaded is skipped, not created: an update says nothing
|
|
72
|
+
* about the fields it does not write. A field the row does not carry is
|
|
73
|
+
* skipped too — the query selected it out, and adding it would hand the UI a
|
|
74
|
+
* field the server never sends for that query.
|
|
75
|
+
*/
|
|
76
|
+
export declare function applyDerived(mutator: DerivedMutator, args: unknown, target: DerivedTarget): void;
|
|
77
|
+
/** The `(entity, id, field) → value` a derived mutator promises, for checking it afterwards. */
|
|
78
|
+
export interface DerivedPrediction {
|
|
79
|
+
entity: string;
|
|
80
|
+
id: EntityId;
|
|
81
|
+
field: string;
|
|
82
|
+
value: unknown;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* What the confirmed row must say once the server has applied the mutation.
|
|
86
|
+
*
|
|
87
|
+
* Only replaced values: an increment's result depends on what the row held
|
|
88
|
+
* when the server applied it, which may legitimately be someone else's write
|
|
89
|
+
* later than anything this browser saw.
|
|
90
|
+
*/
|
|
91
|
+
export declare function derivedPredictions(mutator: DerivedMutator, args: unknown): DerivedPrediction[];
|
|
92
|
+
/** Fields a derived mutator writes, per entity — what a filter must not depend on. */
|
|
93
|
+
export declare function derivedFields(mutator: DerivedMutator): Map<string, Set<string>>;
|
|
94
|
+
/** Structural equality of two JSON values, for comparing a prediction with the server. */
|
|
95
|
+
export declare function sameValue(a: unknown, b: unknown): boolean;
|
|
96
|
+
//# sourceMappingURL=derived.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"derived.d.ts","sourceRoot":"","sources":["../../src/core/derived.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,QAAQ,EA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,GAAG,OAAO,CAAC;CACjD;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,GAAG,IAAI,CAmChG;AAED,gGAAgG;AAChG,MAAM,WAAW,iBAAiB;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,EAAE,EAAE,QAAQ,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,OAAO,GAAG,iBAAiB,EAAE,CAY9F;AAED,sFAAsF;AACtF,wBAAgB,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAS/E;AAED,0FAA0F;AAC1F,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAIzD"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveDerived = resolveDerived;
|
|
4
|
+
exports.canonical = canonical;
|
|
5
|
+
exports.applyDerived = applyDerived;
|
|
6
|
+
exports.derivedPredictions = derivedPredictions;
|
|
7
|
+
exports.derivedFields = derivedFields;
|
|
8
|
+
exports.sameValue = sameValue;
|
|
9
|
+
/** The value a template stands for in one call, or `undefined` if the path is not there. */
|
|
10
|
+
function resolveDerived(value, args) {
|
|
11
|
+
if ("const" in value)
|
|
12
|
+
return value.const;
|
|
13
|
+
let current = args;
|
|
14
|
+
for (const key of value.arg) {
|
|
15
|
+
if (current === null || typeof current !== "object")
|
|
16
|
+
return undefined;
|
|
17
|
+
current = current[key];
|
|
18
|
+
}
|
|
19
|
+
return canonical(current);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A value as it appears in a row the server sent: dates as ISO strings, the
|
|
23
|
+
* rest as JSON has it. Arguments arrive as JSON already; this is for the rare
|
|
24
|
+
* caller that passes a `Date` directly.
|
|
25
|
+
*/
|
|
26
|
+
function canonical(value) {
|
|
27
|
+
if (value === undefined)
|
|
28
|
+
return undefined;
|
|
29
|
+
if (value instanceof Date)
|
|
30
|
+
return value.toISOString();
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Apply a derived mutator to the overlay.
|
|
35
|
+
*
|
|
36
|
+
* A row that is not loaded is skipped, not created: an update says nothing
|
|
37
|
+
* about the fields it does not write. A field the row does not carry is
|
|
38
|
+
* skipped too — the query selected it out, and adding it would hand the UI a
|
|
39
|
+
* field the server never sends for that query.
|
|
40
|
+
*/
|
|
41
|
+
function applyDerived(mutator, args, target) {
|
|
42
|
+
// All or nothing: a row some query was not told about vetoes the whole
|
|
43
|
+
// mutator, or the UI would show half of a write the server does at once.
|
|
44
|
+
for (const write of mutator.writes) {
|
|
45
|
+
const rawId = resolveDerived(write.id, args);
|
|
46
|
+
if (rawId === undefined || rawId === null)
|
|
47
|
+
continue;
|
|
48
|
+
const id = String(rawId);
|
|
49
|
+
if (target.get(write.entity, id) && !target.allows(write.entity, id))
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
for (const write of mutator.writes) {
|
|
53
|
+
const rawId = resolveDerived(write.id, args);
|
|
54
|
+
if (rawId === undefined || rawId === null)
|
|
55
|
+
continue;
|
|
56
|
+
const id = String(rawId);
|
|
57
|
+
const row = target.get(write.entity, id);
|
|
58
|
+
if (!row)
|
|
59
|
+
continue;
|
|
60
|
+
if (write.op === "delete") {
|
|
61
|
+
target.stage(write.entity, id, null);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const next = { ...row };
|
|
65
|
+
for (const [field, source] of Object.entries(write.set ?? {})) {
|
|
66
|
+
if (!(field in next))
|
|
67
|
+
continue;
|
|
68
|
+
const value = resolveDerived(source, args);
|
|
69
|
+
if (value !== undefined)
|
|
70
|
+
next[field] = value;
|
|
71
|
+
}
|
|
72
|
+
for (const [field, source] of Object.entries(write.inc ?? {})) {
|
|
73
|
+
if (!(field in next))
|
|
74
|
+
continue;
|
|
75
|
+
const amount = resolveDerived(source, args);
|
|
76
|
+
const current = next[field];
|
|
77
|
+
if (typeof amount === "number" && typeof current === "number")
|
|
78
|
+
next[field] = current + amount;
|
|
79
|
+
}
|
|
80
|
+
target.stage(write.entity, id, next);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* What the confirmed row must say once the server has applied the mutation.
|
|
85
|
+
*
|
|
86
|
+
* Only replaced values: an increment's result depends on what the row held
|
|
87
|
+
* when the server applied it, which may legitimately be someone else's write
|
|
88
|
+
* later than anything this browser saw.
|
|
89
|
+
*/
|
|
90
|
+
function derivedPredictions(mutator, args) {
|
|
91
|
+
const out = [];
|
|
92
|
+
for (const write of mutator.writes) {
|
|
93
|
+
if (write.op !== "update")
|
|
94
|
+
continue;
|
|
95
|
+
const rawId = resolveDerived(write.id, args);
|
|
96
|
+
if (rawId === undefined || rawId === null)
|
|
97
|
+
continue;
|
|
98
|
+
for (const [field, source] of Object.entries(write.set ?? {})) {
|
|
99
|
+
const value = resolveDerived(source, args);
|
|
100
|
+
if (value !== undefined)
|
|
101
|
+
out.push({ entity: write.entity, id: String(rawId), field, value });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
/** Fields a derived mutator writes, per entity — what a filter must not depend on. */
|
|
107
|
+
function derivedFields(mutator) {
|
|
108
|
+
const out = new Map();
|
|
109
|
+
for (const write of mutator.writes) {
|
|
110
|
+
let fields = out.get(write.entity);
|
|
111
|
+
if (!fields)
|
|
112
|
+
out.set(write.entity, (fields = new Set()));
|
|
113
|
+
for (const field of Object.keys(write.set ?? {}))
|
|
114
|
+
fields.add(field);
|
|
115
|
+
for (const field of Object.keys(write.inc ?? {}))
|
|
116
|
+
fields.add(field);
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
/** Structural equality of two JSON values, for comparing a prediction with the server. */
|
|
121
|
+
function sameValue(a, b) {
|
|
122
|
+
if (a === b)
|
|
123
|
+
return true;
|
|
124
|
+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object")
|
|
125
|
+
return false;
|
|
126
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
127
|
+
}
|