@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
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import type { AIChatSubmissionWSObj } from '@jsb188/app/types/app.d.ts';
|
|
2
|
+
|
|
3
|
+
interface AIChatSubmissionState {
|
|
4
|
+
acceptReceived: boolean;
|
|
5
|
+
clientRequestId: string;
|
|
6
|
+
generationActive: boolean;
|
|
7
|
+
listeners: Set<(waitingForGeneration: boolean) => void>;
|
|
8
|
+
serverReceived: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const AI_CHAT_SUBMISSION_STATES = new Map<string, AIChatSubmissionState>();
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* Return whether one chat should render its transient server-received placeholder.
|
|
15
|
+
*/
|
|
16
|
+
function isWaitingForAIChatGeneration(state: AIChatSubmissionState) {
|
|
17
|
+
return (
|
|
18
|
+
state.acceptReceived && state.serverReceived && !state.generationActive
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
* Notify every mounted observer when one chat's transient submission phase changes.
|
|
24
|
+
*/
|
|
25
|
+
function notifyAIChatSubmissionState(state: AIChatSubmissionState) {
|
|
26
|
+
const waitingForGeneration = isWaitingForAIChatGeneration(state);
|
|
27
|
+
state.listeners.forEach((listener) => {
|
|
28
|
+
listener(waitingForGeneration);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* Register the request identity that the requester tab expects the server to acknowledge.
|
|
34
|
+
*/
|
|
35
|
+
export function registerAIChatSubmission(
|
|
36
|
+
aiChatId: string,
|
|
37
|
+
clientRequestId: string,
|
|
38
|
+
) {
|
|
39
|
+
const previousState = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
40
|
+
const state: AIChatSubmissionState = {
|
|
41
|
+
acceptReceived: true,
|
|
42
|
+
clientRequestId,
|
|
43
|
+
generationActive: previousState?.generationActive || false,
|
|
44
|
+
listeners: previousState?.listeners || new Set(),
|
|
45
|
+
serverReceived: false,
|
|
46
|
+
};
|
|
47
|
+
AI_CHAT_SUBMISSION_STATES.set(aiChatId, state);
|
|
48
|
+
notifyAIChatSubmissionState(state);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Apply one server acknowledgement only when it matches the current request in this tab.
|
|
53
|
+
*/
|
|
54
|
+
export function receiveAIChatSubmission(update: AIChatSubmissionWSObj) {
|
|
55
|
+
const state = AI_CHAT_SUBMISSION_STATES.get(update.aiChatId);
|
|
56
|
+
if (
|
|
57
|
+
!state ||
|
|
58
|
+
state.clientRequestId !== update.clientRequestId ||
|
|
59
|
+
update.phase !== 'RECEIVED'
|
|
60
|
+
) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (state.generationActive) {
|
|
64
|
+
state.acceptReceived = false;
|
|
65
|
+
state.serverReceived = false;
|
|
66
|
+
notifyAIChatSubmissionState(state);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
state.serverReceived = true;
|
|
71
|
+
notifyAIChatSubmissionState(state);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Supersede the transient server acknowledgement before exposing real generation state.
|
|
76
|
+
*/
|
|
77
|
+
export function startAIChatGeneration(aiChatId: string) {
|
|
78
|
+
const state = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
79
|
+
if (!state) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
state.acceptReceived = false;
|
|
84
|
+
state.generationActive = true;
|
|
85
|
+
state.serverReceived = false;
|
|
86
|
+
notifyAIChatSubmissionState(state);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/*
|
|
90
|
+
* Record that real generation ended without allowing an older acknowledgement to reappear.
|
|
91
|
+
*/
|
|
92
|
+
export function finishAIChatGeneration(aiChatId: string) {
|
|
93
|
+
const state = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
94
|
+
if (!state) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
state.generationActive = false;
|
|
99
|
+
notifyAIChatSubmissionState(state);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/*
|
|
103
|
+
* Clear one failed or abandoned submission while retaining mounted chat observers.
|
|
104
|
+
*/
|
|
105
|
+
export function clearAIChatSubmission(
|
|
106
|
+
aiChatId: string,
|
|
107
|
+
clientRequestId?: string | null,
|
|
108
|
+
) {
|
|
109
|
+
const state = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
110
|
+
if (
|
|
111
|
+
!state ||
|
|
112
|
+
(clientRequestId && state.clientRequestId !== clientRequestId)
|
|
113
|
+
) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
state.acceptReceived = false;
|
|
118
|
+
state.serverReceived = false;
|
|
119
|
+
notifyAIChatSubmissionState(state);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
* Observe the requester-tab loading phase for one mounted AI chat.
|
|
124
|
+
*/
|
|
125
|
+
export function subscribeToAIChatSubmission(
|
|
126
|
+
aiChatId: string,
|
|
127
|
+
listener: (waitingForGeneration: boolean) => void,
|
|
128
|
+
) {
|
|
129
|
+
let state = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
130
|
+
if (!state) {
|
|
131
|
+
state = {
|
|
132
|
+
acceptReceived: false,
|
|
133
|
+
clientRequestId: '',
|
|
134
|
+
generationActive: false,
|
|
135
|
+
listeners: new Set(),
|
|
136
|
+
serverReceived: false,
|
|
137
|
+
};
|
|
138
|
+
AI_CHAT_SUBMISSION_STATES.set(aiChatId, state);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
state.listeners.add(listener);
|
|
142
|
+
listener(isWaitingForAIChatGeneration(state));
|
|
143
|
+
|
|
144
|
+
return () => {
|
|
145
|
+
state.listeners.delete(listener);
|
|
146
|
+
if (!state.listeners.size) {
|
|
147
|
+
AI_CHAT_SUBMISSION_STATES.delete(aiChatId);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { DataChangeKind, WSDataChange } from '@jsb188/app/types/app.d.ts';
|
|
2
|
+
import { makeGraphQLVariablesKey } from '@jsb188/app/utils/logic.ts';
|
|
3
|
+
import { DATA_TYPES, type DataTypeKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
|
|
4
|
+
|
|
5
|
+
// What one change means for one query that depends on its type (design 4.3)
|
|
6
|
+
export type QueryRefreshDecision = 'none' | 'refresh' | 'refresh-if-missing' | 'refresh-if-held';
|
|
7
|
+
|
|
8
|
+
// One query cache or mounted-hook key, '#name:' + makeGraphQLVariablesKey(variables), taken apart
|
|
9
|
+
export interface ParsedQueryKey {
|
|
10
|
+
name: string;
|
|
11
|
+
variablesKey: string;
|
|
12
|
+
variables: Record<string, any>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/*
|
|
16
|
+
* Decide what one change means for one query that depends on its type (design 4.3).
|
|
17
|
+
*/
|
|
18
|
+
export function decideQueryRefresh(
|
|
19
|
+
relation: 'lists' | 'derived',
|
|
20
|
+
kind: DataChangeKind,
|
|
21
|
+
refresh: 'auto' | 'notice',
|
|
22
|
+
): QueryRefreshDecision {
|
|
23
|
+
if (relation === 'derived' || kind === 'touched') return 'refresh';
|
|
24
|
+
if (kind === 'added') return 'refresh-if-missing';
|
|
25
|
+
if (kind === 'moved') return refresh === 'auto' ? 'refresh' : 'refresh-if-missing';
|
|
26
|
+
if (kind === 'deleted') return refresh === 'auto' ? 'refresh-if-held' : 'none';
|
|
27
|
+
return 'none';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
* Return the scope a change is matched with: its parent ids plus its own id under the type's idKey.
|
|
32
|
+
*/
|
|
33
|
+
export function getChangeScope(change: WSDataChange): Record<string, string> {
|
|
34
|
+
const idKey = Object.hasOwn(DATA_TYPES, change.type) ? DATA_TYPES[change.type as DataTypeKey].idKey : null;
|
|
35
|
+
return idKey && change.id ? { ...change.scope, [idKey]: change.id } : { ...change.scope };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* Return the query name of a '#name:...' key, or null for a key in any other format.
|
|
40
|
+
*/
|
|
41
|
+
export function getQueryKeyName(queryKey: string): string | null {
|
|
42
|
+
const separator = queryKey.indexOf(':');
|
|
43
|
+
return queryKey.startsWith('#') && separator > 1 ? queryKey.slice(1, separator) : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/*
|
|
47
|
+
* Split '#name:@{json}' into its name and parsed variables; null for a key in any other format.
|
|
48
|
+
*/
|
|
49
|
+
export function parseQueryKey(queryKey: string): ParsedQueryKey | null {
|
|
50
|
+
const name = getQueryKeyName(queryKey);
|
|
51
|
+
const variablesKey = name ? queryKey.slice(name.length + 2) : '';
|
|
52
|
+
if (!name || !variablesKey.startsWith('@')) return null;
|
|
53
|
+
try {
|
|
54
|
+
const variables = JSON.parse(variablesKey.slice(1));
|
|
55
|
+
return variables && typeof variables === 'object' && !Array.isArray(variables) ? { name, variablesKey, variables } : null;
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/*
|
|
62
|
+
* Read a dotted variable path ('filter.workflowId'). A missing step, or a step
|
|
63
|
+
* named __proto__, constructor or prototype, reads as undefined.
|
|
64
|
+
*/
|
|
65
|
+
function readVariablePath(variables: Record<string, any>, path: string): unknown {
|
|
66
|
+
let value: unknown = variables;
|
|
67
|
+
for (const key of path.split('.')) {
|
|
68
|
+
if (key === '__proto__' || key === 'constructor' || key === 'prototype') return undefined;
|
|
69
|
+
if (!value || typeof value !== 'object' || !Object.hasOwn(value, key)) return undefined;
|
|
70
|
+
value = (value as Record<string, unknown>)[key];
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/*
|
|
76
|
+
* Return whether a query's variables are compatible with a change's scope
|
|
77
|
+
* (design 3.1). Only a definite mismatch excludes the query: an absent, null
|
|
78
|
+
* or empty variable, and one that cannot be compared (an object), do not
|
|
79
|
+
* constrain it.
|
|
80
|
+
*/
|
|
81
|
+
export function queryVariablesMatchScope(
|
|
82
|
+
variables: Record<string, any>,
|
|
83
|
+
scope: Record<string, string>,
|
|
84
|
+
aliases?: Readonly<Record<string, string | null>>,
|
|
85
|
+
): boolean {
|
|
86
|
+
for (const [key, expected] of Object.entries(scope)) {
|
|
87
|
+
const path = aliases && Object.hasOwn(aliases, key) ? aliases[key] : key;
|
|
88
|
+
if (path === null) continue;
|
|
89
|
+
const value = readVariablePath(variables, path);
|
|
90
|
+
if (value === null || value === undefined || value === '') continue;
|
|
91
|
+
if (Array.isArray(value)) {
|
|
92
|
+
if (value.length && !value.some((item) => String(item) === expected)) return false;
|
|
93
|
+
} else if (typeof value !== 'object' && String(value) !== expected) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/*
|
|
101
|
+
* Return the variables key of a query without its paging variables, so all pages of one list share it.
|
|
102
|
+
*/
|
|
103
|
+
export function getQueryPageFamily(variables: Record<string, any>): string {
|
|
104
|
+
const { cursor: _cursor, after: _after, offset: _offset, afterId: _afterId, ...rest } = variables;
|
|
105
|
+
return makeGraphQLVariablesKey(rest);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/*
|
|
109
|
+
* Return whether a value is a plain record that can be merged field by field.
|
|
110
|
+
*/
|
|
111
|
+
function isRecordObject(value: any) {
|
|
112
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/*
|
|
116
|
+
* Merge one appended value: arrays are added to the cached arrays (in front
|
|
117
|
+
* for 'start', behind for 'end'), records merge recursively, anything else replaces.
|
|
118
|
+
*/
|
|
119
|
+
function mergeAppendedValue(currentValue: any, nextValue: any, append: 'start' | 'end'): any {
|
|
120
|
+
if (Array.isArray(nextValue)) {
|
|
121
|
+
const currentArray = Array.isArray(currentValue) ? currentValue : [];
|
|
122
|
+
return append === 'start' ? [...nextValue, ...currentArray] : [...currentArray, ...nextValue];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (isRecordObject(nextValue)) {
|
|
126
|
+
const currentObject = isRecordObject(currentValue) ? currentValue : {};
|
|
127
|
+
const merged = { ...currentObject, ...nextValue };
|
|
128
|
+
for (const [key, value] of Object.entries(nextValue)) {
|
|
129
|
+
if (Array.isArray(value) || isRecordObject(value)) {
|
|
130
|
+
merged[key] = mergeAppendedValue(currentObject[key], value, append);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return merged;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return nextValue;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/*
|
|
140
|
+
* Merge change data into a record; with `append`, arrays are added to the cached arrays instead of replacing them.
|
|
141
|
+
*/
|
|
142
|
+
export function mergeChangeData(current: any, data: any, append?: 'start' | 'end'): any {
|
|
143
|
+
const safeCurrent = isRecordObject(current) ? current : {};
|
|
144
|
+
const safeData = isRecordObject(data) ? data : {};
|
|
145
|
+
return append ? mergeAppendedValue(safeCurrent, safeData, append) : { ...safeCurrent, ...safeData };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/*
|
|
149
|
+
* Return whether a stored query value references any of the given fragment
|
|
150
|
+
* keys. Like queryReferencesFragment, it reads only the references stored in
|
|
151
|
+
* the query, never the fragment cache, so one pass covers a whole set of records.
|
|
152
|
+
*/
|
|
153
|
+
export function queryReferencesAnyFragment(value: unknown, fragmentKeys: ReadonlySet<string>): boolean {
|
|
154
|
+
if (!value || typeof value !== 'object') return false;
|
|
155
|
+
if (Array.isArray(value)) {
|
|
156
|
+
return value.some(
|
|
157
|
+
(item) => (typeof item === 'string' && fragmentKeys.has(item)) || queryReferencesAnyFragment(item, fragmentKeys),
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return Object.values(value).some((item) => queryReferencesAnyFragment(item, fragmentKeys));
|
|
161
|
+
}
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import type { WSDataChange, WSDataUpdateObj } from '@jsb188/app/types/app.d.ts';
|
|
2
|
+
import { DATA_TYPES, type DataTypeKey, makeFragmentKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
|
|
3
|
+
import { isStaleAutomaticAIChatFragment } from '@jsb188/mday/utils/aiChat.ts';
|
|
4
|
+
import {
|
|
5
|
+
addFragmentToCache,
|
|
6
|
+
getCachedQueryKeys,
|
|
7
|
+
getGraphQLCacheEpoch,
|
|
8
|
+
loadFragment,
|
|
9
|
+
peekQuery,
|
|
10
|
+
resetQuery,
|
|
11
|
+
updateFragment,
|
|
12
|
+
} from '../cache/index.ts';
|
|
13
|
+
import { queryReferencesFragment } from '../cache/index.helpers.ts';
|
|
14
|
+
import { getMountedQueryKeys, isQueryKeyMounted } from '../cache/mounted-queries.ts';
|
|
15
|
+
import { noteRealtimeWrite } from '../cache/realtime-fence.ts';
|
|
16
|
+
import { getQueryReactivitiesForType, getQueryReactivity, type RegisteredQueryReactivity } from '../gql/reactivity.ts';
|
|
17
|
+
import { RECORD_READS } from '../gql/recordReads.ts';
|
|
18
|
+
import type { UpdateObserversFn } from '../types.d.ts';
|
|
19
|
+
import {
|
|
20
|
+
decideQueryRefresh,
|
|
21
|
+
getChangeScope,
|
|
22
|
+
getQueryKeyName,
|
|
23
|
+
getQueryPageFamily,
|
|
24
|
+
mergeChangeData,
|
|
25
|
+
parseQueryKey,
|
|
26
|
+
queryReferencesAnyFragment,
|
|
27
|
+
queryVariablesMatchScope,
|
|
28
|
+
} from './data-changes.helpers.ts';
|
|
29
|
+
import { shouldApplyFragmentRealtimeSequence } from './fragment-realtime-sequence.ts';
|
|
30
|
+
import { queueRecordRead } from './record-read.ts';
|
|
31
|
+
|
|
32
|
+
// auto: refetch now (throttled); notice: drop the pages and show the notice; self: the same, refetched by this tab without a notice
|
|
33
|
+
type RefreshMode = 'auto' | 'notice' | 'self';
|
|
34
|
+
// All cached pages of one list with one set of filters, plus the mounted hooks with those variables
|
|
35
|
+
type QueryFamily = { id: string; keys: string[] };
|
|
36
|
+
type QueuedRefresh = QueryFamily & { mode: RefreshMode };
|
|
37
|
+
// The query keys one packet works with: cached and mounted keys by query name, and each key's variables, parsed when first needed
|
|
38
|
+
type QueryKeyIndex = {
|
|
39
|
+
byName: Map<string, string[]>;
|
|
40
|
+
parsed: Map<string, { variables: Record<string, any>; pageFamily: string } | null>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Automatic refreshes per family: when each last ran, and the one waiting for the end of its one-second window
|
|
44
|
+
// together with the cache epoch it was queued in (a cache reset drops it)
|
|
45
|
+
const AUTO_REFRESH = {
|
|
46
|
+
last: new Map<string, number>(),
|
|
47
|
+
pending: new Map<string, { keys: Set<string>; timer: ReturnType<typeof setTimeout>; epoch: number }>(),
|
|
48
|
+
};
|
|
49
|
+
// Per data type: the different records that needed reading in the current one-second window of one cache epoch
|
|
50
|
+
const RECORD_READ_BURSTS = new Map<string, { epoch: number; startedAt: number; fragmentKeys: Set<string> }>();
|
|
51
|
+
// Data types already warned about once: unknown to this client, or sent thin without a record read
|
|
52
|
+
const WARNED_TYPES = { unknown: new Set<string>(), unreadable: new Set<string>() };
|
|
53
|
+
const FILE_CONSTANTS = { autoRefreshIntervalMs: 1000, autoRefreshPruneAbove: 500, recordReadBurstLimit: 20 };
|
|
54
|
+
|
|
55
|
+
/*
|
|
56
|
+
* Apply one realtime packet: update the records, then refresh the queries
|
|
57
|
+
* that depend on them. Thin changes whose record cannot be read refresh the
|
|
58
|
+
* queries that hold the record instead.
|
|
59
|
+
*/
|
|
60
|
+
export function applyDataChanges(update: Pick<WSDataUpdateObj, 'changes' | 'origin'>, updateObservers: UpdateObserversFn) {
|
|
61
|
+
const origin = !!update.origin;
|
|
62
|
+
const index = indexQueryKeysByName();
|
|
63
|
+
const refreshes = new Map<string, QueuedRefresh>();
|
|
64
|
+
const unreadFragmentKeys = new Set<string>();
|
|
65
|
+
for (const change of update.changes || []) {
|
|
66
|
+
if (!Object.hasOwn(DATA_TYPES, change.type)) {
|
|
67
|
+
warnUnknownDataType(change.type);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const definition = DATA_TYPES[change.type as DataTypeKey];
|
|
72
|
+
const fragmentKey = definition.fragment && change.id ? makeFragmentKey(definition.fragment, change.id) : null;
|
|
73
|
+
// An older revision than one already applied changes nothing
|
|
74
|
+
if (fragmentKey && !applyRecordChange(change, fragmentKey, updateObservers)) continue;
|
|
75
|
+
const holderRefetches = collectQueryRefreshes(change, fragmentKey, origin, index, refreshes);
|
|
76
|
+
// A thin change: this client reads the record itself, unless a mounted query that holds it is refetching anyway
|
|
77
|
+
if (fragmentKey && change.partial && change.kind !== 'deleted' && !holderRefetches && loadFragment(fragmentKey)) {
|
|
78
|
+
if (!queueThinRecordRead(change, fragmentKey, updateObservers)) unreadFragmentKeys.add(fragmentKey);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (unreadFragmentKeys.size) collectHolderRefreshes(unreadFragmentKeys, origin, index, refreshes);
|
|
83
|
+
flushQueryRefreshes(refreshes, updateObservers);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/*
|
|
87
|
+
* Group the keys of every cached query and every mounted query hook by query
|
|
88
|
+
* name, once per packet. Mounted hooks without a cached result (first load in
|
|
89
|
+
* flight, pages dropped by a notice, never-cached queries) are reached only here.
|
|
90
|
+
*/
|
|
91
|
+
function indexQueryKeysByName(): QueryKeyIndex {
|
|
92
|
+
const byName = new Map<string, string[]>();
|
|
93
|
+
const seen = new Set<string>();
|
|
94
|
+
for (const keys of [getCachedQueryKeys(), getMountedQueryKeys()]) {
|
|
95
|
+
for (const key of keys) {
|
|
96
|
+
const name = getQueryKeyName(key);
|
|
97
|
+
if (!name || seen.has(key)) continue;
|
|
98
|
+
seen.add(key);
|
|
99
|
+
const named = byName.get(name);
|
|
100
|
+
if (named) named.push(key);
|
|
101
|
+
else byName.set(name, [key]);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { byName, parsed: new Map() };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/*
|
|
108
|
+
* Return one indexed key's variables and page family, parsing it on first use; null for a key in another format.
|
|
109
|
+
*/
|
|
110
|
+
function readIndexedQueryKey(index: QueryKeyIndex, key: string) {
|
|
111
|
+
let parsed = index.parsed.get(key);
|
|
112
|
+
if (parsed === undefined) {
|
|
113
|
+
const query = parseQueryKey(key);
|
|
114
|
+
parsed = query ? { variables: query.variables, pageFamily: getQueryPageFamily(query.variables) } : null;
|
|
115
|
+
index.parsed.set(key, parsed);
|
|
116
|
+
}
|
|
117
|
+
return parsed;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/*
|
|
121
|
+
* Update one cached record from a change. Returns false when the change is
|
|
122
|
+
* older than what was already applied. Only an 'added' change with full data
|
|
123
|
+
* creates a record; a deleted record is marked, never removed.
|
|
124
|
+
*/
|
|
125
|
+
function applyRecordChange(change: WSDataChange, fragmentKey: string, updateObservers: UpdateObserversFn): boolean {
|
|
126
|
+
if (!shouldApplyFragmentRealtimeSequence(change.revKey ? `${change.revKey}:${change.id}` : fragmentKey, change.rev)) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const cached = loadFragment(fragmentKey);
|
|
131
|
+
const data = change.data && Object.keys(change.data).length ? change.data : null;
|
|
132
|
+
// Only a record that is marked deleted needs the marker cleared; other records never get the field
|
|
133
|
+
const restored = change.kind === 'added' && cached?.__deleted === true;
|
|
134
|
+
const patch = change.kind === 'deleted' ? { ...data, __deleted: true } : restored ? { ...data, __deleted: false } : data;
|
|
135
|
+
noteRecordChangeWrite(change, fragmentKey, data, !!cached);
|
|
136
|
+
if (!patch) return true;
|
|
137
|
+
|
|
138
|
+
if (cached) {
|
|
139
|
+
updateFragment(fragmentKey, (current: any) => mergeRecordPatch(change, fragmentKey, current, patch), null, false, updateObservers);
|
|
140
|
+
} else if (change.kind === 'added' && data && !change.partial) {
|
|
141
|
+
addFragmentToCache(fragmentKey, patch);
|
|
142
|
+
updateObservers({ fragmentIds: [fragmentKey] });
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/*
|
|
148
|
+
* Remember what a change wrote, so a response that started before it cannot
|
|
149
|
+
* overwrite it; also for a record that is not cached. An 'added' change lifts
|
|
150
|
+
* a deletion the fence remembers, so such a response cannot mark the restored
|
|
151
|
+
* record deleted again. Appended arrays are additions, not field values: they
|
|
152
|
+
* are remembered only with the cached record they extend, or a response that
|
|
153
|
+
* creates the record would keep the addition in place of its own array.
|
|
154
|
+
*/
|
|
155
|
+
function noteRecordChangeWrite(change: WSDataChange, fragmentKey: string, data: Record<string, unknown> | null, cached: boolean) {
|
|
156
|
+
const fields = change.append && !cached ? null : data;
|
|
157
|
+
const fenced =
|
|
158
|
+
change.kind === 'deleted' ? { ...fields, __deleted: true } : change.kind === 'added' ? { ...fields, __deleted: false } : fields;
|
|
159
|
+
if (fenced) noteRealtimeWrite(fragmentKey, fenced);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
* Merge a change's patch into the cached record. Appended arrays extend the
|
|
164
|
+
* record as stored; an automatic chat summary never replaces a user-managed one.
|
|
165
|
+
*/
|
|
166
|
+
function mergeRecordPatch(change: WSDataChange, fragmentKey: string, current: any, patch: Record<string, unknown>) {
|
|
167
|
+
const latest = change.append ? loadFragment(fragmentKey) : current;
|
|
168
|
+
if (change.type === 'aiChat' && isStaleAutomaticAIChatFragment(latest, change.data)) return latest;
|
|
169
|
+
return mergeChangeData(latest, patch, change.append);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/*
|
|
173
|
+
* Return how a query refreshes for a packet: its declared mode, with a notice
|
|
174
|
+
* refetched by this tab when the tab caused the change; 'auto' when undeclared.
|
|
175
|
+
*/
|
|
176
|
+
function getRefreshMode(reactivity: RegisteredQueryReactivity | undefined, origin: boolean): RefreshMode {
|
|
177
|
+
if (reactivity?.refresh !== 'notice') return 'auto';
|
|
178
|
+
return origin ? 'self' : 'notice';
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/*
|
|
182
|
+
* Return the families of one declared query whose variables are in a change's
|
|
183
|
+
* scope. A family is all loaded pages of one list with one set of filters,
|
|
184
|
+
* under every name the query is cached or mounted by.
|
|
185
|
+
*/
|
|
186
|
+
function getQueryFamiliesInScope(reactivity: RegisteredQueryReactivity, scope: Record<string, string>, index: QueryKeyIndex) {
|
|
187
|
+
const families = new Map<string, QueryFamily>();
|
|
188
|
+
for (const name of reactivity.names) {
|
|
189
|
+
for (const key of index.byName.get(name) || []) {
|
|
190
|
+
const parsed = readIndexedQueryKey(index, key);
|
|
191
|
+
if (!parsed || !queryVariablesMatchScope(parsed.variables, scope, reactivity.scope)) continue;
|
|
192
|
+
const id = `${reactivity.names[0]}:${parsed.pageFamily}`;
|
|
193
|
+
const family = families.get(id);
|
|
194
|
+
if (family) family.keys.push(key);
|
|
195
|
+
else families.set(id, { id, keys: [key] });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return families.values();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/*
|
|
202
|
+
* Find the queries one change affects and record which of them must refresh.
|
|
203
|
+
* Returns true when a mounted self-updating query that holds the record is among them.
|
|
204
|
+
*/
|
|
205
|
+
function collectQueryRefreshes(
|
|
206
|
+
change: WSDataChange,
|
|
207
|
+
fragmentKey: string | null,
|
|
208
|
+
origin: boolean,
|
|
209
|
+
index: QueryKeyIndex,
|
|
210
|
+
refreshes: Map<string, QueuedRefresh>,
|
|
211
|
+
): boolean {
|
|
212
|
+
const type = change.type as DataTypeKey;
|
|
213
|
+
const scope = getChangeScope(change);
|
|
214
|
+
let holderRefetches = false;
|
|
215
|
+
for (const reactivity of getQueryReactivitiesForType(type)) {
|
|
216
|
+
if (origin && reactivity.skipOwnChanges) continue;
|
|
217
|
+
const relation = reactivity.derived?.includes(type) ? 'derived' : 'lists';
|
|
218
|
+
const decision = decideQueryRefresh(relation, change.kind, reactivity.refresh);
|
|
219
|
+
if (decision === 'none') continue;
|
|
220
|
+
const mode = getRefreshMode(reactivity, origin);
|
|
221
|
+
for (const family of getQueryFamiliesInScope(reactivity, scope, index)) {
|
|
222
|
+
// Holders decide membership, and whether an automatic refetch brings the record itself
|
|
223
|
+
const holders =
|
|
224
|
+
fragmentKey && (decision !== 'refresh' || mode === 'auto')
|
|
225
|
+
? family.keys.filter((key) => queryReferencesFragment(peekQuery(key), fragmentKey))
|
|
226
|
+
: [];
|
|
227
|
+
if (decision === 'refresh-if-missing' && holders.length) continue;
|
|
228
|
+
if (decision === 'refresh-if-held' && !holders.length) continue;
|
|
229
|
+
refreshes.set(family.id, { ...family, mode });
|
|
230
|
+
if (mode === 'auto' && holders.some(isQueryKeyMounted)) holderRefetches = true;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return holderRefetches;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/*
|
|
237
|
+
* Queue the record read of a thin change. Returns false when the record is not
|
|
238
|
+
* read: its type has no record read (warned once), more records of its type
|
|
239
|
+
* than the burst limit needed reading within a second, or the change cannot be read.
|
|
240
|
+
*/
|
|
241
|
+
function queueThinRecordRead(change: WSDataChange, fragmentKey: string, updateObservers: UpdateObserversFn): boolean {
|
|
242
|
+
if (!RECORD_READS[change.type as DataTypeKey]) {
|
|
243
|
+
warnUnreadableDataType(change.type);
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
return isWithinRecordReadBurstLimit(change.type, fragmentKey) && !!queueRecordRead(change, updateObservers);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/*
|
|
250
|
+
* Count a record that needs reading and return whether records of its data
|
|
251
|
+
* type are still read one by one: they are until more records of the type
|
|
252
|
+
* than the burst limit needed reading within one second (design 6). The limit
|
|
253
|
+
* counts records, not changes: more thin changes of a counted record join its
|
|
254
|
+
* read. A cache reset starts a new window.
|
|
255
|
+
*/
|
|
256
|
+
function isWithinRecordReadBurstLimit(type: string, fragmentKey: string): boolean {
|
|
257
|
+
const now = Date.now();
|
|
258
|
+
const epoch = getGraphQLCacheEpoch();
|
|
259
|
+
let burst = RECORD_READ_BURSTS.get(type);
|
|
260
|
+
if (!burst || burst.epoch !== epoch || now - burst.startedAt >= 1000) {
|
|
261
|
+
burst = { epoch, startedAt: now, fragmentKeys: new Set() };
|
|
262
|
+
RECORD_READ_BURSTS.set(type, burst);
|
|
263
|
+
}
|
|
264
|
+
// One record past the limit is enough to know that the window is over it
|
|
265
|
+
if (burst.fragmentKeys.size <= FILE_CONSTANTS.recordReadBurstLimit) burst.fragmentKeys.add(fragmentKey);
|
|
266
|
+
return burst.fragmentKeys.size <= FILE_CONSTANTS.recordReadBurstLimit;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/*
|
|
270
|
+
* Queue a refresh for every query family that holds one of the records that
|
|
271
|
+
* could not be read, each in its own mode, in one pass over the indexed keys.
|
|
272
|
+
* Scope is not checked: holding the record is what matters.
|
|
273
|
+
*/
|
|
274
|
+
function collectHolderRefreshes(
|
|
275
|
+
fragmentKeys: ReadonlySet<string>,
|
|
276
|
+
origin: boolean,
|
|
277
|
+
index: QueryKeyIndex,
|
|
278
|
+
refreshes: Map<string, QueuedRefresh>,
|
|
279
|
+
) {
|
|
280
|
+
const families = new Map<string, QueuedRefresh & { holds: boolean }>();
|
|
281
|
+
for (const [name, keys] of index.byName) {
|
|
282
|
+
const reactivity = getQueryReactivity(name);
|
|
283
|
+
if (origin && reactivity?.skipOwnChanges) continue;
|
|
284
|
+
for (const key of keys) {
|
|
285
|
+
const parsed = readIndexedQueryKey(index, key);
|
|
286
|
+
if (!parsed) continue;
|
|
287
|
+
const id = `${reactivity?.names[0] || name}:${parsed.pageFamily}`;
|
|
288
|
+
let family = families.get(id);
|
|
289
|
+
if (!family) {
|
|
290
|
+
family = { id, keys: [], mode: getRefreshMode(reactivity, origin), holds: false };
|
|
291
|
+
families.set(id, family);
|
|
292
|
+
}
|
|
293
|
+
family.keys.push(key);
|
|
294
|
+
family.holds ||= queryReferencesAnyFragment(peekQuery(key), fragmentKeys);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
for (const { holds, ...family } of families.values()) {
|
|
299
|
+
if (holds) refreshes.set(family.id, family);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/*
|
|
304
|
+
* Run the collected refreshes: notices at once, automatic refetches through
|
|
305
|
+
* the throttle. Events are published per exact query key, never per target.
|
|
306
|
+
*/
|
|
307
|
+
function flushQueryRefreshes(refreshes: Map<string, QueuedRefresh>, updateObservers: UpdateObserversFn) {
|
|
308
|
+
for (const refresh of refreshes.values()) {
|
|
309
|
+
if (refresh.mode === 'auto') {
|
|
310
|
+
scheduleAutoRefresh(refresh, updateObservers);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
// A page nobody has mounted is only dropped from the cache; an event for it would never be consumed
|
|
314
|
+
for (const key of refresh.keys) {
|
|
315
|
+
resetQuery(key, false, isQueryKeyMounted(key) ? updateObservers : undefined, true, refresh.mode === 'self');
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/*
|
|
321
|
+
* Refresh a family now, or once at the end of its one-second window when it
|
|
322
|
+
* refreshed recently. A cache reset in the meantime drops the waiting refresh;
|
|
323
|
+
* a refresh queued after the reset replaces it instead of joining it.
|
|
324
|
+
*/
|
|
325
|
+
function scheduleAutoRefresh(refresh: QueuedRefresh, updateObservers: UpdateObserversFn) {
|
|
326
|
+
const epoch = getGraphQLCacheEpoch();
|
|
327
|
+
const pending = AUTO_REFRESH.pending.get(refresh.id);
|
|
328
|
+
if (pending?.epoch === epoch) {
|
|
329
|
+
for (const key of refresh.keys) pending.keys.add(key);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (pending) {
|
|
333
|
+
clearTimeout(pending.timer);
|
|
334
|
+
AUTO_REFRESH.pending.delete(refresh.id);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const wait = (AUTO_REFRESH.last.get(refresh.id) || 0) + FILE_CONSTANTS.autoRefreshIntervalMs - Date.now();
|
|
338
|
+
if (wait <= 0) {
|
|
339
|
+
runAutoRefresh(refresh.id, refresh.keys, updateObservers);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const keys = new Set(refresh.keys);
|
|
344
|
+
const timer = setTimeout(() => {
|
|
345
|
+
AUTO_REFRESH.pending.delete(refresh.id);
|
|
346
|
+
if (epoch === getGraphQLCacheEpoch()) runAutoRefresh(refresh.id, keys, updateObservers);
|
|
347
|
+
}, wait);
|
|
348
|
+
AUTO_REFRESH.pending.set(refresh.id, { keys, timer, epoch });
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/*
|
|
352
|
+
* Refetch every key of a family through the forced-refetch path and remember
|
|
353
|
+
* when, pruning entries older than the window once there are many.
|
|
354
|
+
*/
|
|
355
|
+
function runAutoRefresh(id: string, keys: Iterable<string>, updateObservers: UpdateObserversFn) {
|
|
356
|
+
const now = Date.now();
|
|
357
|
+
AUTO_REFRESH.last.set(id, now);
|
|
358
|
+
if (AUTO_REFRESH.last.size > FILE_CONSTANTS.autoRefreshPruneAbove) {
|
|
359
|
+
for (const [familyId, at] of AUTO_REFRESH.last) {
|
|
360
|
+
if (now - at >= FILE_CONSTANTS.autoRefreshIntervalMs) AUTO_REFRESH.last.delete(familyId);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
for (const key of keys) resetQuery(key, true, updateObservers);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/*
|
|
367
|
+
* Warn once per type about a change whose type this client does not know (a newer server); the change is skipped.
|
|
368
|
+
*/
|
|
369
|
+
function warnUnknownDataType(type: string) {
|
|
370
|
+
if (WARNED_TYPES.unknown.has(type)) return;
|
|
371
|
+
WARNED_TYPES.unknown.add(type);
|
|
372
|
+
console.warn(`Skipping realtime changes of unknown data type "${type}".`);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/*
|
|
376
|
+
* Warn once per type about a thin change without a record read; the queries that hold the record refresh instead.
|
|
377
|
+
*/
|
|
378
|
+
function warnUnreadableDataType(type: string) {
|
|
379
|
+
if (WARNED_TYPES.unreadable.has(type)) return;
|
|
380
|
+
WARNED_TYPES.unreadable.add(type);
|
|
381
|
+
console.warn(`Data type "${type}" has no record read; refreshing the queries that hold its thin changes instead.`);
|
|
382
|
+
}
|