nowity 1.10.0 → 1.11.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 +65 -15
- package/dist/core/types.d.ts +7 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/server/api/configure.d.ts +5 -1
- package/dist/server/api/configure.d.ts.map +1 -1
- package/dist/server/api/configure.js +2 -0
- package/dist/server/change-source/change-source.d.ts +23 -5
- package/dist/server/change-source/change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.d.ts +46 -12
- package/dist/server/change-source/outbox-change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.js +153 -65
- package/dist/server/change-source/pg-snapshot.d.ts +25 -0
- package/dist/server/change-source/pg-snapshot.d.ts.map +1 -0
- package/dist/server/change-source/pg-snapshot.js +43 -0
- package/dist/server/sync-engine.d.ts +110 -0
- package/dist/server/sync-engine.d.ts.map +1 -1
- package/dist/server/sync-engine.js +409 -111
- package/dist/server/ws-server.d.ts +8 -0
- package/dist/server/ws-server.d.ts.map +1 -1
- package/dist/server/ws-server.js +32 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -635,8 +635,8 @@ Three things follow from it being the database that coordinates:
|
|
|
635
635
|
- **Connections are sticky, but only for their lifetime.** A socket lives on one
|
|
636
636
|
instance; a client that reconnects elsewhere resends its subscriptions and
|
|
637
637
|
continues its mutation sequence from where the database says it got to.
|
|
638
|
-
- **A new instance starts from now.** It resumes
|
|
639
|
-
rather than replaying history, and its clients are still correct because a
|
|
638
|
+
- **A new instance starts from now.** It resumes from the snapshot its engine
|
|
639
|
+
started in rather than replaying history, and its clients are still correct because a
|
|
640
640
|
subscription's first answer is a snapshot of the database, not a replay of the
|
|
641
641
|
stream.
|
|
642
642
|
- **Retention waits for the slowest live reader.** `nowity.nowity_prune_outbox` deletes
|
|
@@ -1038,7 +1038,8 @@ Off by default. `true`, or a function decided once per connection at its hello
|
|
|
1038
1038
|
`timing` on every `mutation-result` — `receivedAt` (on the socket), `startedAt`
|
|
1039
1039
|
(its turn in the connection's queue), `committedAt`, `sentAt` — and on every
|
|
1040
1040
|
`tx` — `writtenAt` (its own earliest outbox row, the *database's* clock),
|
|
1041
|
-
`readAt` (handed over by the change source
|
|
1041
|
+
`readAt` (handed over by the change source; for the `tx` that carries the
|
|
1042
|
+
writer's own data with the ack, the commit), `fanOutAt`, `sentAt`. The recorder
|
|
1042
1043
|
keeps them on the mutation (`meta.server`, and `meta.push` with `meta.dataAt`
|
|
1043
1044
|
from the first data that included it), and `mutationPhases(record)` lays the
|
|
1044
1045
|
whole round on one axis: sent → received → started → written → committed →
|
|
@@ -1096,15 +1097,23 @@ sees the full `OLD` row — which the WAL does not, unless you opt into
|
|
|
1096
1097
|
`bigserial` alone is not a usable cursor: sequence values are handed out before
|
|
1097
1098
|
commit, so ordering by `seq` can walk past a row whose transaction commits later,
|
|
1098
1099
|
and `seq > cursor` would then hide it forever. Every outbox row therefore also
|
|
1099
|
-
stores `pg_current_xact_id()`, and the
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1100
|
+
stores `pg_current_xact_id()`, and the cursor is a *snapshot*
|
|
1101
|
+
(`pg_current_snapshot()`): "every transaction it sees has been delivered". A read
|
|
1102
|
+
returns the rows of the transactions its own statement snapshot sees and the
|
|
1103
|
+
cursor does not (`pg_visible_in_snapshot`), and that snapshot becomes the cursor.
|
|
1104
|
+
Each transaction becomes visible in exactly one such interval, so nothing is
|
|
1105
|
+
skipped and nothing is read twice.
|
|
1106
|
+
|
|
1107
|
+
Up to 1.10 the cursor was the oldest transaction still in flight
|
|
1108
|
+
(`pg_snapshot_xmin`), and one long write transaction anywhere in the database
|
|
1109
|
+
held every change behind it. With the snapshot cursor a transaction in flight is
|
|
1110
|
+
just not seen yet; the rest flows past it and its rows arrive after its commit
|
|
1111
|
+
(scenario 34e).
|
|
1112
|
+
|
|
1113
|
+
The same property closes the snapshot/stream hole: the engine starts from the
|
|
1114
|
+
snapshot its starting state was read in, and every later snapshot sees at least
|
|
1115
|
+
what the cursor sees. Anything a snapshot does not see is not under the cursor
|
|
1116
|
+
and gets streamed. Delivery is
|
|
1108
1117
|
at-least-once; because patches come from re-running the query and diffing, a replay
|
|
1109
1118
|
produces an empty diff, so the client observes exactly once.
|
|
1110
1119
|
|
|
@@ -1128,11 +1137,52 @@ snapshot there: the fan-out, the first answer of a subscription, server-side
|
|
|
1128
1137
|
rendering in that process. The engine fans out one batch at a time, so a pool of
|
|
1129
1138
|
a few connections is plenty.
|
|
1130
1139
|
|
|
1140
|
+
### Parallel fan-out (1.11)
|
|
1141
|
+
|
|
1142
|
+
A batch with a lot to recompute is spread over several snapshots at once:
|
|
1143
|
+
`createNowityServer({ fanOut: { concurrency, minPerSnapshot } })`, default 4
|
|
1144
|
+
snapshots and one per 8 recomputations, so a small batch still takes one. A
|
|
1145
|
+
session is never split between two snapshots — a client never sees half of a
|
|
1146
|
+
transaction — and batches still go out one after another, so the patches of one
|
|
1147
|
+
subscription keep their order. Each snapshot is a connection of the snapshot
|
|
1148
|
+
pool: size the pool to `concurrency` plus whatever subscribes, acks and
|
|
1149
|
+
server-side rendering need alongside.
|
|
1150
|
+
|
|
1151
|
+
### The writer's data comes with the ack (1.11)
|
|
1152
|
+
|
|
1153
|
+
A mutation reads, in its own transaction and in the same statement that moves
|
|
1154
|
+
`last_mutation_id`, what the capture trigger put in the outbox for it. Right
|
|
1155
|
+
after the commit the engine recomputes the writer's subscriptions those writes
|
|
1156
|
+
can reach and sends them as a `tx` — with `lastMutationId` = the mutation —
|
|
1157
|
+
immediately before `mutation-result`. The client needs nothing new: the `tx`
|
|
1158
|
+
confirms the mutation and drops its optimistic copy, the result resolves the
|
|
1159
|
+
promise. When the stream later carries the same transaction, the writer's
|
|
1160
|
+
subscriptions remember the snapshot the ack read in, see the transaction in it
|
|
1161
|
+
and are skipped (`engine.stats.skipped`); a batch that also carries something
|
|
1162
|
+
newer is recomputed and diffed as usual, so the writer is never sent the same
|
|
1163
|
+
state twice.
|
|
1164
|
+
|
|
1165
|
+
The ack is all or nothing and falls back to the stream when it cannot be
|
|
1166
|
+
complete: a write to a permission table (the invalidation path stays the
|
|
1167
|
+
stream's), a subscribe of the same connection still being evaluated, a change
|
|
1168
|
+
source that cannot say what a transaction wrote (WAL), or `ackWithState: false`.
|
|
1169
|
+
Its snapshots and recomputations are counted apart (`stats.acks`,
|
|
1170
|
+
`ackSnapshots`, `ackEvaluations`). Other clients get the transaction through the
|
|
1171
|
+
stream as before.
|
|
1172
|
+
|
|
1173
|
+
Mutations of one socket no longer queue behind its subscribes: they keep their
|
|
1174
|
+
order among themselves and wait for the socket's `hello`, but a slow subscribe
|
|
1175
|
+
in front of them does not hold them up. The start of a mutation's transaction is
|
|
1176
|
+
one statement instead of three (create-or-lock the client row and stamp the
|
|
1177
|
+
transaction).
|
|
1178
|
+
|
|
1131
1179
|
## Known limitations
|
|
1132
1180
|
|
|
1133
|
-
- **A long-running write transaction
|
|
1134
|
-
|
|
1135
|
-
|
|
1181
|
+
- **A long-running write transaction costs the outbox reads index range.** It no
|
|
1182
|
+
longer stalls the stream (1.11), but every read scans the outbox from the oldest
|
|
1183
|
+
xid in flight in its cursor, so a transaction open for minutes makes each read
|
|
1184
|
+
walk the rows written since it began (filtered out by visibility). It also holds
|
|
1185
|
+
retention back, as before.
|
|
1136
1186
|
- **Incremental view maintenance is naive**: on a relevant change the query is
|
|
1137
1187
|
re-executed and its result diffed. Correct for any query, O(result size) per
|
|
1138
1188
|
change. Which subscriptions are *relevant* is now decided by row rather than by
|
package/dist/core/types.d.ts
CHANGED
|
@@ -92,6 +92,13 @@ export interface TransactionBatch {
|
|
|
92
92
|
* means one.
|
|
93
93
|
*/
|
|
94
94
|
transactions?: number;
|
|
95
|
+
/**
|
|
96
|
+
* Ids of every transaction in the batch, when the source knows them (the
|
|
97
|
+
* outbox does). Lets the engine tell that a subscription it has already
|
|
98
|
+
* recomputed after these transactions committed needs nothing from the
|
|
99
|
+
* batch. Absent means "unknown", which only ever costs a recomputation.
|
|
100
|
+
*/
|
|
101
|
+
xids?: string[];
|
|
95
102
|
events: ChangeEvent[];
|
|
96
103
|
}
|
|
97
104
|
/** Field-level upsert for a single entity instance. */
|
package/dist/core/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,6EAA6E;AAC7E,MAAM,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE1C,4EAA4E;AAC5E,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC;AAE9B,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAEtD,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAExD;;;;;GAKG;AACH,MAAM,WAAW,OAAO;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,QAAQ,CAAC;CACnB;AAED,kDAAkD;AAClD,eAAO,MAAM,QAAQ,EAAG,KAAc,CAAC;AAEvC,iFAAiF;AACjF,MAAM,MAAM,SAAS,GAAG,OAAO,EAAE,GAAG,OAAO,QAAQ,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,MAAM,UAAU,GAAG,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AAEhD;;;;;;GAMG;AACH,MAAM,WAAW,WAAW;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,EAAE,QAAQ,CAAC;IACb,+DAA+D;IAC/D,EAAE,EAAE,GAAG,CAAC;IACR,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC;IACnB,KAAK,EAAE,GAAG,GAAG,IA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,MAAM,EAAE,CAAC;CACpB;AAED,yEAAyE;AACzE,MAAM,WAAW,OAAO;IACpB,MAAM,EAAE,QAAQ,EAAE,CAAC;IACnB,MAAM,EAAE,QAAQ,EAAE,CAAC;CACtB;AAED,kEAAkE;AAClE,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAElD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ChangeSource } from "../change-source/change-source";
|
|
2
|
-
import { SyncEngine } from "../sync-engine";
|
|
2
|
+
import { SyncEngine, type SyncEngineOptions } from "../sync-engine";
|
|
3
3
|
import { NowitySyncServer, type NowitySyncServerOptions } from "../ws-server";
|
|
4
4
|
import { PresenceTracker, type PresenceOptions } from "../presence";
|
|
5
5
|
import { SchedulerWorker, type SchedulerOptions } from "../scheduler";
|
|
@@ -22,6 +22,10 @@ export interface NowityServerConfig extends NowityRuntimeConfig {
|
|
|
22
22
|
syncServer?: Omit<NowitySyncServerOptions, "engine">;
|
|
23
23
|
/** See {@link SyncEngineOptions.narrowInvalidation}. On unless set. */
|
|
24
24
|
narrowInvalidation?: boolean;
|
|
25
|
+
/** See {@link SyncEngineOptions.ackWithState}. On unless `false`. */
|
|
26
|
+
ackWithState?: boolean;
|
|
27
|
+
/** See {@link SyncEngineOptions.fanOut}. */
|
|
28
|
+
fanOut?: SyncEngineOptions["fanOut"];
|
|
25
29
|
/**
|
|
26
30
|
* Record who is connected, so endpoints can read it through the injectable
|
|
27
31
|
* {@link Presence} and react to users going offline (`Presence.onOffline`).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configure.d.ts","sourceRoot":"","sources":["../../../src/server/api/configure.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAGnE,OAAO,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"configure.d.ts","sourceRoot":"","sources":["../../../src/server/api/configure.ts"],"names":[],"mappings":"AAUA,OA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,MAAM,CAAC;CAC5B;AAED,2CAA2C;AAC3C,eAAO,MAAM,yBAAyB,QAAS,CAAC;AAEhD,MAAM,WAAW,YAAa,SAAQ,aAAa;IAC/C,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,gBAAgB,CAAC;IACzB,sDAAsD;IACtD,QAAQ,EAAE,eAAe,GAAG,IAAI,CAAC;IACjC,2DAA2D;IAC3D,SAAS,EAAE,eAAe,GAAG,IAAI,CAAC;IAClC,sEAAsE;IACtE,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB;AAoBD,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,kBAAkB,GAAG,YAAY,CA+D3E"}
|
|
@@ -57,6 +57,8 @@ function createNowityServer(config) {
|
|
|
57
57
|
derived: runtime.api.derived,
|
|
58
58
|
api: { contracts: runtime.api.manifest.contracts, entities: runtime.api.entities },
|
|
59
59
|
narrowInvalidation: config.narrowInvalidation,
|
|
60
|
+
ackWithState: config.ackWithState,
|
|
61
|
+
fanOut: config.fanOut,
|
|
60
62
|
...(presence ? { presence } : {}),
|
|
61
63
|
onError: config.onError,
|
|
62
64
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { TransactionBatch } from "../../core/types";
|
|
1
|
+
import type { ChangeEvent, TransactionBatch } from "../../core/types";
|
|
2
2
|
import type { SnapshotContext } from "../db";
|
|
3
3
|
export type BatchHandler = (batch: TransactionBatch) => Promise<void>;
|
|
4
4
|
/**
|
|
@@ -9,10 +9,13 @@ export type BatchHandler = (batch: TransactionBatch) => Promise<void>;
|
|
|
9
9
|
* WAL can be swapped in later without touching the sync engine.
|
|
10
10
|
*
|
|
11
11
|
* Contract every implementation must honour:
|
|
12
|
-
* 1. `handler` is called with whole database transactions
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
12
|
+
* 1. `handler` is called with whole, committed database transactions — one
|
|
13
|
+
* per call, or several together (the outbox does that per read, so the
|
|
14
|
+
* engine takes one snapshot for all of them). A transaction is never split
|
|
15
|
+
* across calls, and one delivered in a later call committed after the ones
|
|
16
|
+
* of an earlier call. Within one call the order is the source's (the
|
|
17
|
+
* outbox orders by xid); the engine recomputes from the database, so it
|
|
18
|
+
* does not depend on it.
|
|
16
19
|
* 2. `handler` is awaited; no further batch is delivered until it resolves.
|
|
17
20
|
* 3. Delivery is at-least-once. Duplicates are allowed — the engine recomputes
|
|
18
21
|
* queries from the database, so replaying a batch produces an empty diff.
|
|
@@ -34,6 +37,21 @@ export interface ChangeSource {
|
|
|
34
37
|
* one.
|
|
35
38
|
*/
|
|
36
39
|
readonly attributesWrites?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* How a mutation can learn, inside its own transaction, which change
|
|
42
|
+
* events it produced — so the engine can recompute the writer's own
|
|
43
|
+
* subscriptions right after the commit and send them with the result,
|
|
44
|
+
* instead of after the round trip through the stream.
|
|
45
|
+
*
|
|
46
|
+
* `sql` is a scalar expression evaluated in the mutation's transaction
|
|
47
|
+
* after its writes; `parse` turns its value into events. Absent, the
|
|
48
|
+
* writer gets its data through the stream like everyone else (logical
|
|
49
|
+
* replication cannot do this: the WAL is only readable after commit).
|
|
50
|
+
*/
|
|
51
|
+
readonly ownWrites?: {
|
|
52
|
+
readonly sql: string;
|
|
53
|
+
parse(value: unknown, clientId: string, mutationId: number): ChangeEvent[];
|
|
54
|
+
};
|
|
37
55
|
startPosition(snap: SnapshotContext): Promise<string>;
|
|
38
56
|
start(from: string, handler: BatchHandler): Promise<void>;
|
|
39
57
|
stop(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"change-source.d.ts","sourceRoot":"","sources":["../../../src/server/change-source/change-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"change-source.d.ts","sourceRoot":"","sources":["../../../src/server/change-source/change-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAE7C,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,gBAAgB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,YAAY;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAEpC;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE;QACjB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QACrB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,WAAW,EAAE,CAAC;KAC9E,CAAC;IAEF,aAAa,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEtD,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1D,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtB,2EAA2E;IAC3E,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B"}
|
|
@@ -60,20 +60,25 @@ interface OutboxRow {
|
|
|
60
60
|
* The interesting part is the read cursor. `seq` alone is not usable: sequence
|
|
61
61
|
* values are assigned before commit, so ordering by `seq` can walk past a row
|
|
62
62
|
* whose transaction commits later, and that row would then be filtered out by
|
|
63
|
-
* `seq > cursor` forever.
|
|
64
|
-
*
|
|
65
|
-
* transaction id still in flight. That set has two properties we need:
|
|
63
|
+
* `seq > cursor` forever. A single `xid` cursor has the same hole for the same
|
|
64
|
+
* reason — xids are handed out at the first write, not at commit.
|
|
66
65
|
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
66
|
+
* The cursor is therefore a *snapshot* (`pg_current_snapshot()`): "every
|
|
67
|
+
* transaction this snapshot sees has been delivered". A read takes the
|
|
68
|
+
* statement's own snapshot and returns the rows of exactly the transactions it
|
|
69
|
+
* sees and the cursor did not. Every transaction becomes visible in exactly one
|
|
70
|
+
* such interval, so nothing is skipped and nothing is read twice, and the new
|
|
71
|
+
* snapshot becomes the cursor.
|
|
71
72
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
73
|
+
* Up to 1.10 the cursor was the transaction horizon (`xmin`) instead, and one
|
|
74
|
+
* long-running *write* transaction anywhere in the database — a scheduler job,
|
|
75
|
+
* a migration, a `psql` session left open — held every change behind it until
|
|
76
|
+
* it finished. With a snapshot cursor a transaction still in flight is simply
|
|
77
|
+
* not seen yet; whatever commits around it flows on, and its own rows arrive
|
|
78
|
+
* in the read after its commit. What such a transaction still costs is index
|
|
79
|
+
* range: the read scans from the oldest in-flight xid of the cursor, so a very
|
|
80
|
+
* long one makes each read walk the rows written since it began (filtered out
|
|
81
|
+
* by visibility, never delivered again).
|
|
77
82
|
*/
|
|
78
83
|
export declare class OutboxChangeSource implements ChangeSource {
|
|
79
84
|
readonly name = "outbox";
|
|
@@ -88,9 +93,21 @@ export declare class OutboxChangeSource implements ChangeSource {
|
|
|
88
93
|
readonly consumerId: string;
|
|
89
94
|
private readonly pruneIntervalMs;
|
|
90
95
|
private readonly pruneStaleAfter;
|
|
96
|
+
/** Every transaction this snapshot sees has been delivered. */
|
|
91
97
|
private cursor;
|
|
98
|
+
/** See {@link hold}. */
|
|
99
|
+
private held;
|
|
92
100
|
/** Last cursor written to `nowity_consumer`; avoids a write per idle lap. */
|
|
93
101
|
private publishedCursor;
|
|
102
|
+
/**
|
|
103
|
+
* What the engine may fold into a mutation's own transaction to learn what
|
|
104
|
+
* it wrote: the capture trigger has already put it here, under this
|
|
105
|
+
* transaction's xid.
|
|
106
|
+
*/
|
|
107
|
+
readonly ownWrites: {
|
|
108
|
+
sql: string;
|
|
109
|
+
parse: (value: unknown, clientId: string, mutationId: number) => ChangeEvent[];
|
|
110
|
+
};
|
|
94
111
|
private handler;
|
|
95
112
|
private listener;
|
|
96
113
|
private timer;
|
|
@@ -115,6 +132,13 @@ export declare class OutboxChangeSource implements ChangeSource {
|
|
|
115
132
|
* early.
|
|
116
133
|
*/
|
|
117
134
|
drain(): Promise<void>;
|
|
135
|
+
/**
|
|
136
|
+
* Stop reading until {@link release}, for tests that need several
|
|
137
|
+
* transactions in one read. Laps still run — `drain()` resolves — they
|
|
138
|
+
* just read nothing.
|
|
139
|
+
*/
|
|
140
|
+
hold(): void;
|
|
141
|
+
release(): Promise<void>;
|
|
118
142
|
private wake;
|
|
119
143
|
private drainLoop;
|
|
120
144
|
/** Tell retention where this reader is, when it has moved. */
|
|
@@ -127,7 +151,17 @@ export declare class OutboxChangeSource implements ChangeSource {
|
|
|
127
151
|
* rather than each other's.
|
|
128
152
|
*/
|
|
129
153
|
prune(): Promise<number>;
|
|
154
|
+
/**
|
|
155
|
+
* One page of a round: rows of transactions `target` sees and the cursor
|
|
156
|
+
* does not, whole transactions only, xids above `after`.
|
|
157
|
+
*
|
|
158
|
+
* The first page of a round has no target yet and takes its own statement
|
|
159
|
+
* snapshot as one, so the rows and the snapshot that becomes the cursor
|
|
160
|
+
* cannot disagree. Later pages read in newer statement snapshots and keep
|
|
161
|
+
* to the target explicitly.
|
|
162
|
+
*/
|
|
130
163
|
private fetch;
|
|
164
|
+
private page;
|
|
131
165
|
}
|
|
132
166
|
/**
|
|
133
167
|
* Whole transactions of one read as one batch, in commit order.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"outbox-change-source.d.ts","sourceRoot":"","sources":["../../../src/server/change-source/outbox-change-source.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAc,MAAM,IAAI,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAC3E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"outbox-change-source.d.ts","sourceRoot":"","sources":["../../../src/server/change-source/outbox-change-source.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAc,MAAM,IAAI,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAC3E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGlE,MAAM,WAAW,yBAAyB;IACtC,IAAI,EAAE,IAAI,CAAC;IACX,yCAAyC;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,wEAAwE;IACxE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,sEAAsE;IACtE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;;;;;OAQG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,yEAAyE;IACzE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC;AAED,UAAU,SAAS;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;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