@jsb188/graphql 1.1.10 → 1.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/audience.d.ts +343 -0
- package/cache/config.ts +98 -50
- package/cache/index.chat-activity.test.ts +119 -0
- package/cache/index.chat.test.ts +92 -0
- package/cache/index.helpers.ts +96 -0
- package/cache/index.regression.test.ts +149 -0
- package/cache/index.test.ts +1017 -127
- package/cache/index.ts +1256 -1269
- package/cache/optimistic-ui-runtime.ts +46 -0
- package/cache/optimistic-ui.ts +401 -0
- package/cache/persist.ts +800 -0
- package/cache/store.ts +395 -0
- package/cache/value.helpers.ts +17 -0
- package/client/draft-persistence-indexed-db.ts +413 -0
- package/client/draft-persistence.ts +152 -0
- package/client/index.ts +3 -0
- package/client/request.helpers.ts +14 -0
- package/client/request.ts +870 -164
- package/deno.lock +187 -0
- package/deno.test.json +10 -4
- package/gql/eagerQueryMap.ts +12 -0
- package/gql/fragments/accountAuthFragments.ts +5 -0
- package/gql/fragments/accountFragments.ts +0 -3
- package/gql/fragments/accountSessionFragments.ts +16 -0
- package/gql/fragments/agentMemoryFragments.ts +23 -0
- package/gql/fragments/agentNoteFragments.ts +150 -0
- package/gql/fragments/aiChatFragments.ts +42 -0
- package/gql/fragments/audienceFragments.ts +141 -0
- package/gql/fragments/campaignFragments.ts +566 -0
- package/gql/fragments/dataTableFragments.ts +50 -192
- package/gql/fragments/emailFragments.ts +32 -17
- package/gql/fragments/fieldDocFragments.ts +264 -0
- package/gql/fragments/inboundContactFragments.ts +2 -0
- package/gql/fragments/logFragments.ts +101 -69
- package/gql/fragments/organizationFragments.ts +53 -5
- package/gql/fragments/qrCodeFragments.ts +29 -0
- package/gql/fragments/scheduleFragments.ts +667 -0
- package/gql/fragments/sheetFragments.ts +304 -63
- package/gql/fragments/storageFragments.ts +45 -0
- package/gql/fragments/workflowFragments.ts +41 -5
- package/gql/mutations/accountMutations.ts +0 -2
- package/gql/mutations/agentMemoryMutations.ts +68 -0
- package/gql/mutations/agentNoteMutations.ts +62 -0
- package/gql/mutations/aiChatMutations.ts +49 -4
- package/gql/mutations/audienceMutations.ts +71 -0
- package/gql/mutations/authMutations.ts +203 -18
- package/gql/mutations/billingMutations.ts +17 -27
- package/gql/mutations/campaignMutations.ts +258 -0
- package/gql/mutations/contactMutations.ts +23 -0
- package/gql/mutations/dataTableMutations.ts +19 -54
- package/gql/mutations/fieldDocMutations.ts +173 -0
- package/gql/mutations/index.ts +10 -2
- package/gql/mutations/logMutations.ts +6 -29
- package/gql/mutations/organizationMutations.ts +19 -4
- package/gql/mutations/qrCodeMutations.ts +20 -0
- package/gql/mutations/scheduleMutations.ts +1121 -0
- package/gql/mutations/sheetMutations.ts +221 -60
- package/gql/mutations/storageMutations.ts +19 -0
- package/gql/mutations/workflowMutations.ts +60 -0
- package/gql/queries/agentMemoryQueries.ts +28 -0
- package/gql/queries/agentNoteQueries.ts +89 -0
- package/gql/queries/aiChatQueries.ts +70 -2
- package/gql/queries/audienceQueries.ts +128 -0
- package/gql/queries/authQueries.ts +47 -0
- package/gql/queries/billingQueries.ts +118 -6
- package/gql/queries/campaignQueries.ts +236 -0
- package/gql/queries/dataTableQueries.ts +61 -13
- package/gql/queries/emailQueries.ts +26 -17
- package/gql/queries/emailReplyReportQueries.ts +76 -0
- package/gql/queries/fieldDocQueries.ts +166 -0
- package/gql/queries/inboundContactQueries.ts +4 -0
- package/gql/queries/index.ts +10 -2
- package/gql/queries/logQueries.ts +68 -30
- package/gql/queries/organizationQueries.ts +65 -1
- package/gql/queries/qrCodeQueries.ts +20 -0
- package/gql/queries/scheduleQueries.ts +691 -0
- package/gql/queries/sheetQueries.ts +244 -26
- package/gql/queries/storageQueries.ts +38 -0
- package/gql/queries/workflowQueries.ts +77 -2
- package/hooks/index.helpers.test.ts +50 -0
- package/hooks/index.helpers.ts +52 -0
- package/hooks/index.ts +2254 -982
- package/hooks/use-account-mtn.ts +76 -81
- package/hooks/use-agentMemory-mtn.ts +71 -0
- package/hooks/use-agentMemory-qry.ts +56 -0
- package/hooks/use-agentNote-mtn.ts +82 -0
- package/hooks/use-agentNote-qry.ts +190 -0
- package/hooks/use-agentNote-recovery.helpers.test.ts +68 -0
- package/hooks/use-agentNote-recovery.helpers.ts +58 -0
- package/hooks/use-agentNote-recovery.ts +72 -0
- package/hooks/use-aiChat-mtn.ts +220 -141
- package/hooks/use-aiChat-qry.helpers.test.ts +63 -0
- package/hooks/use-aiChat-qry.helpers.ts +124 -0
- package/hooks/use-aiChat-qry.ts +341 -237
- package/hooks/use-aiChat-stream-mtn.ts +14 -0
- package/hooks/use-app-qry.ts +12 -9
- package/hooks/use-audience-mtn.ts +194 -0
- package/hooks/use-audience-qry.helpers.test.ts +80 -0
- package/hooks/use-audience-qry.helpers.ts +63 -0
- package/hooks/use-audience-qry.ts +330 -0
- package/hooks/use-auth-mtn.ts +435 -252
- package/hooks/use-auth-qry.ts +51 -0
- package/hooks/use-billing-mtn.ts +31 -0
- package/hooks/use-billing-qry.ts +76 -0
- package/hooks/use-campaign-generation-recovery.ts +53 -0
- package/hooks/use-campaign-mtn.ts +436 -0
- package/hooks/use-campaign-qry.helpers.ts +40 -0
- package/hooks/use-campaign-qry.ts +834 -0
- package/hooks/use-dataTable-mtn.ts +37 -46
- package/hooks/use-dataTable-qry.ts +102 -128
- package/hooks/use-email-qry.helpers.ts +37 -0
- package/hooks/use-email-qry.ts +56 -86
- package/hooks/use-emailCampaignReplyReport-qry.ts +27 -0
- package/hooks/use-fieldDoc-mtn.ts +262 -0
- package/hooks/use-fieldDoc-qry.helpers.test.ts +65 -0
- package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
- package/hooks/use-fieldDoc-qry.ts +212 -0
- package/hooks/use-inboundContact-mtn.ts +19 -16
- package/hooks/use-inboundContact-qry.ts +62 -56
- package/hooks/use-integration-mtn.ts +56 -38
- package/hooks/use-integration-qry.ts +24 -9
- package/hooks/use-log-mtn.ts +88 -59
- package/hooks/use-log-qry.ts +150 -73
- package/hooks/use-organization-mtn.ts +334 -277
- package/hooks/use-organization-qry.ts +155 -129
- package/hooks/use-publicFormEmail-mtn.ts +27 -0
- package/hooks/use-qrCode-mtn.ts +40 -0
- package/hooks/use-qrCode-qry.ts +78 -0
- package/hooks/use-schedule-mtn.ts +589 -0
- package/hooks/use-schedule-qry.ts +738 -0
- package/hooks/use-schedule-reactive.ts +486 -0
- package/hooks/use-sheet-mtn.ts +157 -79
- package/hooks/use-sheet-qry.ts +221 -92
- package/hooks/use-storage-mtn.ts +81 -41
- package/hooks/use-storage-qry.ts +128 -14
- package/hooks/use-workflow-mtn.ts +72 -0
- package/hooks/use-workflow-qry.ts +184 -47
- package/libs/ai-chat-outgoing.helpers.ts +153 -0
- package/libs/ai-chat-outgoing.ts +11 -0
- package/libs/ai-chat-outgoing.types.ts +25 -0
- package/libs/ai-chat-run-realtime.ts +20 -0
- package/libs/ai-chat-submission-realtime.ts +150 -0
- package/libs/fragment-realtime-sequence.ts +55 -0
- package/libs/fragment-recovery.test.ts +326 -0
- package/libs/fragment-recovery.ts +325 -0
- package/libs/open-sheet.ts +138 -0
- package/libs/orchestrator-realtime.helpers.test.ts +229 -0
- package/libs/orchestrator-realtime.helpers.ts +317 -0
- package/libs/orchestrator-realtime.ts +433 -0
- package/libs/request-cache-regression.test.ts +122 -0
- package/libs/request-managed-session.test.ts +574 -0
- package/libs/request-no-store.test.ts +419 -0
- package/libs/sheet-command-batch.ts +44 -0
- package/libs/sheet-view-realtime-router.test.ts +214 -0
- package/libs/sheet-view-realtime-router.ts +99 -0
- package/libs/sse-ticket.test.ts +26 -0
- package/loaders/campaign.test.ts +186 -0
- package/loaders/campaign.ts +110 -0
- package/loaders/emailPreference.ts +75 -0
- package/loaders/rest.ts +12 -0
- package/package.json +119 -71
- package/schedule.d.ts +1790 -0
- package/sse.helpers.ts +19 -0
- package/sse.ts +70 -106
- package/tests/agent-note-detail-cache.test.ts +120 -0
- package/tests/agent-note-search.test.ts +444 -0
- package/tsconfig.json +11 -10
- package/types.d.ts +77 -26
- package/utils.ts +78 -49
- package/gql/fragments/billingFragments.ts +0 -25
- package/gql/fragments/productFragments.ts +0 -39
- package/gql/fragments/reportFragments.ts +0 -90
- package/gql/queries/productQueries.ts +0 -31
- package/gql/queries/reportQueries.ts +0 -134
- package/hooks/use-product-qry.ts +0 -67
- package/hooks/use-report-qry.ts +0 -153
package/cache/persist.ts
ADDED
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
import { CACHE_SCHEMA_VERSION, RULES } from './config.ts';
|
|
2
|
+
import { isOptimisticUIFragmentActive } from './optimistic-ui-runtime.ts';
|
|
3
|
+
import { cacheStore } from './store.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fragment-driven IndexedDB persistence for the GraphQL client cache.
|
|
7
|
+
*
|
|
8
|
+
* Fragments are persisted individually and query records persist only their
|
|
9
|
+
* fragment reference structures, mirroring the in-memory normalized store.
|
|
10
|
+
* The adapter subscribes to the store's dirty-key notifications, so every
|
|
11
|
+
* write source (query results, mutation results, websocket realtime events)
|
|
12
|
+
* keeps the disk copy in sync automatically with no per-call-site code.
|
|
13
|
+
*
|
|
14
|
+
* Memory-first and best-effort by design: when IndexedDB is unavailable
|
|
15
|
+
* (private browsing, quota, corruption) or any operation fails, persistence
|
|
16
|
+
* silently disables itself and the cache behaves exactly like before.
|
|
17
|
+
* Multi-tab writes are last-write-wins; tabs converge through realtime
|
|
18
|
+
* updates and refetches.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
type PersistenceContext = {
|
|
22
|
+
accountId: string;
|
|
23
|
+
organizationId: string;
|
|
24
|
+
buildId: string;
|
|
25
|
+
// Managed-session id the snapshot was written under; a new login session
|
|
26
|
+
// (new session id) wipes the snapshot so role/ACL changes applied between
|
|
27
|
+
// sessions can never hydrate pre-change private data. Null when the auth
|
|
28
|
+
// mode has no managed session.
|
|
29
|
+
sessionId: string | null;
|
|
30
|
+
// Durable server-side generation advanced by authorization mutations.
|
|
31
|
+
// Missing generations disable private persistence instead of hydrating a
|
|
32
|
+
// snapshot whose permissions cannot be proven current.
|
|
33
|
+
cacheAuthorizationEpoch: string | null;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type PersistedMeta = PersistenceContext & {
|
|
37
|
+
schemaVersion: number;
|
|
38
|
+
savedAt: number;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type OnHydratedFn = (hydratedFragmentKeys: string[]) => void;
|
|
42
|
+
|
|
43
|
+
const PERSIST_CONSTANTS = {
|
|
44
|
+
dbName: 'jsb188-gql-cache',
|
|
45
|
+
dbVersion: 1,
|
|
46
|
+
fragmentsStore: 'fragments',
|
|
47
|
+
queriesStore: 'queries',
|
|
48
|
+
metaStore: 'meta',
|
|
49
|
+
metaKey: 'meta',
|
|
50
|
+
flushDelayMs: 600,
|
|
51
|
+
flushMaxDelayMs: 2500,
|
|
52
|
+
maxSnapshotAgeMs: 6.048e8, // 7 days; older snapshots are discarded whole
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Module state
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
let DB: IDBDatabase | null = null;
|
|
60
|
+
let ENABLED = false;
|
|
61
|
+
let INIT_PROMISE: Promise<boolean> | null = null;
|
|
62
|
+
let ACTIVE_CONTEXT: PersistenceContext | null = null;
|
|
63
|
+
let PERSISTENCE_GENERATION = 0;
|
|
64
|
+
let UNSUBSCRIBE_STORE: (() => void) | null = null;
|
|
65
|
+
|
|
66
|
+
let LAST_HYDRATED_FRAGMENT_KEYS: string[] = [];
|
|
67
|
+
|
|
68
|
+
const DIRTY_FRAGMENT_KEYS = new Set<string>();
|
|
69
|
+
const DIRTY_QUERY_KEYS = new Set<string>();
|
|
70
|
+
let FLUSH_TIMER: ReturnType<typeof setTimeout> | null = null;
|
|
71
|
+
let FLUSH_DEADLINE = 0;
|
|
72
|
+
let PAGE_HIDE_FLUSH_BOUND = false;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Check whether IndexedDB exists in this runtime (browser only; entry.server
|
|
76
|
+
* and deno tests import this module through cache/index.ts and must no-op).
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
function isIndexedDBAvailable(): boolean {
|
|
80
|
+
return typeof indexedDB !== 'undefined';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Require a complete managed-session authorization boundary before private
|
|
85
|
+
* normalized data can be written to or hydrated from disk.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
function contextAllowsPersistence(context: PersistenceContext): boolean {
|
|
89
|
+
return Boolean(
|
|
90
|
+
context.accountId &&
|
|
91
|
+
context.organizationId &&
|
|
92
|
+
context.buildId &&
|
|
93
|
+
context.sessionId &&
|
|
94
|
+
context.cacheAuthorizationEpoch &&
|
|
95
|
+
/^[1-9][0-9]*$/.test(context.cacheAuthorizationEpoch),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Return whether a fragment cache key may be persisted; fragments opted out
|
|
101
|
+
* in RULES (e.g. auth payloads) never touch disk. Applies to their
|
|
102
|
+
* ".spreads" sidecar keys too, since both share the "$name:" prefix.
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
function fragmentKeyIsPersistable(fragmentKey: string): boolean {
|
|
106
|
+
const nameEnd = fragmentKey.indexOf(':');
|
|
107
|
+
if (fragmentKey[0] !== '$' || nameEnd < 0) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const fragmentName = fragmentKey.substring(1, nameEnd);
|
|
112
|
+
return RULES[fragmentName] !== false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Return the parent fragment key for a ".spreads" sidecar key, or the key
|
|
117
|
+
* itself when it is not a sidecar.
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
function getSpreadsParentFragmentKey(fragmentKey: string): string {
|
|
121
|
+
return fragmentKey.endsWith('.spreads')
|
|
122
|
+
? fragmentKey.slice(0, -'.spreads'.length)
|
|
123
|
+
: fragmentKey;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Return whether one in-memory fragment (or, for a ".spreads" sidecar, its
|
|
128
|
+
* parent fragment) carries the client-only deleted marker. Deleted fragments
|
|
129
|
+
* stay memory-only: the session keeps rendering them (struck-through), but
|
|
130
|
+
* they are removed from disk so a reload drops them entirely.
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
function fragmentRecordIsDeleted(fragmentKey: string): boolean {
|
|
134
|
+
const value = cacheStore.fragments.peek(
|
|
135
|
+
getSpreadsParentFragmentKey(fragmentKey),
|
|
136
|
+
);
|
|
137
|
+
return Boolean(
|
|
138
|
+
value &&
|
|
139
|
+
typeof value === 'object' &&
|
|
140
|
+
!Array.isArray(value) &&
|
|
141
|
+
(value as any).__deleted === true,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Return whether one persisted fragment record carries the client-only
|
|
147
|
+
* deleted marker (written before deleted fragments stopped being persisted).
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
function persistedFragmentRecordIsDeleted(record: any): boolean {
|
|
151
|
+
return Boolean(
|
|
152
|
+
record?.d &&
|
|
153
|
+
typeof record.d === 'object' &&
|
|
154
|
+
!Array.isArray(record.d) &&
|
|
155
|
+
record.d.__deleted === true,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Return a copy of one fragment value safe for storage: top-level arrays are
|
|
161
|
+
* stripped of transient optimistic placeholders.
|
|
162
|
+
*/
|
|
163
|
+
|
|
164
|
+
function sanitizeValueForStorage(value: any) {
|
|
165
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
166
|
+
return value;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let sanitized = value;
|
|
170
|
+
for (const key in value) {
|
|
171
|
+
if (
|
|
172
|
+
!Object.prototype.hasOwnProperty.call(value, key) ||
|
|
173
|
+
!Array.isArray(value[key])
|
|
174
|
+
) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const cleanedArray = value[key].filter(
|
|
179
|
+
(item: any) =>
|
|
180
|
+
!(
|
|
181
|
+
item &&
|
|
182
|
+
typeof item === 'object' &&
|
|
183
|
+
item.__optimisticPlaceholder === true
|
|
184
|
+
),
|
|
185
|
+
);
|
|
186
|
+
if (cleanedArray.length !== value[key].length) {
|
|
187
|
+
if (sanitized === value) {
|
|
188
|
+
sanitized = { ...value };
|
|
189
|
+
}
|
|
190
|
+
sanitized[key] = cleanedArray;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return sanitized;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Promise wrappers for IDBRequest
|
|
199
|
+
*/
|
|
200
|
+
|
|
201
|
+
function requestAsPromise<T>(request: IDBRequest<T>): Promise<T> {
|
|
202
|
+
return new Promise((resolve, reject) => {
|
|
203
|
+
request.onsuccess = () => resolve(request.result);
|
|
204
|
+
request.onerror = () => reject(request.error);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Open (and create/upgrade) the cache database.
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
function openDatabase(allowRecovery = true): Promise<IDBDatabase> {
|
|
213
|
+
return new Promise((resolve, reject) => {
|
|
214
|
+
const openRequest = indexedDB.open(
|
|
215
|
+
PERSIST_CONSTANTS.dbName,
|
|
216
|
+
PERSIST_CONSTANTS.dbVersion,
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
openRequest.onupgradeneeded = () => {
|
|
220
|
+
const db = openRequest.result;
|
|
221
|
+
if (!db.objectStoreNames.contains(PERSIST_CONSTANTS.fragmentsStore)) {
|
|
222
|
+
db.createObjectStore(PERSIST_CONSTANTS.fragmentsStore, {
|
|
223
|
+
keyPath: 'k',
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
if (!db.objectStoreNames.contains(PERSIST_CONSTANTS.queriesStore)) {
|
|
227
|
+
db.createObjectStore(PERSIST_CONSTANTS.queriesStore, { keyPath: 'k' });
|
|
228
|
+
}
|
|
229
|
+
if (!db.objectStoreNames.contains(PERSIST_CONSTANTS.metaStore)) {
|
|
230
|
+
db.createObjectStore(PERSIST_CONSTANTS.metaStore);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
openRequest.onsuccess = () => {
|
|
235
|
+
const db = openRequest.result;
|
|
236
|
+
|
|
237
|
+
// A database created outside this adapter (e.g. a versionless open)
|
|
238
|
+
// can exist without our object stores; rebuild it once from scratch.
|
|
239
|
+
const hasAllStores =
|
|
240
|
+
db.objectStoreNames.contains(PERSIST_CONSTANTS.fragmentsStore) &&
|
|
241
|
+
db.objectStoreNames.contains(PERSIST_CONSTANTS.queriesStore) &&
|
|
242
|
+
db.objectStoreNames.contains(PERSIST_CONSTANTS.metaStore);
|
|
243
|
+
|
|
244
|
+
if (hasAllStores) {
|
|
245
|
+
resolve(db);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
db.close();
|
|
250
|
+
|
|
251
|
+
if (!allowRecovery) {
|
|
252
|
+
reject(new Error('IndexedDB cache database is missing object stores.'));
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const deleteRequest = indexedDB.deleteDatabase(PERSIST_CONSTANTS.dbName);
|
|
257
|
+
deleteRequest.onsuccess = () => {
|
|
258
|
+
openDatabase(false).then(resolve, reject);
|
|
259
|
+
};
|
|
260
|
+
deleteRequest.onerror = () => reject(deleteRequest.error);
|
|
261
|
+
deleteRequest.onblocked = () =>
|
|
262
|
+
reject(new Error('IndexedDB recovery delete was blocked.'));
|
|
263
|
+
};
|
|
264
|
+
openRequest.onerror = () => reject(openRequest.error);
|
|
265
|
+
openRequest.onblocked = () =>
|
|
266
|
+
reject(new Error('IndexedDB open was blocked by another connection.'));
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Disable persistence for the rest of the session; the in-memory cache keeps
|
|
272
|
+
* working exactly as before.
|
|
273
|
+
*/
|
|
274
|
+
|
|
275
|
+
function disablePersistence(reason: any) {
|
|
276
|
+
ENABLED = false;
|
|
277
|
+
|
|
278
|
+
if (UNSUBSCRIBE_STORE) {
|
|
279
|
+
UNSUBSCRIBE_STORE();
|
|
280
|
+
UNSUBSCRIBE_STORE = null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (FLUSH_TIMER) {
|
|
284
|
+
clearTimeout(FLUSH_TIMER);
|
|
285
|
+
FLUSH_TIMER = null;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
DIRTY_FRAGMENT_KEYS.clear();
|
|
289
|
+
DIRTY_QUERY_KEYS.clear();
|
|
290
|
+
|
|
291
|
+
if (DB) {
|
|
292
|
+
try {
|
|
293
|
+
DB.close();
|
|
294
|
+
} catch (_err) {
|
|
295
|
+
// Already closing/broken; nothing else to do
|
|
296
|
+
}
|
|
297
|
+
DB = null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
(console as any).dev?.('GraphQL cache persistence disabled:', reason);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Empty every object store (fragments, queries, meta) in one transaction.
|
|
305
|
+
*/
|
|
306
|
+
|
|
307
|
+
function clearAllStores(db: IDBDatabase): Promise<void> {
|
|
308
|
+
return new Promise((resolve, reject) => {
|
|
309
|
+
const tx = db.transaction(
|
|
310
|
+
[
|
|
311
|
+
PERSIST_CONSTANTS.fragmentsStore,
|
|
312
|
+
PERSIST_CONSTANTS.queriesStore,
|
|
313
|
+
PERSIST_CONSTANTS.metaStore,
|
|
314
|
+
],
|
|
315
|
+
'readwrite',
|
|
316
|
+
);
|
|
317
|
+
tx.objectStore(PERSIST_CONSTANTS.fragmentsStore).clear();
|
|
318
|
+
tx.objectStore(PERSIST_CONSTANTS.queriesStore).clear();
|
|
319
|
+
tx.objectStore(PERSIST_CONSTANTS.metaStore).clear();
|
|
320
|
+
tx.oncomplete = () => resolve();
|
|
321
|
+
tx.onerror = () => reject(tx.error);
|
|
322
|
+
tx.onabort = () => reject(tx.error);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Load persisted fragments and query references into the in-memory store.
|
|
328
|
+
* Existing in-memory entries always win (they are newer than the snapshot);
|
|
329
|
+
* hydrated writes never echo back into the dirty buffer.
|
|
330
|
+
*/
|
|
331
|
+
|
|
332
|
+
async function hydrateFromDatabase(db: IDBDatabase, isCurrent: () => boolean): Promise<string[]> {
|
|
333
|
+
const tx = db.transaction([PERSIST_CONSTANTS.fragmentsStore, PERSIST_CONSTANTS.queriesStore], 'readonly');
|
|
334
|
+
|
|
335
|
+
const [fragmentRecords, queryRecords] = await Promise.all([
|
|
336
|
+
requestAsPromise<any[]>(
|
|
337
|
+
tx.objectStore(PERSIST_CONSTANTS.fragmentsStore).getAll(),
|
|
338
|
+
),
|
|
339
|
+
requestAsPromise<any[]>(
|
|
340
|
+
tx.objectStore(PERSIST_CONSTANTS.queriesStore).getAll(),
|
|
341
|
+
),
|
|
342
|
+
]);
|
|
343
|
+
// IndexedDB reads may finish after logout or another context starts loading.
|
|
344
|
+
if (!isCurrent()) return [];
|
|
345
|
+
|
|
346
|
+
const hydratedFragmentKeys: string[] = [];
|
|
347
|
+
|
|
348
|
+
// Records persisted with the deleted marker (written before deleted
|
|
349
|
+
// fragments stopped being flushed) never rejoin memory; purge them and
|
|
350
|
+
// their sidecars from disk after hydration.
|
|
351
|
+
const staleFragmentKeys = new Set<string>();
|
|
352
|
+
for (const record of fragmentRecords) {
|
|
353
|
+
if (record?.k && persistedFragmentRecordIsDeleted(record)) {
|
|
354
|
+
staleFragmentKeys.add(record.k);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
for (const record of fragmentRecords) {
|
|
359
|
+
if (
|
|
360
|
+
record?.k &&
|
|
361
|
+
fragmentKeyIsPersistable(record.k) &&
|
|
362
|
+
!staleFragmentKeys.has(record.k) &&
|
|
363
|
+
!staleFragmentKeys.has(getSpreadsParentFragmentKey(record.k))
|
|
364
|
+
) {
|
|
365
|
+
if (cacheStore.fragments.hydrate(record.k, record.d)) {
|
|
366
|
+
hydratedFragmentKeys.push(record.k);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (staleFragmentKeys.size) {
|
|
372
|
+
purgeDeletedFragmentRecords(db, staleFragmentKeys);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
for (const record of queryRecords) {
|
|
376
|
+
if (record?.k && cacheStore.queries.hydrate(record.k, record.d)) {
|
|
377
|
+
if (typeof record.t === 'number') {
|
|
378
|
+
cacheStore.hydrateQueryFetchTime(record.k, record.t);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return hydratedFragmentKeys;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Best-effort removal of persisted fragment records that carry the deleted
|
|
388
|
+
* marker, plus their spreads sidecars. Failures are ignored; the records are
|
|
389
|
+
* skipped again on the next boot either way.
|
|
390
|
+
*/
|
|
391
|
+
|
|
392
|
+
function purgeDeletedFragmentRecords(
|
|
393
|
+
db: IDBDatabase,
|
|
394
|
+
fragmentKeys: Set<string>,
|
|
395
|
+
) {
|
|
396
|
+
try {
|
|
397
|
+
const tx = db.transaction(PERSIST_CONSTANTS.fragmentsStore, 'readwrite');
|
|
398
|
+
const store = tx.objectStore(PERSIST_CONSTANTS.fragmentsStore);
|
|
399
|
+
fragmentKeys.forEach((key) => {
|
|
400
|
+
store.delete(key);
|
|
401
|
+
store.delete(`${key}.spreads`);
|
|
402
|
+
});
|
|
403
|
+
} catch (_err) {
|
|
404
|
+
// Best-effort only
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Write the meta record binding this snapshot to (account, organization,
|
|
410
|
+
* session, authorization epoch, schema version, build).
|
|
411
|
+
*/
|
|
412
|
+
|
|
413
|
+
function putMeta(tx: IDBTransaction) {
|
|
414
|
+
const meta: PersistedMeta = {
|
|
415
|
+
...(ACTIVE_CONTEXT as PersistenceContext),
|
|
416
|
+
schemaVersion: CACHE_SCHEMA_VERSION,
|
|
417
|
+
savedAt: Date.now(),
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
tx.objectStore(PERSIST_CONSTANTS.metaStore).put(
|
|
421
|
+
meta,
|
|
422
|
+
PERSIST_CONSTANTS.metaKey,
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Flush buffered dirty keys to disk in one readwrite transaction. Deleted or
|
|
428
|
+
* evicted keys are removed; records that cannot be structured-cloned are
|
|
429
|
+
* skipped silently (e.g. custom cacheMap values holding functions).
|
|
430
|
+
*/
|
|
431
|
+
|
|
432
|
+
function flushDirtyKeys() {
|
|
433
|
+
FLUSH_TIMER = null;
|
|
434
|
+
FLUSH_DEADLINE = 0;
|
|
435
|
+
|
|
436
|
+
if (
|
|
437
|
+
!ENABLED ||
|
|
438
|
+
!DB ||
|
|
439
|
+
(!DIRTY_FRAGMENT_KEYS.size && !DIRTY_QUERY_KEYS.size)
|
|
440
|
+
) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const fragmentKeys = Array.from(DIRTY_FRAGMENT_KEYS).filter(
|
|
445
|
+
(key) => !isOptimisticUIFragmentActive(key),
|
|
446
|
+
);
|
|
447
|
+
const queryKeys = Array.from(DIRTY_QUERY_KEYS);
|
|
448
|
+
DIRTY_FRAGMENT_KEYS.clear();
|
|
449
|
+
DIRTY_QUERY_KEYS.clear();
|
|
450
|
+
|
|
451
|
+
try {
|
|
452
|
+
const tx = DB.transaction(
|
|
453
|
+
[
|
|
454
|
+
PERSIST_CONSTANTS.fragmentsStore,
|
|
455
|
+
PERSIST_CONSTANTS.queriesStore,
|
|
456
|
+
PERSIST_CONSTANTS.metaStore,
|
|
457
|
+
],
|
|
458
|
+
'readwrite',
|
|
459
|
+
);
|
|
460
|
+
const fragmentsStore = tx.objectStore(PERSIST_CONSTANTS.fragmentsStore);
|
|
461
|
+
const queriesStore = tx.objectStore(PERSIST_CONSTANTS.queriesStore);
|
|
462
|
+
|
|
463
|
+
for (const key of fragmentKeys) {
|
|
464
|
+
if (!fragmentKeyIsPersistable(key)) {
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const value = cacheStore.fragments.peek(key);
|
|
469
|
+
if (value === undefined) {
|
|
470
|
+
fragmentsStore.delete(key);
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (fragmentRecordIsDeleted(key)) {
|
|
475
|
+
// Deleted marker: drop the record and its sidecar from disk so a
|
|
476
|
+
// reload cannot resurrect an item the session only keeps in memory
|
|
477
|
+
// for strikethrough display.
|
|
478
|
+
fragmentsStore.delete(key);
|
|
479
|
+
fragmentsStore.delete(`${key}.spreads`);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
try {
|
|
484
|
+
// Validates structured-cloneability before the async put can abort
|
|
485
|
+
// the whole transaction on an unserializable record
|
|
486
|
+
fragmentsStore.put(
|
|
487
|
+
structuredClone({ k: key, d: sanitizeValueForStorage(value) }),
|
|
488
|
+
);
|
|
489
|
+
} catch (_err) {
|
|
490
|
+
// Unserializable record; leave it memory-only
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
for (const key of queryKeys) {
|
|
495
|
+
const value = cacheStore.queries.peek(key);
|
|
496
|
+
if (value === undefined) {
|
|
497
|
+
queriesStore.delete(key);
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
try {
|
|
502
|
+
queriesStore.put(
|
|
503
|
+
structuredClone({
|
|
504
|
+
k: key,
|
|
505
|
+
d: value,
|
|
506
|
+
t: cacheStore.getQueryFetchTime(key),
|
|
507
|
+
}),
|
|
508
|
+
);
|
|
509
|
+
} catch (_err) {
|
|
510
|
+
// Unserializable record; leave it memory-only
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
putMeta(tx);
|
|
515
|
+
|
|
516
|
+
tx.onerror = () => disablePersistence(tx.error);
|
|
517
|
+
tx.onabort = () => disablePersistence(tx.error || 'transaction aborted');
|
|
518
|
+
|
|
519
|
+
// Commit now instead of at the end of the task, so a flush started while
|
|
520
|
+
// the page unloads still reaches disk
|
|
521
|
+
tx.commit?.();
|
|
522
|
+
} catch (err) {
|
|
523
|
+
disablePersistence(err);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Debounced flush scheduling: waits for a write-quiet moment but never lets
|
|
529
|
+
* a busy stream of updates defer disk writes beyond the maximum delay.
|
|
530
|
+
*/
|
|
531
|
+
|
|
532
|
+
function scheduleFlush() {
|
|
533
|
+
const now = Date.now();
|
|
534
|
+
|
|
535
|
+
if (!FLUSH_DEADLINE) {
|
|
536
|
+
FLUSH_DEADLINE = now + PERSIST_CONSTANTS.flushMaxDelayMs;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (FLUSH_TIMER) {
|
|
540
|
+
clearTimeout(FLUSH_TIMER);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const delay = Math.min(
|
|
544
|
+
PERSIST_CONSTANTS.flushDelayMs,
|
|
545
|
+
Math.max(0, FLUSH_DEADLINE - now),
|
|
546
|
+
);
|
|
547
|
+
FLUSH_TIMER = setTimeout(flushDirtyKeys, delay);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Flush buffered dirty keys immediately when the page is hidden or unloads.
|
|
552
|
+
* A reload inside the debounce window would otherwise drop the pending write
|
|
553
|
+
* and hydrate the previous snapshot, which still looks fresh by fetch time.
|
|
554
|
+
*/
|
|
555
|
+
|
|
556
|
+
function flushDirtyKeysOnPageHide(event: Event) {
|
|
557
|
+
if (
|
|
558
|
+
event.type === 'visibilitychange' &&
|
|
559
|
+
document.visibilityState !== 'hidden'
|
|
560
|
+
) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (FLUSH_TIMER) {
|
|
565
|
+
clearTimeout(FLUSH_TIMER);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
flushDirtyKeys();
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Bind the page-hide flush once per page; flushDirtyKeys() no-ops while
|
|
573
|
+
* persistence is disabled, so the listeners never need removing.
|
|
574
|
+
*/
|
|
575
|
+
|
|
576
|
+
function bindPageHideFlush() {
|
|
577
|
+
if (PAGE_HIDE_FLUSH_BOUND || typeof document === 'undefined') {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
PAGE_HIDE_FLUSH_BOUND = true;
|
|
582
|
+
globalThis.addEventListener('pagehide', flushDirtyKeysOnPageHide);
|
|
583
|
+
document.addEventListener('visibilitychange', flushDirtyKeysOnPageHide);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Store subscription: buffer dirty keys and schedule a flush.
|
|
588
|
+
*/
|
|
589
|
+
|
|
590
|
+
function onStoreDirty(dirty: {
|
|
591
|
+
fragmentKeys: Set<string>;
|
|
592
|
+
queryKeys: Set<string>;
|
|
593
|
+
}) {
|
|
594
|
+
if (!ENABLED) {
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
dirty.fragmentKeys.forEach((key) => {
|
|
599
|
+
if (!isOptimisticUIFragmentActive(key)) {
|
|
600
|
+
DIRTY_FRAGMENT_KEYS.add(key);
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
dirty.queryKeys.forEach((key) => {
|
|
604
|
+
DIRTY_QUERY_KEYS.add(key);
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
if (DIRTY_FRAGMENT_KEYS.size || DIRTY_QUERY_KEYS.size) {
|
|
608
|
+
scheduleFlush();
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Check whether a persisted meta record matches the current context.
|
|
614
|
+
*/
|
|
615
|
+
|
|
616
|
+
function metaMatchesContext(
|
|
617
|
+
meta: PersistedMeta | undefined,
|
|
618
|
+
context: PersistenceContext,
|
|
619
|
+
): boolean {
|
|
620
|
+
return Boolean(
|
|
621
|
+
meta &&
|
|
622
|
+
meta.schemaVersion === CACHE_SCHEMA_VERSION &&
|
|
623
|
+
meta.accountId === context.accountId &&
|
|
624
|
+
meta.organizationId === context.organizationId &&
|
|
625
|
+
meta.buildId === context.buildId &&
|
|
626
|
+
(meta.sessionId ?? null) === (context.sessionId ?? null) &&
|
|
627
|
+
(meta.cacheAuthorizationEpoch ?? null) ===
|
|
628
|
+
(context.cacheAuthorizationEpoch ?? null) &&
|
|
629
|
+
typeof meta.savedAt === 'number' &&
|
|
630
|
+
meta.savedAt <= Date.now() &&
|
|
631
|
+
Date.now() - meta.savedAt < PERSIST_CONSTANTS.maxSnapshotAgeMs,
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Initialize cache persistence for one (account, organization, build,
|
|
637
|
+
* session, authorization epoch) context. Safe to call repeatedly; a context
|
|
638
|
+
* change wipes the previous snapshot. Resolves true when a snapshot was
|
|
639
|
+
* hydrated into memory.
|
|
640
|
+
*
|
|
641
|
+
* onHydrated receives the hydrated fragment keys so the caller can notify
|
|
642
|
+
* fragment observers (kept as a callback to avoid a hooks<->cache import
|
|
643
|
+
* cycle).
|
|
644
|
+
*/
|
|
645
|
+
|
|
646
|
+
export function initGraphQLCachePersistence(
|
|
647
|
+
context: PersistenceContext,
|
|
648
|
+
onHydrated?: OnHydratedFn,
|
|
649
|
+
): Promise<boolean> {
|
|
650
|
+
if (!isIndexedDBAvailable()) {
|
|
651
|
+
return Promise.resolve(false);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if (!contextAllowsPersistence(context)) {
|
|
655
|
+
clearGraphQLCachePersistence();
|
|
656
|
+
return Promise.resolve(false);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
if (
|
|
660
|
+
INIT_PROMISE &&
|
|
661
|
+
ACTIVE_CONTEXT &&
|
|
662
|
+
ACTIVE_CONTEXT.accountId === context.accountId &&
|
|
663
|
+
ACTIVE_CONTEXT.organizationId === context.organizationId &&
|
|
664
|
+
ACTIVE_CONTEXT.buildId === context.buildId &&
|
|
665
|
+
(ACTIVE_CONTEXT.sessionId ?? null) === (context.sessionId ?? null) &&
|
|
666
|
+
(ACTIVE_CONTEXT.cacheAuthorizationEpoch ?? null) ===
|
|
667
|
+
(context.cacheAuthorizationEpoch ?? null)
|
|
668
|
+
) {
|
|
669
|
+
// Same context (e.g. app init after a pre-React warm start): deliver the
|
|
670
|
+
// hydrated keys to this caller's observer too. Duplicate notifications
|
|
671
|
+
// are cheap; fragment hooks are version-stamp guarded.
|
|
672
|
+
if (onHydrated) {
|
|
673
|
+
const generation = PERSISTENCE_GENERATION;
|
|
674
|
+
INIT_PROMISE.then(() => {
|
|
675
|
+
if (generation === PERSISTENCE_GENERATION && LAST_HYDRATED_FRAGMENT_KEYS.length) {
|
|
676
|
+
onHydrated(LAST_HYDRATED_FRAGMENT_KEYS);
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
return INIT_PROMISE;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// Context changed mid-session (e.g. organization switch without reload):
|
|
685
|
+
// drop the previous adapter state and start over.
|
|
686
|
+
if (ENABLED) {
|
|
687
|
+
disablePersistence('context changed');
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
ACTIVE_CONTEXT = context;
|
|
691
|
+
const generation = ++PERSISTENCE_GENERATION;
|
|
692
|
+
/* Check ownership after every asynchronous storage boundary. */
|
|
693
|
+
const isCurrent = () => generation === PERSISTENCE_GENERATION;
|
|
694
|
+
INIT_PROMISE = (async () => {
|
|
695
|
+
let db: IDBDatabase | null = null;
|
|
696
|
+
try {
|
|
697
|
+
db = await openDatabase();
|
|
698
|
+
if (!isCurrent()) {
|
|
699
|
+
db.close();
|
|
700
|
+
return false;
|
|
701
|
+
}
|
|
702
|
+
const metaTx = db.transaction(PERSIST_CONSTANTS.metaStore, 'readonly');
|
|
703
|
+
const meta = await requestAsPromise<PersistedMeta | undefined>(
|
|
704
|
+
metaTx
|
|
705
|
+
.objectStore(PERSIST_CONSTANTS.metaStore)
|
|
706
|
+
.get(PERSIST_CONSTANTS.metaKey),
|
|
707
|
+
);
|
|
708
|
+
if (!isCurrent()) {
|
|
709
|
+
db.close();
|
|
710
|
+
return false;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
let hydratedFragmentKeys: string[] = [];
|
|
714
|
+
const matched = metaMatchesContext(meta, context);
|
|
715
|
+
|
|
716
|
+
if (matched) {
|
|
717
|
+
hydratedFragmentKeys = await hydrateFromDatabase(db, isCurrent);
|
|
718
|
+
} else {
|
|
719
|
+
// Different account/org/schema/build or stale snapshot: wipe
|
|
720
|
+
await clearAllStores(db);
|
|
721
|
+
}
|
|
722
|
+
if (!isCurrent()) {
|
|
723
|
+
db.close();
|
|
724
|
+
return false;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
LAST_HYDRATED_FRAGMENT_KEYS = hydratedFragmentKeys;
|
|
728
|
+
|
|
729
|
+
DB = db;
|
|
730
|
+
ENABLED = true;
|
|
731
|
+
UNSUBSCRIBE_STORE = cacheStore.subscribe(onStoreDirty);
|
|
732
|
+
bindPageHideFlush();
|
|
733
|
+
|
|
734
|
+
db.onversionchange = () => {
|
|
735
|
+
// Another tab upgraded/deleted the database; step aside
|
|
736
|
+
if (isCurrent()) disablePersistence('database version change');
|
|
737
|
+
else db?.close();
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
if (hydratedFragmentKeys.length && onHydrated) {
|
|
741
|
+
onHydrated(hydratedFragmentKeys);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
return hydratedFragmentKeys.length > 0;
|
|
745
|
+
} catch (err) {
|
|
746
|
+
if (isCurrent()) disablePersistence(err);
|
|
747
|
+
else db?.close();
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
})();
|
|
751
|
+
|
|
752
|
+
return INIT_PROMISE;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* Wipe the persisted cache (sign-out, organization access revoked). Called
|
|
757
|
+
* from clearGraphQLClientCache(); best-effort when the adapter was never
|
|
758
|
+
* initialized.
|
|
759
|
+
*/
|
|
760
|
+
|
|
761
|
+
export function clearGraphQLCachePersistence() {
|
|
762
|
+
PERSISTENCE_GENERATION += 1;
|
|
763
|
+
INIT_PROMISE = null;
|
|
764
|
+
ACTIVE_CONTEXT = null;
|
|
765
|
+
LAST_HYDRATED_FRAGMENT_KEYS = [];
|
|
766
|
+
if (!isIndexedDBAvailable()) {
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
DIRTY_FRAGMENT_KEYS.clear();
|
|
771
|
+
DIRTY_QUERY_KEYS.clear();
|
|
772
|
+
|
|
773
|
+
if (FLUSH_TIMER) {
|
|
774
|
+
clearTimeout(FLUSH_TIMER);
|
|
775
|
+
FLUSH_TIMER = null;
|
|
776
|
+
FLUSH_DEADLINE = 0;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
if (DB) {
|
|
780
|
+
// Start the clear transaction before closing this context's connection.
|
|
781
|
+
void clearAllStores(DB).catch(() => {});
|
|
782
|
+
disablePersistence('cache cleared');
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
// Adapter never initialized in this session; remove any leftover database
|
|
787
|
+
try {
|
|
788
|
+
indexedDB.deleteDatabase(PERSIST_CONSTANTS.dbName);
|
|
789
|
+
} catch (_err) {
|
|
790
|
+
// Best-effort only
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Whether persistence is currently active (for diagnostics/tests).
|
|
796
|
+
*/
|
|
797
|
+
|
|
798
|
+
export function isGraphQLCachePersistenceEnabled(): boolean {
|
|
799
|
+
return ENABLED;
|
|
800
|
+
}
|