nowity 1.4.0 → 1.6.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 +32 -2
- package/dist/client/connection.d.ts.map +1 -1
- package/dist/client/connection.js +3 -1
- package/dist/client/nowity-client.d.ts +40 -2
- package/dist/client/nowity-client.d.ts.map +1 -1
- package/dist/client/nowity-client.js +41 -4
- package/dist/client/overlay.d.ts +52 -2
- package/dist/client/overlay.d.ts.map +1 -1
- package/dist/client/overlay.js +43 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +9 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -214,6 +214,29 @@ Acquisition is asynchronous, so the two things a `status` field would carry are
|
|
|
214
214
|
not in the result at all: "not there yet" is a pending promise, "not allowed" is
|
|
215
215
|
a rejected one.
|
|
216
216
|
|
|
217
|
+
#### Strict optimistic mode (tests)
|
|
218
|
+
|
|
219
|
+
A mutation without a client mutator is legal — it just waits for the server, and
|
|
220
|
+
nobody notices the lag until a user does. `strictOptimistic: true` (on
|
|
221
|
+
`NowityProvider`, `NowityConnection` or `NowityClient`; default from
|
|
222
|
+
`NOWITY_STRICT_OPTIMISTIC=1` where there is a `process.env`) turns that into an
|
|
223
|
+
error: every mutation needs either a mutator with `local()` or an explicit
|
|
224
|
+
declaration that it waits on purpose.
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
const mutators: ClientMutatorDef[] = [
|
|
228
|
+
{ name: "documents.rename", local: ([id, title], tx) => { tx.of("Document").get(id)!.title = title; } },
|
|
229
|
+
{ name: "billing.pay", pessimistic: "the charge is the server's to decide" },
|
|
230
|
+
];
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Anything else rejects with `StrictOptimisticError` (naming the mutation, with the
|
|
234
|
+
caller's stack), is not sent, and is reported through `reportError` so that a
|
|
235
|
+
swallowed rejection still shows up as an uncaught page error. A mutator that
|
|
236
|
+
runs but stages nothing for some arguments (an illegal move) is fine — the rule
|
|
237
|
+
is that somebody wrote the optimistic half, not that it always changes
|
|
238
|
+
something. Meant for test runs; leave it off in production.
|
|
239
|
+
|
|
217
240
|
### React
|
|
218
241
|
|
|
219
242
|
```tsx
|
|
@@ -411,11 +434,18 @@ it is the only part written twice:
|
|
|
411
434
|
|
|
412
435
|
```ts
|
|
413
436
|
{ name: "scores.add", local: ([id, delta], tx) => {
|
|
414
|
-
const
|
|
415
|
-
if (
|
|
437
|
+
const score = tx.of<{ points: number }>("Score").get(id);
|
|
438
|
+
if (score) score.points += delta;
|
|
416
439
|
} }
|
|
417
440
|
```
|
|
418
441
|
|
|
442
|
+
`tx.of(entity)` gives `get(id)`, `all()`, `create(row)` and `remove(id)`. A row from `get` is a
|
|
443
|
+
draft: reading it gives the current value, assigning to a field stages the change in the
|
|
444
|
+
overlay (not in the live MobX object, which the next rebase rebuilds). A row that is not
|
|
445
|
+
loaded gives `undefined`. `tx.get/all/update/put/remove(entity, …)` still work and are
|
|
446
|
+
deprecated. Type the entities once with `declare module "nowity/client" { interface
|
|
447
|
+
LocalEntities { Score: { id: string; points: number } } }` and `tx.of("Score")` knows its fields.
|
|
448
|
+
|
|
419
449
|
Rollback of a refused write and reconciliation with the confirmed one need no
|
|
420
450
|
code at all — that is what the rebase is.
|
|
421
451
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/client/connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAA0C,KAAK,gBAAgB,EAAE,MAAM,MAAM,CAAC;AACrF,OAAO,KAAK,EAAE,gBAAgB,EAAiB,MAAM,kBAAkB,CAAC;AAExE,OAAO,EAAmB,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAC1F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAsB,KAAK,UAAU,EAAE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AM,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC;IAuB3C,OAAO,CAAC,IAAI;IAON,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;YA4B7C,MAAM;IAepB;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,SAAS;IAehD,MAAM,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI;IAK9B,OAAO,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI;IAK/B,OAAO,CAAC,eAAe;IAsBvB,OAAO,CAAC,UAAU;CAMrB"}
|
|
@@ -211,9 +211,11 @@ class NowityConnection {
|
|
|
211
211
|
return fn;
|
|
212
212
|
}
|
|
213
213
|
async call(name, args) {
|
|
214
|
+
// Taken before the first await, where the caller is still on the stack.
|
|
215
|
+
const callSite = this.client.strictOptimistic ? new Error() : undefined;
|
|
214
216
|
const kind = await this.kindOf(name);
|
|
215
217
|
if (kind === "mutation") {
|
|
216
|
-
return this.client.mutate(name, args);
|
|
218
|
+
return this.client.mutate(name, args, callSite);
|
|
217
219
|
}
|
|
218
220
|
if (kind === "list") {
|
|
219
221
|
throw new Error(`"${name}" is a @list endpoint, so it has no single result to hand ` +
|
|
@@ -15,7 +15,8 @@ export interface EntityDescriptor {
|
|
|
15
15
|
idField?: string;
|
|
16
16
|
relations?: Record<string, RelationDescriptor>;
|
|
17
17
|
}
|
|
18
|
-
export
|
|
18
|
+
export type ClientMutatorDef<A = any> = OptimisticMutatorDef<A> | PessimisticMutatorDef;
|
|
19
|
+
export interface OptimisticMutatorDef<A = any> {
|
|
19
20
|
name: string;
|
|
20
21
|
/**
|
|
21
22
|
* Optimistic implementation. Runs immediately when the mutation is issued and
|
|
@@ -23,6 +24,26 @@ export interface ClientMutatorDef<A = any> {
|
|
|
23
24
|
* no randomness, no `Date.now()`, no network.
|
|
24
25
|
*/
|
|
25
26
|
local(args: A, tx: LocalTx): void;
|
|
27
|
+
pessimistic?: never;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A write that waits for the server on purpose.
|
|
31
|
+
*
|
|
32
|
+
* Some writes have nothing to show before the server decides — a payment, a
|
|
33
|
+
* sign-in, anything whose result only the server can know. Declaring them is
|
|
34
|
+
* what {@link NowityClientOptions.strictOptimistic} asks for: the reason is
|
|
35
|
+
* the record that somebody decided, rather than forgot.
|
|
36
|
+
*/
|
|
37
|
+
export interface PessimisticMutatorDef {
|
|
38
|
+
name: string;
|
|
39
|
+
/** Why this write has no optimistic effect. Must not be empty. */
|
|
40
|
+
pessimistic: string;
|
|
41
|
+
local?: never;
|
|
42
|
+
}
|
|
43
|
+
/** Thrown in strict mode by a mutation the client has no answer for. */
|
|
44
|
+
export declare class StrictOptimisticError extends Error {
|
|
45
|
+
readonly mutation: string;
|
|
46
|
+
constructor(mutation: string);
|
|
26
47
|
}
|
|
27
48
|
export interface NowityClientOptions {
|
|
28
49
|
transport: Transport;
|
|
@@ -40,6 +61,21 @@ export interface NowityClientOptions {
|
|
|
40
61
|
entities?: Record<string, EntityDescriptor>;
|
|
41
62
|
queries?: Record<string, QueryDescriptor>;
|
|
42
63
|
mutators?: ClientMutatorDef[];
|
|
64
|
+
/**
|
|
65
|
+
* Refuse a mutation that has neither a client mutator nor a declared
|
|
66
|
+
* `pessimistic` reason.
|
|
67
|
+
*
|
|
68
|
+
* A testing aid. Off, a mutation without a mutator simply lands when the
|
|
69
|
+
* server confirms it — legal, and invisible until somebody notices the lag.
|
|
70
|
+
* On, the same call rejects with {@link StrictOptimisticError} and is also
|
|
71
|
+
* reported as an uncaught error (`reportError`), so a caller that swallows
|
|
72
|
+
* the rejection cannot hide it from a test watching for page errors. Nothing
|
|
73
|
+
* is sent. Leave it off in production.
|
|
74
|
+
*
|
|
75
|
+
* Default: `NOWITY_STRICT_OPTIMISTIC=1` in the environment, where there is
|
|
76
|
+
* one; otherwise off.
|
|
77
|
+
*/
|
|
78
|
+
strictOptimistic?: boolean;
|
|
43
79
|
/**
|
|
44
80
|
* Called once the server has announced its API shape.
|
|
45
81
|
*
|
|
@@ -103,6 +139,8 @@ export declare class NowityClient {
|
|
|
103
139
|
*/
|
|
104
140
|
private readonly sentIdFields;
|
|
105
141
|
private readonly onError;
|
|
142
|
+
/** See {@link NowityClientOptions.strictOptimistic}. */
|
|
143
|
+
readonly strictOptimistic: boolean;
|
|
106
144
|
private pending;
|
|
107
145
|
/**
|
|
108
146
|
* Who is waiting for each mutation's answer. Separate from {@link pending}:
|
|
@@ -173,7 +211,7 @@ export declare class NowityClient {
|
|
|
173
211
|
* endpoint returned. It rejects if the mutation was refused — by then the
|
|
174
212
|
* optimistic effect is already undone.
|
|
175
213
|
*/
|
|
176
|
-
mutate<A, R = unknown>(name: string, args: A): Promise<R>;
|
|
214
|
+
mutate<A, R = unknown>(name: string, args: A, callSite?: Error): Promise<R>;
|
|
177
215
|
private handleMessage;
|
|
178
216
|
private handleWelcome;
|
|
179
217
|
private handleSnapshotMessage;
|
|
@@ -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,EAGhB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,QAAQ,EAAO,MAAM,eAAe,CAAC;AAEnD,OAAO,EACH,SAAS,EAET,KAAK,eAAe,EAEvB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAW,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,WAAW,kBAAkB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,gEAAgE;IAChE,QAAQ,CAAC,EAAE,MAAM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AAC,MAAM,CAgBX;IAEH,+EAA+E;IAC/E,OAAO,CAAC,SAAS;IAiBjB,OAAO,CAAC,WAAW;IAkBnB,OAAO,CAAC,cAAc;IAmBtB,OAAO,CAAC,eAAe;IAiBvB,wFAAwF;IACxF,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS;IAI5C,OAAO,CAAC,SAAS;IASjB,OAAO,CAAC,YAAY;IAMpB,8EAA8E;IAC9E,IAAI,oBAAoB,IAAI,MAAM,CAEjC;IAED,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,0EAA0E;IAC1E,aAAa,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI;CAG9C;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,MAAM,CAEpE"}
|
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NowityClient = void 0;
|
|
3
|
+
exports.NowityClient = exports.StrictOptimisticError = void 0;
|
|
4
4
|
exports.subscriptionKey = subscriptionKey;
|
|
5
5
|
const mobx_1 = require("mobx");
|
|
6
6
|
const protocol_1 = require("../core/protocol");
|
|
7
7
|
const confirmed_store_1 = require("./confirmed-store");
|
|
8
8
|
const live_query_1 = require("./live-query");
|
|
9
9
|
const overlay_1 = require("./overlay");
|
|
10
|
+
/** Thrown in strict mode by a mutation the client has no answer for. */
|
|
11
|
+
class StrictOptimisticError extends Error {
|
|
12
|
+
constructor(mutation) {
|
|
13
|
+
super(`Mutation "${mutation}" was called without an optimistic effect ` +
|
|
14
|
+
`(strictOptimistic is on). Register a client mutator for it — ` +
|
|
15
|
+
`{ name: "${mutation}", local(args, tx) { ... } } — or, if it ` +
|
|
16
|
+
`must wait for the server, declare it so: ` +
|
|
17
|
+
`{ name: "${mutation}", pessimistic: "<why>" }.`);
|
|
18
|
+
this.mutation = mutation;
|
|
19
|
+
this.name = "StrictOptimisticError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.StrictOptimisticError = StrictOptimisticError;
|
|
10
23
|
/** Shared empty result, so a query with no local inserts allocates nothing. */
|
|
11
24
|
const EMPTY_IDS = [];
|
|
12
25
|
/**
|
|
@@ -85,7 +98,7 @@ class NowityClient {
|
|
|
85
98
|
const overlay = new overlay_1.Overlay(this.confirmed, (entity) => this.idFieldOf(entity));
|
|
86
99
|
for (const mutation of this.pending) {
|
|
87
100
|
const def = this.mutators.get(mutation.name);
|
|
88
|
-
if (!def)
|
|
101
|
+
if (!def?.local)
|
|
89
102
|
continue;
|
|
90
103
|
try {
|
|
91
104
|
def.local(mutation.args, overlay.tx());
|
|
@@ -102,7 +115,11 @@ class NowityClient {
|
|
|
102
115
|
this.opts = opts;
|
|
103
116
|
this.onError = opts.onError ?? ((err) => console.error("[nowity/client]", err));
|
|
104
117
|
this.context = opts.context ?? null;
|
|
118
|
+
this.strictOptimistic = opts.strictOptimistic ?? strictFromEnv();
|
|
105
119
|
for (const mutator of opts.mutators ?? []) {
|
|
120
|
+
if (mutator.local === undefined && !mutator.pessimistic?.trim()) {
|
|
121
|
+
throw new Error(`Client mutator "${mutator.name}" has neither local() nor a pessimistic reason.`);
|
|
122
|
+
}
|
|
106
123
|
this.mutators.set(mutator.name, mutator);
|
|
107
124
|
}
|
|
108
125
|
}
|
|
@@ -272,10 +289,25 @@ class NowityClient {
|
|
|
272
289
|
* endpoint returned. It rejects if the mutation was refused — by then the
|
|
273
290
|
* optimistic effect is already undone.
|
|
274
291
|
*/
|
|
275
|
-
mutate(name, args) {
|
|
292
|
+
mutate(name, args, callSite) {
|
|
276
293
|
// A mutation with no local counterpart is legal: it simply has no
|
|
277
294
|
// optimistic effect and lands when the server confirms it. Refusing it
|
|
278
|
-
// here would make every write opt into optimism, which is backwards
|
|
295
|
+
// here would make every write opt into optimism, which is backwards —
|
|
296
|
+
// unless a test asked for exactly that.
|
|
297
|
+
if (this.strictOptimistic && !this.mutators.has(name)) {
|
|
298
|
+
const error = new StrictOptimisticError(name);
|
|
299
|
+
// The stack of the caller, not of this frame: the proxy awaits the
|
|
300
|
+
// handshake before it gets here, which cuts the click off the stack.
|
|
301
|
+
if (callSite?.stack) {
|
|
302
|
+
error.stack = `${error.name}: ${error.message}\n${callSite.stack.split("\n").slice(1).join("\n")}`;
|
|
303
|
+
}
|
|
304
|
+
const report = globalThis.reportError;
|
|
305
|
+
if (typeof report === "function")
|
|
306
|
+
report(error);
|
|
307
|
+
else
|
|
308
|
+
this.onError(error);
|
|
309
|
+
return Promise.reject(error);
|
|
310
|
+
}
|
|
279
311
|
const mutationId = ++this.lastMutationId;
|
|
280
312
|
return new Promise((resolve, reject) => {
|
|
281
313
|
this.callers.set(mutationId, { resolve: resolve, reject });
|
|
@@ -632,3 +664,8 @@ function stableStringify(value) {
|
|
|
632
664
|
.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`);
|
|
633
665
|
return `{${entries.join(",")}}`;
|
|
634
666
|
}
|
|
667
|
+
function strictFromEnv() {
|
|
668
|
+
// No `process` in a browser bundle, and a bundler may define it partially.
|
|
669
|
+
const env = globalThis.process?.env;
|
|
670
|
+
return env?.NOWITY_STRICT_OPTIMISTIC === "1";
|
|
671
|
+
}
|
package/dist/client/overlay.d.ts
CHANGED
|
@@ -1,13 +1,55 @@
|
|
|
1
1
|
import type { EntityId, Row } from "../core/types";
|
|
2
2
|
import type { ConfirmedStore } from "./confirmed-store";
|
|
3
|
+
/**
|
|
4
|
+
* Optionally maps entity names to their row types, so `tx.of("Score")` knows
|
|
5
|
+
* its fields. Augment it from application code:
|
|
6
|
+
*
|
|
7
|
+
* declare module "nowity/client" {
|
|
8
|
+
* interface LocalEntities { Score: { id: string; points: number } }
|
|
9
|
+
* }
|
|
10
|
+
*
|
|
11
|
+
* Left empty, `of` takes a row type as a type argument instead.
|
|
12
|
+
*/
|
|
13
|
+
export interface LocalEntities {
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A row as a mutator sees it: reads give the row's current value (confirmed data
|
|
17
|
+
* plus whatever earlier mutators staged), assignments stage a change.
|
|
18
|
+
*
|
|
19
|
+
* It is not the live MobX object the UI holds. Those are the *output* of a
|
|
20
|
+
* rebase and are rebuilt from it, so a write there would be gone on the next
|
|
21
|
+
* message; a draft writes into the overlay, which is what the rebase replays.
|
|
22
|
+
*/
|
|
23
|
+
export type Draft<T extends Row = Row> = T;
|
|
24
|
+
/** The rows of one entity, as a mutator sees them. */
|
|
25
|
+
export interface EntityTx<T extends Row = Row> {
|
|
26
|
+
/** The row, or undefined if it is not loaded. Assign to its fields to change it. */
|
|
27
|
+
get(id: EntityId): Draft<T> | undefined;
|
|
28
|
+
all(): Draft<T>[];
|
|
29
|
+
/** Insert a row that does not exist yet. An existing id is replaced whole. */
|
|
30
|
+
create(row: T): void;
|
|
31
|
+
remove(id: EntityId): void;
|
|
32
|
+
}
|
|
3
33
|
/** Write surface a client-side mutator gets. */
|
|
4
34
|
export interface LocalTx {
|
|
35
|
+
/** The rows of one entity: `tx.of("Score").get(id).points += delta`. */
|
|
36
|
+
of<K extends keyof LocalEntities>(entity: K): EntityTx<LocalEntities[K] & Row>;
|
|
37
|
+
of<T extends Row = Row>(entity: string): EntityTx<T>;
|
|
38
|
+
/** @deprecated Use `tx.of(entity).get(id)`. */
|
|
5
39
|
get(entity: string, id: EntityId): Row | undefined;
|
|
40
|
+
/** @deprecated Use `tx.of(entity).all()`. */
|
|
6
41
|
all(entity: string): Row[];
|
|
7
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Merge fields into an existing row. No-op if the row is not loaded.
|
|
44
|
+
* @deprecated Assign to the fields of `tx.of(entity).get(id)`.
|
|
45
|
+
*/
|
|
8
46
|
update(entity: string, id: EntityId, fields: Row): void;
|
|
9
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* Insert or replace a whole row.
|
|
49
|
+
* @deprecated Use `tx.of(entity).create(row)`, or assign to the fields of the row.
|
|
50
|
+
*/
|
|
10
51
|
put(entity: string, row: Row): void;
|
|
52
|
+
/** @deprecated Use `tx.of(entity).remove(id)`. */
|
|
11
53
|
remove(entity: string, id: EntityId): void;
|
|
12
54
|
}
|
|
13
55
|
/**
|
|
@@ -25,6 +67,14 @@ export declare class Overlay {
|
|
|
25
67
|
private readonly changes;
|
|
26
68
|
constructor(confirmed: ConfirmedStore, idFieldOf: (entity: string) => string);
|
|
27
69
|
tx(): LocalTx;
|
|
70
|
+
private entityTx;
|
|
71
|
+
/**
|
|
72
|
+
* A view of one row that reads through to the overlay on every access, so
|
|
73
|
+
* it stays right as later writes land and a row removed meanwhile reads as
|
|
74
|
+
* empty. Writes to a row that is gone are dropped, like `update` on one that
|
|
75
|
+
* is not loaded.
|
|
76
|
+
*/
|
|
77
|
+
private draft;
|
|
28
78
|
get(entity: string, id: EntityId): Row | undefined;
|
|
29
79
|
/**
|
|
30
80
|
* Rows a pending mutation created, per entity.
|
|
@@ -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,gDAAgD;AAChD,MAAM,WAAW,OAAO;IACpB,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,GAAG,GAAG,SAAS,CAAC;IACnD,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,EAAE,CAAC;IAC3B
|
|
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,mBA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AM,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,OAAO,CAAC,KAAK;CAQhB"}
|
package/dist/client/overlay.js
CHANGED
|
@@ -18,6 +18,7 @@ class Overlay {
|
|
|
18
18
|
}
|
|
19
19
|
tx() {
|
|
20
20
|
return {
|
|
21
|
+
of: ((entity) => this.entityTx(entity)),
|
|
21
22
|
get: (entity, id) => this.get(entity, id),
|
|
22
23
|
all: (entity) => [...this.merged(entity).values()],
|
|
23
24
|
update: (entity, id, fields) => {
|
|
@@ -33,6 +34,48 @@ class Overlay {
|
|
|
33
34
|
remove: (entity, id) => this.stage(entity, id, null),
|
|
34
35
|
};
|
|
35
36
|
}
|
|
37
|
+
entityTx(entity) {
|
|
38
|
+
return {
|
|
39
|
+
get: (id) => (this.get(entity, id) ? this.draft(entity, id) : undefined),
|
|
40
|
+
all: () => [...this.merged(entity).keys()].map((id) => this.draft(entity, id)),
|
|
41
|
+
create: (row) => this.stage(entity, String(row[this.idFieldOf(entity)]), { ...row }),
|
|
42
|
+
remove: (id) => this.stage(entity, id, null),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A view of one row that reads through to the overlay on every access, so
|
|
47
|
+
* it stays right as later writes land and a row removed meanwhile reads as
|
|
48
|
+
* empty. Writes to a row that is gone are dropped, like `update` on one that
|
|
49
|
+
* is not loaded.
|
|
50
|
+
*/
|
|
51
|
+
draft(entity, id) {
|
|
52
|
+
const current = () => this.get(entity, id);
|
|
53
|
+
return new Proxy({}, {
|
|
54
|
+
get: (_, key) => (typeof key === "string" ? current()?.[key] : undefined),
|
|
55
|
+
has: (_, key) => typeof key === "string" && current() !== undefined && key in current(),
|
|
56
|
+
set: (_, key, value) => {
|
|
57
|
+
const row = current();
|
|
58
|
+
if (row && typeof key === "string")
|
|
59
|
+
this.stage(entity, id, { ...row, [key]: value });
|
|
60
|
+
return true;
|
|
61
|
+
},
|
|
62
|
+
deleteProperty: (_, key) => {
|
|
63
|
+
const row = current();
|
|
64
|
+
if (row && typeof key === "string") {
|
|
65
|
+
const { [key]: _removed, ...rest } = row;
|
|
66
|
+
this.stage(entity, id, rest);
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
},
|
|
70
|
+
ownKeys: () => Object.keys(current() ?? {}),
|
|
71
|
+
getOwnPropertyDescriptor: (_, key) => {
|
|
72
|
+
const row = current();
|
|
73
|
+
if (!row || typeof key !== "string" || !(key in row))
|
|
74
|
+
return undefined;
|
|
75
|
+
return { value: row[key], writable: true, enumerable: true, configurable: true };
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
}
|
|
36
79
|
get(entity, id) {
|
|
37
80
|
const staged = this.changes.get(entity);
|
|
38
81
|
if (staged?.has(id))
|
package/dist/client.d.ts
CHANGED
|
@@ -37,8 +37,8 @@ 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 type
|
|
41
|
-
export type { LocalTx } from "./client/overlay";
|
|
40
|
+
export { StrictOptimisticError, type ClientMutatorDef, type OptimisticMutatorDef, type PessimisticMutatorDef, } from "./client/nowity-client";
|
|
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";
|
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,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAQ5E,OAAO,EACH,QAAQ,EACR,gBAAgB,EAChB,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,QAAQ,EACR,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,WAAW,EACX,cAAc;AACd;;;;;GAKG;AACH,UAAU,EACV,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,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AgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,KAAK,uBAAuB,GAC/B,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACjF;;;;;GAKG;AACH,OAAO,EACH,qBAAqB,EACrB,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EACzB,KAAK,qBAAqB,GAC7B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAChF,OAAO,EAAE,kBAAkB,EAAE,KAAK,yBAAyB,EAAE,MAAM,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"}
|
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.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.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; } });
|
|
@@ -72,6 +72,14 @@ var connection_1 = require("./client/connection");
|
|
|
72
72
|
Object.defineProperty(exports, "NowityConnection", { enumerable: true, get: function () { return connection_1.NowityConnection; } });
|
|
73
73
|
Object.defineProperty(exports, "activeConnection", { enumerable: true, get: function () { return connection_1.activeConnection; } });
|
|
74
74
|
Object.defineProperty(exports, "currentConnection", { enumerable: true, get: function () { return connection_1.currentConnection; } });
|
|
75
|
+
/**
|
|
76
|
+
* The optimistic half of a write, which is the one thing about an endpoint that
|
|
77
|
+
* cannot be derived: a mutation's body is server code and never reaches the
|
|
78
|
+
* bundle, so an application that wants the change on screen before the server
|
|
79
|
+
* has answered states its effect here.
|
|
80
|
+
*/
|
|
81
|
+
var nowity_client_1 = require("./client/nowity-client");
|
|
82
|
+
Object.defineProperty(exports, "StrictOptimisticError", { enumerable: true, get: function () { return nowity_client_1.StrictOptimisticError; } });
|
|
75
83
|
var ws_transport_1 = require("./client/ws-transport");
|
|
76
84
|
Object.defineProperty(exports, "WebSocketTransport", { enumerable: true, get: function () { return ws_transport_1.WebSocketTransport; } });
|
|
77
85
|
var client_1 = require("./ironbean/client");
|