nowity 1.6.0 → 1.8.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 +78 -6
- 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/core/types.d.ts +9 -1
- package/dist/core/types.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 +26 -0
- package/dist/server/api/runtime.d.ts.map +1 -1
- package/dist/server/api/runtime.js +3 -1
- package/dist/server/api/tracked-client.d.ts.map +1 -1
- package/dist/server/api/tracked-client.js +9 -0
- package/dist/server/change-source/change-source.d.ts +4 -2
- package/dist/server/change-source/change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.d.ts +35 -0
- package/dist/server/change-source/outbox-change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.js +23 -1
- 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 +27 -1
- package/dist/server/sync-engine.d.ts.map +1 -1
- package/dist/server/sync-engine.js +81 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -237,6 +237,64 @@ runs but stages nothing for some arguments (an illegal move) is fine — the rul
|
|
|
237
237
|
is that somebody wrote the optimistic half, not that it always changes
|
|
238
238
|
something. Meant for test runs; leave it off in production.
|
|
239
239
|
|
|
240
|
+
#### Derived client mutators
|
|
241
|
+
|
|
242
|
+
For the plainest writes the optimistic half is a restatement of the server
|
|
243
|
+
code: `update Document set title = $2 where id = $1`. So the server can derive
|
|
244
|
+
it — opt in with `createNowityServer({ derivedMutators: true })`. Every
|
|
245
|
+
`@mutation` runs over the tracking client, which records each model write;
|
|
246
|
+
after a committed call the writes are matched against the call's arguments and
|
|
247
|
+
the result is the mutation's *write fingerprint* — which model rows it writes
|
|
248
|
+
(by id), which fields, and whether each value is an argument, the caller's own
|
|
249
|
+
user id, a constant, or an increment. It is sent to nobody before two calls
|
|
250
|
+
agree on it (one call cannot tell an argument or a constant from a value that
|
|
251
|
+
happened to equal it); any later call that disagrees demotes the mutation to
|
|
252
|
+
the fallback for good. Writes Nowity itself makes inside a mutation (the
|
|
253
|
+
scheduler, presence) are not part of the fingerprint.
|
|
254
|
+
|
|
255
|
+
Each live query is sent the fingerprints that can change it, per instance (query
|
|
256
|
+
and arguments): the query holds rows of the written model, and none of the
|
|
257
|
+
written fields is a column its `where` or `orderBy` mentioned — otherwise the
|
|
258
|
+
row could leave, arrive or move, and that is the server's to say — including
|
|
259
|
+
columns of other tables it filters by through a relation, and the `where` and
|
|
260
|
+
`orderBy` of nested `include`s. Only the
|
|
261
|
+
template travels (`{ arg: [1] }`), never code. The browser then runs it over the
|
|
262
|
+
overlay when the mutation is called; a hand-written `local()` or a declared
|
|
263
|
+
`pessimistic` reason always wins over it, and a row shared with a query that was
|
|
264
|
+
not sent it is left alone (and then so is the rest of that mutator: all of it
|
|
265
|
+
or nothing).
|
|
266
|
+
|
|
267
|
+
The fallback — send the mutation, wait for the server's data — is everything
|
|
268
|
+
else: inserts and upserts (ids, defaults), writes to many rows, rows not named by
|
|
269
|
+
their id, values read or computed (the clock, `title + "!"`), conditions over
|
|
270
|
+
data the fingerprint never saw, nested and JSON writes, raw SQL, views of the
|
|
271
|
+
endpoint's own shape (`@query({ idField })`), and anything outside the database.
|
|
272
|
+
|
|
273
|
+
Once the server's data includes a mutation (`lastMutationId`), the browser
|
|
274
|
+
compares every value a derived mutator replaced with the confirmed row; a
|
|
275
|
+
difference (`onDerivedMismatch`) stops that browser predicting the mutation.
|
|
276
|
+
Only that browser: the row may carry someone else's later write, and the
|
|
277
|
+
server already checks every call against the fingerprint. Fingerprints live in
|
|
278
|
+
the memory of each sync process and are learned again after a restart;
|
|
279
|
+
`nowity.api.derived.report()` says where every mutation seen so far stands.
|
|
280
|
+
|
|
281
|
+
Switches, for comparing the two behaviours:
|
|
282
|
+
|
|
283
|
+
- `createNowityServer({ derivedMutators })` — off by default: learn nothing,
|
|
284
|
+
send nothing.
|
|
285
|
+
- `optimistic: false` on `NowityProvider` / `NowityConnection` / `NowityClient`,
|
|
286
|
+
or `client.setOptimistic(false)` at run time — no overlay at all, derived or
|
|
287
|
+
hand-written: the mutation is sent and the UI changes when the data arrives.
|
|
288
|
+
The confirmed data is the same either way.
|
|
289
|
+
|
|
290
|
+
Two related client options: `onDisconnect: "drop"` binds unconfirmed mutations to
|
|
291
|
+
the connection — a drop forgets them, rejects their callers with
|
|
292
|
+
`ConnectionLostError`, and nothing is resent (the default `"resend"` replays them
|
|
293
|
+
with the same numbers) — and `@mutation({ dedupe: true })` makes a second call
|
|
294
|
+
with the same arguments, while the first is unanswered, return the first call's
|
|
295
|
+
promise (off by default: `+1` twice is two increments). `client.hasPendingMutations`
|
|
296
|
+
is observable, for marking an action that is not confirmed yet.
|
|
297
|
+
|
|
240
298
|
### React
|
|
241
299
|
|
|
242
300
|
```tsx
|
|
@@ -949,6 +1007,26 @@ invisible in the snapshot is above the cursor and gets streamed. Delivery is
|
|
|
949
1007
|
at-least-once; because patches come from re-running the query and diffing, a replay
|
|
950
1008
|
produces an empty diff, so the client observes exactly once.
|
|
951
1009
|
|
|
1010
|
+
### One snapshot per read
|
|
1011
|
+
|
|
1012
|
+
Every transaction one read of the outbox returns has committed before the engine
|
|
1013
|
+
opens its snapshot, so they are fanned out together: each subscription they touch
|
|
1014
|
+
is recomputed once, and the mutation watermarks of every client that wrote are
|
|
1015
|
+
read in one statement. Under a burst that is a handful of read transactions a
|
|
1016
|
+
second instead of one per write. A client never sees half of a transaction —
|
|
1017
|
+
the snapshot is taken after all of them — but it may skip the states between two
|
|
1018
|
+
of them, which the next snapshot would have overwritten anyway. A
|
|
1019
|
+
hand-built `OutboxChangeSource` can turn it off with `coalesce: false`; the WAL
|
|
1020
|
+
source still delivers one transaction at a time.
|
|
1021
|
+
|
|
1022
|
+
Snapshots are interactive transactions. On the application's own Prisma client
|
|
1023
|
+
they wait in the same pool as its writes, and under load each starves the other.
|
|
1024
|
+
`createNowityServer({ snapshotPrisma })` takes a second client of the same schema
|
|
1025
|
+
— typically the same URL with a small `connection_limit` — and runs every
|
|
1026
|
+
snapshot there: the fan-out, the first answer of a subscription, server-side
|
|
1027
|
+
rendering in that process. The engine fans out one batch at a time, so a pool of
|
|
1028
|
+
a few connections is plenty.
|
|
1029
|
+
|
|
952
1030
|
## Known limitations
|
|
953
1031
|
|
|
954
1032
|
- **A long-running write transaction stalls the outbox stream** until it finishes
|
|
@@ -960,12 +1038,6 @@ produces an empty diff, so the client observes exactly once.
|
|
|
960
1038
|
table (see above), so what is left is one database round trip per subscription
|
|
961
1039
|
the change could actually have altered — about 1.3ms each, and the dominant
|
|
962
1040
|
term in every remaining measurement.
|
|
963
|
-
- **Change batches are not coalesced.** Each committed transaction gets its own
|
|
964
|
-
fan-out pass, including its own snapshot, even when fifty of them are already
|
|
965
|
-
queued. Merging what is waiting would save about a quarter of the wall time
|
|
966
|
-
under a burst (measured: 250 moves/s in `npm run perf -- throughput`), at the
|
|
967
|
-
cost of the change source's "one batch per transaction" contract. Not done:
|
|
968
|
-
the contract is relied on elsewhere, and the ratio does not justify it yet.
|
|
969
1041
|
- **Read keys narrow by equality only.** A range, a `contains`, a `NOT` or a
|
|
970
1042
|
case-insensitive comparison contributes no narrowing, and a query with nothing
|
|
971
1043
|
else to go on watches its whole table. The cost is the pre-existing one; the
|
package/dist/api/contract.d.ts
CHANGED
|
@@ -48,6 +48,15 @@ export type ListOptions = QueryOptions;
|
|
|
48
48
|
export interface MutationOptions {
|
|
49
49
|
/** Purely descriptive; mutations declare no read dependencies. */
|
|
50
50
|
description?: string;
|
|
51
|
+
/**
|
|
52
|
+
* A second call with the same arguments while the first is still waiting
|
|
53
|
+
* for its answer is the same call — the double click, not a second intent.
|
|
54
|
+
* The browser hands back the first call's promise and sends nothing.
|
|
55
|
+
*
|
|
56
|
+
* Off unless set: `+1` twice is two increments, and only the endpoint knows
|
|
57
|
+
* whether repeating it means anything.
|
|
58
|
+
*/
|
|
59
|
+
dedupe?: boolean;
|
|
51
60
|
}
|
|
52
61
|
export interface EndpointMeta {
|
|
53
62
|
method: string;
|
|
@@ -68,6 +77,8 @@ export interface ContractMeta {
|
|
|
68
77
|
export interface ManifestEndpoint {
|
|
69
78
|
method: string;
|
|
70
79
|
kind: EndpointKind;
|
|
80
|
+
/** See {@link MutationOptions.dedupe}. */
|
|
81
|
+
dedupe?: true;
|
|
71
82
|
}
|
|
72
83
|
export interface ManifestContract {
|
|
73
84
|
name: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/api/contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,kBAAkB,CAAC;AAG1B,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,UAAU,GAAG,MAAM,CAAC;AAEzD,6EAA6E;AAC7E,MAAM,WAAW,YAAY;IACzB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mEAAmE;IACnE,OAAO,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,YAAY,CAAC;AAEvC,MAAM,WAAW,eAAe;IAC5B,kEAAkE;IAClE,WAAW,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/api/contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,kBAAkB,CAAC;AAG1B,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,UAAU,GAAG,MAAM,CAAC;AAEzD,6EAA6E;AAC7E,MAAM,WAAW,YAAY;IACzB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mEAAmE;IACnE,OAAO,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,YAAY,CAAC;AAEvC,MAAM,WAAW,eAAe;IAC5B,kEAAkE;IAClE,WAAW,CAA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AAO,GAAE,WAAgB,GAAG,eAAe,CAI/D;AAED,0FAA0F;AAC1F,wBAAgB,QAAQ,CAAC,OAAO,GAAE,eAAoB,GAAG,eAAe,CAIvE;AAgBD;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,IAAI,YAAY,EAAE,CAmBjD;AAED,uFAAuF;AACvF,wBAAgB,cAAc,IAAI,YAAY,EAAE,CAE/C;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,QAAQ,GAAG,YAAY,GAAG,SAAS,CAErE;AAED,0EAA0E;AAC1E,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,MAAM,CAUhE;AAED,oDAAoD;AACpD,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAErE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,oEAAoE;AACpE,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,aAAa,CAAC,SAAS,EAAE,YAAY,EAAE,GAAG,WAAW,CAWpE;AAED,mDAAmD;AACnD,wBAAgB,cAAc,IAAI,IAAI,CAIrC"}
|
package/dist/api/contract.js
CHANGED
|
@@ -239,7 +239,11 @@ function buildManifest(contracts) {
|
|
|
239
239
|
return {
|
|
240
240
|
contracts: contracts.map((meta) => ({
|
|
241
241
|
name: meta.name,
|
|
242
|
-
endpoints: [...meta.endpoints.values()].map(({ method, kind }) => ({
|
|
242
|
+
endpoints: [...meta.endpoints.values()].map(({ method, kind, mutation }) => ({
|
|
243
|
+
method,
|
|
244
|
+
kind,
|
|
245
|
+
...(mutation?.dedupe ? { dedupe: true } : {}),
|
|
246
|
+
})),
|
|
243
247
|
})),
|
|
244
248
|
};
|
|
245
249
|
}
|
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
import type { EntityId, Row, RowPatch } from "../core/types";
|
|
2
|
-
/**
|
|
3
|
-
* The server-authoritative state of the client, as plain data.
|
|
4
|
-
*
|
|
5
|
-
* This is deliberately *not* the MobX layer. Optimistic mutations are replayed on
|
|
6
|
-
* top of this on every rebase, so it has to stay untouched by local writes —
|
|
7
|
-
* otherwise a rejected mutation could never be undone.
|
|
8
|
-
*
|
|
9
|
-
* Rows are shared between subscriptions and reference-counted: two live queries
|
|
10
|
-
* returning the same document hold one row, and the row survives until the last
|
|
11
|
-
* of them stops referencing it.
|
|
12
|
-
*/
|
|
13
2
|
export declare class ConfirmedStore {
|
|
14
3
|
private readonly rows;
|
|
15
4
|
private readonly refs;
|
|
@@ -18,6 +7,8 @@ export declare class ConfirmedStore {
|
|
|
18
7
|
entities(): string[];
|
|
19
8
|
rowsOf(entity: string): Map<EntityId, Row>;
|
|
20
9
|
get(entity: string, id: EntityId): Row | undefined;
|
|
10
|
+
/** Subscriptions holding a row; empty when nobody does. */
|
|
11
|
+
holders(entity: string, id: EntityId): ReadonlySet<string>;
|
|
21
12
|
/** Replace everything `subId` holds for `entity`. */
|
|
22
13
|
setSnapshot(subId: string, entity: string, rows: Row[], idField: string): void;
|
|
23
14
|
applyUpsert(subId: string, entity: string, patches: RowPatch[]): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"confirmed-store.d.ts","sourceRoot":"","sources":["../../src/client/confirmed-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"confirmed-store.d.ts","sourceRoot":"","sources":["../../src/client/confirmed-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAe7D,qBAAa,cAAc;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAyC;IAC9D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAiD;IACtE,8EAA8E;IAC9E,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiD;IAEvE,QAAQ,IAAI,MAAM,EAAE;IAIpB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC;IAI1C,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,GAAG,GAAG,SAAS;IAIlD,2DAA2D;IAC3D,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,WAAW,CAAC,MAAM,CAAC;IAI1D,qDAAqD;IACrD,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAgB9E,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,IAAI;IAWrE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,IAAI;IAIjE,qDAAqD;IACrD,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAS/B,KAAK,IAAI,IAAI;IAMb,OAAO,CAAC,MAAM;IASd,OAAO,CAAC,SAAS;IAcjB,OAAO,CAAC,MAAM;IAed,OAAO,CAAC,OAAO;CAUlB"}
|
|
@@ -12,6 +12,7 @@ exports.ConfirmedStore = void 0;
|
|
|
12
12
|
* returning the same document hold one row, and the row survives until the last
|
|
13
13
|
* of them stops referencing it.
|
|
14
14
|
*/
|
|
15
|
+
const EMPTY_HOLDERS = new Set();
|
|
15
16
|
class ConfirmedStore {
|
|
16
17
|
constructor() {
|
|
17
18
|
this.rows = new Map();
|
|
@@ -28,6 +29,10 @@ class ConfirmedStore {
|
|
|
28
29
|
get(entity, id) {
|
|
29
30
|
return this.rows.get(entity)?.get(id);
|
|
30
31
|
}
|
|
32
|
+
/** Subscriptions holding a row; empty when nobody does. */
|
|
33
|
+
holders(entity, id) {
|
|
34
|
+
return this.refs.get(entity)?.get(id) ?? EMPTY_HOLDERS;
|
|
35
|
+
}
|
|
31
36
|
/** Replace everything `subId` holds for `entity`. */
|
|
32
37
|
setSnapshot(subId, entity, rows, idField) {
|
|
33
38
|
const nextIds = new Set();
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { observable } from "mobx";
|
|
2
|
+
import { type DerivedMutator } from "../core/derived";
|
|
2
3
|
import type { HydrationPayload, ServerMessage, ServerWelcome } from "../core/protocol";
|
|
3
4
|
import type { EntityId } from "../core/types";
|
|
4
5
|
import { LiveQuery, type QueryDescriptor } from "./live-query";
|
|
@@ -45,6 +46,15 @@ export declare class StrictOptimisticError extends Error {
|
|
|
45
46
|
readonly mutation: string;
|
|
46
47
|
constructor(mutation: string);
|
|
47
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* A mutation that was waiting when the connection dropped, in `onDisconnect:
|
|
51
|
+
* "drop"` mode. It may or may not have reached the server; the data that
|
|
52
|
+
* arrives after the reconnect says which, and nothing resends it.
|
|
53
|
+
*/
|
|
54
|
+
export declare class ConnectionLostError extends Error {
|
|
55
|
+
readonly mutation: string;
|
|
56
|
+
constructor(mutation: string);
|
|
57
|
+
}
|
|
48
58
|
export interface NowityClientOptions {
|
|
49
59
|
transport: Transport;
|
|
50
60
|
/**
|
|
@@ -76,6 +86,36 @@ export interface NowityClientOptions {
|
|
|
76
86
|
* one; otherwise off.
|
|
77
87
|
*/
|
|
78
88
|
strictOptimistic?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Show writes before the server confirms them.
|
|
91
|
+
*
|
|
92
|
+
* On (the default), a mutation runs its client mutator over the overlay
|
|
93
|
+
* the moment it is called — a hand-written `local()`, or failing that the
|
|
94
|
+
* mutator the server derived for the live queries it affects. Off, nothing
|
|
95
|
+
* is predicted: the mutation is sent and the UI changes when the server's
|
|
96
|
+
* data arrives. The confirmed data is the same either way; the switch is
|
|
97
|
+
* there to compare the two (see also {@link NowityClient.setOptimistic}).
|
|
98
|
+
*/
|
|
99
|
+
optimistic?: boolean;
|
|
100
|
+
/**
|
|
101
|
+
* What happens to unconfirmed mutations when the connection drops.
|
|
102
|
+
*
|
|
103
|
+
* `"resend"` (the default) keeps them queued and sends them again with the
|
|
104
|
+
* same numbers after the reconnect; the server applies each number once.
|
|
105
|
+
* `"drop"` binds them to the connection: they are forgotten, their callers
|
|
106
|
+
* reject with {@link ConnectionLostError}, and the data the server sends
|
|
107
|
+
* after the reconnect says what actually happened. Nothing is repeated
|
|
108
|
+
* behind the user's back — the UI shows that the action was not confirmed.
|
|
109
|
+
*/
|
|
110
|
+
onDisconnect?: "resend" | "drop";
|
|
111
|
+
/**
|
|
112
|
+
* A derived mutator predicted a value the server's data then contradicted.
|
|
113
|
+
* This client stops using it. Local on purpose: the confirmed row may
|
|
114
|
+
* already carry someone else's later write, so a contradiction here is a
|
|
115
|
+
* reason for caution, not proof — the server checks every call of a
|
|
116
|
+
* mutation against its fingerprint itself.
|
|
117
|
+
*/
|
|
118
|
+
onDerivedMismatch?: (mutation: string) => void;
|
|
79
119
|
/**
|
|
80
120
|
* Called once the server has announced its API shape.
|
|
81
121
|
*
|
|
@@ -141,6 +181,14 @@ export declare class NowityClient {
|
|
|
141
181
|
private readonly onError;
|
|
142
182
|
/** See {@link NowityClientOptions.strictOptimistic}. */
|
|
143
183
|
readonly strictOptimistic: boolean;
|
|
184
|
+
/** See {@link NowityClientOptions.optimistic}. */
|
|
185
|
+
private optimisticOn;
|
|
186
|
+
/** Derived mutators per subscription, by mutation name. */
|
|
187
|
+
private readonly derivedBySub;
|
|
188
|
+
/** Derived mutators the server's data contradicted; never used again here. */
|
|
189
|
+
private readonly derivedFallback;
|
|
190
|
+
/** Mutations the server declared `dedupe`. */
|
|
191
|
+
private dedupe;
|
|
144
192
|
private pending;
|
|
145
193
|
/**
|
|
146
194
|
* Who is waiting for each mutation's answer. Separate from {@link pending}:
|
|
@@ -171,6 +219,8 @@ export declare class NowityClient {
|
|
|
171
219
|
private reconnectTimer;
|
|
172
220
|
private intentionalClose;
|
|
173
221
|
private connected;
|
|
222
|
+
/** The current connection has been welcomed (its numbering is known). */
|
|
223
|
+
private welcomed;
|
|
174
224
|
private everWelcomed;
|
|
175
225
|
/** What this connection says it is looking at; resent after every reconnect. */
|
|
176
226
|
private context;
|
|
@@ -194,6 +244,12 @@ export declare class NowityClient {
|
|
|
194
244
|
setContext(context: Record<string, unknown> | null): void;
|
|
195
245
|
private handleClose;
|
|
196
246
|
private send;
|
|
247
|
+
/**
|
|
248
|
+
* Forget every unconfirmed mutation: the connection they were bound to is
|
|
249
|
+
* gone. Their effect leaves the overlay now; whatever of it the server did
|
|
250
|
+
* apply comes back with the data after the reconnect.
|
|
251
|
+
*/
|
|
252
|
+
private dropPending;
|
|
197
253
|
/**
|
|
198
254
|
* Seed the store with data rendered on the server (`readInitialData`).
|
|
199
255
|
*
|
|
@@ -212,10 +268,22 @@ export declare class NowityClient {
|
|
|
212
268
|
* optimistic effect is already undone.
|
|
213
269
|
*/
|
|
214
270
|
mutate<A, R = unknown>(name: string, args: A, callSite?: Error): Promise<R>;
|
|
271
|
+
/** See {@link NowityClientOptions.optimistic}. Takes effect immediately. */
|
|
272
|
+
get optimistic(): boolean;
|
|
273
|
+
setOptimistic(on: boolean): void;
|
|
274
|
+
/**
|
|
275
|
+
* Whether something called here is not confirmed yet — observable, for a
|
|
276
|
+
* UI that marks an action as still in flight.
|
|
277
|
+
*/
|
|
278
|
+
get hasPendingMutations(): boolean;
|
|
279
|
+
/** The derived mutators a subscription holds, for diagnostics and tests. */
|
|
280
|
+
derivedMutatorsOf(subId: string): DerivedMutator[];
|
|
215
281
|
private handleMessage;
|
|
216
282
|
private handleWelcome;
|
|
217
283
|
private handleSnapshotMessage;
|
|
218
284
|
private applySnapshot;
|
|
285
|
+
private applyMutators;
|
|
286
|
+
private setDerived;
|
|
219
287
|
private applyPatch;
|
|
220
288
|
private applyRevoked;
|
|
221
289
|
private handleMutationResult;
|
|
@@ -231,6 +299,23 @@ export declare class NowityClient {
|
|
|
231
299
|
* arrives — the same flicker the whole rebase mechanism exists to prevent.
|
|
232
300
|
*/
|
|
233
301
|
private ackThrough;
|
|
302
|
+
/**
|
|
303
|
+
* Compare what derived mutators predicted with what the server confirmed.
|
|
304
|
+
*
|
|
305
|
+
* The confirmed store already holds data that includes these mutations, so
|
|
306
|
+
* every value a derived mutator replaced must be there — unless a later
|
|
307
|
+
* mutation, still pending, writes the same field. A difference means the
|
|
308
|
+
* fingerprint was wrong (a value it took for a constant was computed, a
|
|
309
|
+
* write it took for unconditional was not), or that someone else wrote the
|
|
310
|
+
* same field in between: either way this client stops predicting it.
|
|
311
|
+
*/
|
|
312
|
+
private checkDerived;
|
|
313
|
+
/**
|
|
314
|
+
* The derived mutator for a mutation, unless a hand-written one exists —
|
|
315
|
+
* `local()` and a declared `pessimistic` reason both win — or the server's
|
|
316
|
+
* data has contradicted it.
|
|
317
|
+
*/
|
|
318
|
+
private derivedOf;
|
|
234
319
|
/** Observable identity map of an entity: id → live object. */
|
|
235
320
|
mapFor(entity: string): ReturnType<typeof observable.map<EntityId, any>>;
|
|
236
321
|
/** A single live entity instance, or undefined if it is not loaded. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nowity-client.d.ts","sourceRoot":"","sources":["../../src/client/nowity-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,UAAU,EAAqD,MAAM,MAAM,CAAC;AAC7F,OAAO,KAAK,EAER,gBAAgB,EAGhB,aAAa,EAEb,aAAa,
|
|
1
|
+
{"version":3,"file":"nowity-client.d.ts","sourceRoot":"","sources":["../../src/client/nowity-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,UAAU,EAAqD,MAAM,MAAM,CAAC;AAC7F,OAAO,EAIH,KAAK,cAAc,EACtB,MAAM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