@jsb188/graphql 1.1.11 → 1.1.12
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/audience.d.ts +343 -0
- package/cache/config.ts +58 -24
- package/cache/index.chat-activity.test.ts +119 -0
- package/cache/index.chat.test.ts +92 -0
- package/cache/index.helpers.ts +96 -0
- package/cache/index.regression.test.ts +149 -0
- package/cache/index.test.ts +370 -42
- package/cache/index.ts +326 -799
- package/cache/optimistic-ui-runtime.ts +46 -0
- package/cache/optimistic-ui.ts +401 -0
- package/cache/persist.ts +98 -19
- 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} +5 -2
- package/client/index.ts +3 -2
- package/client/request.helpers.ts +14 -0
- package/client/request.ts +130 -215
- package/deno.lock +17 -0
- package/deno.test.json +2 -0
- package/gql/fragments/agentMemoryFragments.ts +5 -1
- package/gql/fragments/agentNoteFragments.ts +150 -0
- package/gql/fragments/aiChatFragments.ts +36 -10
- package/gql/fragments/audienceFragments.ts +141 -0
- package/gql/fragments/campaignFragments.ts +415 -126
- package/gql/fragments/dataTableFragments.ts +48 -80
- package/gql/fragments/emailFragments.ts +28 -23
- package/gql/fragments/fieldDocFragments.ts +222 -40
- package/gql/fragments/inboundContactFragments.ts +2 -0
- package/gql/fragments/logFragments.ts +93 -2
- package/gql/fragments/organizationFragments.ts +10 -0
- package/gql/fragments/qrCodeFragments.ts +29 -0
- package/gql/fragments/scheduleFragments.ts +652 -140
- package/gql/fragments/sheetFragments.ts +84 -373
- package/gql/fragments/storageFragments.ts +25 -1
- package/gql/fragments/workflowFragments.ts +13 -1
- package/gql/mutations/agentMemoryMutations.ts +32 -0
- package/gql/mutations/agentNoteMutations.ts +62 -0
- package/gql/mutations/aiChatMutations.ts +36 -4
- package/gql/mutations/audienceMutations.ts +71 -0
- package/gql/mutations/authMutations.ts +45 -1
- package/gql/mutations/billingMutations.ts +24 -0
- package/gql/mutations/campaignMutations.ts +170 -127
- package/gql/mutations/contactMutations.ts +23 -0
- package/gql/mutations/dataTableMutations.ts +18 -75
- package/gql/mutations/fieldDocMutations.ts +162 -13
- package/gql/mutations/index.ts +8 -5
- package/gql/mutations/organizationMutations.ts +6 -0
- package/gql/mutations/qrCodeMutations.ts +20 -0
- package/gql/mutations/scheduleMutations.ts +1052 -354
- package/gql/mutations/sheetMutations.ts +62 -118
- package/gql/mutations/storageMutations.ts +0 -1
- package/gql/mutations/workflowMutations.ts +18 -0
- package/gql/queries/agentMemoryQueries.ts +6 -0
- package/gql/queries/agentNoteQueries.ts +89 -0
- package/gql/queries/aiChatQueries.ts +56 -4
- package/gql/queries/audienceQueries.ts +128 -0
- package/gql/queries/authQueries.ts +6 -0
- package/gql/queries/billingQueries.ts +95 -6
- package/gql/queries/campaignQueries.ts +132 -23
- package/gql/queries/dataTableQueries.ts +24 -65
- package/gql/queries/emailQueries.ts +18 -61
- package/gql/queries/emailReplyReportQueries.ts +76 -0
- package/gql/queries/fieldDocQueries.ts +100 -92
- package/gql/queries/inboundContactQueries.ts +4 -0
- package/gql/queries/index.ts +3 -1
- package/gql/queries/logQueries.ts +62 -7
- package/gql/queries/organizationQueries.ts +13 -0
- package/gql/queries/qrCodeQueries.ts +20 -0
- package/gql/queries/scheduleQueries.ts +633 -257
- package/gql/queries/sheetQueries.ts +59 -152
- package/gql/queries/workflowQueries.ts +10 -4
- package/hooks/index.helpers.test.ts +50 -0
- package/hooks/index.helpers.ts +52 -0
- package/hooks/index.ts +863 -685
- package/hooks/use-agentMemory-mtn.ts +29 -31
- package/hooks/use-agentMemory-qry.ts +15 -3
- package/hooks/use-agentNote-mtn.ts +82 -0
- package/hooks/use-agentNote-qry.ts +190 -0
- package/hooks/use-agentNote-recovery.helpers.test.ts +68 -0
- package/hooks/use-agentNote-recovery.helpers.ts +58 -0
- package/hooks/use-agentNote-recovery.ts +72 -0
- package/hooks/use-aiChat-mtn.ts +96 -112
- package/hooks/use-aiChat-qry.helpers.test.ts +63 -0
- package/hooks/use-aiChat-qry.helpers.ts +124 -0
- package/hooks/use-aiChat-qry.ts +229 -383
- package/hooks/use-aiChat-stream-mtn.ts +14 -0
- package/hooks/use-audience-mtn.ts +194 -0
- package/hooks/use-audience-qry.helpers.test.ts +80 -0
- package/hooks/use-audience-qry.helpers.ts +63 -0
- package/hooks/use-audience-qry.ts +330 -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-campaign-generation-recovery.ts +53 -0
- package/hooks/use-campaign-mtn.ts +224 -165
- package/hooks/use-campaign-qry.helpers.ts +40 -0
- package/hooks/use-campaign-qry.ts +631 -54
- package/hooks/use-dataTable-mtn.ts +23 -115
- package/hooks/use-dataTable-qry.ts +66 -916
- package/hooks/use-email-qry.helpers.ts +37 -0
- package/hooks/use-email-qry.ts +33 -178
- package/hooks/use-emailCampaignReplyReport-qry.ts +27 -0
- package/hooks/use-fieldDoc-mtn.ts +244 -32
- package/hooks/use-fieldDoc-qry.helpers.test.ts +65 -0
- 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-log-mtn.ts +25 -12
- package/hooks/use-log-qry.ts +99 -46
- package/hooks/use-organization-qry.ts +16 -60
- package/hooks/use-publicFormEmail-mtn.ts +27 -0
- package/hooks/use-qrCode-mtn.ts +40 -0
- package/hooks/use-qrCode-qry.ts +78 -0
- package/hooks/use-schedule-mtn.ts +506 -333
- package/hooks/use-schedule-qry.ts +682 -101
- package/hooks/use-schedule-reactive.ts +486 -0
- package/hooks/use-sheet-mtn.ts +91 -199
- package/hooks/use-sheet-qry.ts +84 -238
- package/hooks/use-storage-qry.ts +84 -61
- package/hooks/use-workflow-mtn.ts +39 -22
- package/hooks/use-workflow-qry.ts +40 -61
- 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/fragment-realtime-sequence.ts +55 -0
- package/libs/fragment-recovery.test.ts +326 -0
- package/libs/fragment-recovery.ts +325 -0
- package/libs/open-sheet.ts +138 -0
- package/libs/orchestrator-realtime.helpers.test.ts +229 -0
- package/libs/orchestrator-realtime.helpers.ts +317 -0
- package/libs/orchestrator-realtime.ts +433 -0
- package/libs/request-cache-regression.test.ts +122 -0
- package/{client → libs}/request-managed-session.test.ts +1 -1
- package/libs/request-no-store.test.ts +419 -0
- package/libs/sheet-command-batch.ts +44 -0
- package/libs/sheet-view-realtime-router.test.ts +214 -0
- package/libs/sheet-view-realtime-router.ts +99 -0
- package/loaders/campaign.test.ts +186 -0
- package/loaders/campaign.ts +56 -83
- package/loaders/emailPreference.ts +75 -0
- package/loaders/rest.ts +12 -0
- package/package.json +42 -17
- package/schedule.d.ts +1790 -0
- package/sse.helpers.ts +19 -0
- package/sse.ts +26 -115
- package/tests/agent-note-detail-cache.test.ts +120 -0
- package/tests/agent-note-search.test.ts +444 -0
- package/types.d.ts +46 -25
- 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-no-store.test.ts +0 -157
- package/gql/fragments/agentDocFragments.ts +0 -46
- package/gql/fragments/billingFragments.ts +0 -14
- package/gql/mutations/agentDocMutations.ts +0 -177
- package/gql/queries/agentDocQueries.ts +0 -120
- package/hooks/use-agentDoc-mtn.ts +0 -285
- package/hooks/use-agentDoc-qry.ts +0 -208
- /package/{client → libs}/sse-ticket.test.ts +0 -0
|
@@ -1,955 +0,0 @@
|
|
|
1
|
-
import WS_ACTIONS from '@jsb188/app/constants/wsActions.ts';
|
|
2
|
-
import { getUnverifiedJWTExpirationMs } from '@jsb188/app/utils/api.ts';
|
|
3
|
-
import { DATA_TABLE_REALTIME_MAX_PAYLOAD_BYTES } from '@jsb188/mday/constants/dataTable.ts';
|
|
4
|
-
import { dataTableRealtimeSequenceGate } from './data-table-realtime.ts';
|
|
5
|
-
|
|
6
|
-
export type DataTableRealtimeScope = {
|
|
7
|
-
dataTableId: string;
|
|
8
|
-
organizationId: string;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export type DataTableRealtimeSubscription = DataTableRealtimeScope & {
|
|
12
|
-
realtimeToken: string;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type DataTableRealtimeWatchTarget = DataTableRealtimeSubscription & {
|
|
16
|
-
lastSeq: string;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export type DataTableRealtimeWatchSyncStatus =
|
|
20
|
-
| 'SNAPSHOT_REQUIRED'
|
|
21
|
-
| 'UP_TO_DATE';
|
|
22
|
-
|
|
23
|
-
export type DataTableRealtimeWatchAcknowledgementTable =
|
|
24
|
-
DataTableRealtimeScope & {
|
|
25
|
-
currentSeq: string;
|
|
26
|
-
syncStatus: DataTableRealtimeWatchSyncStatus;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export type DataTableRealtimeWatchErrorCode =
|
|
30
|
-
| 'AUTHORIZATION_FAILED'
|
|
31
|
-
| 'INTERNAL_ERROR'
|
|
32
|
-
| 'INVALID_TARGETS'
|
|
33
|
-
| 'PAYLOAD_TOO_LARGE'
|
|
34
|
-
| 'TOO_MANY_TARGETS';
|
|
35
|
-
|
|
36
|
-
export type DataTableRealtimeWatchAcknowledgement =
|
|
37
|
-
| {
|
|
38
|
-
errorCode: null;
|
|
39
|
-
status: 'OK';
|
|
40
|
-
tables: DataTableRealtimeWatchAcknowledgementTable[];
|
|
41
|
-
watchId: string;
|
|
42
|
-
}
|
|
43
|
-
| {
|
|
44
|
-
errorCode: DataTableRealtimeWatchErrorCode;
|
|
45
|
-
status: 'ERROR';
|
|
46
|
-
tables: [];
|
|
47
|
-
watchId: string;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export type DataTableRealtimeCursorProvider = (
|
|
51
|
-
scope: DataTableRealtimeScope,
|
|
52
|
-
) => string | null;
|
|
53
|
-
|
|
54
|
-
export type DataTableRealtimeRoomReadinessListener = () => void;
|
|
55
|
-
|
|
56
|
-
export type DataTableRealtimeSubscriptionTransport = (
|
|
57
|
-
eventName: string,
|
|
58
|
-
payload: {
|
|
59
|
-
tables: Array<DataTableRealtimeScope | DataTableRealtimeWatchTarget>;
|
|
60
|
-
},
|
|
61
|
-
) => void;
|
|
62
|
-
|
|
63
|
-
const FILE_CONSTANTS = {
|
|
64
|
-
capabilityRenewalLeadMs: 45_000,
|
|
65
|
-
capabilityRenewalRetryMs: 15_000,
|
|
66
|
-
flushDelayMs: 16,
|
|
67
|
-
maxSubscriptions: 1000,
|
|
68
|
-
maxSeq: 9223372036854775807n,
|
|
69
|
-
maxWatchIdLength: 200,
|
|
70
|
-
textEncoder: new TextEncoder(),
|
|
71
|
-
watchAcknowledgementKeys: new Set([
|
|
72
|
-
'errorCode',
|
|
73
|
-
'status',
|
|
74
|
-
'tables',
|
|
75
|
-
'watchId',
|
|
76
|
-
]),
|
|
77
|
-
watchAcknowledgementTableKeys: new Set([
|
|
78
|
-
'currentSeq',
|
|
79
|
-
'dataTableId',
|
|
80
|
-
'organizationId',
|
|
81
|
-
'syncStatus',
|
|
82
|
-
]),
|
|
83
|
-
watchErrorCodes: new Set<DataTableRealtimeWatchErrorCode>([
|
|
84
|
-
'AUTHORIZATION_FAILED',
|
|
85
|
-
'INTERNAL_ERROR',
|
|
86
|
-
'INVALID_TARGETS',
|
|
87
|
-
'PAYLOAD_TOO_LARGE',
|
|
88
|
-
'TOO_MANY_TARGETS',
|
|
89
|
-
]),
|
|
90
|
-
watchSyncStatuses: new Set<DataTableRealtimeWatchSyncStatus>([
|
|
91
|
-
'SNAPSHOT_REQUIRED',
|
|
92
|
-
'UP_TO_DATE',
|
|
93
|
-
]),
|
|
94
|
-
watchTargetKeys: new Set([
|
|
95
|
-
'dataTableId',
|
|
96
|
-
'lastSeq',
|
|
97
|
-
'organizationId',
|
|
98
|
-
'realtimeToken',
|
|
99
|
-
]),
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
export const DATA_TABLE_REALTIME_SUBSCRIPTION_MAX =
|
|
103
|
-
FILE_CONSTANTS.maxSubscriptions;
|
|
104
|
-
|
|
105
|
-
/*
|
|
106
|
-
* Return the empty journal cursor used before a table has an applied baseline.
|
|
107
|
-
*/
|
|
108
|
-
function getEmptyDataTableRealtimeCursor() {
|
|
109
|
-
return '0';
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/*
|
|
113
|
-
* Return whether one exact-set watch fits the shared full-envelope byte cap.
|
|
114
|
-
*/
|
|
115
|
-
export function dataTableRealtimeWatchPayloadIsWithinLimit(
|
|
116
|
-
subscriptions: DataTableRealtimeWatchTarget[],
|
|
117
|
-
) {
|
|
118
|
-
try {
|
|
119
|
-
return (
|
|
120
|
-
2 +
|
|
121
|
-
FILE_CONSTANTS.textEncoder.encode(
|
|
122
|
-
JSON.stringify([
|
|
123
|
-
WS_ACTIONS.data_table_watch,
|
|
124
|
-
{
|
|
125
|
-
tables: subscriptions,
|
|
126
|
-
// The app transport adds this correlation after the registry
|
|
127
|
-
// returns. Reserve the server-accepted maximum here so a
|
|
128
|
-
// locally accepted set cannot fail only after transport.
|
|
129
|
-
watchId: 'w'.repeat(FILE_CONSTANTS.maxWatchIdLength),
|
|
130
|
-
},
|
|
131
|
-
]),
|
|
132
|
-
).byteLength <=
|
|
133
|
-
DATA_TABLE_REALTIME_MAX_PAYLOAD_BYTES
|
|
134
|
-
);
|
|
135
|
-
} catch {
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/*
|
|
141
|
-
* Return the stable client registry key for one organization-owned DataTable.
|
|
142
|
-
*/
|
|
143
|
-
export function getDataTableRealtimeSubscriptionKey(
|
|
144
|
-
subscription: DataTableRealtimeScope,
|
|
145
|
-
) {
|
|
146
|
-
return `${subscription.organizationId}:${subscription.dataTableId}`;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/*
|
|
150
|
-
* Normalize one precision-safe table-local journal sequence.
|
|
151
|
-
*/
|
|
152
|
-
export function normalizeDataTableRealtimeWatchSeq(value: unknown) {
|
|
153
|
-
if (typeof value !== 'string' || !/^(0|[1-9]\d*)$/.test(value)) {
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
try {
|
|
158
|
-
return BigInt(value) <= FILE_CONSTANTS.maxSeq ? value : null;
|
|
159
|
-
} catch {
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/*
|
|
165
|
-
* Normalize an exact cursor-bearing watch set without accepting malformed or
|
|
166
|
-
* duplicate targets that could make one acknowledgement ambiguous.
|
|
167
|
-
*/
|
|
168
|
-
export function normalizeDataTableRealtimeWatchTargets(value: unknown) {
|
|
169
|
-
if (!Array.isArray(value)) {
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const normalized = new Map<string, DataTableRealtimeWatchTarget>();
|
|
174
|
-
for (const item of value) {
|
|
175
|
-
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
const target = item as Record<string, unknown>;
|
|
179
|
-
const keys = Object.keys(target);
|
|
180
|
-
if (
|
|
181
|
-
keys.length !== FILE_CONSTANTS.watchTargetKeys.size ||
|
|
182
|
-
keys.some((key) => !FILE_CONSTANTS.watchTargetKeys.has(key)) ||
|
|
183
|
-
typeof target.organizationId !== 'string' ||
|
|
184
|
-
!target.organizationId ||
|
|
185
|
-
typeof target.dataTableId !== 'string' ||
|
|
186
|
-
!target.dataTableId ||
|
|
187
|
-
typeof target.realtimeToken !== 'string' ||
|
|
188
|
-
!target.realtimeToken
|
|
189
|
-
) {
|
|
190
|
-
return null;
|
|
191
|
-
}
|
|
192
|
-
const lastSeq = normalizeDataTableRealtimeWatchSeq(target.lastSeq);
|
|
193
|
-
if (lastSeq === null) {
|
|
194
|
-
return null;
|
|
195
|
-
}
|
|
196
|
-
const normalizedTarget = {
|
|
197
|
-
dataTableId: target.dataTableId,
|
|
198
|
-
lastSeq,
|
|
199
|
-
organizationId: target.organizationId,
|
|
200
|
-
realtimeToken: target.realtimeToken,
|
|
201
|
-
};
|
|
202
|
-
const targetKey = getDataTableRealtimeSubscriptionKey(normalizedTarget);
|
|
203
|
-
if (normalized.has(targetKey)) {
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
|
-
normalized.set(targetKey, normalizedTarget);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return [...normalized.values()].sort((left, right) =>
|
|
210
|
-
getDataTableRealtimeSubscriptionKey(left).localeCompare(
|
|
211
|
-
getDataTableRealtimeSubscriptionKey(right),
|
|
212
|
-
),
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/*
|
|
217
|
-
* Attach the latest committed cursor to every desired subscription immediately
|
|
218
|
-
* before a watch is emitted.
|
|
219
|
-
*/
|
|
220
|
-
export function buildDataTableRealtimeWatchTargets(
|
|
221
|
-
subscriptions: DataTableRealtimeSubscription[],
|
|
222
|
-
cursorProvider: DataTableRealtimeCursorProvider,
|
|
223
|
-
) {
|
|
224
|
-
try {
|
|
225
|
-
return normalizeDataTableRealtimeWatchTargets(
|
|
226
|
-
normalizeDataTableRealtimeSubscriptions(subscriptions).map(
|
|
227
|
-
(subscription) => ({
|
|
228
|
-
...subscription,
|
|
229
|
-
lastSeq: cursorProvider(subscription) ?? '0',
|
|
230
|
-
}),
|
|
231
|
-
),
|
|
232
|
-
);
|
|
233
|
-
} catch {
|
|
234
|
-
return null;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/*
|
|
239
|
-
* Normalize exact per-table convergence results from one watch acknowledgement.
|
|
240
|
-
*/
|
|
241
|
-
export function normalizeDataTableRealtimeWatchAcknowledgementTables(
|
|
242
|
-
value: unknown,
|
|
243
|
-
) {
|
|
244
|
-
if (!Array.isArray(value)) {
|
|
245
|
-
return null;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const normalized = new Map<
|
|
249
|
-
string,
|
|
250
|
-
DataTableRealtimeWatchAcknowledgementTable
|
|
251
|
-
>();
|
|
252
|
-
for (const item of value) {
|
|
253
|
-
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
|
254
|
-
return null;
|
|
255
|
-
}
|
|
256
|
-
const table = item as Record<string, unknown>;
|
|
257
|
-
const keys = Object.keys(table);
|
|
258
|
-
if (
|
|
259
|
-
keys.length !== FILE_CONSTANTS.watchAcknowledgementTableKeys.size ||
|
|
260
|
-
keys.some(
|
|
261
|
-
(key) => !FILE_CONSTANTS.watchAcknowledgementTableKeys.has(key),
|
|
262
|
-
) ||
|
|
263
|
-
typeof table.organizationId !== 'string' ||
|
|
264
|
-
!table.organizationId ||
|
|
265
|
-
typeof table.dataTableId !== 'string' ||
|
|
266
|
-
!table.dataTableId ||
|
|
267
|
-
!FILE_CONSTANTS.watchSyncStatuses.has(
|
|
268
|
-
table.syncStatus as DataTableRealtimeWatchSyncStatus,
|
|
269
|
-
)
|
|
270
|
-
) {
|
|
271
|
-
return null;
|
|
272
|
-
}
|
|
273
|
-
const currentSeq = normalizeDataTableRealtimeWatchSeq(table.currentSeq);
|
|
274
|
-
if (currentSeq === null) {
|
|
275
|
-
return null;
|
|
276
|
-
}
|
|
277
|
-
const normalizedTable = {
|
|
278
|
-
currentSeq,
|
|
279
|
-
dataTableId: table.dataTableId,
|
|
280
|
-
organizationId: table.organizationId,
|
|
281
|
-
syncStatus: table.syncStatus as DataTableRealtimeWatchSyncStatus,
|
|
282
|
-
};
|
|
283
|
-
const tableKey = getDataTableRealtimeSubscriptionKey(normalizedTable);
|
|
284
|
-
if (normalized.has(tableKey)) {
|
|
285
|
-
return null;
|
|
286
|
-
}
|
|
287
|
-
normalized.set(tableKey, normalizedTable);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
return [...normalized.values()].sort((left, right) =>
|
|
291
|
-
getDataTableRealtimeSubscriptionKey(left).localeCompare(
|
|
292
|
-
getDataTableRealtimeSubscriptionKey(right),
|
|
293
|
-
),
|
|
294
|
-
);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/*
|
|
298
|
-
* Normalize one complete direct-cutover watch acknowledgement. Extension keys,
|
|
299
|
-
* uncorrelated ids, and mixed success/error shapes fail closed.
|
|
300
|
-
*/
|
|
301
|
-
export function normalizeDataTableRealtimeWatchAcknowledgement(value: unknown) {
|
|
302
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
303
|
-
return null;
|
|
304
|
-
}
|
|
305
|
-
const acknowledgement = value as Record<string, unknown>;
|
|
306
|
-
const keys = Object.keys(acknowledgement);
|
|
307
|
-
if (
|
|
308
|
-
keys.length !== FILE_CONSTANTS.watchAcknowledgementKeys.size ||
|
|
309
|
-
keys.some((key) => !FILE_CONSTANTS.watchAcknowledgementKeys.has(key)) ||
|
|
310
|
-
typeof acknowledgement.watchId !== 'string' ||
|
|
311
|
-
!acknowledgement.watchId ||
|
|
312
|
-
acknowledgement.watchId.length > FILE_CONSTANTS.maxWatchIdLength ||
|
|
313
|
-
acknowledgement.watchId.trim() !== acknowledgement.watchId
|
|
314
|
-
) {
|
|
315
|
-
return null;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
if (acknowledgement.status === 'OK') {
|
|
319
|
-
const tables = normalizeDataTableRealtimeWatchAcknowledgementTables(
|
|
320
|
-
acknowledgement.tables,
|
|
321
|
-
);
|
|
322
|
-
return acknowledgement.errorCode === null && tables
|
|
323
|
-
? {
|
|
324
|
-
errorCode: null,
|
|
325
|
-
status: 'OK' as const,
|
|
326
|
-
tables,
|
|
327
|
-
watchId: acknowledgement.watchId,
|
|
328
|
-
}
|
|
329
|
-
: null;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
if (
|
|
333
|
-
acknowledgement.status !== 'ERROR' ||
|
|
334
|
-
!FILE_CONSTANTS.watchErrorCodes.has(
|
|
335
|
-
acknowledgement.errorCode as DataTableRealtimeWatchErrorCode,
|
|
336
|
-
) ||
|
|
337
|
-
!Array.isArray(acknowledgement.tables) ||
|
|
338
|
-
acknowledgement.tables.length
|
|
339
|
-
) {
|
|
340
|
-
return null;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
return {
|
|
344
|
-
errorCode: acknowledgement.errorCode as DataTableRealtimeWatchErrorCode,
|
|
345
|
-
status: 'ERROR' as const,
|
|
346
|
-
tables: [] as [],
|
|
347
|
-
watchId: acknowledgement.watchId,
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/*
|
|
352
|
-
* Normalize and dedupe one subscription list without retaining empty ids.
|
|
353
|
-
*/
|
|
354
|
-
export function normalizeDataTableRealtimeSubscriptions(
|
|
355
|
-
subscriptions: DataTableRealtimeSubscription[],
|
|
356
|
-
) {
|
|
357
|
-
const normalized = new Map<string, DataTableRealtimeSubscription>();
|
|
358
|
-
for (const subscription of subscriptions) {
|
|
359
|
-
const organizationId = String(subscription?.organizationId || '');
|
|
360
|
-
const dataTableId = String(subscription?.dataTableId || '');
|
|
361
|
-
const realtimeToken = String(subscription?.realtimeToken || '');
|
|
362
|
-
if (!organizationId || !dataTableId || !realtimeToken) {
|
|
363
|
-
continue;
|
|
364
|
-
}
|
|
365
|
-
const value = { organizationId, dataTableId, realtimeToken };
|
|
366
|
-
normalized.set(getDataTableRealtimeSubscriptionKey(value), value);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
return [...normalized.values()].sort((left, right) =>
|
|
370
|
-
getDataTableRealtimeSubscriptionKey(left).localeCompare(
|
|
371
|
-
getDataTableRealtimeSubscriptionKey(right),
|
|
372
|
-
),
|
|
373
|
-
);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/*
|
|
377
|
-
* Normalize public DataTable scopes returned by a watch acknowledgement. The
|
|
378
|
-
* server never echoes signed capabilities back to the browser.
|
|
379
|
-
*/
|
|
380
|
-
export function normalizeDataTableRealtimeScopes(
|
|
381
|
-
scopes: DataTableRealtimeScope[],
|
|
382
|
-
) {
|
|
383
|
-
const normalized = new Map<string, DataTableRealtimeScope>();
|
|
384
|
-
for (const scope of scopes) {
|
|
385
|
-
const organizationId = String(scope?.organizationId || '');
|
|
386
|
-
const dataTableId = String(scope?.dataTableId || '');
|
|
387
|
-
if (!organizationId || !dataTableId) {
|
|
388
|
-
continue;
|
|
389
|
-
}
|
|
390
|
-
const value = { organizationId, dataTableId };
|
|
391
|
-
normalized.set(getDataTableRealtimeSubscriptionKey(value), value);
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
return [...normalized.values()].sort((left, right) =>
|
|
395
|
-
getDataTableRealtimeSubscriptionKey(left).localeCompare(
|
|
396
|
-
getDataTableRealtimeSubscriptionKey(right),
|
|
397
|
-
),
|
|
398
|
-
);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
/*
|
|
402
|
-
* Return the exact table subscription for a singular query, or none when the
|
|
403
|
-
* query cannot run. This closes the metadata/delete window before rows mount.
|
|
404
|
-
*/
|
|
405
|
-
export function getDataTableRealtimeQuerySubscriptions(
|
|
406
|
-
organizationId?: string | null,
|
|
407
|
-
dataTableId?: string | null,
|
|
408
|
-
realtimeToken?: string | null,
|
|
409
|
-
skip = false,
|
|
410
|
-
) {
|
|
411
|
-
return !skip && organizationId && dataTableId && realtimeToken
|
|
412
|
-
? [{ organizationId, dataTableId, realtimeToken }]
|
|
413
|
-
: [];
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
/*
|
|
417
|
-
* Merge reactive DataTable metadata with only the latest query-scoped
|
|
418
|
-
* capability. Normalized fragments must never override a rotated token.
|
|
419
|
-
*/
|
|
420
|
-
export function resolveDataTableRealtimeQueryResult<T>(
|
|
421
|
-
queryDataTable: T | null | undefined,
|
|
422
|
-
cachedDataTable: T | null | undefined,
|
|
423
|
-
) {
|
|
424
|
-
const metadata = cachedDataTable || queryDataTable;
|
|
425
|
-
if (!metadata || !cachedDataTable) {
|
|
426
|
-
return metadata;
|
|
427
|
-
}
|
|
428
|
-
const queryCapability = (queryDataTable as { realtimeToken?: unknown } | null)
|
|
429
|
-
?.realtimeToken;
|
|
430
|
-
const cachedCapability = (cachedDataTable as { realtimeToken?: unknown })
|
|
431
|
-
.realtimeToken;
|
|
432
|
-
if (queryCapability === cachedCapability) {
|
|
433
|
-
return cachedDataTable;
|
|
434
|
-
}
|
|
435
|
-
const { realtimeToken: _staleCapability, ...cachedMetadata } =
|
|
436
|
-
cachedDataTable as Record<string, unknown> & { realtimeToken?: unknown };
|
|
437
|
-
|
|
438
|
-
return {
|
|
439
|
-
...cachedMetadata,
|
|
440
|
-
...(queryCapability === undefined
|
|
441
|
-
? null
|
|
442
|
-
: { realtimeToken: queryCapability }),
|
|
443
|
-
} as T;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/*
|
|
447
|
-
* Remove query-scoped credentials before manually populating a normalized
|
|
448
|
-
* DataTable fragment during snapshot recovery.
|
|
449
|
-
*/
|
|
450
|
-
export function getDataTableRealtimeFragmentValue<T>(dataTable: T) {
|
|
451
|
-
if (!dataTable || typeof dataTable !== 'object') {
|
|
452
|
-
return dataTable;
|
|
453
|
-
}
|
|
454
|
-
const { realtimeToken: _queryCapability, ...fragmentValue } =
|
|
455
|
-
dataTable as Record<string, unknown> & { realtimeToken?: unknown };
|
|
456
|
-
|
|
457
|
-
return fragmentValue as T;
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/*
|
|
461
|
-
* Reference-count mounted DataTable consumers and batch their socket room
|
|
462
|
-
* synchronization. Every watch contains the complete desired set, allowing
|
|
463
|
-
* the server to reauthorize and replace rooms exactly on reconnect/focus.
|
|
464
|
-
*/
|
|
465
|
-
export class DataTableRealtimeSubscriptionRegistry {
|
|
466
|
-
private entries = new Map<
|
|
467
|
-
string,
|
|
468
|
-
{
|
|
469
|
-
count: number;
|
|
470
|
-
subscription: DataTableRealtimeSubscription;
|
|
471
|
-
}
|
|
472
|
-
>();
|
|
473
|
-
private joined = new Map<string, DataTableRealtimeSubscription>();
|
|
474
|
-
private membershipIntentRevision = 0;
|
|
475
|
-
private readySubscriptionKeys = new Set<string>();
|
|
476
|
-
private readinessListenersByKey = new Map<
|
|
477
|
-
string,
|
|
478
|
-
Set<DataTableRealtimeRoomReadinessListener>
|
|
479
|
-
>();
|
|
480
|
-
private transport: DataTableRealtimeSubscriptionTransport | null = null;
|
|
481
|
-
private cursorProvider: DataTableRealtimeCursorProvider;
|
|
482
|
-
private flushTimer: ReturnType<typeof setTimeout> | null = null;
|
|
483
|
-
private capabilityRenewalHandler:
|
|
484
|
-
| ((subscriptions: DataTableRealtimeSubscription[]) => void)
|
|
485
|
-
| null = null;
|
|
486
|
-
private capabilityRenewalLeadMs = FILE_CONSTANTS.capabilityRenewalLeadMs;
|
|
487
|
-
private capabilityRenewalRetryMs = FILE_CONSTANTS.capabilityRenewalRetryMs;
|
|
488
|
-
private capabilityRenewalRetryNotBefore = 0;
|
|
489
|
-
private capabilityRenewalTimer: ReturnType<typeof setTimeout> | null = null;
|
|
490
|
-
private forceSync = false;
|
|
491
|
-
|
|
492
|
-
/*
|
|
493
|
-
* Create a registry whose watch cursor is resolved at each actual flush.
|
|
494
|
-
*/
|
|
495
|
-
constructor(
|
|
496
|
-
cursorProvider: DataTableRealtimeCursorProvider = getEmptyDataTableRealtimeCursor,
|
|
497
|
-
) {
|
|
498
|
-
this.cursorProvider = cursorProvider;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
/*
|
|
502
|
-
* Register one or more mounted consumers and return an idempotent cleanup.
|
|
503
|
-
*/
|
|
504
|
-
register(subscriptions: DataTableRealtimeSubscription[]) {
|
|
505
|
-
const normalized = normalizeDataTableRealtimeSubscriptions(subscriptions);
|
|
506
|
-
let desiredSetChanged = false;
|
|
507
|
-
normalized.forEach((subscription) => {
|
|
508
|
-
const key = getDataTableRealtimeSubscriptionKey(subscription);
|
|
509
|
-
const current = this.entries.get(key);
|
|
510
|
-
if (
|
|
511
|
-
!current ||
|
|
512
|
-
current.subscription.realtimeToken !== subscription.realtimeToken
|
|
513
|
-
) {
|
|
514
|
-
desiredSetChanged = true;
|
|
515
|
-
}
|
|
516
|
-
if (
|
|
517
|
-
current &&
|
|
518
|
-
current.subscription.realtimeToken !== subscription.realtimeToken
|
|
519
|
-
) {
|
|
520
|
-
this.clearRoomReadinessKeys([key]);
|
|
521
|
-
}
|
|
522
|
-
this.entries.set(key, {
|
|
523
|
-
count: (current?.count || 0) + 1,
|
|
524
|
-
subscription,
|
|
525
|
-
});
|
|
526
|
-
});
|
|
527
|
-
if (desiredSetChanged) {
|
|
528
|
-
this.advanceMembershipIntentRevision();
|
|
529
|
-
}
|
|
530
|
-
this.scheduleFlush();
|
|
531
|
-
this.scheduleCapabilityRenewal();
|
|
532
|
-
|
|
533
|
-
let active = true;
|
|
534
|
-
return () => {
|
|
535
|
-
if (!active) {
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
active = false;
|
|
539
|
-
let desiredSetChanged = false;
|
|
540
|
-
normalized.forEach((subscription) => {
|
|
541
|
-
const key = getDataTableRealtimeSubscriptionKey(subscription);
|
|
542
|
-
const current = this.entries.get(key);
|
|
543
|
-
if (!current || current.count <= 1) {
|
|
544
|
-
desiredSetChanged = desiredSetChanged || Boolean(current);
|
|
545
|
-
this.entries.delete(key);
|
|
546
|
-
this.clearRoomReadinessKeys([key]);
|
|
547
|
-
} else {
|
|
548
|
-
current.count -= 1;
|
|
549
|
-
}
|
|
550
|
-
});
|
|
551
|
-
if (desiredSetChanged) {
|
|
552
|
-
this.advanceMembershipIntentRevision();
|
|
553
|
-
}
|
|
554
|
-
this.scheduleFlush();
|
|
555
|
-
this.scheduleCapabilityRenewal();
|
|
556
|
-
};
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
/*
|
|
560
|
-
* Attach the current connection transport, or detach on disconnect.
|
|
561
|
-
*/
|
|
562
|
-
setTransport(transport: DataTableRealtimeSubscriptionTransport | null) {
|
|
563
|
-
this.advanceMembershipIntentRevision();
|
|
564
|
-
this.clearRoomReadiness();
|
|
565
|
-
if (!transport) {
|
|
566
|
-
this.stopCapabilityRenewal();
|
|
567
|
-
if (this.flushTimer) {
|
|
568
|
-
clearTimeout(this.flushTimer);
|
|
569
|
-
this.flushTimer = null;
|
|
570
|
-
}
|
|
571
|
-
if (this.transport && this.joined.size) {
|
|
572
|
-
// An auth reset can replace the transport without disconnecting the
|
|
573
|
-
// socket. Explicitly leave the complete old room set before forgetting
|
|
574
|
-
// it, including when an unregister flush has not run yet.
|
|
575
|
-
this.transport(WS_ACTIONS.data_table_unwatch, { tables: [] });
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
this.transport = transport;
|
|
579
|
-
this.joined.clear();
|
|
580
|
-
this.forceSync = Boolean(transport && this.entries.size);
|
|
581
|
-
this.scheduleFlush();
|
|
582
|
-
this.scheduleCapabilityRenewal();
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
/*
|
|
586
|
-
* Refetch signed capabilities shortly before their embedded expiry. The JWT
|
|
587
|
-
* payload is only a scheduling hint; the websocket server remains the sole
|
|
588
|
-
* authority and locally verifies every replacement token.
|
|
589
|
-
*/
|
|
590
|
-
startCapabilityRenewal(
|
|
591
|
-
handler: (subscriptions: DataTableRealtimeSubscription[]) => void,
|
|
592
|
-
options: { leadMs?: number; retryMs?: number } = {},
|
|
593
|
-
) {
|
|
594
|
-
this.stopCapabilityRenewal();
|
|
595
|
-
this.capabilityRenewalHandler = handler;
|
|
596
|
-
this.capabilityRenewalLeadMs = Math.max(
|
|
597
|
-
0,
|
|
598
|
-
Math.floor(options.leadMs ?? FILE_CONSTANTS.capabilityRenewalLeadMs),
|
|
599
|
-
);
|
|
600
|
-
this.capabilityRenewalRetryMs = Math.max(
|
|
601
|
-
1,
|
|
602
|
-
Math.floor(options.retryMs ?? FILE_CONSTANTS.capabilityRenewalRetryMs),
|
|
603
|
-
);
|
|
604
|
-
this.capabilityRenewalRetryNotBefore = 0;
|
|
605
|
-
this.scheduleCapabilityRenewal();
|
|
606
|
-
|
|
607
|
-
return () => {
|
|
608
|
-
if (this.capabilityRenewalHandler === handler) {
|
|
609
|
-
this.stopCapabilityRenewal();
|
|
610
|
-
}
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
/*
|
|
615
|
-
* Stop proactive capability refresh and release its pending timer.
|
|
616
|
-
*/
|
|
617
|
-
stopCapabilityRenewal() {
|
|
618
|
-
if (this.capabilityRenewalTimer) {
|
|
619
|
-
clearTimeout(this.capabilityRenewalTimer);
|
|
620
|
-
}
|
|
621
|
-
this.capabilityRenewalTimer = null;
|
|
622
|
-
this.capabilityRenewalHandler = null;
|
|
623
|
-
this.capabilityRenewalRetryNotBefore = 0;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
/*
|
|
627
|
-
* Force the complete desired set through server authorization again.
|
|
628
|
-
*/
|
|
629
|
-
sync() {
|
|
630
|
-
if (!this.transport || !this.entries.size) {
|
|
631
|
-
return;
|
|
632
|
-
}
|
|
633
|
-
this.advanceMembershipIntentRevision();
|
|
634
|
-
this.clearRoomReadiness();
|
|
635
|
-
this.forceSync = true;
|
|
636
|
-
this.scheduleFlush();
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
/*
|
|
640
|
-
* Flush immediately for deterministic lifecycle handling and tests.
|
|
641
|
-
*/
|
|
642
|
-
flushNow() {
|
|
643
|
-
if (this.flushTimer) {
|
|
644
|
-
clearTimeout(this.flushTimer);
|
|
645
|
-
this.flushTimer = null;
|
|
646
|
-
}
|
|
647
|
-
this.flush();
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
/*
|
|
651
|
-
* Return the currently desired subscriptions for diagnostics and tests.
|
|
652
|
-
*/
|
|
653
|
-
getDesiredSubscriptions() {
|
|
654
|
-
return normalizeDataTableRealtimeSubscriptions(
|
|
655
|
-
[...this.entries.values()].map((entry) => entry.subscription),
|
|
656
|
-
);
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
/*
|
|
660
|
-
* Return the current desired set with a freshly resolved cursor per table.
|
|
661
|
-
*/
|
|
662
|
-
getDesiredWatchTargets() {
|
|
663
|
-
return buildDataTableRealtimeWatchTargets(
|
|
664
|
-
this.getDesiredSubscriptions(),
|
|
665
|
-
this.cursorProvider,
|
|
666
|
-
);
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
/*
|
|
670
|
-
* Return the current exact-set intent revision so async ACK work can prove it
|
|
671
|
-
* still belongs to the latest token, subscription, and sync boundary.
|
|
672
|
-
*/
|
|
673
|
-
getMembershipIntentRevision() {
|
|
674
|
-
return this.membershipIntentRevision;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
/*
|
|
678
|
-
* Return whether the latest correlated exact-set acknowledgement authorizes
|
|
679
|
-
* one mounted DataTable scope on the current physical connection.
|
|
680
|
-
*/
|
|
681
|
-
getRoomReadiness(scope: DataTableRealtimeScope) {
|
|
682
|
-
return this.readySubscriptionKeys.has(
|
|
683
|
-
getDataTableRealtimeSubscriptionKey(scope),
|
|
684
|
-
);
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
/*
|
|
688
|
-
* Subscribe one React consumer to authoritative readiness changes for an
|
|
689
|
-
* exact organization/DataTable scope.
|
|
690
|
-
*/
|
|
691
|
-
subscribeRoomReadiness(
|
|
692
|
-
scope: DataTableRealtimeScope,
|
|
693
|
-
listener: DataTableRealtimeRoomReadinessListener,
|
|
694
|
-
) {
|
|
695
|
-
const key = getDataTableRealtimeSubscriptionKey(scope);
|
|
696
|
-
const listeners = this.readinessListenersByKey.get(key) || new Set();
|
|
697
|
-
listeners.add(listener);
|
|
698
|
-
this.readinessListenersByKey.set(key, listeners);
|
|
699
|
-
|
|
700
|
-
let active = true;
|
|
701
|
-
return () => {
|
|
702
|
-
if (!active) {
|
|
703
|
-
return;
|
|
704
|
-
}
|
|
705
|
-
active = false;
|
|
706
|
-
const currentListeners = this.readinessListenersByKey.get(key);
|
|
707
|
-
currentListeners?.delete(listener);
|
|
708
|
-
if (!currentListeners?.size) {
|
|
709
|
-
this.readinessListenersByKey.delete(key);
|
|
710
|
-
}
|
|
711
|
-
};
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
/*
|
|
715
|
-
* Publish the exact authorized room set from the latest correlated successful
|
|
716
|
-
* watch acknowledgement. Scopes without a mounted registry entry fail closed.
|
|
717
|
-
*/
|
|
718
|
-
replaceRoomReadiness(scopes: DataTableRealtimeScope[]) {
|
|
719
|
-
const nextKeys = new Set(
|
|
720
|
-
normalizeDataTableRealtimeScopes(scopes)
|
|
721
|
-
.map(getDataTableRealtimeSubscriptionKey)
|
|
722
|
-
.filter((key) => this.entries.has(key)),
|
|
723
|
-
);
|
|
724
|
-
const changedKeys = new Set<string>();
|
|
725
|
-
this.readySubscriptionKeys.forEach((key) => {
|
|
726
|
-
if (!nextKeys.has(key)) {
|
|
727
|
-
changedKeys.add(key);
|
|
728
|
-
}
|
|
729
|
-
});
|
|
730
|
-
nextKeys.forEach((key) => {
|
|
731
|
-
if (!this.readySubscriptionKeys.has(key)) {
|
|
732
|
-
changedKeys.add(key);
|
|
733
|
-
}
|
|
734
|
-
});
|
|
735
|
-
this.readySubscriptionKeys = nextKeys;
|
|
736
|
-
this.notifyRoomReadinessKeys(changedKeys);
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
/*
|
|
740
|
-
* Fail every scope closed at a connection, authentication, request, or exact-
|
|
741
|
-
* set replacement boundary.
|
|
742
|
-
*/
|
|
743
|
-
clearRoomReadiness() {
|
|
744
|
-
this.clearRoomReadinessKeys(this.readySubscriptionKeys);
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
/*
|
|
748
|
-
* Schedule one animation-frame-sized subscription diff.
|
|
749
|
-
*/
|
|
750
|
-
private scheduleFlush() {
|
|
751
|
-
if (!this.transport || this.flushTimer) {
|
|
752
|
-
return;
|
|
753
|
-
}
|
|
754
|
-
this.flushTimer = setTimeout(() => {
|
|
755
|
-
this.flushTimer = null;
|
|
756
|
-
this.flush();
|
|
757
|
-
}, FILE_CONSTANTS.flushDelayMs);
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
/*
|
|
761
|
-
* Advance the monotonic local boundary that invalidates older async ACK work.
|
|
762
|
-
*/
|
|
763
|
-
private advanceMembershipIntentRevision() {
|
|
764
|
-
this.membershipIntentRevision =
|
|
765
|
-
this.membershipIntentRevision >= Number.MAX_SAFE_INTEGER
|
|
766
|
-
? 1
|
|
767
|
-
: this.membershipIntentRevision + 1;
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
/*
|
|
771
|
-
* Remove readiness for the provided keys and notify only affected consumers.
|
|
772
|
-
*/
|
|
773
|
-
private clearRoomReadinessKeys(keys: Iterable<string>) {
|
|
774
|
-
const changedKeys = new Set<string>();
|
|
775
|
-
for (const key of keys) {
|
|
776
|
-
if (this.readySubscriptionKeys.delete(key)) {
|
|
777
|
-
changedKeys.add(key);
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
this.notifyRoomReadinessKeys(changedKeys);
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
/*
|
|
784
|
-
* Notify mounted consumers whose exact readiness snapshot changed.
|
|
785
|
-
*/
|
|
786
|
-
private notifyRoomReadinessKeys(keys: Iterable<string>) {
|
|
787
|
-
for (const key of keys) {
|
|
788
|
-
this.readinessListenersByKey.get(key)?.forEach((listener) => {
|
|
789
|
-
listener();
|
|
790
|
-
});
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
|
|
794
|
-
/*
|
|
795
|
-
* Schedule the earliest desired capability renewal, retrying a still-stale
|
|
796
|
-
* token at a bounded cadence until its mounted query publishes a replacement.
|
|
797
|
-
*/
|
|
798
|
-
private scheduleCapabilityRenewal() {
|
|
799
|
-
if (this.capabilityRenewalTimer) {
|
|
800
|
-
clearTimeout(this.capabilityRenewalTimer);
|
|
801
|
-
this.capabilityRenewalTimer = null;
|
|
802
|
-
}
|
|
803
|
-
if (
|
|
804
|
-
!this.transport ||
|
|
805
|
-
!this.capabilityRenewalHandler ||
|
|
806
|
-
!this.entries.size
|
|
807
|
-
) {
|
|
808
|
-
return;
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
const desired = this.getDesiredSubscriptions();
|
|
812
|
-
const now = Date.now();
|
|
813
|
-
const earliestRenewalAt = Math.min(
|
|
814
|
-
...desired.map((subscription) => {
|
|
815
|
-
const expirationMs = getUnverifiedJWTExpirationMs(
|
|
816
|
-
subscription.realtimeToken,
|
|
817
|
-
);
|
|
818
|
-
return expirationMs === null
|
|
819
|
-
? now
|
|
820
|
-
: expirationMs - this.capabilityRenewalLeadMs;
|
|
821
|
-
}),
|
|
822
|
-
);
|
|
823
|
-
const renewalAt = Math.max(
|
|
824
|
-
now,
|
|
825
|
-
earliestRenewalAt,
|
|
826
|
-
this.capabilityRenewalRetryNotBefore,
|
|
827
|
-
);
|
|
828
|
-
this.capabilityRenewalTimer = setTimeout(
|
|
829
|
-
() => {
|
|
830
|
-
this.capabilityRenewalTimer = null;
|
|
831
|
-
const renewalNow = Date.now();
|
|
832
|
-
const expiring = this.getDesiredSubscriptions().filter(
|
|
833
|
-
(subscription) => {
|
|
834
|
-
const expirationMs = getUnverifiedJWTExpirationMs(
|
|
835
|
-
subscription.realtimeToken,
|
|
836
|
-
);
|
|
837
|
-
return (
|
|
838
|
-
expirationMs === null ||
|
|
839
|
-
expirationMs - renewalNow <= this.capabilityRenewalLeadMs
|
|
840
|
-
);
|
|
841
|
-
},
|
|
842
|
-
);
|
|
843
|
-
if (expiring.length) {
|
|
844
|
-
this.capabilityRenewalRetryNotBefore =
|
|
845
|
-
renewalNow + this.capabilityRenewalRetryMs;
|
|
846
|
-
this.capabilityRenewalHandler?.(expiring);
|
|
847
|
-
}
|
|
848
|
-
this.scheduleCapabilityRenewal();
|
|
849
|
-
},
|
|
850
|
-
Math.min(2_147_483_647, renewalAt - now),
|
|
851
|
-
);
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
/*
|
|
855
|
-
* Replace every non-empty desired set with one authorized watch. Empty intent
|
|
856
|
-
* leaves every room so a pending replacement cannot retain the old set.
|
|
857
|
-
*/
|
|
858
|
-
private flush() {
|
|
859
|
-
if (!this.transport) {
|
|
860
|
-
return;
|
|
861
|
-
}
|
|
862
|
-
const desired = this.getDesiredSubscriptions();
|
|
863
|
-
const watchTargets = buildDataTableRealtimeWatchTargets(
|
|
864
|
-
desired,
|
|
865
|
-
this.cursorProvider,
|
|
866
|
-
);
|
|
867
|
-
if (!watchTargets) {
|
|
868
|
-
console.error(
|
|
869
|
-
'DataTable realtime watch cursor provider returned an invalid sequence.',
|
|
870
|
-
);
|
|
871
|
-
if (this.joined.size) {
|
|
872
|
-
this.transport(WS_ACTIONS.data_table_unwatch, { tables: [] });
|
|
873
|
-
}
|
|
874
|
-
this.clearRoomReadiness();
|
|
875
|
-
this.joined.clear();
|
|
876
|
-
this.forceSync = false;
|
|
877
|
-
return;
|
|
878
|
-
}
|
|
879
|
-
if (desired.length > FILE_CONSTANTS.maxSubscriptions) {
|
|
880
|
-
// The exact-set protocol cannot safely truncate or split one desired set.
|
|
881
|
-
// Send the complete invalid request so the server can fail closed and
|
|
882
|
-
// return an explicit correlated TOO_MANY_TARGETS acknowledgement.
|
|
883
|
-
console.error(
|
|
884
|
-
`DataTable realtime supports at most ${FILE_CONSTANTS.maxSubscriptions} simultaneous table subscriptions; received ${desired.length}.`,
|
|
885
|
-
);
|
|
886
|
-
this.transport(WS_ACTIONS.data_table_watch, { tables: watchTargets });
|
|
887
|
-
this.clearRoomReadiness();
|
|
888
|
-
this.joined.clear();
|
|
889
|
-
this.forceSync = false;
|
|
890
|
-
return;
|
|
891
|
-
}
|
|
892
|
-
if (!dataTableRealtimeWatchPayloadIsWithinLimit(watchTargets)) {
|
|
893
|
-
console.error(
|
|
894
|
-
`DataTable realtime exact-set watch exceeds ${DATA_TABLE_REALTIME_MAX_PAYLOAD_BYTES} encoded bytes.`,
|
|
895
|
-
);
|
|
896
|
-
if (this.joined.size) {
|
|
897
|
-
this.transport(WS_ACTIONS.data_table_unwatch, { tables: [] });
|
|
898
|
-
}
|
|
899
|
-
this.clearRoomReadiness();
|
|
900
|
-
this.joined.clear();
|
|
901
|
-
this.forceSync = false;
|
|
902
|
-
return;
|
|
903
|
-
}
|
|
904
|
-
const desiredByKey = new Map(
|
|
905
|
-
desired.map((subscription) => [
|
|
906
|
-
getDataTableRealtimeSubscriptionKey(subscription),
|
|
907
|
-
subscription,
|
|
908
|
-
]),
|
|
909
|
-
);
|
|
910
|
-
const removed = [...this.joined.entries()]
|
|
911
|
-
.filter(([key]) => !desiredByKey.has(key))
|
|
912
|
-
.map(([, subscription]) => subscription);
|
|
913
|
-
const changed =
|
|
914
|
-
this.forceSync ||
|
|
915
|
-
removed.length > 0 ||
|
|
916
|
-
desired.some((subscription) => {
|
|
917
|
-
const joined = this.joined.get(
|
|
918
|
-
getDataTableRealtimeSubscriptionKey(subscription),
|
|
919
|
-
);
|
|
920
|
-
return !joined || joined.realtimeToken !== subscription.realtimeToken;
|
|
921
|
-
});
|
|
922
|
-
if (!changed) {
|
|
923
|
-
return;
|
|
924
|
-
}
|
|
925
|
-
|
|
926
|
-
if (removed.length && !desired.length) {
|
|
927
|
-
// A replacement watch may still be awaiting server authorization while
|
|
928
|
-
// joined already reflects its intended set. Empty intent must therefore
|
|
929
|
-
// leave every server room, including the pre-cutover set.
|
|
930
|
-
this.transport(WS_ACTIONS.data_table_unwatch, { tables: [] });
|
|
931
|
-
}
|
|
932
|
-
if (desired.length) {
|
|
933
|
-
// The server replaces the complete authorized set atomically. Keep the
|
|
934
|
-
// previous rooms until this direct-cutover watch has been verified.
|
|
935
|
-
this.transport(WS_ACTIONS.data_table_watch, { tables: watchTargets });
|
|
936
|
-
}
|
|
937
|
-
this.joined = desiredByKey;
|
|
938
|
-
this.forceSync = false;
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
/*
|
|
943
|
-
* Return the latest fully applied table cursor for a direct-cutover watch.
|
|
944
|
-
*/
|
|
945
|
-
function getDataTableRealtimeCommittedCursor(scope: DataTableRealtimeScope) {
|
|
946
|
-
return dataTableRealtimeSequenceGate.getLastSeq(
|
|
947
|
-
scope.organizationId,
|
|
948
|
-
scope.dataTableId,
|
|
949
|
-
);
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
export const dataTableRealtimeSubscriptions =
|
|
953
|
-
new DataTableRealtimeSubscriptionRegistry(
|
|
954
|
-
getDataTableRealtimeCommittedCursor,
|
|
955
|
-
);
|