@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
package/cache/index.ts
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from '
|
|
7
|
-
import {
|
|
8
|
-
getQueryResetTargetId,
|
|
9
|
-
queryCacheKeyMatchesResetTarget,
|
|
10
|
-
type QueryResetTarget,
|
|
11
|
-
} from '@jsb188/app/utils/queryReset.ts';
|
|
1
|
+
import { makeGraphQLVariablesKey } from '@jsb188/app/utils/logic.ts';
|
|
2
|
+
import { FRAGMENT_NAMES } from '@jsb188/mday/constants/graphqlReactivity.ts';
|
|
3
|
+
import { mapArrayLikeObjects, uniq } from '@jsb188/app/utils/object.ts';
|
|
4
|
+
import { getQueryResetTargetId, type QueryResetTarget, queryCacheKeyMatchesResetTarget } from '@jsb188/app/utils/queryReset.ts';
|
|
5
|
+
import { clearAllDrafts } from '../client/draft-persistence.ts';
|
|
6
|
+
import { clearAIChatOutgoingMessages } from '../libs/ai-chat-outgoing.helpers.ts';
|
|
7
|
+
import { clearAIChatLinkEvents } from '../libs/ai-chat-link-events.ts';
|
|
12
8
|
import type { UpdateObserversFn } from '../types.d.ts';
|
|
13
9
|
import { PARTIAL_NULL_PRESERVE_FIELDS, PARTIALS_MAP, RULES } from './config.ts';
|
|
14
|
-
import { cacheStore } from './store.ts';
|
|
15
10
|
import { clearGraphQLCachePersistence } from './persist.ts';
|
|
16
|
-
import {
|
|
11
|
+
import { clearRealtimeWrites, keepNewerRealtimeFields } from './realtime-fence.ts';
|
|
12
|
+
import { cacheStore } from './store.ts';
|
|
13
|
+
import { mergeGraphQLObjectData, reconcileAIChatCacheSnapshot } from './index.helpers.ts';
|
|
17
14
|
|
|
18
15
|
export {
|
|
19
16
|
initGraphQLCachePersistence,
|
|
@@ -25,9 +22,13 @@ export {
|
|
|
25
22
|
// and notify persistence subscribers.
|
|
26
23
|
const QUERIES = cacheStore.queries;
|
|
27
24
|
const FRAGMENTS = cacheStore.fragments;
|
|
25
|
+
let graphQLCacheEpoch = 0;
|
|
28
26
|
const RESET_TIME = new Map();
|
|
29
27
|
const QUERY_DOCUMENT_CACHE_IDS = new WeakMap<object, number>();
|
|
28
|
+
const QUERY_FRAGMENT_DEFINITIONS = new WeakMap<object, Record<string, any>>();
|
|
30
29
|
let NEXT_QUERY_DOCUMENT_CACHE_ID = 1;
|
|
30
|
+
// When the network request whose response is being written began (0 outside graphqlRequest writes)
|
|
31
|
+
let ACTIVE_WRITE_STARTED_AT = 0;
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Return a stable runtime id for one parsed GraphQL document.
|
|
@@ -55,88 +56,12 @@ function getGraphQLSelectionResponseName(selection: any) {
|
|
|
55
56
|
return selection.alias?.value || selection.name?.value;
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
/**
|
|
59
|
-
* JS Map will sometimes convert nested Array into Array-like Object
|
|
60
|
-
* ie. "{"0": "a", "1": "b"}" etc
|
|
61
|
-
* This will convert Array-like back to Array
|
|
62
|
-
*
|
|
63
|
-
* NOTE: There's a known vulnerability here that sometimes the developer may
|
|
64
|
-
* actually design a Array-like schema; but I don't see a need for that right now.
|
|
65
|
-
*/
|
|
66
|
-
|
|
67
|
-
function enforceArrayType(obj: any): any {
|
|
68
|
-
if (obj && typeof obj === 'object') {
|
|
69
|
-
if (Array.isArray(obj)) {
|
|
70
|
-
return obj.map(enforceArrayType);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const keys = Object.keys(obj);
|
|
74
|
-
if (keys.every((key, i) => key === i.toString())) {
|
|
75
|
-
return keys.map((key) => obj[key]);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
for (const key in obj) {
|
|
79
|
-
if (
|
|
80
|
-
Object.prototype.hasOwnProperty.call(obj, key) &&
|
|
81
|
-
typeof obj[key] === 'object'
|
|
82
|
-
) {
|
|
83
|
-
obj[key] = enforceArrayType(obj[key]);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return obj;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Merge nested objects together
|
|
92
|
-
*/
|
|
93
|
-
|
|
94
|
-
function mergeNestedObjects(obj1: any, obj2: any) {
|
|
95
|
-
if (Array.isArray(obj1) || Array.isArray(obj2)) {
|
|
96
|
-
if (Array.isArray(obj2)) {
|
|
97
|
-
return [...obj2];
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (Array.isArray(obj1)) {
|
|
101
|
-
return [...obj1];
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return obj2;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (!obj1) {
|
|
108
|
-
if (!obj2 || typeof obj2 !== 'object') {
|
|
109
|
-
return obj2;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return { ...obj2 };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const newObj = { ...obj1 };
|
|
116
|
-
for (const key in obj2) {
|
|
117
|
-
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
|
|
118
|
-
if (typeof obj2[key] === 'object' && obj2[key] !== null) {
|
|
119
|
-
newObj[key] = mergeNestedObjects(obj1[key], obj2[key]);
|
|
120
|
-
} else {
|
|
121
|
-
newObj[key] = obj2[key];
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return newObj;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
59
|
/*
|
|
130
60
|
* Omit configured null state fields from partial-fragment writes while
|
|
131
61
|
* retaining nulls for volatile fields that must clear stale one-shot data.
|
|
132
62
|
*/
|
|
133
|
-
function getPartialFragmentMergeData(
|
|
134
|
-
fragmentName
|
|
135
|
-
data: any,
|
|
136
|
-
) {
|
|
137
|
-
const preserveFields = fragmentName
|
|
138
|
-
? PARTIAL_NULL_PRESERVE_FIELDS[fragmentName]
|
|
139
|
-
: null;
|
|
63
|
+
function getPartialFragmentMergeData(fragmentName: string | undefined, data: any) {
|
|
64
|
+
const preserveFields = fragmentName ? PARTIAL_NULL_PRESERVE_FIELDS[fragmentName] : null;
|
|
140
65
|
if (!preserveFields?.length || !data || typeof data !== 'object') {
|
|
141
66
|
return data;
|
|
142
67
|
}
|
|
@@ -163,11 +88,11 @@ function isPlainObject(value: any) {
|
|
|
163
88
|
}
|
|
164
89
|
|
|
165
90
|
/**
|
|
166
|
-
* Return whether one object is an optimistic
|
|
91
|
+
* Return whether one object is an optimistic placeholder.
|
|
167
92
|
*/
|
|
168
93
|
|
|
169
|
-
function
|
|
170
|
-
return isPlainObject(value) && value.
|
|
94
|
+
function isOptimisticPlaceholder(value: any) {
|
|
95
|
+
return isPlainObject(value) && value.__optimisticPlaceholder === true;
|
|
171
96
|
}
|
|
172
97
|
|
|
173
98
|
/**
|
|
@@ -183,18 +108,18 @@ function getObjectIdKey(value: any) {
|
|
|
183
108
|
}
|
|
184
109
|
|
|
185
110
|
/**
|
|
186
|
-
* Return a copy of one top-level array without optimistic
|
|
111
|
+
* Return a copy of one top-level array without optimistic placeholders.
|
|
187
112
|
*/
|
|
188
113
|
|
|
189
|
-
function
|
|
190
|
-
return value.filter((item) => !
|
|
114
|
+
function removeOptimisticPlaceholdersFromArray(value: any[]) {
|
|
115
|
+
return value.filter((item) => !isOptimisticPlaceholder(item));
|
|
191
116
|
}
|
|
192
117
|
|
|
193
118
|
/**
|
|
194
|
-
* Return a fragment with optimistic
|
|
119
|
+
* Return a fragment with optimistic placeholders removed from top-level arrays.
|
|
195
120
|
*/
|
|
196
121
|
|
|
197
|
-
function
|
|
122
|
+
function removeOptimisticPlaceholdersFromArrayFields(fragment: any) {
|
|
198
123
|
if (!isPlainObject(fragment)) {
|
|
199
124
|
return fragment;
|
|
200
125
|
}
|
|
@@ -202,14 +127,11 @@ function removeOptimisticUIObjectsFromArrayFields(fragment: any) {
|
|
|
202
127
|
let nextFragment: Record<string, any> | null = null;
|
|
203
128
|
|
|
204
129
|
for (const key in fragment) {
|
|
205
|
-
if (
|
|
206
|
-
!Object.prototype.hasOwnProperty.call(fragment, key) ||
|
|
207
|
-
!Array.isArray(fragment[key])
|
|
208
|
-
) {
|
|
130
|
+
if (!Object.hasOwn(fragment, key) || !Array.isArray(fragment[key])) {
|
|
209
131
|
continue;
|
|
210
132
|
}
|
|
211
133
|
|
|
212
|
-
const cleanedArray =
|
|
134
|
+
const cleanedArray = removeOptimisticPlaceholdersFromArray(fragment[key]);
|
|
213
135
|
if (cleanedArray.length === fragment[key].length) {
|
|
214
136
|
continue;
|
|
215
137
|
}
|
|
@@ -225,20 +147,15 @@ function removeOptimisticUIObjectsFromArrayFields(fragment: any) {
|
|
|
225
147
|
}
|
|
226
148
|
|
|
227
149
|
/**
|
|
228
|
-
* Return spread-backed array data plus unmatched optimistic
|
|
150
|
+
* Return spread-backed array data plus unmatched optimistic placeholders.
|
|
229
151
|
*/
|
|
230
152
|
|
|
231
|
-
function
|
|
232
|
-
spreadData:
|
|
233
|
-
fragmentData: any[],
|
|
234
|
-
) {
|
|
235
|
-
const spreadIds = new Set(
|
|
236
|
-
spreadData.map(getObjectIdKey).filter((id): id is string => id !== null),
|
|
237
|
-
);
|
|
153
|
+
function mergeSpreadBackedArrayWithOptimisticPlaceholders(spreadData: any[], fragmentData: any[]) {
|
|
154
|
+
const spreadIds = new Set(spreadData.map(getObjectIdKey).filter((id): id is string => id !== null));
|
|
238
155
|
const mergedData = [...spreadData];
|
|
239
156
|
|
|
240
157
|
fragmentData.forEach((item, index) => {
|
|
241
|
-
if (!
|
|
158
|
+
if (!isOptimisticPlaceholder(item)) {
|
|
242
159
|
return;
|
|
243
160
|
}
|
|
244
161
|
|
|
@@ -286,18 +203,14 @@ function makePrioritizedFieldTree(fields: string[]) {
|
|
|
286
203
|
* Merge values while prioritizing main fragment data for selected field paths.
|
|
287
204
|
*/
|
|
288
205
|
|
|
289
|
-
function mergeWithPriorityPaths(
|
|
290
|
-
innerValue: any,
|
|
291
|
-
mainValue: any,
|
|
292
|
-
priorityNode: any,
|
|
293
|
-
) {
|
|
206
|
+
function mergeWithPriorityPaths(innerValue: any, mainValue: any, priorityNode: any) {
|
|
294
207
|
if (!priorityNode) {
|
|
295
208
|
return innerValue;
|
|
296
209
|
}
|
|
297
210
|
|
|
298
211
|
if (priorityNode.__self) {
|
|
299
212
|
if (isPlainObject(innerValue) && isPlainObject(mainValue)) {
|
|
300
|
-
return
|
|
213
|
+
return mergeGraphQLObjectData(innerValue, mainValue);
|
|
301
214
|
}
|
|
302
215
|
|
|
303
216
|
return mainValue;
|
|
@@ -309,16 +222,9 @@ function mergeWithPriorityPaths(
|
|
|
309
222
|
|
|
310
223
|
const mergedValue = { ...innerValue };
|
|
311
224
|
for (const key in priorityNode) {
|
|
312
|
-
if (
|
|
313
|
-
key !== '__self' &&
|
|
314
|
-
Object.prototype.hasOwnProperty.call(priorityNode, key)
|
|
315
|
-
) {
|
|
225
|
+
if (key !== '__self' && Object.hasOwn(priorityNode, key)) {
|
|
316
226
|
if (mainValue[key] !== undefined) {
|
|
317
|
-
mergedValue[key] = mergeWithPriorityPaths(
|
|
318
|
-
innerValue[key],
|
|
319
|
-
mainValue[key],
|
|
320
|
-
priorityNode[key],
|
|
321
|
-
);
|
|
227
|
+
mergedValue[key] = mergeWithPriorityPaths(innerValue[key], mainValue[key], priorityNode[key]);
|
|
322
228
|
}
|
|
323
229
|
}
|
|
324
230
|
}
|
|
@@ -327,50 +233,26 @@ function mergeWithPriorityPaths(
|
|
|
327
233
|
}
|
|
328
234
|
|
|
329
235
|
/**
|
|
330
|
-
*
|
|
331
|
-
*/
|
|
332
|
-
|
|
333
|
-
function getSpreadValueScore(spreadValue: any) {
|
|
334
|
-
if (!spreadValue) {
|
|
335
|
-
return 0;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
if (typeof spreadValue === 'string') {
|
|
339
|
-
return 1;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
if (Array.isArray(spreadValue)) {
|
|
343
|
-
return spreadValue.length;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
if (spreadValue?.__list && Array.isArray(spreadValue.data)) {
|
|
347
|
-
return spreadValue.data.length;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
return 0;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Merge two spread values while preferring richer existing spread metadata.
|
|
236
|
+
* Merge object selection metadata while treating server lists as authoritative.
|
|
355
237
|
*/
|
|
356
238
|
|
|
357
239
|
function mergeSpreadValue(currentValue: any, nextValue: any) {
|
|
358
|
-
if (
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
if (nextScore >= currentScore) {
|
|
240
|
+
if (nextValue === undefined) return currentValue;
|
|
241
|
+
// A server list is authoritative even when it shrinks or becomes empty/null.
|
|
242
|
+
if (!Array.isArray(currentValue) || !Array.isArray(nextValue)) return nextValue;
|
|
243
|
+
const currentReferences = currentValue.filter((value) => typeof value === 'string');
|
|
244
|
+
const nextReferences = nextValue.filter((value) => typeof value === 'string');
|
|
245
|
+
if (
|
|
246
|
+
nextReferences.some((next) =>
|
|
247
|
+
currentReferences.some((current) => current.split(':')[0] === next.split(':')[0] && current !== next),
|
|
248
|
+
)
|
|
249
|
+
)
|
|
370
250
|
return nextValue;
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
251
|
+
// Tuple arrays describe object selections, not list membership. Retain fields
|
|
252
|
+
// omitted by a narrower operation, but replace every explicitly returned field.
|
|
253
|
+
const entries = new Map(currentValue.map((value) => [Array.isArray(value) ? value[0] : value, value]));
|
|
254
|
+
for (const value of nextValue) entries.set(Array.isArray(value) ? value[0] : value, value);
|
|
255
|
+
return [...entries.values()];
|
|
374
256
|
}
|
|
375
257
|
|
|
376
258
|
/**
|
|
@@ -388,11 +270,8 @@ function mergeSpreads(currentSpreads: any, nextSpreads: any) {
|
|
|
388
270
|
|
|
389
271
|
const mergedSpreads = { ...currentSpreads };
|
|
390
272
|
for (const key in nextSpreads) {
|
|
391
|
-
if (Object.
|
|
392
|
-
mergedSpreads[key] = mergeSpreadValue(
|
|
393
|
-
currentSpreads[key],
|
|
394
|
-
nextSpreads[key],
|
|
395
|
-
);
|
|
273
|
+
if (Object.hasOwn(nextSpreads, key)) {
|
|
274
|
+
mergedSpreads[key] = mergeSpreadValue(currentSpreads[key], nextSpreads[key]);
|
|
396
275
|
}
|
|
397
276
|
}
|
|
398
277
|
|
|
@@ -403,11 +282,7 @@ function mergeSpreads(currentSpreads: any, nextSpreads: any) {
|
|
|
403
282
|
* Make Object for fragment, omitting what you don't need
|
|
404
283
|
*/
|
|
405
284
|
|
|
406
|
-
function extractFragment(
|
|
407
|
-
dataObj: any,
|
|
408
|
-
innerSelection: any[],
|
|
409
|
-
isTest?: boolean,
|
|
410
|
-
) {
|
|
285
|
+
function extractFragment(dataObj: any, innerSelection: any[], _isTest?: boolean) {
|
|
411
286
|
if (innerSelection.length <= 1) {
|
|
412
287
|
return dataObj;
|
|
413
288
|
}
|
|
@@ -415,7 +290,7 @@ function extractFragment(
|
|
|
415
290
|
const fragmentObj = { ...dataObj };
|
|
416
291
|
for (const sel of innerSelection) {
|
|
417
292
|
if (sel.kind === 'Field') {
|
|
418
|
-
delete fragmentObj[sel
|
|
293
|
+
delete fragmentObj[getGraphQLSelectionResponseName(sel)];
|
|
419
294
|
}
|
|
420
295
|
}
|
|
421
296
|
|
|
@@ -434,11 +309,7 @@ function getFragmentCacheScope(data: any, fallbackScope?: string) {
|
|
|
434
309
|
* Build a fragment cache key using id when available, or a scoped fallback.
|
|
435
310
|
*/
|
|
436
311
|
|
|
437
|
-
function makeFragmentCacheKey(
|
|
438
|
-
fragmentName: string,
|
|
439
|
-
data: any,
|
|
440
|
-
fallbackScope?: string,
|
|
441
|
-
) {
|
|
312
|
+
function makeFragmentCacheKey(fragmentName: string, data: any, fallbackScope?: string) {
|
|
442
313
|
return `$${fragmentName}:${getFragmentCacheScope(data, fallbackScope)}`;
|
|
443
314
|
}
|
|
444
315
|
|
|
@@ -446,15 +317,13 @@ function makeFragmentCacheKey(
|
|
|
446
317
|
* Rebuild one cached list item from fragment refs and inline field tuples.
|
|
447
318
|
*/
|
|
448
319
|
|
|
449
|
-
function mapCachedListItem(
|
|
450
|
-
cachedItemData
|
|
451
|
-
loadFragmentData: (fragmentKey: string) => any,
|
|
452
|
-
) {
|
|
320
|
+
function mapCachedListItem(cachedItemData: any[], loadFragmentData: (fragmentKey: string) => any) {
|
|
321
|
+
if (cachedItemData === null) return null;
|
|
453
322
|
let item: Record<string, any> = {};
|
|
454
323
|
let cacheIsValid = true;
|
|
455
324
|
|
|
456
325
|
cachedItemData.forEach((cachedValue: any) => {
|
|
457
|
-
let mapCacheId;
|
|
326
|
+
let mapCacheId: string | undefined;
|
|
458
327
|
if (Array.isArray(cachedValue)) {
|
|
459
328
|
if (cachedValue.length === 1 && cachedValue?.[0]?.startsWith('$')) {
|
|
460
329
|
mapCacheId = cachedValue[0];
|
|
@@ -470,8 +339,7 @@ function mapCachedListItem(
|
|
|
470
339
|
return;
|
|
471
340
|
} else if (cachedValue[1]?.__reactiveFragmentName) {
|
|
472
341
|
const clientReactiveFragmentKey = `${cachedValue[1].__reactiveFragmentName}:${cachedValue[1].id}`;
|
|
473
|
-
item[cachedValue[0]] =
|
|
474
|
-
FRAGMENTS.get(clientReactiveFragmentKey) || cachedValue[1];
|
|
342
|
+
item[cachedValue[0]] = FRAGMENTS.get(clientReactiveFragmentKey) || cachedValue[1];
|
|
475
343
|
return;
|
|
476
344
|
} else {
|
|
477
345
|
item[cachedValue[0]] = cachedValue[1];
|
|
@@ -488,9 +356,7 @@ function mapCachedListItem(
|
|
|
488
356
|
const frgIndex = mapCacheId.indexOf(':');
|
|
489
357
|
const frgName = mapCacheId.substring(1, frgIndex);
|
|
490
358
|
const frgMap = PARTIALS_MAP[frgName];
|
|
491
|
-
const fragmentKey = frgMap
|
|
492
|
-
? `$${frgMap}:${mapCacheId.substring(frgIndex + 1)}`
|
|
493
|
-
: mapCacheId;
|
|
359
|
+
const fragmentKey = frgMap ? `$${frgMap}:${mapCacheId.substring(frgIndex + 1)}` : mapCacheId;
|
|
494
360
|
|
|
495
361
|
const cachedFragment = loadFragmentData(fragmentKey);
|
|
496
362
|
if (cachedFragment) {
|
|
@@ -504,7 +370,7 @@ function mapCachedListItem(
|
|
|
504
370
|
});
|
|
505
371
|
|
|
506
372
|
if (!cacheIsValid) {
|
|
507
|
-
return
|
|
373
|
+
return undefined;
|
|
508
374
|
}
|
|
509
375
|
|
|
510
376
|
return item;
|
|
@@ -521,9 +387,24 @@ export function loadQuery(queryKey: string, referencesOnly?: boolean) {
|
|
|
521
387
|
|
|
522
388
|
// Memoized: while the query references and every fragment they touch are
|
|
523
389
|
// unchanged, the exact same assembled object reference is returned.
|
|
524
|
-
return cacheStore.memoizeRead(`Q|${queryKey}`, () =>
|
|
525
|
-
|
|
526
|
-
|
|
390
|
+
return cacheStore.memoizeRead(`Q|${queryKey}`, () => loadQueryInternal(queryKey));
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/*
|
|
394
|
+
* Return the keys of every cached query result ('#name:@{variables}'). The
|
|
395
|
+
* iterator is live: copy it before deleting cached queries while reading it.
|
|
396
|
+
*/
|
|
397
|
+
export function getCachedQueryKeys(): IterableIterator<string> {
|
|
398
|
+
return QUERIES.keys();
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/*
|
|
402
|
+
* Return one cached query's stored value (fragment references, not assembled
|
|
403
|
+
* data) without recording a memo dependency or touching its LRU stamp.
|
|
404
|
+
* Undefined when the query is not cached.
|
|
405
|
+
*/
|
|
406
|
+
export function peekQuery(queryKey: string): any {
|
|
407
|
+
return QUERIES.peek(queryKey);
|
|
527
408
|
}
|
|
528
409
|
|
|
529
410
|
/**
|
|
@@ -532,49 +413,23 @@ export function loadQuery(queryKey: string, referencesOnly?: boolean) {
|
|
|
532
413
|
|
|
533
414
|
function loadQueryInternal(queryKey: string) {
|
|
534
415
|
const references = QUERIES.get(queryKey);
|
|
416
|
+
if (references === undefined) return null;
|
|
535
417
|
|
|
536
|
-
if (
|
|
537
|
-
// This is important, not returning null will cause infinite loop
|
|
538
|
-
return null;
|
|
539
|
-
}
|
|
418
|
+
if (references == null || typeof references !== 'object') return references;
|
|
540
419
|
|
|
541
420
|
// Lets nested spread lists be compared against this query cache entry's own
|
|
542
421
|
// write recency instead of always losing to the flat fragment copy
|
|
543
422
|
const queryVersion = QUERIES.version(queryKey);
|
|
544
423
|
|
|
545
424
|
if (Array.isArray(references)) {
|
|
546
|
-
const mappedCache = references.map((cacheObj) =>
|
|
547
|
-
|
|
548
|
-
);
|
|
549
|
-
if (mappedCache.some((c) => !c)) {
|
|
425
|
+
const mappedCache = references.map((cacheObj) => appendFragmentToCache(cacheObj, false, queryVersion));
|
|
426
|
+
if (mappedCache.some((c) => c === undefined)) {
|
|
550
427
|
return undefined;
|
|
551
428
|
}
|
|
552
429
|
|
|
553
|
-
// let hasInvalidatedCache = false;
|
|
554
|
-
// const mappedCache = references.map((cacheObj) => {
|
|
555
|
-
// if (!hasInvalidatedCache) {
|
|
556
|
-
// const singleCache = appendFragmentToCache(cacheObj);
|
|
557
|
-
|
|
558
|
-
// if (singleCache) {
|
|
559
|
-
// return singleCache;
|
|
560
|
-
// } else {
|
|
561
|
-
// hasInvalidatedCache = true;
|
|
562
|
-
// }
|
|
563
|
-
// }
|
|
564
|
-
// });
|
|
565
|
-
// if (!hasInvalidatedCache) {
|
|
566
|
-
// return mappedCache;
|
|
567
|
-
// }
|
|
568
|
-
|
|
569
430
|
return mappedCache;
|
|
570
431
|
} else if (references) {
|
|
571
|
-
|
|
572
|
-
const singleCacheData = appendFragmentToCache(
|
|
573
|
-
references,
|
|
574
|
-
true,
|
|
575
|
-
queryVersion,
|
|
576
|
-
);
|
|
577
|
-
// console.log('singleCacheData >>>>:', singleCacheData);
|
|
432
|
+
const singleCacheData = appendFragmentToCache(references, true, queryVersion);
|
|
578
433
|
return singleCacheData;
|
|
579
434
|
}
|
|
580
435
|
}
|
|
@@ -587,26 +442,17 @@ export function loadFragment(id: string, mainPrioritizedFields: string[] = []) {
|
|
|
587
442
|
// Memoized with automatic dependency tracking: while this fragment, its
|
|
588
443
|
// spreads and every transitively loaded fragment are unchanged, the exact
|
|
589
444
|
// same assembled object reference is returned (structural sharing).
|
|
590
|
-
const memoKey = mainPrioritizedFields.length
|
|
591
|
-
? `${id}|${mainPrioritizedFields.join(',')}`
|
|
592
|
-
: id;
|
|
445
|
+
const memoKey = mainPrioritizedFields.length ? `${id}|${mainPrioritizedFields.join(',')}` : id;
|
|
593
446
|
|
|
594
|
-
return cacheStore.memoizeRead(memoKey, () =>
|
|
595
|
-
loadFragmentInternal(id, mainPrioritizedFields),
|
|
596
|
-
);
|
|
447
|
+
return cacheStore.memoizeRead(memoKey, () => loadFragmentInternal(id, mainPrioritizedFields));
|
|
597
448
|
}
|
|
598
449
|
|
|
599
450
|
/**
|
|
600
451
|
* Assemble one fragment with its spreads (uncached implementation).
|
|
601
452
|
*/
|
|
602
453
|
|
|
603
|
-
function loadFragmentInternal(
|
|
604
|
-
id
|
|
605
|
-
mainPrioritizedFields: string[] = [],
|
|
606
|
-
) {
|
|
607
|
-
// console.log('load fragment:', id, FRAGMENTS.get(id));
|
|
608
|
-
|
|
609
|
-
const fragment = enforceArrayType(FRAGMENTS.get(id));
|
|
454
|
+
function loadFragmentInternal(id: string, mainPrioritizedFields: string[] = []) {
|
|
455
|
+
const fragment = FRAGMENTS.get(id);
|
|
610
456
|
if (!fragment) {
|
|
611
457
|
return null;
|
|
612
458
|
}
|
|
@@ -616,91 +462,65 @@ function loadFragmentInternal(
|
|
|
616
462
|
return fragment;
|
|
617
463
|
}
|
|
618
464
|
|
|
619
|
-
|
|
620
|
-
// if (isTest) {
|
|
621
|
-
// ..
|
|
622
|
-
// }
|
|
623
|
-
|
|
624
|
-
let fragmentWithSpreads = { ...fragment };
|
|
465
|
+
const fragmentWithSpreads = { ...fragment };
|
|
625
466
|
const prioritizedFieldTree = makePrioritizedFieldTree(mainPrioritizedFields);
|
|
626
467
|
for (const key in spreads) {
|
|
627
|
-
if (Object.
|
|
468
|
+
if (Object.hasOwn(spreads, key)) {
|
|
628
469
|
const spreadValue = spreads[key];
|
|
629
470
|
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
// console.log('SPREAD:', key, '->', spreadValue, typeof spreadValue);
|
|
636
|
-
// }
|
|
637
|
-
|
|
638
|
-
let spreadData;
|
|
639
|
-
if (typeof spreadValue === 'string') {
|
|
640
|
-
spreadData = enforceArrayType(FRAGMENTS.get(spreadValue));
|
|
471
|
+
let spreadData: any;
|
|
472
|
+
if (spreadValue === null) {
|
|
473
|
+
spreadData = null;
|
|
474
|
+
} else if (typeof spreadValue === 'string') {
|
|
475
|
+
spreadData = loadFragment(spreadValue) ?? undefined;
|
|
641
476
|
} else if (Array.isArray(spreadValue)) {
|
|
642
477
|
spreadData = spreadValue.reduce((acc, value) => {
|
|
643
|
-
// if (isTest) {
|
|
644
|
-
// console.log('????', value);
|
|
645
|
-
// }
|
|
646
|
-
|
|
647
478
|
if (typeof value === 'string') {
|
|
648
|
-
const fragmentData =
|
|
649
|
-
|
|
650
|
-
// if (isTest) {
|
|
651
|
-
// console.log('1:', value, fragmentData);
|
|
652
|
-
// }
|
|
479
|
+
const fragmentData = loadFragment(value);
|
|
480
|
+
if (!fragmentData) return undefined;
|
|
653
481
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
...fragmentData,
|
|
657
|
-
};
|
|
482
|
+
if (acc !== undefined) Object.assign(acc, fragmentData);
|
|
483
|
+
return acc;
|
|
658
484
|
} else if (Array.isArray(value)) {
|
|
659
485
|
const [namespace, innerValue] = value;
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
[namespace]: innerValue,
|
|
663
|
-
};
|
|
486
|
+
if (acc !== undefined) acc[namespace] = innerValue;
|
|
487
|
+
return acc;
|
|
664
488
|
}
|
|
665
489
|
|
|
666
490
|
return acc;
|
|
667
491
|
}, {});
|
|
668
492
|
} else if (spreadValue?.__list === true) {
|
|
669
|
-
spreadData = spreadValue.data
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
493
|
+
spreadData = spreadValue.data.map((val: any) => {
|
|
494
|
+
if (val === null) return null;
|
|
495
|
+
if (typeof val === 'string') {
|
|
496
|
+
return loadFragment(val) ?? undefined;
|
|
497
|
+
} else if (Array.isArray(val)) {
|
|
498
|
+
return mapCachedListItem(val, loadFragment);
|
|
499
|
+
}
|
|
676
500
|
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
501
|
+
return undefined;
|
|
502
|
+
});
|
|
503
|
+
if (spreadData.some((item: any) => item === undefined)) spreadData = undefined;
|
|
680
504
|
}
|
|
681
505
|
|
|
682
|
-
if (spreadData) {
|
|
683
|
-
|
|
506
|
+
if (spreadData == null) {
|
|
507
|
+
fragmentWithSpreads[key] = spreadData;
|
|
508
|
+
} else {
|
|
509
|
+
const spreadDataWithOptimisticPlaceholders =
|
|
684
510
|
Array.isArray(spreadData) && Array.isArray(fragmentWithSpreads[key])
|
|
685
|
-
?
|
|
686
|
-
spreadData,
|
|
687
|
-
fragmentWithSpreads[key],
|
|
688
|
-
)
|
|
511
|
+
? mergeSpreadBackedArrayWithOptimisticPlaceholders(spreadData, fragmentWithSpreads[key])
|
|
689
512
|
: spreadData;
|
|
690
513
|
const priorityNode = prioritizedFieldTree[key];
|
|
691
514
|
if (priorityNode && fragmentWithSpreads[key] !== undefined) {
|
|
692
515
|
fragmentWithSpreads[key] = mergeWithPriorityPaths(
|
|
693
|
-
|
|
516
|
+
spreadDataWithOptimisticPlaceholders,
|
|
694
517
|
fragmentWithSpreads[key],
|
|
695
518
|
priorityNode,
|
|
696
519
|
);
|
|
697
520
|
} else if (fragmentWithSpreads[key] !== undefined) {
|
|
698
|
-
fragmentWithSpreads[key] =
|
|
699
|
-
fragmentWithSpreads[key],
|
|
700
|
-
spreadDataWithOptimisticUI,
|
|
701
|
-
);
|
|
521
|
+
fragmentWithSpreads[key] = mergeGraphQLObjectData(fragmentWithSpreads[key], spreadDataWithOptimisticPlaceholders);
|
|
702
522
|
} else {
|
|
703
|
-
fragmentWithSpreads[key] =
|
|
523
|
+
fragmentWithSpreads[key] = spreadDataWithOptimisticPlaceholders;
|
|
704
524
|
}
|
|
705
525
|
}
|
|
706
526
|
}
|
|
@@ -709,6 +529,41 @@ function loadFragmentInternal(
|
|
|
709
529
|
return fragmentWithSpreads;
|
|
710
530
|
}
|
|
711
531
|
|
|
532
|
+
/*
|
|
533
|
+
* Normalize a selected shared fragment once, retaining partial fields and
|
|
534
|
+
* notifying observers only when its stored value actually changes.
|
|
535
|
+
*/
|
|
536
|
+
function writeSelectedFragment(
|
|
537
|
+
data: any,
|
|
538
|
+
name: string,
|
|
539
|
+
selections: any[],
|
|
540
|
+
definitions: Record<string, any>,
|
|
541
|
+
updatedKeys: string[],
|
|
542
|
+
scope?: string,
|
|
543
|
+
) {
|
|
544
|
+
const key = makeFragmentCacheKey(PARTIALS_MAP[name] || name, data, scope);
|
|
545
|
+
if (RULES[name] === false) return key;
|
|
546
|
+
const current = loadFragment(key);
|
|
547
|
+
const version = FRAGMENTS.version(key);
|
|
548
|
+
// A field written by a realtime change after this request began is newer than the response's value
|
|
549
|
+
const incoming = keepNewerRealtimeFields(
|
|
550
|
+
key,
|
|
551
|
+
getPartialFragmentMergeData(name, extractFragment(data, selections)),
|
|
552
|
+
current,
|
|
553
|
+
ACTIVE_WRITE_STARTED_AT,
|
|
554
|
+
);
|
|
555
|
+
FRAGMENTS.set(
|
|
556
|
+
key,
|
|
557
|
+
mergeGraphQLObjectData(
|
|
558
|
+
removeOptimisticPlaceholdersFromArrayFields(extractFragment(current, selections)),
|
|
559
|
+
reconcileAIChatCacheSnapshot(key, current, incoming, loadFragment),
|
|
560
|
+
definitions[name]?.selectionSet?.selections,
|
|
561
|
+
),
|
|
562
|
+
);
|
|
563
|
+
if (FRAGMENTS.version(key) !== version) updatedKeys.push(key);
|
|
564
|
+
return key;
|
|
565
|
+
}
|
|
566
|
+
|
|
712
567
|
/**
|
|
713
568
|
* Map each inner selection under a graphql query object
|
|
714
569
|
*/
|
|
@@ -718,33 +573,23 @@ function mapSelections(
|
|
|
718
573
|
innerSelections: any[],
|
|
719
574
|
updatedKeys: string[] = [],
|
|
720
575
|
fallbackScope?: string,
|
|
721
|
-
|
|
576
|
+
fragmentDefinitions: Record<string, any> = {},
|
|
577
|
+
): any[] | null {
|
|
578
|
+
if (data === null) return null;
|
|
722
579
|
return innerSelections
|
|
723
|
-
.
|
|
724
|
-
|
|
725
|
-
|
|
580
|
+
.flatMap((inner) => {
|
|
581
|
+
if (inner.kind === 'InlineFragment') {
|
|
582
|
+
return inlineFragmentMatchesCachedType(data, inner)
|
|
583
|
+
? mapSelections(data, inner.selectionSet.selections, updatedKeys, fallbackScope, fragmentDefinitions) || []
|
|
584
|
+
: [];
|
|
585
|
+
}
|
|
726
586
|
if (inner.kind === 'FragmentSpread') {
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
sourceFragmentName = frgName;
|
|
730
|
-
const frgMap = PARTIALS_MAP[frgName];
|
|
731
|
-
|
|
732
|
-
fragmentKey = makeFragmentCacheKey(
|
|
733
|
-
frgMap || frgName,
|
|
734
|
-
data,
|
|
735
|
-
fallbackScope,
|
|
736
|
-
);
|
|
737
|
-
} else if (
|
|
738
|
-
inner.kind === 'Field' &&
|
|
739
|
-
inner.selectionSet &&
|
|
740
|
-
inner.selectionSet.selections?.length
|
|
741
|
-
) {
|
|
587
|
+
return [writeSelectedFragment(data, inner.name.value, innerSelections, fragmentDefinitions, updatedKeys, fallbackScope)];
|
|
588
|
+
} else if (inner.kind === 'Field' && inner.selectionSet && inner.selectionSet.selections?.length) {
|
|
742
589
|
// These are nested fragments, they need to be cached as a fragment
|
|
743
590
|
|
|
744
|
-
const innerData = data?.[inner
|
|
745
|
-
const innerSelections = inner.selectionSet.selections?.filter(
|
|
746
|
-
(sel: any) => sel.kind === 'FragmentSpread',
|
|
747
|
-
);
|
|
591
|
+
const innerData = data?.[getGraphQLSelectionResponseName(inner)];
|
|
592
|
+
const innerSelections = inner.selectionSet.selections?.filter((sel: any) => sel.kind === 'FragmentSpread');
|
|
748
593
|
|
|
749
594
|
if (innerData && Array.isArray(innerSelections)) {
|
|
750
595
|
if (Array.isArray(innerData)) {
|
|
@@ -755,6 +600,7 @@ function mapSelections(
|
|
|
755
600
|
inner.selectionSet.selections,
|
|
756
601
|
updatedKeys,
|
|
757
602
|
`${parentScope}.${inner.name.value}.${itemIndex}`,
|
|
603
|
+
fragmentDefinitions,
|
|
758
604
|
);
|
|
759
605
|
}
|
|
760
606
|
} else {
|
|
@@ -763,64 +609,15 @@ function mapSelections(
|
|
|
763
609
|
inner.selectionSet.selections,
|
|
764
610
|
updatedKeys,
|
|
765
611
|
`${getFragmentCacheScope(data, fallbackScope)}.${inner.name.value}`,
|
|
612
|
+
fragmentDefinitions,
|
|
766
613
|
);
|
|
767
614
|
}
|
|
768
615
|
}
|
|
769
|
-
} else if (inner.kind === 'InlineFragment') {
|
|
770
|
-
// This is Union, etc
|
|
771
|
-
const frgNamesJoined = inner.selectionSet.selections
|
|
772
|
-
.map((sel: any) => PARTIALS_MAP[sel.name.value] || sel.name.value)
|
|
773
|
-
.join('.');
|
|
774
|
-
|
|
775
|
-
if (
|
|
776
|
-
frgNamesJoined.toLowerCase().indexOf(data.__typename.toLowerCase()) <
|
|
777
|
-
0
|
|
778
|
-
) {
|
|
779
|
-
// This is untested, but should be fine
|
|
780
|
-
return null;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
fragmentKey = makeFragmentCacheKey(frgNamesJoined, data, fallbackScope);
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
if (fragmentKey) {
|
|
787
|
-
// Whether fragment is partial or not, always merge data;
|
|
788
|
-
// Because partials might have data that's not in the main fragment
|
|
789
|
-
const currentData = loadFragment(fragmentKey);
|
|
790
|
-
|
|
791
|
-
// console.log(fragmentKey);
|
|
792
|
-
// if (fragmentKey.startsWith('$fieldDocFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
793
|
-
// console.log('>>>>');
|
|
794
|
-
// console.log('>>>>');
|
|
795
|
-
// console.log('>>>>');
|
|
796
|
-
// console.log('>>>>');
|
|
797
|
-
// console.log(currentData);
|
|
798
|
-
// console.log(data);
|
|
799
|
-
// console.log(extractFragment(data, innerSelections));
|
|
800
|
-
// }
|
|
801
|
-
|
|
802
|
-
const fragmentFromData = getPartialFragmentMergeData(
|
|
803
|
-
sourceFragmentName,
|
|
804
|
-
extractFragment(data, innerSelections),
|
|
805
|
-
);
|
|
806
|
-
FRAGMENTS.set(
|
|
807
|
-
fragmentKey,
|
|
808
|
-
mergeNestedObjects(
|
|
809
|
-
removeOptimisticUIObjectsFromArrayFields(currentData),
|
|
810
|
-
fragmentFromData,
|
|
811
|
-
),
|
|
812
|
-
);
|
|
813
|
-
updatedKeys.push(fragmentKey);
|
|
814
|
-
|
|
815
|
-
return fragmentKey;
|
|
816
616
|
}
|
|
817
617
|
|
|
818
618
|
// These are non-fragment fields
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
// name: inner.name.value,
|
|
822
|
-
// value: data[key]?.[inner.name.value]
|
|
823
|
-
// };
|
|
619
|
+
const name = getGraphQLSelectionResponseName(inner);
|
|
620
|
+
return data?.[name] === undefined ? [] : [[name, data[name]]];
|
|
824
621
|
})
|
|
825
622
|
.filter(Boolean);
|
|
826
623
|
}
|
|
@@ -834,14 +631,33 @@ function makeCacheObject(
|
|
|
834
631
|
selections: any[],
|
|
835
632
|
variables?: any,
|
|
836
633
|
cacheMap?: any,
|
|
837
|
-
|
|
634
|
+
fragmentDefinitions: Record<string, any> = {},
|
|
635
|
+
): [Record<string, any> | null, string[]] {
|
|
636
|
+
if (data === null) return [null, []];
|
|
838
637
|
const cacheObj: Record<string, any> = {};
|
|
839
638
|
const updatedKeys: string[] = [];
|
|
840
639
|
|
|
841
640
|
for (const sel of selections) {
|
|
842
|
-
|
|
641
|
+
if (sel.kind === 'InlineFragment') {
|
|
642
|
+
const typeName = sel.typeCondition?.name?.value;
|
|
643
|
+
if (typeName && data?.__typename !== typeName) {
|
|
644
|
+
continue;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const [inlineCacheObj, inlineUpdatedKeys] = makeCacheObject(
|
|
648
|
+
data,
|
|
649
|
+
sel.selectionSet?.selections || [],
|
|
650
|
+
variables,
|
|
651
|
+
cacheMap,
|
|
652
|
+
fragmentDefinitions,
|
|
653
|
+
);
|
|
654
|
+
Object.assign(cacheObj, inlineCacheObj);
|
|
655
|
+
updatedKeys.push(...inlineUpdatedKeys);
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
const key = getGraphQLSelectionResponseName(sel);
|
|
843
660
|
if (data[key] === undefined && sel.kind !== 'FragmentSpread') {
|
|
844
|
-
// console.log('~~~', key, sel.kind);
|
|
845
661
|
continue;
|
|
846
662
|
}
|
|
847
663
|
|
|
@@ -849,29 +665,19 @@ function makeCacheObject(
|
|
|
849
665
|
case 'Field':
|
|
850
666
|
{
|
|
851
667
|
const innerSelections = sel.selectionSet?.selections;
|
|
852
|
-
if (innerSelections) {
|
|
668
|
+
if (innerSelections && data[key] !== null) {
|
|
853
669
|
if (Array.isArray(data[key])) {
|
|
854
670
|
cacheObj[key] = {
|
|
855
671
|
__cache: true,
|
|
856
672
|
__list: true,
|
|
857
673
|
data: data[key].map((item, itemIndex) =>
|
|
858
|
-
mapSelections(
|
|
859
|
-
item,
|
|
860
|
-
innerSelections,
|
|
861
|
-
updatedKeys,
|
|
862
|
-
`${key}.${itemIndex}`,
|
|
863
|
-
),
|
|
674
|
+
mapSelections(item, innerSelections, updatedKeys, `${key}.${itemIndex}`, fragmentDefinitions),
|
|
864
675
|
),
|
|
865
676
|
};
|
|
866
677
|
} else {
|
|
867
678
|
cacheObj[key] = {
|
|
868
679
|
__cache: true,
|
|
869
|
-
data: mapSelections(
|
|
870
|
-
data[key],
|
|
871
|
-
innerSelections,
|
|
872
|
-
updatedKeys,
|
|
873
|
-
key,
|
|
874
|
-
),
|
|
680
|
+
data: mapSelections(data[key], innerSelections, updatedKeys, key, fragmentDefinitions),
|
|
875
681
|
};
|
|
876
682
|
}
|
|
877
683
|
} else {
|
|
@@ -879,53 +685,11 @@ function makeCacheObject(
|
|
|
879
685
|
}
|
|
880
686
|
}
|
|
881
687
|
break;
|
|
882
|
-
case 'FragmentSpread':
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
const frgMap = PARTIALS_MAP[frgName];
|
|
886
|
-
const fragmentKey = makeFragmentCacheKey(frgMap || frgName, data);
|
|
887
|
-
|
|
888
|
-
if (RULES[frgName] !== false) {
|
|
889
|
-
// if (testMode) {
|
|
890
|
-
// console.log('Array.isArray(data)');
|
|
891
|
-
// console.log(Array.isArray(data));
|
|
892
|
-
// console.log(data);
|
|
893
|
-
// console.log(extractFragment(data, selections));
|
|
894
|
-
// }
|
|
895
|
-
|
|
896
|
-
// Whether fragment is partial or not, always merge data;
|
|
897
|
-
// Because partials might have data that's not in the main fragment
|
|
898
|
-
const currentData = loadFragment(fragmentKey);
|
|
899
|
-
|
|
900
|
-
// if (fragmentKey.startsWith('$fieldDocFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
901
|
-
// console.log('>>>>');
|
|
902
|
-
// console.log('>>>>');
|
|
903
|
-
// console.log('>>>>');
|
|
904
|
-
// console.log('>>>>');
|
|
905
|
-
// console.log(currentData);
|
|
906
|
-
// console.log(data);
|
|
907
|
-
// }
|
|
908
|
-
|
|
909
|
-
// console.log('>>>>', fragmentKey);
|
|
910
|
-
FRAGMENTS.set(
|
|
911
|
-
fragmentKey,
|
|
912
|
-
mergeNestedObjects(
|
|
913
|
-
removeOptimisticUIObjectsFromArrayFields(currentData),
|
|
914
|
-
getPartialFragmentMergeData(
|
|
915
|
-
frgName,
|
|
916
|
-
extractFragment(data, selections),
|
|
917
|
-
),
|
|
918
|
-
),
|
|
919
|
-
);
|
|
920
|
-
updatedKeys.push(fragmentKey);
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
cacheObj.__flat = {
|
|
924
|
-
__cache: true,
|
|
925
|
-
data: [fragmentKey],
|
|
926
|
-
};
|
|
927
|
-
}
|
|
688
|
+
case 'FragmentSpread': {
|
|
689
|
+
const fragmentKey = writeSelectedFragment(data, sel.name.value, selections, fragmentDefinitions, updatedKeys);
|
|
690
|
+
cacheObj.__flat = { __cache: true, data: [fragmentKey] };
|
|
928
691
|
break;
|
|
692
|
+
}
|
|
929
693
|
default:
|
|
930
694
|
}
|
|
931
695
|
|
|
@@ -935,15 +699,8 @@ function makeCacheObject(
|
|
|
935
699
|
// so cacheMap has to handle the cache object itself
|
|
936
700
|
|
|
937
701
|
if (cacheMap?.[key]) {
|
|
938
|
-
const customCacheData = cacheMap[key](
|
|
939
|
-
|
|
940
|
-
cacheObj[key],
|
|
941
|
-
sel,
|
|
942
|
-
variables,
|
|
943
|
-
);
|
|
944
|
-
cacheObj[key] =
|
|
945
|
-
typeof customCacheData === 'undefined' ? null : customCacheData;
|
|
946
|
-
continue;
|
|
702
|
+
const customCacheData = cacheMap[key](data[key], cacheObj[key], sel, variables);
|
|
703
|
+
cacheObj[key] = typeof customCacheData === 'undefined' ? null : customCacheData;
|
|
947
704
|
}
|
|
948
705
|
}
|
|
949
706
|
|
|
@@ -952,10 +709,7 @@ function makeCacheObject(
|
|
|
952
709
|
|
|
953
710
|
let spreads: Record<string, any> | undefined;
|
|
954
711
|
for (const cKey in cacheObj) {
|
|
955
|
-
if (
|
|
956
|
-
cKey !== '__flat' &&
|
|
957
|
-
Object.prototype.hasOwnProperty.call(cacheObj, cKey)
|
|
958
|
-
) {
|
|
712
|
+
if (cKey !== '__flat' && Object.hasOwn(cacheObj, cKey)) {
|
|
959
713
|
if (!spreads) {
|
|
960
714
|
spreads = {};
|
|
961
715
|
}
|
|
@@ -963,12 +717,16 @@ function makeCacheObject(
|
|
|
963
717
|
// Spreads here
|
|
964
718
|
|
|
965
719
|
if (
|
|
966
|
-
cacheObj[cKey]
|
|
967
|
-
|
|
720
|
+
cacheObj[cKey] === null &&
|
|
721
|
+
selections.some(
|
|
722
|
+
(selection) => selection.kind === 'Field' && getGraphQLSelectionResponseName(selection) === cKey && selection.selectionSet,
|
|
723
|
+
)
|
|
968
724
|
) {
|
|
725
|
+
spreads[cKey] = null;
|
|
726
|
+
} else if (cacheObj[cKey]?.data?.length === 1 && typeof cacheObj[cKey]?.data?.[0] === 'string') {
|
|
969
727
|
// Single spread with single fragment
|
|
970
728
|
spreads[cKey] = cacheObj[cKey].data[0];
|
|
971
|
-
} else if (cacheObj[cKey]
|
|
729
|
+
} else if (cacheObj[cKey]?.__list === true) {
|
|
972
730
|
// List of spreads
|
|
973
731
|
spreads[cKey] = {
|
|
974
732
|
__list: true,
|
|
@@ -982,13 +740,10 @@ function makeCacheObject(
|
|
|
982
740
|
}
|
|
983
741
|
|
|
984
742
|
if (spreads) {
|
|
985
|
-
// if (spreadsFragmentKey.startsWith('$emailFragment:')) {
|
|
986
|
-
// console.log('>>>>', spreadsFragmentKey);
|
|
987
|
-
// console.log(data);
|
|
988
|
-
// console.log(spreads);
|
|
989
|
-
// }
|
|
990
743
|
const currentSpreads = FRAGMENTS.get(spreadsFragmentKey);
|
|
744
|
+
const version = FRAGMENTS.version(spreadsFragmentKey);
|
|
991
745
|
FRAGMENTS.set(spreadsFragmentKey, mergeSpreads(currentSpreads, spreads));
|
|
746
|
+
if (FRAGMENTS.version(spreadsFragmentKey) !== version) updatedKeys.push(cacheObj.__flat.data[0]);
|
|
992
747
|
}
|
|
993
748
|
}
|
|
994
749
|
|
|
@@ -1007,107 +762,45 @@ function saveQueryToMemory(
|
|
|
1007
762
|
cacheMap?: any,
|
|
1008
763
|
saveQueryToCache?: boolean,
|
|
1009
764
|
updateObservers?: UpdateObserversFn,
|
|
765
|
+
markFresh = true,
|
|
766
|
+
fragmentDefinitions: Record<string, any> = {},
|
|
1010
767
|
) {
|
|
1011
|
-
const
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
}
|
|
768
|
+
const name = getGraphQLSelectionResponseName(selection);
|
|
769
|
+
const data = requestData?.[name];
|
|
770
|
+
if (data === undefined) return;
|
|
1015
771
|
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
const
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
772
|
+
const queryId = `#${name}:${variablesKey}`;
|
|
773
|
+
const selections = selection.selectionSet?.selections;
|
|
774
|
+
const customMap = typeof cacheMap === 'function';
|
|
775
|
+
const fieldMap = customMap ? null : cacheMap;
|
|
776
|
+
let cacheData: any;
|
|
777
|
+
let updatedFragmentIds: string[];
|
|
778
|
+
if (data === null || !selections) {
|
|
779
|
+
cacheData = data;
|
|
780
|
+
updatedFragmentIds = [];
|
|
781
|
+
} else if (Array.isArray(data)) {
|
|
782
|
+
const normalized = data.map((item) => makeCacheObject(item, selections, variables, fieldMap, fragmentDefinitions));
|
|
783
|
+
cacheData = normalized.map(([item]) => item);
|
|
784
|
+
updatedFragmentIds = normalized.flatMap(([, keys]) => keys);
|
|
785
|
+
} else {
|
|
786
|
+
[cacheData, updatedFragmentIds] = makeCacheObject(data, selections, variables, fieldMap, fragmentDefinitions);
|
|
1025
787
|
}
|
|
1026
788
|
|
|
1027
|
-
if (
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
//
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
const cacheMapObj = typeof cacheMap === 'function' ? null : cacheMap;
|
|
1035
|
-
|
|
1036
|
-
let cacheData;
|
|
1037
|
-
let updatedFragmentIds;
|
|
1038
|
-
if (Array.isArray(data)) {
|
|
1039
|
-
const result = data.map((dataObj) =>
|
|
1040
|
-
makeCacheObject(dataObj, selections, variables, cacheMapObj),
|
|
1041
|
-
);
|
|
1042
|
-
cacheData = result.map((r) => r[0]);
|
|
1043
|
-
updatedFragmentIds = result.reduce((acc, r) => acc.concat(r[1]), []);
|
|
1044
|
-
} else {
|
|
1045
|
-
[cacheData, updatedFragmentIds] = makeCacheObject(
|
|
1046
|
-
data,
|
|
1047
|
-
selections,
|
|
1048
|
-
variables,
|
|
1049
|
-
cacheMapObj,
|
|
1050
|
-
);
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
// console.log(updatedFragmentIds);
|
|
1054
|
-
// console.log(cacheData);
|
|
1055
|
-
|
|
1056
|
-
if (typeof cacheMap === 'function') {
|
|
1057
|
-
// cacheMap[key](data[key], cacheObj[key], sel, variables);
|
|
1058
|
-
if (saveQueryToCache) {
|
|
1059
|
-
const customCacheData = cacheMap(
|
|
1060
|
-
data,
|
|
1061
|
-
cacheData,
|
|
1062
|
-
selections,
|
|
1063
|
-
variables,
|
|
1064
|
-
);
|
|
1065
|
-
// console.log('1--->', queryId);
|
|
1066
|
-
// console.log('UPDATING (1):', typeof customCacheData === 'undefined' ? null : customCacheData);
|
|
1067
|
-
QUERIES.set(
|
|
1068
|
-
queryId,
|
|
1069
|
-
typeof customCacheData === 'undefined' ? null : customCacheData,
|
|
1070
|
-
);
|
|
1071
|
-
cacheStore.setQueryFetchTime(queryId, Date.now());
|
|
1072
|
-
|
|
1073
|
-
if (updateObservers) {
|
|
1074
|
-
updateObservers({
|
|
1075
|
-
queryId,
|
|
1076
|
-
});
|
|
1077
|
-
}
|
|
1078
|
-
}
|
|
1079
|
-
} else {
|
|
1080
|
-
if (saveQueryToCache) {
|
|
1081
|
-
// console.log('2--->', queryId);
|
|
1082
|
-
// console.log('2--->', cacheData);
|
|
1083
|
-
// console.log('UPDATING (2):', cacheData);
|
|
1084
|
-
QUERIES.set(queryId, cacheData);
|
|
1085
|
-
cacheStore.setQueryFetchTime(queryId, Date.now());
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
|
-
if (updateObservers) {
|
|
1089
|
-
updateObservers({
|
|
1090
|
-
fragmentIds: updatedFragmentIds as string[],
|
|
1091
|
-
});
|
|
1092
|
-
}
|
|
1093
|
-
|
|
1094
|
-
// I don't think observer is needed for custom cache maps, but if it does, bring it up
|
|
1095
|
-
// FRAGMENT_OBSERVER.value = {
|
|
1096
|
-
// count: FRAGMENT_OBSERVER.value.count + 1,
|
|
1097
|
-
// list: updatedFragmentIds,
|
|
1098
|
-
// };
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
return updatedFragmentIds;
|
|
789
|
+
if (saveQueryToCache) {
|
|
790
|
+
const mapped = customMap ? cacheMap(data, cacheData, selections, variables) : cacheData;
|
|
791
|
+
QUERIES.set(queryId, mapped === undefined ? null : mapped);
|
|
792
|
+
// A response read during a realtime gap must not count as fetched after the gap ended
|
|
793
|
+
if (markFresh) cacheStore.setQueryFetchTime(queryId, ACTIVE_WRITE_STARTED_AT || Date.now());
|
|
794
|
+
else cacheStore.clearQueryFetchTime(queryId);
|
|
795
|
+
if (customMap) updateObservers?.({ queryId });
|
|
1102
796
|
}
|
|
797
|
+
if (!customMap && updatedFragmentIds.length) updateObservers?.({ fragmentIds: [...new Set(updatedFragmentIds)] });
|
|
798
|
+
return updatedFragmentIds;
|
|
1103
799
|
}
|
|
1104
800
|
|
|
1105
801
|
/**
|
|
1106
|
-
*
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
/**
|
|
1110
|
-
* Save cache
|
|
802
|
+
* Save cache. requestStartedAt is when the network request that produced
|
|
803
|
+
* requestData began; fields a realtime change wrote after it are kept.
|
|
1111
804
|
*/
|
|
1112
805
|
|
|
1113
806
|
export function setDataToCache(
|
|
@@ -1116,26 +809,38 @@ export function setDataToCache(
|
|
|
1116
809
|
variables?: any,
|
|
1117
810
|
cacheMap?: any,
|
|
1118
811
|
updateObservers?: UpdateObserversFn,
|
|
812
|
+
markFresh = true,
|
|
813
|
+
requestStartedAt = 0,
|
|
1119
814
|
) {
|
|
1120
815
|
// query[variablesKey] = cache;
|
|
1121
816
|
const definitions = gqlQuery.definitions;
|
|
1122
|
-
const variablesKey =
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
817
|
+
const variablesKey = makeGraphQLVariablesKey(variables);
|
|
818
|
+
const fragmentDefinitions = getFragmentDefinitions(gqlQuery);
|
|
819
|
+
const outerWriteStartedAt = ACTIVE_WRITE_STARTED_AT;
|
|
820
|
+
ACTIVE_WRITE_STARTED_AT = requestStartedAt;
|
|
821
|
+
|
|
822
|
+
try {
|
|
823
|
+
for (const def of definitions) {
|
|
824
|
+
if (def.kind === 'OperationDefinition') {
|
|
825
|
+
def?.selectionSet?.selections.forEach((selection: any) => {
|
|
826
|
+
const qryName = selection.name?.value;
|
|
827
|
+
saveQueryToMemory(
|
|
828
|
+
requestData,
|
|
829
|
+
selection,
|
|
830
|
+
variables,
|
|
831
|
+
variablesKey,
|
|
832
|
+
cacheMap,
|
|
833
|
+
def.operation !== 'mutation' && RULES[qryName] !== false,
|
|
834
|
+
updateObservers,
|
|
835
|
+
markFresh,
|
|
836
|
+
fragmentDefinitions,
|
|
837
|
+
);
|
|
838
|
+
});
|
|
839
|
+
}
|
|
1138
840
|
}
|
|
841
|
+
} finally {
|
|
842
|
+
// The start time applies only to this synchronous write; restore the enclosing write's (0 when not nested)
|
|
843
|
+
ACTIVE_WRITE_STARTED_AT = outerWriteStartedAt;
|
|
1139
844
|
}
|
|
1140
845
|
}
|
|
1141
846
|
|
|
@@ -1143,12 +848,10 @@ export function setDataToCache(
|
|
|
1143
848
|
* Append fragment to cache
|
|
1144
849
|
*/
|
|
1145
850
|
|
|
1146
|
-
export function appendFragmentToCache(
|
|
1147
|
-
cacheData
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
) {
|
|
1151
|
-
let newObj = cloneArrayLike(cacheData);
|
|
851
|
+
export function appendFragmentToCache(cacheData: any, _testMode?: boolean, queryVersion = 0): any {
|
|
852
|
+
if (cacheData === null || typeof cacheData !== 'object') return cacheData;
|
|
853
|
+
if (Array.isArray(cacheData)) return cacheData.map((item) => appendFragmentToCache(item, false, queryVersion));
|
|
854
|
+
const newObj = { ...cacheData };
|
|
1152
855
|
let flatObj: any;
|
|
1153
856
|
const flatSpreadKeys = new Set<string>();
|
|
1154
857
|
|
|
@@ -1160,16 +863,7 @@ export function appendFragmentToCache(
|
|
|
1160
863
|
const queryListKeys = new Set<string>();
|
|
1161
864
|
let flatSpreadVersion = 0;
|
|
1162
865
|
|
|
1163
|
-
// if (testMode) {
|
|
1164
|
-
// console.log('appendFragmentToCache:', newObj);
|
|
1165
|
-
// }
|
|
1166
|
-
|
|
1167
866
|
for (const key in newObj) {
|
|
1168
|
-
// if (testMode) {
|
|
1169
|
-
// console.log('******* key:', key);
|
|
1170
|
-
// console.log('******* ??:', newObj[key]);
|
|
1171
|
-
// }
|
|
1172
|
-
|
|
1173
867
|
if (newObj[key] && typeof newObj[key] === 'object') {
|
|
1174
868
|
const isFlat = key === '__flat';
|
|
1175
869
|
const isCache = newObj[key].__cache && newObj[key].data;
|
|
@@ -1185,7 +879,7 @@ export function appendFragmentToCache(
|
|
|
1185
879
|
|
|
1186
880
|
cachedObj = newObj[key].data.map((di: any[]) => {
|
|
1187
881
|
const item = mapCachedListItem(di, loadFragment);
|
|
1188
|
-
if (
|
|
882
|
+
if (item === undefined) {
|
|
1189
883
|
cacheIsValid = false;
|
|
1190
884
|
return {};
|
|
1191
885
|
}
|
|
@@ -1194,13 +888,6 @@ export function appendFragmentToCache(
|
|
|
1194
888
|
});
|
|
1195
889
|
} else {
|
|
1196
890
|
newObj[key].data.forEach((d: any) => {
|
|
1197
|
-
// if (testMode) {
|
|
1198
|
-
// console.log('cachedObj', cachedObj);
|
|
1199
|
-
// console.log('key', key);
|
|
1200
|
-
// console.log('d', d);
|
|
1201
|
-
// console.log('d', typeof d);
|
|
1202
|
-
// }
|
|
1203
|
-
|
|
1204
891
|
if (Array.isArray(d)) {
|
|
1205
892
|
cachedObj[d[0]] = d[1];
|
|
1206
893
|
} else if (typeof d === 'string') {
|
|
@@ -1208,7 +895,7 @@ export function appendFragmentToCache(
|
|
|
1208
895
|
const frgName = d.substring(1, frgIndex);
|
|
1209
896
|
const frgMap = PARTIALS_MAP[frgName];
|
|
1210
897
|
|
|
1211
|
-
let fragmentKey;
|
|
898
|
+
let fragmentKey: string;
|
|
1212
899
|
if (frgMap) {
|
|
1213
900
|
fragmentKey = `$${frgMap}:${d.substring(frgIndex + 1)}`;
|
|
1214
901
|
} else {
|
|
@@ -1219,27 +906,14 @@ export function appendFragmentToCache(
|
|
|
1219
906
|
|
|
1220
907
|
if (cachedFragment) {
|
|
1221
908
|
if (isFlat) {
|
|
1222
|
-
// if (testMode) {
|
|
1223
|
-
// console.log('///////');
|
|
1224
|
-
// console.log('///////');
|
|
1225
|
-
// console.log('newObj before:', newObj);
|
|
1226
|
-
// console.log('fragmentKey:', fragmentKey);
|
|
1227
|
-
// console.log('cachedFragment:', cachedFragment);
|
|
1228
|
-
// }
|
|
1229
|
-
|
|
1230
909
|
// newObj = {
|
|
1231
910
|
// ...newObj,
|
|
1232
911
|
// ...cachedFragment,
|
|
1233
912
|
// };
|
|
1234
913
|
|
|
1235
914
|
flatObj = cachedFragment;
|
|
1236
|
-
flatSpreadVersion = Math.max(
|
|
1237
|
-
|
|
1238
|
-
FRAGMENTS.version(`${fragmentKey}.spreads`),
|
|
1239
|
-
);
|
|
1240
|
-
Object.keys(
|
|
1241
|
-
FRAGMENTS.get(`${fragmentKey}.spreads`) || {},
|
|
1242
|
-
).forEach((spreadKey) => {
|
|
915
|
+
flatSpreadVersion = Math.max(FRAGMENTS.version(fragmentKey), FRAGMENTS.version(`${fragmentKey}.spreads`));
|
|
916
|
+
Object.keys(FRAGMENTS.get(`${fragmentKey}.spreads`) || {}).forEach((spreadKey) => {
|
|
1243
917
|
flatSpreadKeys.add(spreadKey);
|
|
1244
918
|
});
|
|
1245
919
|
delete newObj.__flat;
|
|
@@ -1258,7 +932,7 @@ export function appendFragmentToCache(
|
|
|
1258
932
|
}
|
|
1259
933
|
|
|
1260
934
|
if (!cacheIsValid) {
|
|
1261
|
-
return
|
|
935
|
+
return undefined;
|
|
1262
936
|
}
|
|
1263
937
|
|
|
1264
938
|
if (!isFlat) {
|
|
@@ -1269,18 +943,9 @@ export function appendFragmentToCache(
|
|
|
1269
943
|
// So we have to get the non-spread fields from query cache
|
|
1270
944
|
|
|
1271
945
|
const cacheInfo = cacheData[key];
|
|
1272
|
-
if (
|
|
1273
|
-
newObj[key] &&
|
|
1274
|
-
typeof newObj[key] === 'object' &&
|
|
1275
|
-
cacheInfo?.__cache &&
|
|
1276
|
-
cacheInfo.data
|
|
1277
|
-
) {
|
|
946
|
+
if (newObj[key] && typeof newObj[key] === 'object' && cacheInfo?.__cache && cacheInfo.data) {
|
|
1278
947
|
cacheInfo.data.forEach((di: any) => {
|
|
1279
|
-
if (
|
|
1280
|
-
Array.isArray(di) &&
|
|
1281
|
-
di.length === 2 &&
|
|
1282
|
-
!di?.[0]?.startsWith('$')
|
|
1283
|
-
) {
|
|
948
|
+
if (Array.isArray(di) && di.length === 2 && !di?.[0]?.startsWith('$')) {
|
|
1284
949
|
newObj[key][di[0]] = di[1];
|
|
1285
950
|
}
|
|
1286
951
|
});
|
|
@@ -1293,15 +958,12 @@ export function appendFragmentToCache(
|
|
|
1293
958
|
|
|
1294
959
|
if (flatObj) {
|
|
1295
960
|
for (const key in flatObj) {
|
|
1296
|
-
// console.log('FLAT:', key, flatObj[key], newObj[key]);
|
|
1297
961
|
// Spread-backed fields can be refreshed by mutation writes even when the query cache is stale.
|
|
1298
962
|
// A query-cache list is the one contested case: it holds every item appended
|
|
1299
963
|
// since the fragment was written, so the flat copy may only replace it when
|
|
1300
964
|
// that copy is the newer write. Otherwise the list truncates back to
|
|
1301
965
|
// whatever the last full response happened to contain.
|
|
1302
|
-
const flatCopyWins =
|
|
1303
|
-
flatSpreadKeys.has(key) &&
|
|
1304
|
-
(!queryListKeys.has(key) || flatSpreadVersion > queryVersion);
|
|
966
|
+
const flatCopyWins = flatSpreadKeys.has(key) && (!queryListKeys.has(key) || flatSpreadVersion > queryVersion);
|
|
1305
967
|
|
|
1306
968
|
if (newObj[key] === undefined || flatCopyWins) {
|
|
1307
969
|
newObj[key] = flatObj[key];
|
|
@@ -1332,40 +994,13 @@ export function loadDataFromCache(
|
|
|
1332
994
|
if (def.kind === 'OperationDefinition') {
|
|
1333
995
|
def?.selectionSet?.selections.forEach((selection: any) => {
|
|
1334
996
|
const qryName = getGraphQLSelectionResponseName(selection);
|
|
1335
|
-
if (
|
|
997
|
+
if (data[qryName] === undefined && allowQueryCacheFallback) {
|
|
1336
998
|
const cacheKey = `#${qryName}:${variablesKey}`;
|
|
1337
999
|
const cachedData = loadQuery(cacheKey);
|
|
1338
1000
|
|
|
1339
|
-
if (cachedData) {
|
|
1001
|
+
if (cachedData !== undefined && QUERIES.has(cacheKey)) {
|
|
1340
1002
|
data[qryName] = cachedData;
|
|
1341
1003
|
}
|
|
1342
|
-
|
|
1343
|
-
// const cacheData = QUERIES.get(cacheKey);
|
|
1344
|
-
|
|
1345
|
-
// if (Array.isArray(cacheData)) {
|
|
1346
|
-
// let hasInvalidatedCache = false;
|
|
1347
|
-
// const mappedCache = cacheData.map((cacheObj, i) => {
|
|
1348
|
-
// if (!hasInvalidatedCache) {
|
|
1349
|
-
// const singleCache = appendFragmentToCache(cacheObj);
|
|
1350
|
-
|
|
1351
|
-
// if (singleCache) {
|
|
1352
|
-
// return singleCache;
|
|
1353
|
-
// } else {
|
|
1354
|
-
// hasInvalidatedCache = true;
|
|
1355
|
-
// }
|
|
1356
|
-
// }
|
|
1357
|
-
// });
|
|
1358
|
-
|
|
1359
|
-
// if (!hasInvalidatedCache) {
|
|
1360
|
-
// data[qryName] = mappedCache;
|
|
1361
|
-
// }
|
|
1362
|
-
// } else if (cacheData) {
|
|
1363
|
-
// const singleCacheData = appendFragmentToCache(cacheData);
|
|
1364
|
-
|
|
1365
|
-
// if (singleCacheData) {
|
|
1366
|
-
// data[qryName] = singleCacheData;
|
|
1367
|
-
// }
|
|
1368
|
-
// }
|
|
1369
1004
|
}
|
|
1370
1005
|
|
|
1371
1006
|
// Set value to "true" because this is meant to be handled by a cacheMap and its standalone logic
|
|
@@ -1375,11 +1010,7 @@ export function loadDataFromCache(
|
|
|
1375
1010
|
data[qryName] = true;
|
|
1376
1011
|
} else if (cacheMap) {
|
|
1377
1012
|
for (const key in cacheMap) {
|
|
1378
|
-
if (
|
|
1379
|
-
cacheMap?.[key] &&
|
|
1380
|
-
data[qryName]?.[key] !== undefined &&
|
|
1381
|
-
data[qryName][key] !== true
|
|
1382
|
-
) {
|
|
1013
|
+
if (cacheMap?.[key] && data[qryName]?.[key] !== undefined && data[qryName][key] !== true) {
|
|
1383
1014
|
// Clone before stamping; the source object may be a shared
|
|
1384
1015
|
// memoized cache assembly that must stay unmutated.
|
|
1385
1016
|
data[qryName] = {
|
|
@@ -1389,19 +1020,10 @@ export function loadDataFromCache(
|
|
|
1389
1020
|
}
|
|
1390
1021
|
}
|
|
1391
1022
|
}
|
|
1392
|
-
} else if (eagerFragmentKeyMap?.[qryName]) {
|
|
1023
|
+
} else if (data[qryName] === undefined && eagerFragmentKeyMap?.[qryName]) {
|
|
1393
1024
|
const fragmentKey = eagerFragmentKeyMap[qryName];
|
|
1394
1025
|
const cachedFragment = loadFragment(fragmentKey);
|
|
1395
1026
|
|
|
1396
|
-
// if (fragmentKey.startsWith('$fieldDocFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
1397
|
-
// console.log('>>>>');
|
|
1398
|
-
// console.log('>>>>');
|
|
1399
|
-
// console.log('>>>>');
|
|
1400
|
-
// console.log('>>>>');
|
|
1401
|
-
// console.log(cachedFragment);
|
|
1402
|
-
// console.log(data);
|
|
1403
|
-
// }
|
|
1404
|
-
|
|
1405
1027
|
if (cachedFragment) {
|
|
1406
1028
|
data[qryName] = cachedFragment;
|
|
1407
1029
|
}
|
|
@@ -1448,33 +1070,12 @@ export function checkDataCompleteness(requestData: any, gqlQuery: any) {
|
|
|
1448
1070
|
*/
|
|
1449
1071
|
|
|
1450
1072
|
export function removeStaleCache() {
|
|
1451
|
-
// function getByteSize(oMap){
|
|
1452
|
-
// function replacer(key, value) {
|
|
1453
|
-
// if(value instanceof Map) {
|
|
1454
|
-
// return {
|
|
1455
|
-
// dataType: 'Map',
|
|
1456
|
-
// value: [...value],
|
|
1457
|
-
// };
|
|
1458
|
-
// } else {
|
|
1459
|
-
// return value;
|
|
1460
|
-
// }
|
|
1461
|
-
// }
|
|
1462
|
-
// return JSON.stringify(oMap, replacer).length;
|
|
1463
|
-
// }
|
|
1464
|
-
|
|
1465
|
-
// console.log(' Queries cache size (count):', QUERIES.size);
|
|
1466
|
-
// console.log(' Queries cache size (bytes):', getByteSize(QUERIES));
|
|
1467
|
-
// console.log('Fragments cache size (count):', FRAGMENTS.size);
|
|
1468
|
-
// console.log('Fragments cache size (bytes):', getByteSize(FRAGMENTS));
|
|
1469
|
-
|
|
1470
1073
|
if (FRAGMENTS.size > RULES.__limit) {
|
|
1471
1074
|
const numToEvict = FRAGMENTS.size - RULES.__limit;
|
|
1472
1075
|
|
|
1473
1076
|
// Least-recently-accessed fragments go first; a fragment and its spreads
|
|
1474
1077
|
// sidecar are always evicted together.
|
|
1475
|
-
const lruKeys = FRAGMENTS.keysByLeastRecentAccess(
|
|
1476
|
-
(key) => !key.endsWith('.spreads'),
|
|
1477
|
-
);
|
|
1078
|
+
const lruKeys = FRAGMENTS.keysByLeastRecentAccess((key) => !key.endsWith('.spreads'));
|
|
1478
1079
|
|
|
1479
1080
|
let evictedCount = 0;
|
|
1480
1081
|
for (const key of lruKeys) {
|
|
@@ -1496,7 +1097,7 @@ export function removeStaleCache() {
|
|
|
1496
1097
|
* Build a lookup map for fragment definitions in a GraphQL document.
|
|
1497
1098
|
*/
|
|
1498
1099
|
|
|
1499
|
-
function makeFragmentDefinitionMap(definitions: any[]) {
|
|
1100
|
+
function makeFragmentDefinitionMap(definitions: any[]): Record<string, any> {
|
|
1500
1101
|
return definitions.reduce(
|
|
1501
1102
|
(acc, def) => {
|
|
1502
1103
|
if (def.kind === 'FragmentDefinition' && def.name?.value) {
|
|
@@ -1509,6 +1110,16 @@ function makeFragmentDefinitionMap(definitions: any[]) {
|
|
|
1509
1110
|
);
|
|
1510
1111
|
}
|
|
1511
1112
|
|
|
1113
|
+
/* Return the shared fragment-definition lookup for one parsed document. */
|
|
1114
|
+
function getFragmentDefinitions(query: any): Record<string, any> {
|
|
1115
|
+
let fragments = QUERY_FRAGMENT_DEFINITIONS.get(query);
|
|
1116
|
+
if (!fragments) {
|
|
1117
|
+
fragments = makeFragmentDefinitionMap(query.definitions);
|
|
1118
|
+
QUERY_FRAGMENT_DEFINITIONS.set(query, fragments);
|
|
1119
|
+
}
|
|
1120
|
+
return fragments;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1512
1123
|
/**
|
|
1513
1124
|
* Check whether an inline fragment applies to the current cached object type.
|
|
1514
1125
|
*/
|
|
@@ -1525,11 +1136,7 @@ function inlineFragmentMatchesCachedType(value: any, selection: any) {
|
|
|
1525
1136
|
* Check whether a cached value contains all fields requested by a selection.
|
|
1526
1137
|
*/
|
|
1527
1138
|
|
|
1528
|
-
function hasCompleteSelectionData(
|
|
1529
|
-
value: any,
|
|
1530
|
-
selection: any,
|
|
1531
|
-
fragmentDefinitions: Record<string, any>,
|
|
1532
|
-
): boolean {
|
|
1139
|
+
function hasCompleteSelectionData(value: any, selection: any, fragmentDefinitions: Record<string, any>): boolean {
|
|
1533
1140
|
if (value === null) {
|
|
1534
1141
|
return true;
|
|
1535
1142
|
}
|
|
@@ -1540,13 +1147,7 @@ function hasCompleteSelectionData(
|
|
|
1540
1147
|
|
|
1541
1148
|
if (selection.kind === 'FragmentSpread') {
|
|
1542
1149
|
const fragmentDef = fragmentDefinitions[selection.name?.value];
|
|
1543
|
-
return fragmentDef
|
|
1544
|
-
? hasCompleteSelectionSetData(
|
|
1545
|
-
value,
|
|
1546
|
-
fragmentDef.selectionSet?.selections || [],
|
|
1547
|
-
fragmentDefinitions,
|
|
1548
|
-
)
|
|
1549
|
-
: true;
|
|
1150
|
+
return fragmentDef ? hasCompleteSelectionSetData(value, fragmentDef.selectionSet?.selections || [], fragmentDefinitions) : true;
|
|
1550
1151
|
}
|
|
1551
1152
|
|
|
1552
1153
|
if (selection.kind === 'InlineFragment') {
|
|
@@ -1554,11 +1155,7 @@ function hasCompleteSelectionData(
|
|
|
1554
1155
|
return true;
|
|
1555
1156
|
}
|
|
1556
1157
|
|
|
1557
|
-
return hasCompleteSelectionSetData(
|
|
1558
|
-
value,
|
|
1559
|
-
selection.selectionSet?.selections || [],
|
|
1560
|
-
fragmentDefinitions,
|
|
1561
|
-
);
|
|
1158
|
+
return hasCompleteSelectionSetData(value, selection.selectionSet?.selections || [], fragmentDefinitions);
|
|
1562
1159
|
}
|
|
1563
1160
|
|
|
1564
1161
|
if (selection.kind !== 'Field') {
|
|
@@ -1577,27 +1174,17 @@ function hasCompleteSelectionData(
|
|
|
1577
1174
|
}
|
|
1578
1175
|
|
|
1579
1176
|
if (Array.isArray(fieldValue)) {
|
|
1580
|
-
return fieldValue.every((item) =>
|
|
1581
|
-
hasCompleteSelectionSetData(item, selections, fragmentDefinitions),
|
|
1582
|
-
);
|
|
1177
|
+
return fieldValue.every((item) => hasCompleteSelectionSetData(item, selections, fragmentDefinitions));
|
|
1583
1178
|
}
|
|
1584
1179
|
|
|
1585
|
-
return hasCompleteSelectionSetData(
|
|
1586
|
-
fieldValue,
|
|
1587
|
-
selections,
|
|
1588
|
-
fragmentDefinitions,
|
|
1589
|
-
);
|
|
1180
|
+
return hasCompleteSelectionSetData(fieldValue, selections, fragmentDefinitions);
|
|
1590
1181
|
}
|
|
1591
1182
|
|
|
1592
1183
|
/**
|
|
1593
1184
|
* Check whether a cached object contains all fields requested by a selection set.
|
|
1594
1185
|
*/
|
|
1595
1186
|
|
|
1596
|
-
function hasCompleteSelectionSetData(
|
|
1597
|
-
value: any,
|
|
1598
|
-
selections: any[],
|
|
1599
|
-
fragmentDefinitions: Record<string, any>,
|
|
1600
|
-
): boolean {
|
|
1187
|
+
function hasCompleteSelectionSetData(value: any, selections: any[], fragmentDefinitions: Record<string, any>): boolean {
|
|
1601
1188
|
if (value === null) {
|
|
1602
1189
|
return true;
|
|
1603
1190
|
}
|
|
@@ -1606,64 +1193,35 @@ function hasCompleteSelectionSetData(
|
|
|
1606
1193
|
return false;
|
|
1607
1194
|
}
|
|
1608
1195
|
|
|
1609
|
-
return selections.every((selection) =>
|
|
1610
|
-
hasCompleteSelectionData(value, selection, fragmentDefinitions),
|
|
1611
|
-
);
|
|
1196
|
+
return selections.every((selection) => hasCompleteSelectionData(value, selection, fragmentDefinitions));
|
|
1612
1197
|
}
|
|
1613
1198
|
|
|
1614
1199
|
/**
|
|
1615
1200
|
* Check if there is cache
|
|
1616
1201
|
*/
|
|
1617
1202
|
|
|
1618
|
-
export function fetchCachedData(
|
|
1619
|
-
gqlQuery: any,
|
|
1620
|
-
variablesKey: string,
|
|
1621
|
-
_updateObservers?: UpdateObserversFn,
|
|
1622
|
-
) {
|
|
1203
|
+
export function fetchCachedData(gqlQuery: any, variablesKey: string, _updateObservers?: UpdateObserversFn) {
|
|
1623
1204
|
const definitions = gqlQuery.definitions;
|
|
1624
|
-
const fragmentDefinitions =
|
|
1205
|
+
const fragmentDefinitions = getFragmentDefinitions(gqlQuery);
|
|
1625
1206
|
const queryDocumentCacheId = getQueryDocumentCacheId(gqlQuery);
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
//
|
|
1636
|
-
//
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
const cachedData = loadQuery(queryId);
|
|
1641
|
-
if (cachedData === null) {
|
|
1642
|
-
return null;
|
|
1643
|
-
}
|
|
1644
|
-
|
|
1645
|
-
if (!cachedData) {
|
|
1646
|
-
return null;
|
|
1647
|
-
}
|
|
1648
|
-
|
|
1649
|
-
if (
|
|
1650
|
-
!hasCompleteSelectionData(
|
|
1651
|
-
{ [qryName]: cachedData },
|
|
1652
|
-
selection,
|
|
1653
|
-
fragmentDefinitions,
|
|
1654
|
-
)
|
|
1655
|
-
) {
|
|
1656
|
-
return null;
|
|
1657
|
-
}
|
|
1658
|
-
|
|
1659
|
-
return { [qryName]: cachedData };
|
|
1660
|
-
},
|
|
1661
|
-
);
|
|
1207
|
+
// Memoize the whole operation, including completeness, so repeated cache
|
|
1208
|
+
// reads do not rebuild selection metadata or silently drop additional roots.
|
|
1209
|
+
return cacheStore.memoizeRead(`VQ|${queryDocumentCacheId}|${variablesKey}`, () => {
|
|
1210
|
+
const data: Record<string, any> = {};
|
|
1211
|
+
for (const def of definitions) {
|
|
1212
|
+
if (def.kind !== 'OperationDefinition') continue;
|
|
1213
|
+
for (const selection of def.selectionSet.selections) {
|
|
1214
|
+
const name = getGraphQLSelectionResponseName(selection);
|
|
1215
|
+
const queryKey = `#${name}:${variablesKey}`;
|
|
1216
|
+
// A missing root and a saved null are different, although the public
|
|
1217
|
+
// loadQuery API returns null for both. Record the missing-key dependency.
|
|
1218
|
+
if (QUERIES.get(queryKey) === undefined) return null;
|
|
1219
|
+
data[name] = loadQuery(queryKey);
|
|
1220
|
+
if (!hasCompleteSelectionData(data, selection, fragmentDefinitions!)) return null;
|
|
1662
1221
|
}
|
|
1663
1222
|
}
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
return null;
|
|
1223
|
+
return Object.keys(data).length ? data : null;
|
|
1224
|
+
});
|
|
1667
1225
|
}
|
|
1668
1226
|
|
|
1669
1227
|
/**
|
|
@@ -1671,7 +1229,11 @@ export function fetchCachedData(
|
|
|
1671
1229
|
*/
|
|
1672
1230
|
|
|
1673
1231
|
export function clearGraphQLClientCache() {
|
|
1232
|
+
graphQLCacheEpoch += 1;
|
|
1674
1233
|
cacheStore.clearAll();
|
|
1234
|
+
clearAIChatOutgoingMessages();
|
|
1235
|
+
clearAIChatLinkEvents();
|
|
1236
|
+
clearRealtimeWrites();
|
|
1675
1237
|
RESET_TIME.clear();
|
|
1676
1238
|
clearGraphQLCachePersistence();
|
|
1677
1239
|
|
|
@@ -1680,6 +1242,14 @@ export function clearGraphQLClientCache() {
|
|
|
1680
1242
|
clearAllDrafts();
|
|
1681
1243
|
}
|
|
1682
1244
|
|
|
1245
|
+
/*
|
|
1246
|
+
* Return the cache epoch, which advances every time the normalized cache is cleared. Asynchronous work (requests, record
|
|
1247
|
+
* reads, delayed refreshes) compares it so nothing started before a clear is written or run after it.
|
|
1248
|
+
*/
|
|
1249
|
+
export function getGraphQLCacheEpoch() {
|
|
1250
|
+
return graphQLCacheEpoch;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1683
1253
|
/**
|
|
1684
1254
|
* Return when one query's cached data last came from the network (ms epoch),
|
|
1685
1255
|
* or undefined when it never did in this session or persisted snapshot.
|
|
@@ -1710,20 +1280,13 @@ export function getNextQueryLastUpdatedCount(
|
|
|
1710
1280
|
observedUpdatedCount: number,
|
|
1711
1281
|
ownQueryObserverCount: number,
|
|
1712
1282
|
) {
|
|
1713
|
-
const acknowledgedUpdatedCount = Math.max(
|
|
1714
|
-
currentLastUpdatedCount || 0,
|
|
1715
|
-
observedUpdatedCount || 0,
|
|
1716
|
-
);
|
|
1283
|
+
const acknowledgedUpdatedCount = Math.max(currentLastUpdatedCount || 0, observedUpdatedCount || 0);
|
|
1717
1284
|
|
|
1718
1285
|
if (!ownQueryObserverCount) {
|
|
1719
1286
|
return acknowledgedUpdatedCount;
|
|
1720
1287
|
}
|
|
1721
1288
|
|
|
1722
|
-
return Math.max(
|
|
1723
|
-
acknowledgedUpdatedCount,
|
|
1724
|
-
(observedUpdatedCount || currentLastUpdatedCount || 0) +
|
|
1725
|
-
ownQueryObserverCount,
|
|
1726
|
-
);
|
|
1289
|
+
return Math.max(acknowledgedUpdatedCount, (observedUpdatedCount || currentLastUpdatedCount || 0) + ownQueryObserverCount);
|
|
1727
1290
|
}
|
|
1728
1291
|
|
|
1729
1292
|
/**
|
|
@@ -1737,10 +1300,7 @@ export function getQueryRefreshTime(queryName: string, variablesKey: string) {
|
|
|
1737
1300
|
// console.log('2 >>>>>>>', RESET_TIME.get(`#${queryName}:${variablesKey}`));
|
|
1738
1301
|
// }
|
|
1739
1302
|
|
|
1740
|
-
return (
|
|
1741
|
-
RESET_TIME.get(`#${queryName}:`) ||
|
|
1742
|
-
RESET_TIME.get(`#${queryName}:${variablesKey}`)
|
|
1743
|
-
);
|
|
1303
|
+
return RESET_TIME.get(`#${queryName}:`) || RESET_TIME.get(`#${queryName}:${variablesKey}`);
|
|
1744
1304
|
}
|
|
1745
1305
|
|
|
1746
1306
|
/**
|
|
@@ -1813,12 +1373,14 @@ export function resetQueryByTarget(
|
|
|
1813
1373
|
if (queryCacheKeyMatchesResetTarget(key, target)) {
|
|
1814
1374
|
RESET_TIME.set(key, Date.now());
|
|
1815
1375
|
console.dev('✅ RESET ->', key);
|
|
1816
|
-
updateObservers({
|
|
1817
|
-
queryId: key,
|
|
1818
|
-
forceRefetch,
|
|
1819
|
-
});
|
|
1820
1376
|
}
|
|
1821
1377
|
}
|
|
1378
|
+
// One scoped event also reaches matching in-flight queries without a cached root.
|
|
1379
|
+
updateObservers({
|
|
1380
|
+
queryId: getQueryResetTargetId(target),
|
|
1381
|
+
queryTarget: target,
|
|
1382
|
+
forceRefetch,
|
|
1383
|
+
});
|
|
1822
1384
|
}
|
|
1823
1385
|
}
|
|
1824
1386
|
|
|
@@ -1826,6 +1388,8 @@ export function resetQueryByTarget(
|
|
|
1826
1388
|
* Clear query cache
|
|
1827
1389
|
* NOTE: If "queryId" is a name of query postfixed by ":" like this => "#queryName:"
|
|
1828
1390
|
* it will reset all queries everywhere for the same GraphQL query.
|
|
1391
|
+
* selfRefresh (with resetOnly): this tab's own request caused the reset, so a
|
|
1392
|
+
* mounted result refetches instead of showing the "new data" notice.
|
|
1829
1393
|
*/
|
|
1830
1394
|
|
|
1831
1395
|
export function resetQuery(
|
|
@@ -1833,6 +1397,7 @@ export function resetQuery(
|
|
|
1833
1397
|
forceRefetch?: boolean,
|
|
1834
1398
|
updateObservers?: UpdateObserversFn,
|
|
1835
1399
|
resetOnly?: boolean,
|
|
1400
|
+
selfRefresh?: boolean,
|
|
1836
1401
|
) {
|
|
1837
1402
|
// if (forceRefetch) {
|
|
1838
1403
|
// QUERIES.delete(queryId);
|
|
@@ -1844,6 +1409,7 @@ export function resetQuery(
|
|
|
1844
1409
|
queryId,
|
|
1845
1410
|
forceRefetch,
|
|
1846
1411
|
resetOnly,
|
|
1412
|
+
selfRefresh,
|
|
1847
1413
|
});
|
|
1848
1414
|
return;
|
|
1849
1415
|
}
|
|
@@ -1856,7 +1422,6 @@ export function resetQuery(
|
|
|
1856
1422
|
queryId,
|
|
1857
1423
|
forceRefetch,
|
|
1858
1424
|
});
|
|
1859
|
-
// console.log('SETTING ->', queryId, RESET_TIME.get(queryId));
|
|
1860
1425
|
}
|
|
1861
1426
|
}
|
|
1862
1427
|
|
|
@@ -1875,33 +1440,32 @@ export function updateFragment(
|
|
|
1875
1440
|
const cache = loadFragment(updateFragmentKey);
|
|
1876
1441
|
|
|
1877
1442
|
if (cache && update) {
|
|
1878
|
-
const
|
|
1879
|
-
|
|
1880
|
-
let updateObj;
|
|
1443
|
+
const cacheWithoutOptimisticPlaceholders = removeOptimisticPlaceholdersFromArrayFields(cache);
|
|
1444
|
+
let updateObj: any;
|
|
1881
1445
|
if (typeof update === 'function') {
|
|
1882
|
-
updateObj = update(mapArrayLikeObjects(
|
|
1446
|
+
updateObj = update(mapArrayLikeObjects(cacheWithoutOptimisticPlaceholders));
|
|
1883
1447
|
} else {
|
|
1884
1448
|
updateObj = update;
|
|
1885
1449
|
}
|
|
1886
1450
|
|
|
1887
|
-
const newUpdatedObj = {
|
|
1451
|
+
const newUpdatedObj = {
|
|
1452
|
+
...cacheWithoutOptimisticPlaceholders,
|
|
1453
|
+
...(updateFragmentKey.startsWith(FRAGMENT_NAMES.aiChat + ':')
|
|
1454
|
+
? reconcileAIChatCacheSnapshot(updateFragmentKey, cacheWithoutOptimisticPlaceholders, updateObj, loadFragment)
|
|
1455
|
+
: updateObj),
|
|
1456
|
+
};
|
|
1888
1457
|
|
|
1889
1458
|
FRAGMENTS.set(updateFragmentKey, newUpdatedObj);
|
|
1890
1459
|
console.dev('✅ FRAGMENT UPDATED ->', updateFragmentKey, newUpdatedObj);
|
|
1891
1460
|
|
|
1892
|
-
if (
|
|
1893
|
-
replacementFragmentKey &&
|
|
1894
|
-
replacementFragmentKey !== updateFragmentKey
|
|
1895
|
-
) {
|
|
1461
|
+
if (replacementFragmentKey && replacementFragmentKey !== updateFragmentKey) {
|
|
1896
1462
|
FRAGMENTS.set(replacementFragmentKey, newUpdatedObj);
|
|
1897
1463
|
}
|
|
1898
1464
|
|
|
1899
1465
|
if (!doNotTriggerObserver && updateObservers) {
|
|
1900
1466
|
let observingFragmentIds = [updateFragmentKey];
|
|
1901
1467
|
if (Array.isArray(otherUpdatedFragmentKeys)) {
|
|
1902
|
-
observingFragmentIds = uniq(
|
|
1903
|
-
observingFragmentIds.concat(otherUpdatedFragmentKeys),
|
|
1904
|
-
);
|
|
1468
|
+
observingFragmentIds = uniq(observingFragmentIds.concat(otherUpdatedFragmentKeys));
|
|
1905
1469
|
}
|
|
1906
1470
|
|
|
1907
1471
|
updateObservers({
|
|
@@ -1930,18 +1494,15 @@ export function updateQuery(
|
|
|
1930
1494
|
) {
|
|
1931
1495
|
const cache = loadQuery(queryId, true);
|
|
1932
1496
|
if (cache && update) {
|
|
1933
|
-
let updateObj;
|
|
1497
|
+
let updateObj: any;
|
|
1934
1498
|
if (typeof update === 'function') {
|
|
1935
1499
|
updateObj = update(mapArrayLikeObjects(cache));
|
|
1936
1500
|
} else {
|
|
1937
1501
|
updateObj = update;
|
|
1938
1502
|
}
|
|
1939
1503
|
|
|
1940
|
-
const newUpdatedObj = Array.isArray(updateObj)
|
|
1941
|
-
? updateObj
|
|
1942
|
-
: { ...cache, ...updateObj };
|
|
1504
|
+
const newUpdatedObj = Array.isArray(updateObj) ? updateObj : { ...cache, ...updateObj };
|
|
1943
1505
|
|
|
1944
|
-
// console.log('UPDATING (3):', updateObj);
|
|
1945
1506
|
QUERIES.set(queryId, newUpdatedObj);
|
|
1946
1507
|
|
|
1947
1508
|
if (!doNotTriggerObserver && updateObservers) {
|
|
@@ -1960,10 +1521,7 @@ export function updateQuery(
|
|
|
1960
1521
|
* This will make observer NOT work.
|
|
1961
1522
|
*/
|
|
1962
1523
|
|
|
1963
|
-
export function triggerObserver(
|
|
1964
|
-
ids: (string | null)[],
|
|
1965
|
-
updateObservers?: UpdateObserversFn,
|
|
1966
|
-
) {
|
|
1524
|
+
export function triggerObserver(ids: (string | null)[], updateObservers?: UpdateObserversFn) {
|
|
1967
1525
|
if (updateObservers) {
|
|
1968
1526
|
const observerListIds = ids.filter((id) => id);
|
|
1969
1527
|
if (observerListIds.length) {
|
|
@@ -1972,9 +1530,7 @@ export function triggerObserver(
|
|
|
1972
1530
|
});
|
|
1973
1531
|
}
|
|
1974
1532
|
} else {
|
|
1975
|
-
console.warn(
|
|
1976
|
-
'trigerObserver() was called without updateObservers(); This does nothing.',
|
|
1977
|
-
);
|
|
1533
|
+
console.warn('trigerObserver() was called without updateObservers(); This does nothing.');
|
|
1978
1534
|
}
|
|
1979
1535
|
}
|
|
1980
1536
|
|