@jsb188/graphql 1.1.11 → 1.1.13
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/LICENSE +21 -0
- package/audience.d.ts +205 -0
- package/cache/config.ts +65 -28
- package/cache/index.helpers.ts +96 -0
- package/cache/index.ts +364 -808
- package/cache/mounted-queries.ts +33 -0
- package/cache/optimistic-ui-runtime.ts +46 -0
- package/cache/optimistic-ui.ts +401 -0
- package/cache/persist.ts +107 -22
- package/cache/realtime-fence.ts +47 -0
- package/cache/realtime-live.ts +17 -0
- package/cache/store.ts +22 -64
- package/cache/value.helpers.ts +17 -0
- package/{drafts/store.ts → client/draft-persistence-indexed-db.ts} +19 -3
- package/{drafts/index.ts → client/draft-persistence.ts} +13 -39
- package/client/index.ts +4 -2
- package/client/request.helpers.ts +14 -0
- package/client/request.ts +122 -232
- package/client/storage-asset.ts +58 -0
- package/gql/fragments/accountAuthFragments.ts +0 -4
- package/gql/fragments/agentMemoryFragments.ts +5 -1
- package/gql/fragments/agentNoteFragments.ts +156 -0
- package/gql/fragments/aiChatFragments.ts +35 -11
- package/gql/fragments/aiChatLinkFragments.ts +8 -0
- package/gql/fragments/audienceFragments.ts +76 -0
- package/gql/fragments/dataTableFragments.ts +48 -80
- package/gql/fragments/emailFragments.ts +49 -21
- package/gql/fragments/emailOutreachFragments.ts +281 -0
- package/gql/fragments/fieldDocFragments.ts +222 -40
- package/gql/fragments/inboundContactFragments.ts +2 -0
- package/gql/fragments/libraryFragments.ts +120 -0
- package/gql/fragments/logFragments.ts +93 -2
- package/gql/fragments/organizationFragments.ts +12 -0
- package/gql/fragments/qrCodeFragments.ts +29 -0
- package/gql/fragments/scheduleFragments.ts +574 -145
- package/gql/fragments/sheetFragments.ts +86 -424
- package/gql/fragments/storageFragments.ts +25 -1
- package/gql/fragments/workWaitFragments.ts +13 -0
- package/gql/fragments/workflowFragments.ts +32 -4
- package/gql/mutations/agentMemoryMutations.ts +32 -0
- package/gql/mutations/agentNoteMutations.ts +64 -0
- package/gql/mutations/aiChatLinkMutations.ts +13 -0
- package/gql/mutations/aiChatMutations.ts +34 -4
- package/gql/mutations/audienceMutations.ts +58 -0
- package/gql/mutations/authMutations.ts +24 -28
- package/gql/mutations/billingMutations.ts +24 -0
- package/gql/mutations/contactMutations.ts +23 -0
- package/gql/mutations/dataTableMutations.ts +18 -75
- package/gql/mutations/emailMutations.ts +22 -0
- package/gql/mutations/emailOutreachMutations.ts +139 -0
- package/gql/mutations/fieldDocMutations.ts +162 -13
- package/gql/mutations/index.ts +10 -5
- package/gql/mutations/organizationMutations.ts +6 -0
- package/gql/mutations/qrCodeMutations.ts +20 -0
- package/gql/mutations/scheduleMutations.ts +389 -417
- package/gql/mutations/sheetMutations.ts +62 -118
- package/gql/mutations/storageMutations.ts +1 -1
- package/gql/mutations/workflowMutations.ts +94 -0
- package/gql/queries/agentMemoryQueries.ts +12 -2
- package/gql/queries/agentNoteQueries.ts +86 -0
- package/gql/queries/aiChatQueries.ts +63 -12
- package/gql/queries/audienceQueries.ts +104 -0
- package/gql/queries/authQueries.ts +20 -4
- package/gql/queries/billingQueries.ts +114 -9
- package/gql/queries/dataTableQueries.ts +43 -75
- package/gql/queries/emailOutreachQueries.ts +282 -0
- package/gql/queries/emailQueries.ts +48 -64
- package/gql/queries/emailReplyReportQueries.ts +127 -0
- package/gql/queries/fieldDocQueries.ts +128 -98
- package/gql/queries/inboundContactQueries.ts +15 -4
- package/gql/queries/index.ts +5 -3
- package/gql/queries/libraryQueries.ts +112 -0
- package/gql/queries/logQueries.ts +86 -12
- package/gql/queries/organizationQueries.ts +44 -12
- package/gql/queries/qrCodeQueries.ts +27 -0
- package/gql/queries/scheduleQueries.ts +401 -278
- package/gql/queries/sheetQueries.ts +94 -165
- package/gql/queries/storageQueries.ts +11 -4
- package/gql/queries/workflowQueries.ts +61 -12
- package/gql/reactivity.ts +98 -0
- package/gql/recordReads.ts +171 -0
- package/hooks/index.helpers.ts +52 -0
- package/hooks/index.ts +920 -715
- package/hooks/use-agentMemory-mtn.ts +29 -31
- package/hooks/use-agentMemory-qry.ts +15 -3
- package/hooks/use-agentNote-mtn.ts +89 -0
- package/hooks/use-agentNote-qry.ts +162 -0
- package/hooks/use-agentNote-recovery.helpers.ts +58 -0
- package/hooks/use-agentNote-recovery.ts +57 -0
- package/hooks/use-aiChat-mtn.ts +104 -115
- package/hooks/use-aiChat-qry.helpers.ts +124 -0
- package/hooks/use-aiChat-qry.ts +254 -387
- package/hooks/use-aiChat-stream-mtn.ts +14 -0
- package/hooks/use-audience-mtn.ts +170 -0
- package/hooks/use-audience-qry.helpers.ts +39 -0
- package/hooks/use-audience-qry.ts +245 -0
- package/hooks/use-auth-mtn.ts +30 -1
- package/hooks/use-billing-mtn.ts +31 -0
- package/hooks/use-billing-qry.ts +76 -0
- package/hooks/use-dataTable-mtn.ts +23 -115
- package/hooks/use-dataTable-qry.ts +69 -914
- package/hooks/use-email-mtn.ts +26 -0
- package/hooks/use-email-qry.helpers.ts +37 -0
- package/hooks/use-email-qry.ts +44 -174
- package/hooks/use-emailOutreach-mtn.ts +211 -0
- package/hooks/use-emailOutreach-qry.helpers.ts +25 -0
- package/hooks/use-emailOutreach-qry.ts +367 -0
- package/hooks/use-emailOutreachReplyReport-qry.ts +46 -0
- package/hooks/use-fieldDoc-mtn.ts +244 -32
- package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
- package/hooks/use-fieldDoc-qry.ts +134 -102
- package/hooks/use-inboundContact-qry.ts +7 -9
- package/hooks/use-library-qry.ts +233 -0
- package/hooks/use-log-mtn.ts +25 -12
- package/hooks/use-log-qry.ts +99 -46
- package/hooks/use-organization-qry.ts +16 -60
- package/hooks/use-qrCode-mtn.ts +40 -0
- package/hooks/use-qrCode-qry.ts +78 -0
- package/hooks/use-schedule-mtn.ts +231 -348
- package/hooks/use-schedule-qry.ts +208 -139
- package/hooks/use-schedule-reactive.ts +218 -0
- package/hooks/use-sheet-mtn.ts +91 -199
- package/hooks/use-sheet-qry.ts +88 -237
- package/hooks/use-storage-qry.ts +84 -61
- package/hooks/use-workflow-mtn.ts +84 -23
- package/hooks/use-workflow-qry.ts +60 -61
- package/libs/ai-chat-link-events.ts +78 -0
- package/libs/ai-chat-outgoing.helpers.ts +153 -0
- package/libs/ai-chat-outgoing.ts +11 -0
- package/libs/ai-chat-outgoing.types.ts +25 -0
- package/libs/ai-chat-run-realtime.ts +20 -0
- package/libs/ai-chat-submission-realtime.ts +150 -0
- package/libs/data-changes.helpers.ts +161 -0
- package/libs/data-changes.ts +382 -0
- package/libs/fragment-realtime-sequence.ts +55 -0
- package/libs/open-sheet.ts +138 -0
- package/libs/record-read.ts +327 -0
- package/libs/sheet-command-batch.ts +45 -0
- package/libs/sheet-realtime-packet.ts +53 -0
- package/libs/sheet-view-realtime-router.ts +99 -0
- package/loaders/emailPreference.ts +69 -0
- package/loaders/rest.ts +12 -0
- package/package.json +77 -25
- package/schedule.d.ts +1183 -0
- package/sse.helpers.ts +19 -0
- package/sse.ts +26 -115
- package/types.d.ts +48 -25
- package/cache/index.test.ts +0 -881
- package/client/data-table-realtime-subscriptions.test.ts +0 -740
- package/client/data-table-realtime-subscriptions.ts +0 -955
- package/client/data-table-realtime.test.ts +0 -792
- package/client/data-table-realtime.ts +0 -1218
- package/client/fragment-realtime-sequence.ts +0 -52
- package/client/open-sheet.ts +0 -137
- package/client/request-managed-session.test.ts +0 -574
- package/client/request-no-store.test.ts +0 -157
- package/client/sse-ticket.test.ts +0 -26
- package/deno.lock +0 -170
- package/deno.test.json +0 -10
- package/gql/fragments/agentDocFragments.ts +0 -46
- package/gql/fragments/billingFragments.ts +0 -14
- package/gql/fragments/campaignFragments.ts +0 -277
- package/gql/mutations/agentDocMutations.ts +0 -177
- package/gql/mutations/campaignMutations.ts +0 -215
- package/gql/queries/agentDocQueries.ts +0 -120
- package/gql/queries/appQueries.ts +0 -14
- package/gql/queries/campaignQueries.ts +0 -127
- package/hooks/use-agentDoc-mtn.ts +0 -285
- package/hooks/use-agentDoc-qry.ts +0 -208
- package/hooks/use-app-qry.ts +0 -22
- package/hooks/use-campaign-mtn.ts +0 -377
- package/hooks/use-campaign-qry.ts +0 -257
- package/loaders/campaign.ts +0 -137
- package/tsconfig.json +0 -13
|
@@ -1,1218 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DataTable realtime ordering primitives. Every DataTable update carries a
|
|
3
|
-
* validated journal range; mounted tables additionally enforce contiguity.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type {
|
|
7
|
-
DataTableChangeWSMeta,
|
|
8
|
-
DataTableWriteOrderWS,
|
|
9
|
-
WSDataUpdateObj,
|
|
10
|
-
} from '@jsb188/app/types/app.d.ts';
|
|
11
|
-
import {
|
|
12
|
-
FRAGMENT_NAMES,
|
|
13
|
-
makeFragmentKey,
|
|
14
|
-
} from '@jsb188/mday/constants/graphqlReactivity.ts';
|
|
15
|
-
import {
|
|
16
|
-
getDataTableRealtimeWriteOrderKey,
|
|
17
|
-
normalizeDataTableRealtimeWriteOrders,
|
|
18
|
-
} from '@jsb188/mday/utils/dataTableRealtimeAuthority.ts';
|
|
19
|
-
|
|
20
|
-
const FILE_CONSTANTS = {
|
|
21
|
-
incompleteBatchTimeoutMs: 5000,
|
|
22
|
-
maxBufferedBatchesPerTable: 32,
|
|
23
|
-
maxChunksPerBatch: 1000,
|
|
24
|
-
maxInactiveHighWaterTables: 256,
|
|
25
|
-
maxSeq: 9223372036854775807n,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export const DATA_TABLE_REALTIME_INCOMPLETE_BATCH_TIMEOUT_MS =
|
|
29
|
-
FILE_CONSTANTS.incompleteBatchTimeoutMs;
|
|
30
|
-
|
|
31
|
-
type ParsedDataTableChangeWSMeta = DataTableChangeWSMeta & {
|
|
32
|
-
fromSeqValue: bigint;
|
|
33
|
-
toSeqValue: bigint;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
type BufferedDataTableRealtimeBatch = {
|
|
37
|
-
meta: ParsedDataTableChangeWSMeta;
|
|
38
|
-
chunks: Map<number, WSDataUpdateObj>;
|
|
39
|
-
startedAt: number;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
type DataTableRealtimeTableState = {
|
|
43
|
-
lastSeq: bigint | null;
|
|
44
|
-
batches: Map<string, BufferedDataTableRealtimeBatch>;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export type DataTableRealtimeSnapshot = {
|
|
48
|
-
currentSeq?: unknown;
|
|
49
|
-
dataTable?: {
|
|
50
|
-
id?: unknown;
|
|
51
|
-
} | null;
|
|
52
|
-
dataTableId?: unknown;
|
|
53
|
-
rows?: Array<{
|
|
54
|
-
id?: unknown;
|
|
55
|
-
cells?: Array<{
|
|
56
|
-
id?: unknown;
|
|
57
|
-
}> | null;
|
|
58
|
-
}> | null;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type DataTableRealtimeRecoveryReason =
|
|
62
|
-
| 'BUFFER_LIMIT'
|
|
63
|
-
| 'CHUNK_MISMATCH'
|
|
64
|
-
| 'CHUNK_TIMEOUT'
|
|
65
|
-
| 'GAP'
|
|
66
|
-
| 'MALFORMED_ENVELOPE'
|
|
67
|
-
| 'OVERLAP'
|
|
68
|
-
| 'SNAPSHOT_REQUIRED'
|
|
69
|
-
| 'UNKNOWN_BASELINE';
|
|
70
|
-
|
|
71
|
-
export type DataTableRealtimeGateResult =
|
|
72
|
-
| {
|
|
73
|
-
type: 'APPLY';
|
|
74
|
-
updates: WSDataUpdateObj[];
|
|
75
|
-
dataTableId: string;
|
|
76
|
-
organizationId: string;
|
|
77
|
-
toSeq: string;
|
|
78
|
-
}
|
|
79
|
-
| {
|
|
80
|
-
type: 'DROP';
|
|
81
|
-
dataTableId: string;
|
|
82
|
-
organizationId: string;
|
|
83
|
-
toSeq: string;
|
|
84
|
-
}
|
|
85
|
-
| {
|
|
86
|
-
type: 'WAIT';
|
|
87
|
-
dataTableId: string;
|
|
88
|
-
organizationId: string;
|
|
89
|
-
toSeq: string;
|
|
90
|
-
}
|
|
91
|
-
| {
|
|
92
|
-
type: 'RECOVER';
|
|
93
|
-
dataTableId: string | null;
|
|
94
|
-
lastSeq: string | null;
|
|
95
|
-
observedToSeq: string | null;
|
|
96
|
-
organizationId: string | null;
|
|
97
|
-
reason: DataTableRealtimeRecoveryReason;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export type DataTableRealtimeTailResolution = {
|
|
101
|
-
targetSeq: string;
|
|
102
|
-
reason: 'MALFORMED' | 'REPLAYABLE' | 'SNAPSHOT_REQUIRED';
|
|
103
|
-
requiresAuthoritativeSnapshot: true;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
export type DataTableRealtimeAuthoritySnapshot = {
|
|
107
|
-
orders: ReadonlyMap<string, DataTableWriteOrderWS>;
|
|
108
|
-
version: number;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const EMPTY_DATA_TABLE_REALTIME_AUTHORITY_SNAPSHOT: DataTableRealtimeAuthoritySnapshot =
|
|
112
|
-
{
|
|
113
|
-
orders: new Map(),
|
|
114
|
-
version: 0,
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
/*
|
|
118
|
-
* Own immutable per-table committed write-order snapshots for React consumers.
|
|
119
|
-
*/
|
|
120
|
-
class DataTableRealtimeAuthorityRegistry {
|
|
121
|
-
private snapshots = new Map<string, DataTableRealtimeAuthoritySnapshot>();
|
|
122
|
-
private listeners = new Map<string, Set<() => void>>();
|
|
123
|
-
|
|
124
|
-
/*
|
|
125
|
-
* Record only monotonic committed authority for each exact property target.
|
|
126
|
-
*/
|
|
127
|
-
record(
|
|
128
|
-
organizationId: string,
|
|
129
|
-
dataTableId: string,
|
|
130
|
-
orders: DataTableWriteOrderWS[],
|
|
131
|
-
) {
|
|
132
|
-
if (!orders.length) {
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
const key = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
136
|
-
const current =
|
|
137
|
-
this.snapshots.get(key) || EMPTY_DATA_TABLE_REALTIME_AUTHORITY_SNAPSHOT;
|
|
138
|
-
let nextOrders: Map<string, DataTableWriteOrderWS> | null = null;
|
|
139
|
-
orders.forEach((order) => {
|
|
140
|
-
const targetKey = getDataTableRealtimeWriteOrderKey(order);
|
|
141
|
-
const previous = (nextOrders || current.orders).get(targetKey);
|
|
142
|
-
if (previous && BigInt(previous.serverSeq) >= BigInt(order.serverSeq)) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
nextOrders ||= new Map(current.orders);
|
|
146
|
-
nextOrders.set(targetKey, order);
|
|
147
|
-
});
|
|
148
|
-
if (!nextOrders) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
this.snapshots.set(key, {
|
|
152
|
-
orders: nextOrders,
|
|
153
|
-
version: current.version + 1,
|
|
154
|
-
});
|
|
155
|
-
this.listeners.get(key)?.forEach((listener) => {
|
|
156
|
-
listener();
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/*
|
|
161
|
-
* Return the stable immutable snapshot for one DataTable.
|
|
162
|
-
*/
|
|
163
|
-
getSnapshot(organizationId: string, dataTableId: string) {
|
|
164
|
-
return (
|
|
165
|
-
this.snapshots.get(
|
|
166
|
-
getDataTableRealtimeKey(organizationId, dataTableId),
|
|
167
|
-
) || EMPTY_DATA_TABLE_REALTIME_AUTHORITY_SNAPSHOT
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/*
|
|
172
|
-
* Subscribe one mounted consumer to committed write-order changes.
|
|
173
|
-
*/
|
|
174
|
-
subscribe(organizationId: string, dataTableId: string, listener: () => void) {
|
|
175
|
-
const key = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
176
|
-
const listeners = this.listeners.get(key) || new Set<() => void>();
|
|
177
|
-
listeners.add(listener);
|
|
178
|
-
this.listeners.set(key, listeners);
|
|
179
|
-
return () => {
|
|
180
|
-
listeners.delete(listener);
|
|
181
|
-
if (!listeners.size) {
|
|
182
|
-
this.listeners.delete(key);
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/*
|
|
188
|
-
* Forget one table's committed authority and notify mounted consumers.
|
|
189
|
-
*/
|
|
190
|
-
clearTable(organizationId: string, dataTableId: string) {
|
|
191
|
-
const key = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
192
|
-
if (this.snapshots.delete(key)) {
|
|
193
|
-
this.listeners.get(key)?.forEach((listener) => {
|
|
194
|
-
listener();
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/*
|
|
200
|
-
* Forget all authority snapshots while preserving active subscriptions.
|
|
201
|
-
*/
|
|
202
|
-
clear() {
|
|
203
|
-
const keys = [...this.snapshots.keys()];
|
|
204
|
-
this.snapshots.clear();
|
|
205
|
-
keys.forEach((key) => {
|
|
206
|
-
this.listeners.get(key)?.forEach((listener) => {
|
|
207
|
-
listener();
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export const dataTableRealtimeAuthorityRegistry =
|
|
214
|
-
new DataTableRealtimeAuthorityRegistry();
|
|
215
|
-
|
|
216
|
-
/*
|
|
217
|
-
* Record committed DataTable authority delivered to a Sheet data room. A Sheet
|
|
218
|
-
* viewer never joins the table's own room, so this is the only settlement
|
|
219
|
-
* signal its bridged region previews receive. The registry only accepts
|
|
220
|
-
* advancing sequences, so a client that IS in both rooms is unaffected by
|
|
221
|
-
* receiving the same authority twice.
|
|
222
|
-
*/
|
|
223
|
-
export function recordSheetLinkedDataTableWriteOrders(value: unknown) {
|
|
224
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
const payload = value as Record<string, unknown>;
|
|
228
|
-
const orders = normalizeDataTableRealtimeWriteOrders(payload.orders);
|
|
229
|
-
if (
|
|
230
|
-
typeof payload.dataTableId !== 'string' ||
|
|
231
|
-
!payload.dataTableId ||
|
|
232
|
-
typeof payload.organizationId !== 'string' ||
|
|
233
|
-
!payload.organizationId ||
|
|
234
|
-
!orders
|
|
235
|
-
) {
|
|
236
|
-
return false;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
dataTableRealtimeAuthorityRegistry.record(
|
|
240
|
-
payload.organizationId,
|
|
241
|
-
payload.dataTableId,
|
|
242
|
-
orders,
|
|
243
|
-
);
|
|
244
|
-
return true;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/*
|
|
248
|
-
* Parse one non-negative decimal BIGINT without accepting coercible values.
|
|
249
|
-
*/
|
|
250
|
-
function parseDataTableRealtimeSeq(value: unknown) {
|
|
251
|
-
if (typeof value !== 'string' || !/^(0|[1-9]\d*)$/.test(value)) {
|
|
252
|
-
return null;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
try {
|
|
256
|
-
const seq = BigInt(value);
|
|
257
|
-
return seq <= FILE_CONSTANTS.maxSeq ? seq : null;
|
|
258
|
-
} catch {
|
|
259
|
-
return null;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/*
|
|
264
|
-
* Return the stable organization/table key used by the in-memory gate.
|
|
265
|
-
*/
|
|
266
|
-
export function getDataTableRealtimeKey(
|
|
267
|
-
organizationId: string,
|
|
268
|
-
dataTableId: string,
|
|
269
|
-
) {
|
|
270
|
-
return `${organizationId}:${dataTableId}`;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/*
|
|
274
|
-
* Validate the complete runtime websocket marker before buffering any chunk.
|
|
275
|
-
*/
|
|
276
|
-
function parseDataTableChangeWSMeta(
|
|
277
|
-
value: unknown,
|
|
278
|
-
): ParsedDataTableChangeWSMeta | null {
|
|
279
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
280
|
-
return null;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const meta = value as Partial<DataTableChangeWSMeta>;
|
|
284
|
-
const fromSeqValue = parseDataTableRealtimeSeq(meta.fromSeq);
|
|
285
|
-
const toSeqValue = parseDataTableRealtimeSeq(meta.toSeq);
|
|
286
|
-
if (
|
|
287
|
-
typeof meta.batchId !== 'string' ||
|
|
288
|
-
!meta.batchId ||
|
|
289
|
-
typeof meta.organizationId !== 'string' ||
|
|
290
|
-
!meta.organizationId ||
|
|
291
|
-
typeof meta.dataTableId !== 'string' ||
|
|
292
|
-
!meta.dataTableId ||
|
|
293
|
-
fromSeqValue === null ||
|
|
294
|
-
toSeqValue === null ||
|
|
295
|
-
toSeqValue < fromSeqValue ||
|
|
296
|
-
!Number.isSafeInteger(meta.chunkIndex) ||
|
|
297
|
-
!Number.isSafeInteger(meta.chunkCount) ||
|
|
298
|
-
meta.chunkIndex! < 0 ||
|
|
299
|
-
meta.chunkCount! < 1 ||
|
|
300
|
-
meta.chunkCount! > FILE_CONSTANTS.maxChunksPerBatch ||
|
|
301
|
-
meta.chunkIndex! >= meta.chunkCount!
|
|
302
|
-
) {
|
|
303
|
-
return null;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
return {
|
|
307
|
-
batchId: meta.batchId,
|
|
308
|
-
organizationId: meta.organizationId,
|
|
309
|
-
dataTableId: meta.dataTableId,
|
|
310
|
-
fromSeq: meta.fromSeq!,
|
|
311
|
-
toSeq: meta.toSeq!,
|
|
312
|
-
chunkIndex: meta.chunkIndex!,
|
|
313
|
-
chunkCount: meta.chunkCount!,
|
|
314
|
-
fromSeqValue,
|
|
315
|
-
toSeqValue,
|
|
316
|
-
};
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/*
|
|
320
|
-
* Return whether a later chunk still belongs to the exact buffered batch.
|
|
321
|
-
*/
|
|
322
|
-
function hasMatchingDataTableRealtimeBatchMetadata(
|
|
323
|
-
left: ParsedDataTableChangeWSMeta,
|
|
324
|
-
right: ParsedDataTableChangeWSMeta,
|
|
325
|
-
) {
|
|
326
|
-
return (
|
|
327
|
-
left.organizationId === right.organizationId &&
|
|
328
|
-
left.dataTableId === right.dataTableId &&
|
|
329
|
-
left.fromSeq === right.fromSeq &&
|
|
330
|
-
left.toSeq === right.toSeq &&
|
|
331
|
-
left.chunkCount === right.chunkCount
|
|
332
|
-
);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/*
|
|
336
|
-
* Return one complete buffered batch in its declared chunk order.
|
|
337
|
-
*/
|
|
338
|
-
function getOrderedDataTableRealtimeBatchUpdates(
|
|
339
|
-
batch: BufferedDataTableRealtimeBatch,
|
|
340
|
-
) {
|
|
341
|
-
return [...batch.chunks.entries()]
|
|
342
|
-
.sort(([left], [right]) => left - right)
|
|
343
|
-
.map(([, chunk]) => chunk);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/*
|
|
347
|
-
* Read the best available routing ids from a malformed runtime marker.
|
|
348
|
-
*/
|
|
349
|
-
function getMalformedDataTableRealtimeRouting(value: unknown) {
|
|
350
|
-
const meta =
|
|
351
|
-
value && typeof value === 'object' && !Array.isArray(value)
|
|
352
|
-
? (value as Partial<DataTableChangeWSMeta>)
|
|
353
|
-
: null;
|
|
354
|
-
|
|
355
|
-
return {
|
|
356
|
-
organizationId:
|
|
357
|
-
typeof meta?.organizationId === 'string' && meta.organizationId
|
|
358
|
-
? meta.organizationId
|
|
359
|
-
: null,
|
|
360
|
-
dataTableId:
|
|
361
|
-
typeof meta?.dataTableId === 'string' && meta.dataTableId
|
|
362
|
-
? meta.dataTableId
|
|
363
|
-
: null,
|
|
364
|
-
observedToSeq: parseDataTableRealtimeSeq(meta?.toSeq)?.toString() || null,
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/*
|
|
369
|
-
* Hold sequence-bearing websocket chunks until one exact contiguous batch is
|
|
370
|
-
* complete.
|
|
371
|
-
*/
|
|
372
|
-
export class DataTableRealtimeSequenceGate {
|
|
373
|
-
private tableStates = new Map<string, DataTableRealtimeTableState>();
|
|
374
|
-
private registrations = new Map<string, number>();
|
|
375
|
-
private inactiveHighWaterKeys = new Map<string, true>();
|
|
376
|
-
private observedSeqs = new Map<string, bigint>();
|
|
377
|
-
private fragmentSeqs = new Map<string, bigint>();
|
|
378
|
-
private fragmentKeysByTable = new Map<string, Set<string>>();
|
|
379
|
-
|
|
380
|
-
/*
|
|
381
|
-
* Consume one websocket update and return the only safe next client action.
|
|
382
|
-
*/
|
|
383
|
-
consume(update: WSDataUpdateObj): DataTableRealtimeGateResult {
|
|
384
|
-
if (!update.dataTableChange) {
|
|
385
|
-
return {
|
|
386
|
-
type: 'RECOVER',
|
|
387
|
-
dataTableId: null,
|
|
388
|
-
lastSeq: null,
|
|
389
|
-
observedToSeq: null,
|
|
390
|
-
organizationId: null,
|
|
391
|
-
reason: 'MALFORMED_ENVELOPE',
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
const meta = parseDataTableChangeWSMeta(update.dataTableChange);
|
|
396
|
-
if (!meta) {
|
|
397
|
-
const routing = getMalformedDataTableRealtimeRouting(
|
|
398
|
-
update.dataTableChange,
|
|
399
|
-
);
|
|
400
|
-
return {
|
|
401
|
-
type: 'RECOVER',
|
|
402
|
-
...routing,
|
|
403
|
-
lastSeq:
|
|
404
|
-
routing.organizationId && routing.dataTableId
|
|
405
|
-
? this.getLastSeq(routing.organizationId, routing.dataTableId)
|
|
406
|
-
: null,
|
|
407
|
-
reason: 'MALFORMED_ENVELOPE',
|
|
408
|
-
};
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
const key = getDataTableRealtimeKey(meta.organizationId, meta.dataTableId);
|
|
412
|
-
if (!this.registrations.has(key) && meta.chunkCount === 1) {
|
|
413
|
-
const observedSeq = this.observedSeqs.get(key);
|
|
414
|
-
if (observedSeq === undefined || meta.toSeqValue > observedSeq) {
|
|
415
|
-
this.observedSeqs.set(key, meta.toSeqValue);
|
|
416
|
-
}
|
|
417
|
-
this.trackInactiveHighWaterKey(key);
|
|
418
|
-
return {
|
|
419
|
-
type: 'APPLY',
|
|
420
|
-
updates: [update],
|
|
421
|
-
organizationId: meta.organizationId,
|
|
422
|
-
dataTableId: meta.dataTableId,
|
|
423
|
-
toSeq: meta.toSeq,
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
if (!this.registrations.has(key)) {
|
|
427
|
-
this.trackInactiveHighWaterKey(key);
|
|
428
|
-
}
|
|
429
|
-
const state = this.tableStates.get(key) || {
|
|
430
|
-
lastSeq: null,
|
|
431
|
-
batches: new Map<string, BufferedDataTableRealtimeBatch>(),
|
|
432
|
-
};
|
|
433
|
-
this.tableStates.set(key, state);
|
|
434
|
-
|
|
435
|
-
let batch = state.batches.get(meta.batchId);
|
|
436
|
-
if (!batch) {
|
|
437
|
-
if (state.batches.size >= FILE_CONSTANTS.maxBufferedBatchesPerTable) {
|
|
438
|
-
return this.clearBatchesAndBuildRecovery(state, meta, 'BUFFER_LIMIT');
|
|
439
|
-
}
|
|
440
|
-
batch = {
|
|
441
|
-
meta,
|
|
442
|
-
chunks: new Map<number, WSDataUpdateObj>(),
|
|
443
|
-
startedAt: Date.now(),
|
|
444
|
-
};
|
|
445
|
-
state.batches.set(meta.batchId, batch);
|
|
446
|
-
} else if (!hasMatchingDataTableRealtimeBatchMetadata(batch.meta, meta)) {
|
|
447
|
-
return this.clearBatchesAndBuildRecovery(state, meta, 'CHUNK_MISMATCH');
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
if (batch.chunks.has(meta.chunkIndex)) {
|
|
451
|
-
return {
|
|
452
|
-
type: 'DROP',
|
|
453
|
-
organizationId: meta.organizationId,
|
|
454
|
-
dataTableId: meta.dataTableId,
|
|
455
|
-
toSeq: meta.toSeq,
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
batch.chunks.set(meta.chunkIndex, update);
|
|
460
|
-
if (batch.chunks.size !== meta.chunkCount) {
|
|
461
|
-
return {
|
|
462
|
-
type: 'WAIT',
|
|
463
|
-
organizationId: meta.organizationId,
|
|
464
|
-
dataTableId: meta.dataTableId,
|
|
465
|
-
toSeq: meta.toSeq,
|
|
466
|
-
};
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
state.batches.delete(meta.batchId);
|
|
470
|
-
if (!this.registrations.has(key)) {
|
|
471
|
-
const observedSeq = this.observedSeqs.get(key);
|
|
472
|
-
if (observedSeq === undefined || meta.toSeqValue > observedSeq) {
|
|
473
|
-
this.observedSeqs.set(key, meta.toSeqValue);
|
|
474
|
-
}
|
|
475
|
-
this.trackInactiveHighWaterKey(key);
|
|
476
|
-
if (!state.batches.size) {
|
|
477
|
-
this.tableStates.delete(key);
|
|
478
|
-
}
|
|
479
|
-
return {
|
|
480
|
-
type: 'APPLY',
|
|
481
|
-
updates: getOrderedDataTableRealtimeBatchUpdates(batch),
|
|
482
|
-
organizationId: meta.organizationId,
|
|
483
|
-
dataTableId: meta.dataTableId,
|
|
484
|
-
toSeq: meta.toSeq,
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
if (state.lastSeq === null) {
|
|
488
|
-
return this.clearBatchesAndBuildRecovery(state, meta, 'UNKNOWN_BASELINE');
|
|
489
|
-
}
|
|
490
|
-
if (meta.toSeqValue <= state.lastSeq) {
|
|
491
|
-
return {
|
|
492
|
-
type: 'DROP',
|
|
493
|
-
organizationId: meta.organizationId,
|
|
494
|
-
dataTableId: meta.dataTableId,
|
|
495
|
-
toSeq: meta.toSeq,
|
|
496
|
-
};
|
|
497
|
-
}
|
|
498
|
-
if (meta.fromSeqValue <= state.lastSeq) {
|
|
499
|
-
return this.clearBatchesAndBuildRecovery(state, meta, 'OVERLAP');
|
|
500
|
-
}
|
|
501
|
-
if (meta.fromSeqValue !== state.lastSeq + 1n) {
|
|
502
|
-
return this.clearBatchesAndBuildRecovery(state, meta, 'GAP');
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
state.lastSeq = meta.toSeqValue;
|
|
506
|
-
for (const [batchId, buffered] of state.batches) {
|
|
507
|
-
if (buffered.meta.toSeqValue <= state.lastSeq) {
|
|
508
|
-
state.batches.delete(batchId);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
return {
|
|
513
|
-
type: 'APPLY',
|
|
514
|
-
updates: getOrderedDataTableRealtimeBatchUpdates(batch),
|
|
515
|
-
organizationId: meta.organizationId,
|
|
516
|
-
dataTableId: meta.dataTableId,
|
|
517
|
-
toSeq: meta.toSeq,
|
|
518
|
-
};
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
/*
|
|
522
|
-
* Return the earliest absolute recovery deadline across incomplete batches.
|
|
523
|
-
* Callers can arm one timeout and ask again whenever gate state changes.
|
|
524
|
-
*/
|
|
525
|
-
getEarliestIncompleteBatchDeadline(
|
|
526
|
-
timeoutMs = FILE_CONSTANTS.incompleteBatchTimeoutMs,
|
|
527
|
-
) {
|
|
528
|
-
const normalizedTimeoutMs = Number.isFinite(timeoutMs)
|
|
529
|
-
? Math.max(0, timeoutMs)
|
|
530
|
-
: FILE_CONSTANTS.incompleteBatchTimeoutMs;
|
|
531
|
-
let earliestDeadline: number | null = null;
|
|
532
|
-
this.tableStates.forEach((state) => {
|
|
533
|
-
state.batches.forEach((batch) => {
|
|
534
|
-
const deadline = batch.startedAt + normalizedTimeoutMs;
|
|
535
|
-
if (earliestDeadline === null || deadline < earliestDeadline) {
|
|
536
|
-
earliestDeadline = deadline;
|
|
537
|
-
}
|
|
538
|
-
});
|
|
539
|
-
});
|
|
540
|
-
|
|
541
|
-
return earliestDeadline;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
/*
|
|
545
|
-
* Consume incomplete batches whose final chunk never arrived. The caller
|
|
546
|
-
* invokes this from the earliest scheduled deadline and runs the same
|
|
547
|
-
* authoritative recovery path used for gaps and malformed envelopes.
|
|
548
|
-
*/
|
|
549
|
-
takeExpiredRecoveries(
|
|
550
|
-
now = Date.now(),
|
|
551
|
-
timeoutMs = FILE_CONSTANTS.incompleteBatchTimeoutMs,
|
|
552
|
-
): DataTableRealtimeGateResult[] {
|
|
553
|
-
const recoveries: DataTableRealtimeGateResult[] = [];
|
|
554
|
-
this.tableStates.forEach((state, key) => {
|
|
555
|
-
const expiredBatches = [...state.batches.values()].filter((batch) => {
|
|
556
|
-
return batch.startedAt <= now - Math.max(0, timeoutMs);
|
|
557
|
-
});
|
|
558
|
-
if (!expiredBatches.length) {
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
const latestBatch = [...state.batches.values()].reduce(
|
|
563
|
-
(latest, batch) => {
|
|
564
|
-
return batch.meta.toSeqValue > latest.meta.toSeqValue
|
|
565
|
-
? batch
|
|
566
|
-
: latest;
|
|
567
|
-
},
|
|
568
|
-
expiredBatches[0],
|
|
569
|
-
);
|
|
570
|
-
state.batches.clear();
|
|
571
|
-
recoveries.push(
|
|
572
|
-
this.buildRecoveryResult(state, latestBatch.meta, 'CHUNK_TIMEOUT'),
|
|
573
|
-
);
|
|
574
|
-
if (!this.registrations.has(key)) {
|
|
575
|
-
this.tableStates.delete(key);
|
|
576
|
-
}
|
|
577
|
-
});
|
|
578
|
-
|
|
579
|
-
return recoveries;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
/*
|
|
583
|
-
* Discard connection-owned partial chunks without losing fully applied
|
|
584
|
-
* cursors, observed snapshot fences, fragment versions, or write authority.
|
|
585
|
-
*/
|
|
586
|
-
discardIncompleteBatches() {
|
|
587
|
-
this.tableStates.forEach((state, key) => {
|
|
588
|
-
state.batches.clear();
|
|
589
|
-
if (state.lastSeq === null && !this.registrations.has(key)) {
|
|
590
|
-
this.tableStates.delete(key);
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
/*
|
|
596
|
-
* Resume from the exact cursor the client sent after the server confirms that
|
|
597
|
-
* cursor is current. A live event applied before its ACK remains authoritative.
|
|
598
|
-
*/
|
|
599
|
-
resumeFromAppliedCursor(
|
|
600
|
-
organizationId: string,
|
|
601
|
-
dataTableId: string,
|
|
602
|
-
seq: string,
|
|
603
|
-
) {
|
|
604
|
-
const seqValue = parseDataTableRealtimeSeq(seq);
|
|
605
|
-
if (!organizationId || !dataTableId || seqValue === null) {
|
|
606
|
-
return false;
|
|
607
|
-
}
|
|
608
|
-
const key = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
609
|
-
const currentState = this.tableStates.get(key);
|
|
610
|
-
const observedSeq = this.observedSeqs.get(key);
|
|
611
|
-
if (observedSeq !== undefined && observedSeq > seqValue) {
|
|
612
|
-
return false;
|
|
613
|
-
}
|
|
614
|
-
this.tableStates.set(key, {
|
|
615
|
-
lastSeq:
|
|
616
|
-
currentState?.lastSeq !== null &&
|
|
617
|
-
currentState?.lastSeq !== undefined &&
|
|
618
|
-
currentState.lastSeq > seqValue
|
|
619
|
-
? currentState.lastSeq
|
|
620
|
-
: seqValue,
|
|
621
|
-
batches: new Map(),
|
|
622
|
-
});
|
|
623
|
-
this.observedSeqs.delete(key);
|
|
624
|
-
return true;
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
/*
|
|
628
|
-
* Acknowledge an awaited authoritative snapshot response. Callers must not
|
|
629
|
-
* invoke this from a tail response or fire-and-forget query reset: the
|
|
630
|
-
* applied snapshot itself must carry this exact currentSeq watermark.
|
|
631
|
-
*/
|
|
632
|
-
acknowledgeSnapshot(
|
|
633
|
-
organizationId: string,
|
|
634
|
-
dataTableId: string,
|
|
635
|
-
seq: string,
|
|
636
|
-
) {
|
|
637
|
-
const seqValue = parseDataTableRealtimeSeq(seq);
|
|
638
|
-
if (!organizationId || !dataTableId || seqValue === null) {
|
|
639
|
-
return false;
|
|
640
|
-
}
|
|
641
|
-
const key = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
642
|
-
const currentSeq = this.tableStates.get(key)?.lastSeq;
|
|
643
|
-
const observedSeq = this.observedSeqs.get(key);
|
|
644
|
-
if (
|
|
645
|
-
(currentSeq !== null &&
|
|
646
|
-
currentSeq !== undefined &&
|
|
647
|
-
seqValue < currentSeq) ||
|
|
648
|
-
(observedSeq !== undefined && seqValue < observedSeq)
|
|
649
|
-
) {
|
|
650
|
-
return false;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
this.tableStates.set(key, {
|
|
654
|
-
lastSeq: seqValue,
|
|
655
|
-
batches: new Map(),
|
|
656
|
-
});
|
|
657
|
-
this.observedSeqs.delete(key);
|
|
658
|
-
return true;
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
/*
|
|
662
|
-
* Return whether one authoritative snapshot is new enough to enter cache.
|
|
663
|
-
*/
|
|
664
|
-
canApplySnapshot(organizationId: string, dataTableId: string, seq: string) {
|
|
665
|
-
const seqValue = parseDataTableRealtimeSeq(seq);
|
|
666
|
-
if (!organizationId || !dataTableId || seqValue === null) {
|
|
667
|
-
return false;
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
const key = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
671
|
-
const currentSeq = this.tableStates.get(key)?.lastSeq;
|
|
672
|
-
const observedSeq = this.observedSeqs.get(key);
|
|
673
|
-
return (
|
|
674
|
-
(currentSeq === null ||
|
|
675
|
-
currentSeq === undefined ||
|
|
676
|
-
seqValue >= currentSeq) &&
|
|
677
|
-
(observedSeq === undefined || seqValue >= observedSeq)
|
|
678
|
-
);
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
/*
|
|
682
|
-
* Register one mounted table consumer. The final unmount drops its baseline,
|
|
683
|
-
* buffered chunks, and fragment-version fence to keep the registry bounded.
|
|
684
|
-
*/
|
|
685
|
-
registerTable(organizationId: string, dataTableId: string) {
|
|
686
|
-
if (!organizationId || !dataTableId) {
|
|
687
|
-
return () => undefined;
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
const key = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
691
|
-
this.inactiveHighWaterKeys.delete(key);
|
|
692
|
-
this.registrations.set(key, (this.registrations.get(key) || 0) + 1);
|
|
693
|
-
|
|
694
|
-
let active = true;
|
|
695
|
-
return () => {
|
|
696
|
-
if (!active) {
|
|
697
|
-
return;
|
|
698
|
-
}
|
|
699
|
-
active = false;
|
|
700
|
-
const nextCount = (this.registrations.get(key) || 1) - 1;
|
|
701
|
-
if (nextCount > 0) {
|
|
702
|
-
this.registrations.set(key, nextCount);
|
|
703
|
-
return;
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
this.registrations.delete(key);
|
|
707
|
-
this.clearTable(organizationId, dataTableId);
|
|
708
|
-
};
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
/*
|
|
712
|
-
* Return whether one table currently has a mounted sequence consumer.
|
|
713
|
-
*/
|
|
714
|
-
isRegistered(organizationId: string, dataTableId: string) {
|
|
715
|
-
return this.registrations.has(
|
|
716
|
-
getDataTableRealtimeKey(organizationId, dataTableId),
|
|
717
|
-
);
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/*
|
|
721
|
-
* Record the exact fragment versions contained in an applied snapshot. This
|
|
722
|
-
* lets a page fetched ahead of the websocket cursor reject older fragments
|
|
723
|
-
* without incorrectly advancing the table-wide cursor.
|
|
724
|
-
*/
|
|
725
|
-
recordSnapshotFragments(
|
|
726
|
-
organizationId: string,
|
|
727
|
-
dataTableId: string,
|
|
728
|
-
snapshot: DataTableRealtimeSnapshot,
|
|
729
|
-
) {
|
|
730
|
-
const seq = parseDataTableRealtimeSeq(snapshot.currentSeq);
|
|
731
|
-
if (seq === null) {
|
|
732
|
-
return false;
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
const fragmentKeys = [
|
|
736
|
-
...(snapshot.dataTable?.id
|
|
737
|
-
? [
|
|
738
|
-
makeFragmentKey(
|
|
739
|
-
FRAGMENT_NAMES.dataTable,
|
|
740
|
-
String(snapshot.dataTable.id),
|
|
741
|
-
),
|
|
742
|
-
]
|
|
743
|
-
: []),
|
|
744
|
-
...(snapshot.rows || []).flatMap((row) => [
|
|
745
|
-
...(row.id
|
|
746
|
-
? [makeFragmentKey(FRAGMENT_NAMES.dataTableRow, String(row.id))]
|
|
747
|
-
: []),
|
|
748
|
-
...(row.cells || []).flatMap((cell) =>
|
|
749
|
-
cell.id
|
|
750
|
-
? [makeFragmentKey(FRAGMENT_NAMES.dataTableCell, String(cell.id))]
|
|
751
|
-
: [],
|
|
752
|
-
),
|
|
753
|
-
]),
|
|
754
|
-
];
|
|
755
|
-
const tableKey = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
756
|
-
const tableFragmentKeys =
|
|
757
|
-
this.fragmentKeysByTable.get(tableKey) || new Set<string>();
|
|
758
|
-
fragmentKeys.forEach((fragmentKey) => {
|
|
759
|
-
const currentSeq = this.fragmentSeqs.get(fragmentKey);
|
|
760
|
-
if (currentSeq === undefined || seq >= currentSeq) {
|
|
761
|
-
this.fragmentSeqs.set(fragmentKey, seq);
|
|
762
|
-
}
|
|
763
|
-
tableFragmentKeys.add(fragmentKey);
|
|
764
|
-
});
|
|
765
|
-
this.fragmentKeysByTable.set(tableKey, tableFragmentKeys);
|
|
766
|
-
return true;
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
/*
|
|
770
|
-
* Remove fragments already newer than one websocket batch and record every
|
|
771
|
-
* fragment that this batch will apply at its committed sequence.
|
|
772
|
-
*/
|
|
773
|
-
filterSequencedUpdate(
|
|
774
|
-
update: WSDataUpdateObj,
|
|
775
|
-
toSeq: string,
|
|
776
|
-
organizationId: string,
|
|
777
|
-
dataTableId: string,
|
|
778
|
-
) {
|
|
779
|
-
const seq = parseDataTableRealtimeSeq(toSeq);
|
|
780
|
-
if (seq === null) {
|
|
781
|
-
return update;
|
|
782
|
-
}
|
|
783
|
-
if (update.dataTableWriteOrders !== undefined) {
|
|
784
|
-
const writeOrders = normalizeDataTableRealtimeWriteOrders(
|
|
785
|
-
update.dataTableWriteOrders,
|
|
786
|
-
);
|
|
787
|
-
if (!writeOrders) {
|
|
788
|
-
throw new Error('Malformed DataTable write-order authority.');
|
|
789
|
-
}
|
|
790
|
-
dataTableRealtimeAuthorityRegistry.record(
|
|
791
|
-
organizationId,
|
|
792
|
-
dataTableId,
|
|
793
|
-
writeOrders,
|
|
794
|
-
);
|
|
795
|
-
}
|
|
796
|
-
if (!Array.isArray(update.fragments)) {
|
|
797
|
-
return update;
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
const tableKey = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
801
|
-
const tableFragmentKeys =
|
|
802
|
-
this.fragmentKeysByTable.get(tableKey) || new Set<string>();
|
|
803
|
-
const fragments = update.fragments.filter((fragment) => {
|
|
804
|
-
if (!fragment?.name || !fragment.dataId) {
|
|
805
|
-
return true;
|
|
806
|
-
}
|
|
807
|
-
const fragmentKey = `${fragment.name}:${fragment.dataId}`;
|
|
808
|
-
const fragmentSeq = this.fragmentSeqs.get(fragmentKey);
|
|
809
|
-
if (fragmentSeq !== undefined && fragmentSeq >= seq) {
|
|
810
|
-
return false;
|
|
811
|
-
}
|
|
812
|
-
this.fragmentSeqs.set(fragmentKey, seq);
|
|
813
|
-
tableFragmentKeys.add(fragmentKey);
|
|
814
|
-
return true;
|
|
815
|
-
});
|
|
816
|
-
this.fragmentKeysByTable.set(tableKey, tableFragmentKeys);
|
|
817
|
-
const retainedFragmentKeys = new Set(
|
|
818
|
-
fragments
|
|
819
|
-
.filter((fragment) => fragment?.name && fragment.dataId)
|
|
820
|
-
.map((fragment) => `${fragment!.name}:${fragment!.dataId}`),
|
|
821
|
-
);
|
|
822
|
-
|
|
823
|
-
return {
|
|
824
|
-
...update,
|
|
825
|
-
fragments,
|
|
826
|
-
reactiveFragmentKeys: update.reactiveFragmentKeys?.filter((key) =>
|
|
827
|
-
retainedFragmentKeys.has(key),
|
|
828
|
-
),
|
|
829
|
-
};
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
/*
|
|
833
|
-
* Return the current committed client sequence for one table.
|
|
834
|
-
*/
|
|
835
|
-
getLastSeq(organizationId: string, dataTableId: string) {
|
|
836
|
-
return (
|
|
837
|
-
this.tableStates
|
|
838
|
-
.get(getDataTableRealtimeKey(organizationId, dataTableId))
|
|
839
|
-
?.lastSeq?.toString() || null
|
|
840
|
-
);
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
/*
|
|
844
|
-
* Forget all identity-scoped baselines and incomplete batches.
|
|
845
|
-
*/
|
|
846
|
-
clear() {
|
|
847
|
-
this.tableStates.clear();
|
|
848
|
-
this.registrations.clear();
|
|
849
|
-
this.inactiveHighWaterKeys.clear();
|
|
850
|
-
this.observedSeqs.clear();
|
|
851
|
-
this.fragmentSeqs.clear();
|
|
852
|
-
this.fragmentKeysByTable.clear();
|
|
853
|
-
dataTableRealtimeAuthorityRegistry.clear();
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
/*
|
|
857
|
-
* Forget socket-era baselines while preserving mounted-table registrations.
|
|
858
|
-
*/
|
|
859
|
-
resetBaselines() {
|
|
860
|
-
this.tableStates.clear();
|
|
861
|
-
this.inactiveHighWaterKeys.clear();
|
|
862
|
-
this.observedSeqs.clear();
|
|
863
|
-
this.fragmentSeqs.clear();
|
|
864
|
-
this.fragmentKeysByTable.clear();
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
/*
|
|
868
|
-
* Forget one table after an unexpected local fragment-application failure.
|
|
869
|
-
*/
|
|
870
|
-
clearTable(organizationId: string, dataTableId: string) {
|
|
871
|
-
const key = getDataTableRealtimeKey(organizationId, dataTableId);
|
|
872
|
-
this.tableStates.delete(key);
|
|
873
|
-
this.inactiveHighWaterKeys.delete(key);
|
|
874
|
-
this.observedSeqs.delete(key);
|
|
875
|
-
this.fragmentKeysByTable.get(key)?.forEach((fragmentKey) => {
|
|
876
|
-
this.fragmentSeqs.delete(fragmentKey);
|
|
877
|
-
});
|
|
878
|
-
this.fragmentKeysByTable.delete(key);
|
|
879
|
-
dataTableRealtimeAuthorityRegistry.clearTable(organizationId, dataTableId);
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
/*
|
|
883
|
-
* Keep only a bounded recent set of pre-registration high-water fences.
|
|
884
|
-
*/
|
|
885
|
-
private trackInactiveHighWaterKey(tableKey: string) {
|
|
886
|
-
this.inactiveHighWaterKeys.delete(tableKey);
|
|
887
|
-
this.inactiveHighWaterKeys.set(tableKey, true);
|
|
888
|
-
while (
|
|
889
|
-
this.inactiveHighWaterKeys.size >
|
|
890
|
-
FILE_CONSTANTS.maxInactiveHighWaterTables
|
|
891
|
-
) {
|
|
892
|
-
const oldestKey = this.inactiveHighWaterKeys.keys().next().value as
|
|
893
|
-
| string
|
|
894
|
-
| undefined;
|
|
895
|
-
if (!oldestKey) {
|
|
896
|
-
break;
|
|
897
|
-
}
|
|
898
|
-
this.inactiveHighWaterKeys.delete(oldestKey);
|
|
899
|
-
this.observedSeqs.delete(oldestKey);
|
|
900
|
-
this.tableStates.delete(oldestKey);
|
|
901
|
-
this.fragmentKeysByTable.get(oldestKey)?.forEach((fragmentKey) => {
|
|
902
|
-
this.fragmentSeqs.delete(fragmentKey);
|
|
903
|
-
});
|
|
904
|
-
this.fragmentKeysByTable.delete(oldestKey);
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
/*
|
|
909
|
-
* Clear an unsafe buffered set while preserving its furthest observed
|
|
910
|
-
* journal watermark for the authoritative recovery request.
|
|
911
|
-
*/
|
|
912
|
-
private clearBatchesAndBuildRecovery(
|
|
913
|
-
state: DataTableRealtimeTableState,
|
|
914
|
-
fallbackMeta: ParsedDataTableChangeWSMeta,
|
|
915
|
-
reason: DataTableRealtimeRecoveryReason,
|
|
916
|
-
) {
|
|
917
|
-
const latestMeta = [...state.batches.values()].reduce(
|
|
918
|
-
(latest, batch) =>
|
|
919
|
-
batch.meta.toSeqValue > latest.toSeqValue ? batch.meta : latest,
|
|
920
|
-
fallbackMeta,
|
|
921
|
-
);
|
|
922
|
-
state.batches.clear();
|
|
923
|
-
return this.buildRecoveryResult(state, latestMeta, reason);
|
|
924
|
-
}
|
|
925
|
-
|
|
926
|
-
/*
|
|
927
|
-
* Build one recovery instruction without exposing buffered partial chunks.
|
|
928
|
-
*/
|
|
929
|
-
private buildRecoveryResult(
|
|
930
|
-
state: DataTableRealtimeTableState,
|
|
931
|
-
meta: ParsedDataTableChangeWSMeta,
|
|
932
|
-
reason: DataTableRealtimeRecoveryReason,
|
|
933
|
-
): DataTableRealtimeGateResult {
|
|
934
|
-
const observedSeq = this.observedSeqs.get(
|
|
935
|
-
getDataTableRealtimeKey(meta.organizationId, meta.dataTableId),
|
|
936
|
-
);
|
|
937
|
-
return {
|
|
938
|
-
type: 'RECOVER',
|
|
939
|
-
organizationId: meta.organizationId,
|
|
940
|
-
dataTableId: meta.dataTableId,
|
|
941
|
-
lastSeq: state.lastSeq?.toString() || null,
|
|
942
|
-
observedToSeq:
|
|
943
|
-
observedSeq !== undefined && observedSeq > meta.toSeqValue
|
|
944
|
-
? observedSeq.toString()
|
|
945
|
-
: meta.toSeq,
|
|
946
|
-
reason,
|
|
947
|
-
};
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
|
|
951
|
-
/*
|
|
952
|
-
* Share one identity-scoped gate between DataTable query hooks and the app's
|
|
953
|
-
* websocket dispatcher.
|
|
954
|
-
*/
|
|
955
|
-
export const dataTableRealtimeSequenceGate =
|
|
956
|
-
new DataTableRealtimeSequenceGate();
|
|
957
|
-
|
|
958
|
-
/*
|
|
959
|
-
* Convert a failed local application of an accepted durable batch into an
|
|
960
|
-
* immediate watermark-fenced snapshot recovery instruction.
|
|
961
|
-
*/
|
|
962
|
-
export function getDataTableRealtimeApplyFailureRecovery(
|
|
963
|
-
result: Extract<DataTableRealtimeGateResult, { type: 'APPLY' }>,
|
|
964
|
-
): DataTableRealtimeGateResult {
|
|
965
|
-
return {
|
|
966
|
-
dataTableId: result.dataTableId,
|
|
967
|
-
lastSeq: result.toSeq,
|
|
968
|
-
observedToSeq: result.toSeq,
|
|
969
|
-
organizationId: result.organizationId,
|
|
970
|
-
reason: 'SNAPSHOT_REQUIRED',
|
|
971
|
-
type: 'RECOVER',
|
|
972
|
-
};
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
/*
|
|
976
|
-
* Convert an applied recovery marker into the awaited snapshot instruction
|
|
977
|
-
* that must finish before later table updates can overtake it in the client
|
|
978
|
-
* queue. Write-order authority in the marker is still applied first.
|
|
979
|
-
*/
|
|
980
|
-
export function getDataTableRealtimeRequiredSnapshotRecovery(
|
|
981
|
-
result: DataTableRealtimeGateResult,
|
|
982
|
-
): DataTableRealtimeGateResult | null {
|
|
983
|
-
if (
|
|
984
|
-
result.type !== 'APPLY' ||
|
|
985
|
-
!result.updates.some((update) => update.dataTableSnapshotRequired === true)
|
|
986
|
-
) {
|
|
987
|
-
return null;
|
|
988
|
-
}
|
|
989
|
-
|
|
990
|
-
return {
|
|
991
|
-
dataTableId: result.dataTableId,
|
|
992
|
-
lastSeq: result.toSeq,
|
|
993
|
-
observedToSeq: result.toSeq,
|
|
994
|
-
organizationId: result.organizationId,
|
|
995
|
-
reason: 'SNAPSHOT_REQUIRED',
|
|
996
|
-
type: 'RECOVER',
|
|
997
|
-
};
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
/*
|
|
1001
|
-
* Return whether a value is one non-array record.
|
|
1002
|
-
*/
|
|
1003
|
-
function isDataTableRealtimeRecord(value: unknown) {
|
|
1004
|
-
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
/*
|
|
1008
|
-
* Return whether a value is one decimal native database identity.
|
|
1009
|
-
*/
|
|
1010
|
-
function isDataTableRealtimeIdentity(value: unknown): value is string {
|
|
1011
|
-
return typeof value === 'string' && /^[1-9]\d*$/.test(value);
|
|
1012
|
-
}
|
|
1013
|
-
|
|
1014
|
-
/*
|
|
1015
|
-
* Return whether one RECORDS snapshot retains its stable replay coordinate.
|
|
1016
|
-
*/
|
|
1017
|
-
function isDataTableRealtimeRecordSnapshot(
|
|
1018
|
-
value: unknown,
|
|
1019
|
-
coordinate: {
|
|
1020
|
-
columnId: string;
|
|
1021
|
-
dataTableRowId: string;
|
|
1022
|
-
cellKey: string;
|
|
1023
|
-
},
|
|
1024
|
-
) {
|
|
1025
|
-
return (
|
|
1026
|
-
value === null ||
|
|
1027
|
-
(isDataTableRealtimeRecord(value) &&
|
|
1028
|
-
isDataTableRealtimeIdentity((value as Record<string, unknown>).id) &&
|
|
1029
|
-
(value as Record<string, unknown>).dataTableRowId ===
|
|
1030
|
-
coordinate.dataTableRowId &&
|
|
1031
|
-
(value as Record<string, unknown>).cellKey === coordinate.cellKey &&
|
|
1032
|
-
isDataTableRealtimeIdentity(
|
|
1033
|
-
(value as Record<string, unknown>).columnId,
|
|
1034
|
-
) &&
|
|
1035
|
-
(value as Record<string, unknown>).columnId === coordinate.columnId)
|
|
1036
|
-
);
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
/*
|
|
1040
|
-
* Return whether one RECORDS delta has valid old and new snapshots.
|
|
1041
|
-
*/
|
|
1042
|
-
function isDataTableRealtimeRecordChange(value: unknown) {
|
|
1043
|
-
if (!isDataTableRealtimeRecord(value)) {
|
|
1044
|
-
return false;
|
|
1045
|
-
}
|
|
1046
|
-
const change = value as Record<string, unknown>;
|
|
1047
|
-
if (
|
|
1048
|
-
!isDataTableRealtimeIdentity(change.dataTableRowId) ||
|
|
1049
|
-
typeof change.cellKey !== 'string' ||
|
|
1050
|
-
!isDataTableRealtimeIdentity(change.columnId)
|
|
1051
|
-
) {
|
|
1052
|
-
return false;
|
|
1053
|
-
}
|
|
1054
|
-
const coordinate = {
|
|
1055
|
-
columnId: change.columnId,
|
|
1056
|
-
dataTableRowId: change.dataTableRowId,
|
|
1057
|
-
cellKey: change.cellKey,
|
|
1058
|
-
};
|
|
1059
|
-
return (
|
|
1060
|
-
isDataTableRealtimeRecordSnapshot(change.previous, coordinate) &&
|
|
1061
|
-
isDataTableRealtimeRecordSnapshot(change.current, coordinate)
|
|
1062
|
-
);
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
/*
|
|
1066
|
-
* Return whether one tail payload has the minimum versioned replay shape.
|
|
1067
|
-
*/
|
|
1068
|
-
function isDataTableRealtimeTailPayloadReplayable(
|
|
1069
|
-
kind: unknown,
|
|
1070
|
-
payload: unknown,
|
|
1071
|
-
) {
|
|
1072
|
-
if (!isDataTableRealtimeRecord(payload)) {
|
|
1073
|
-
return false;
|
|
1074
|
-
}
|
|
1075
|
-
const value = payload as Record<string, unknown>;
|
|
1076
|
-
|
|
1077
|
-
switch (kind) {
|
|
1078
|
-
case 'RECORDS':
|
|
1079
|
-
return (
|
|
1080
|
-
value.v === 2 &&
|
|
1081
|
-
typeof value.requestFingerprint === 'string' &&
|
|
1082
|
-
/^[0-9a-f]{64}$/.test(value.requestFingerprint) &&
|
|
1083
|
-
Array.isArray(value.changes) &&
|
|
1084
|
-
value.changes.every(isDataTableRealtimeRecordChange)
|
|
1085
|
-
);
|
|
1086
|
-
case 'ROWS':
|
|
1087
|
-
return (
|
|
1088
|
-
value.v === 1 &&
|
|
1089
|
-
['INSERT', 'UPDATE', 'DELETE'].includes(String(value.changeKind)) &&
|
|
1090
|
-
Array.isArray(value.dataTableRowIds) &&
|
|
1091
|
-
value.dataTableRowIds.every((id) => isDataTableRealtimeIdentity(id))
|
|
1092
|
-
);
|
|
1093
|
-
case 'STRUCTURE':
|
|
1094
|
-
return (
|
|
1095
|
-
value.v === 1 &&
|
|
1096
|
-
typeof value.changeKind === 'string' &&
|
|
1097
|
-
isDataTableRealtimeIdentity(value.dataTableId)
|
|
1098
|
-
);
|
|
1099
|
-
case 'DESIGN':
|
|
1100
|
-
return value.v === 1 && isDataTableRealtimeRecord(value.design);
|
|
1101
|
-
case 'DELETE':
|
|
1102
|
-
return (
|
|
1103
|
-
value.v === 1 &&
|
|
1104
|
-
value.changeKind === 'DELETE' &&
|
|
1105
|
-
isDataTableRealtimeIdentity(value.dataTableId)
|
|
1106
|
-
);
|
|
1107
|
-
default:
|
|
1108
|
-
return false;
|
|
1109
|
-
}
|
|
1110
|
-
}
|
|
1111
|
-
|
|
1112
|
-
/*
|
|
1113
|
-
* Return one tail classification that can only target a future authoritative
|
|
1114
|
-
* snapshot; a tail never acknowledges the client cache by itself.
|
|
1115
|
-
*/
|
|
1116
|
-
function buildDataTableRealtimeTailResolution(
|
|
1117
|
-
targetSeq: string,
|
|
1118
|
-
reason: DataTableRealtimeTailResolution['reason'],
|
|
1119
|
-
): DataTableRealtimeTailResolution {
|
|
1120
|
-
return {
|
|
1121
|
-
targetSeq,
|
|
1122
|
-
reason,
|
|
1123
|
-
requiresAuthoritativeSnapshot: true,
|
|
1124
|
-
};
|
|
1125
|
-
}
|
|
1126
|
-
|
|
1127
|
-
/*
|
|
1128
|
-
* Validate a coherent tail and identify the watermark a future authoritative
|
|
1129
|
-
* snapshot must meet. Journal payloads are storage transitions, not resolved
|
|
1130
|
-
* fragments, so every result still requires an awaited snapshot response.
|
|
1131
|
-
*/
|
|
1132
|
-
export function resolveDataTableRealtimeTail(
|
|
1133
|
-
tail: unknown,
|
|
1134
|
-
params: {
|
|
1135
|
-
afterSeq: string;
|
|
1136
|
-
dataTableId: string;
|
|
1137
|
-
observedToSeq?: string | null;
|
|
1138
|
-
},
|
|
1139
|
-
): DataTableRealtimeTailResolution {
|
|
1140
|
-
const parsedAfterSeq = parseDataTableRealtimeSeq(params.afterSeq);
|
|
1141
|
-
const parsedObservedToSeq =
|
|
1142
|
-
params.observedToSeq === null || params.observedToSeq === undefined
|
|
1143
|
-
? parsedAfterSeq
|
|
1144
|
-
: parseDataTableRealtimeSeq(params.observedToSeq);
|
|
1145
|
-
if (parsedAfterSeq === null || parsedObservedToSeq === null) {
|
|
1146
|
-
return buildDataTableRealtimeTailResolution(
|
|
1147
|
-
parsedAfterSeq?.toString() || '0',
|
|
1148
|
-
'MALFORMED',
|
|
1149
|
-
);
|
|
1150
|
-
}
|
|
1151
|
-
const afterSeq = parsedAfterSeq;
|
|
1152
|
-
const observedToSeq = parsedObservedToSeq;
|
|
1153
|
-
const fallbackTargetSeq = (
|
|
1154
|
-
observedToSeq > afterSeq ? observedToSeq : afterSeq
|
|
1155
|
-
).toString();
|
|
1156
|
-
if (!isDataTableRealtimeRecord(tail)) {
|
|
1157
|
-
return buildDataTableRealtimeTailResolution(fallbackTargetSeq, 'MALFORMED');
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
const value = tail as Record<string, unknown>;
|
|
1161
|
-
const currentSeq = parseDataTableRealtimeSeq(value.currentSeq);
|
|
1162
|
-
if (
|
|
1163
|
-
value.dataTableId !== params.dataTableId ||
|
|
1164
|
-
typeof value.requiresSnapshot !== 'boolean' ||
|
|
1165
|
-
!Array.isArray(value.changes)
|
|
1166
|
-
) {
|
|
1167
|
-
return buildDataTableRealtimeTailResolution(fallbackTargetSeq, 'MALFORMED');
|
|
1168
|
-
}
|
|
1169
|
-
|
|
1170
|
-
if (value.requiresSnapshot) {
|
|
1171
|
-
return buildDataTableRealtimeTailResolution(
|
|
1172
|
-
currentSeq !== null && currentSeq > observedToSeq
|
|
1173
|
-
? currentSeq.toString()
|
|
1174
|
-
: fallbackTargetSeq,
|
|
1175
|
-
'SNAPSHOT_REQUIRED',
|
|
1176
|
-
);
|
|
1177
|
-
}
|
|
1178
|
-
if (
|
|
1179
|
-
currentSeq === null ||
|
|
1180
|
-
currentSeq < afterSeq ||
|
|
1181
|
-
currentSeq < observedToSeq
|
|
1182
|
-
) {
|
|
1183
|
-
return buildDataTableRealtimeTailResolution(fallbackTargetSeq, 'MALFORMED');
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
let expectedSeq = afterSeq + 1n;
|
|
1187
|
-
for (const change of value.changes) {
|
|
1188
|
-
if (!isDataTableRealtimeRecord(change)) {
|
|
1189
|
-
return buildDataTableRealtimeTailResolution(
|
|
1190
|
-
fallbackTargetSeq,
|
|
1191
|
-
'MALFORMED',
|
|
1192
|
-
);
|
|
1193
|
-
}
|
|
1194
|
-
const entry = change as Record<string, unknown>;
|
|
1195
|
-
const seq = parseDataTableRealtimeSeq(entry.seq);
|
|
1196
|
-
if (
|
|
1197
|
-
entry.dataTableId !== params.dataTableId ||
|
|
1198
|
-
seq === null ||
|
|
1199
|
-
seq !== expectedSeq ||
|
|
1200
|
-
!isDataTableRealtimeTailPayloadReplayable(entry.kind, entry.payload)
|
|
1201
|
-
) {
|
|
1202
|
-
return buildDataTableRealtimeTailResolution(
|
|
1203
|
-
fallbackTargetSeq,
|
|
1204
|
-
'MALFORMED',
|
|
1205
|
-
);
|
|
1206
|
-
}
|
|
1207
|
-
expectedSeq += 1n;
|
|
1208
|
-
}
|
|
1209
|
-
|
|
1210
|
-
if (expectedSeq - 1n !== currentSeq) {
|
|
1211
|
-
return buildDataTableRealtimeTailResolution(fallbackTargetSeq, 'MALFORMED');
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
return buildDataTableRealtimeTailResolution(
|
|
1215
|
-
currentSeq.toString(),
|
|
1216
|
-
'REPLAYABLE',
|
|
1217
|
-
);
|
|
1218
|
-
}
|