Repository navigation
Expand file tree
/
Copy pathreactQueryUtils.ts
More file actions
506 lines (433 loc) Β· 17 KB
/
Copy pathreactQueryUtils.ts
File metadata and controls
506 lines (433 loc) Β· 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import { hydrate } from '@tanstack/react-query';
import { createMMKV } from 'react-native-mmkv';
import { IS_DEV, IS_STORE_INSTALL } from '@/env';
import { showLogSheet, type LogEntry } from '@/features/debug/screens/LogSheet';
import { getDateFormatter } from '@/helpers/intl';
import { logger, RainbowError } from '@/logger';
import { persistOptions, queryClient } from '@/react-query';
import { favoritesQueryKey } from '@/resources/favorites';
import { REACT_QUERY_STORAGE_ID } from '@/storage/legacy';
// ============ clearReactQueryCache =========================================== //
/**
* Represents the entire persisted React Query cache.
*/
interface ReactQueryCache {
data: {
clientState: {
queries: CachedQuery[];
};
};
}
/**
* Represents an individual query record in the cache.
*/
interface CachedQuery {
queryKey: string | object[];
state?: {
data: unknown;
dataUpdatedAt?: number;
};
}
/**
* Clears the React Query cache, optionally preserving favorite-related queries.
*
* @param analyzeAfterClearing - If `true`, analyzes the cache after clearing. Defaults to `true` in dev and TestFlight envs.
* @param onComplete - A callback function that is called after the cache is cleared.
* @param preserveFavorites - If `true`, preserves favorites-related queries.
* @returns A promise that resolves after the cache is cleared.
*/
export async function clearReactQueryCache({
analyzeAfterClearing = !IS_STORE_INSTALL,
onComplete,
preserveFavorites = true,
}: {
analyzeAfterClearing?: boolean;
onComplete?: (deletedQueries: number, totalSizeMb: number) => void;
preserveFavorites?: boolean;
} = {}): Promise<void> {
let deletedQueries: number | null = null;
let totalSizeMb: number | null = null;
try {
if (preserveFavorites) {
const store = createMMKV({ id: REACT_QUERY_STORAGE_ID });
const storedString = store.getString(REACT_QUERY_STORAGE_ID);
if (!storedString) {
devLog('No queries found in storage');
return;
}
const parsedCache: ReactQueryCache = JSON.parse(storedString);
const clientState = parsedCache?.data?.clientState;
if (!clientState?.queries) {
devLog('No queries found in clientState');
return;
}
const { queries } = clientState;
const favorites = queries.filter(query => isFavoritesQuery(query));
devLog(`Found ${queries.length - favorites.length} queries to remove`);
// Re-save only favorites data
const filteredCache: ReactQueryCache = {
...parsedCache,
data: {
...parsedCache.data,
clientState: {
...clientState,
queries: favorites,
},
},
};
store.set(REACT_QUERY_STORAGE_ID, JSON.stringify(filteredCache));
queryClient.clear();
if (analyzeAfterClearing || onComplete) {
deletedQueries = queries.length - favorites.length;
totalSizeMb = storedString.length / 1024 / 1024;
}
devLog('Successfully removed all queries (except favorites)');
} else {
// Completely delete all queries
queryClient.clear();
const store = createMMKV({ id: REACT_QUERY_STORAGE_ID });
store.clearAll();
devLog('Successfully deleted all queries');
}
await forceRehydrateQueryClient();
onComplete?.(deletedQueries ?? 0, totalSizeMb ?? 0);
// Optionally run analysis
if (analyzeAfterClearing) {
analyzeReactQueryStore({
displayReport: true,
transformReport: report => {
// Insert deletion stats into the report
if (report[1]) {
const remainingQueries = parseFloat(report[1].title.split(' ')[1]);
report[1].title = `Preserved ${remainingQueries} ${remainingQueries === 1 ? 'Query' : 'Queries'}`;
}
return [
{ title: 'β
React Query Cache Cleared', message: '' },
{ title: `Deleted ${deletedQueries ?? 0} Queries`, message: '' },
{ title: `Total Size Deleted: ${(totalSizeMb ?? 0).toFixed(2)} MB`, message: '' },
...report,
];
},
});
}
} catch (error) {
if (preserveFavorites) {
logger.error(new RainbowError('First attempt to clear React Query cache failed (preserveFavorites = true):', error));
try {
clearReactQueryCache({ preserveFavorites: false });
} catch (retryError) {
logger.error(new RainbowError('Second attempt to clear React Query cache failed (preserveFavorites = false):', retryError));
}
} else {
logger.error(new RainbowError('Failed to clear React Query cache:', error));
}
}
}
// ============ forceRehydrateQueryClient ====================================== //
/**
* Reloads the latest persisted React Query data from MMKV and rehydrates the query client.
* @returns A promise that resolves after the query client is rehydrated.
*/
export async function forceRehydrateQueryClient(): Promise<void> {
try {
const persistedClient = await persistOptions.persister.restoreClient();
if (persistedClient?.clientState) {
hydrate(queryClient, persistedClient.clientState);
devLog('β
[forceRehydrateQueryClient] Query client successfully rehydrated from persisted state');
} else {
devLog('β [forceRehydrateQueryClient] No persisted state found for rehydration');
}
} catch (error) {
logger.error(new RainbowError('[forceRehydrateQueryClient] Error during forced React Query rehydration:', error));
}
}
// ============ analyzeReactQueryStore ========================================= //
interface QueryInfo {
lastUpdated?: number;
queryKey: string | object[];
size: number;
type: string;
}
/**
* Analyzes the persisted React Query cache and logs the results.
* Note: This is a development tool and should not be used in production.
*
* @param displayReport - Whether to display a sheet containing analysis results. Defaults to false in production, true in dev/testflight.
* @param logExtendedBreakdown - Whether to log an extended breakdown of queries. Defaults to true.
* @param logQueryDataShape - Whether to log the shape of the persisted data. Defaults to false.
* @param transformReport - A function that transforms the report array before display. Defaults to no transformation.
* @returns A formatted report array or undefined if not displayed.
*/
export function analyzeReactQueryStore({
displayReport = !IS_STORE_INSTALL,
logExtendedBreakdown = true,
logQueryDataShape: logQueryDataShapeParam = false,
transformReport = report => report,
}: {
displayReport?: boolean;
logExtendedBreakdown?: boolean;
logQueryDataShape?: boolean;
transformReport?: (report: LogEntry[]) => LogEntry[];
} = {}): ReturnType<typeof formatReport> | undefined {
if (IS_STORE_INSTALL) return;
devLog('\n[React Query Store Analysis]');
const report: Report | null = displayReport ? { title: { title: 'π¦ React Query Analysis', message: '' } } : null;
// Get the React Query cache directly from MMKV
const store = createMMKV({ id: REACT_QUERY_STORAGE_ID });
const storedData = store.getString(REACT_QUERY_STORAGE_ID);
if (!storedData) {
devLog('No React Query cache found');
if (displayReport) {
showLogSheet({
data: transformReport([{ title: 'React Query Analysis', message: 'No React Query cache found' }]),
});
}
return;
}
try {
const parsedCache: ReactQueryCache = JSON.parse(storedData);
// Optionally log the data shape
if (logQueryDataShapeParam) logQueryDataShape(parsedCache);
// Extract queries or early exit if none
const clientState = parsedCache?.data?.clientState;
if (!clientState?.queries) {
devLog('No queries found in clientState');
if (displayReport) {
showLogSheet({
data: transformReport([{ title: 'React Query Analysis', message: 'No queries found in the cache' }]),
});
}
return;
}
const queries = clientState.queries;
devLog(`\nFound ${queries.length} ${queries.length === 1 ? 'query' : 'queries'}`);
if (report) {
report.numberOfQueries = {
title: `Found ${queries.length} ${queries.length === 1 ? 'Query' : 'Queries'}`,
message: '',
};
}
// Build an array of QueryInfo objects
const querySizes = queries.map(query => {
const dataString = JSON.stringify(query.state?.data || {});
return {
size: dataString.length,
queryKey: query.queryKey,
lastUpdated: query.state?.dataUpdatedAt,
type: Array.isArray(query.queryKey) && query.queryKey.length > 1 ? String(query.queryKey[1]) : 'Unknown',
};
});
// Group queries by type
const sizeByType = buildSizeByType(querySizes);
// Report total size
logTotalSize(querySizes, report);
// Report size by type
logSizeByType(sizeByType, report);
// Report query size distribution
logQuerySizeDistribution(querySizes, report);
// Extended breakdown: Top 10 largest by aggregated type size
if (logExtendedBreakdown) logTop10BySize(sizeByType, report);
// Top 10 queries by number of cache entries
logTop10ByEntries(querySizes, report);
// Finally, display the analysis if requested
if (report) {
showLogSheet({ data: transformReport(formatReport(report)) });
return formatReport(report);
}
} catch (error) {
devLog('Error analyzing queries:', error);
if (error instanceof Error) {
devLog('Error message:', error.message);
devLog('Error stack:', error.stack);
}
}
}
/**
* Logs the shape of the React Query cache.
*/
function logQueryDataShape(parsedCache: ReactQueryCache) {
const analyzeShape = (obj: unknown, depth = 0): string => {
if (depth > 10) return 'Exceeds Maximum Depth';
if (obj === null) return 'null';
if (Array.isArray(obj)) {
if (!obj.length) return 'empty[]';
return `Array(${obj.length})<${analyzeShape(obj[0], depth + 1)}>`;
}
if (typeof obj === 'object') {
const entries = Object.entries(obj);
if (!entries.length) return '{}';
const indent = ' '.repeat((depth + 1) * 2);
const mapped = entries.map(([key, value]) => `${indent}${key}: ${analyzeShape(value, depth + 1)}`).join(',\n');
return `{\n${mapped}\n${' '.repeat(depth * 2)}}`;
}
return typeof obj;
};
devLog('Shape:', analyzeShape(parsedCache));
}
/**
* Builds a record of type -> { totalSize, queries[] } from an array of QueryInfo objects.
*/
function buildSizeByType(querySizes: QueryInfo[]): Record<string, { totalSize: number; queries: QueryInfo[] }> {
return querySizes.reduce<Record<string, { totalSize: number; queries: QueryInfo[] }>>((acc, entry) => {
if (!acc[entry.type]) acc[entry.type] = { totalSize: 0, queries: [] };
acc[entry.type].totalSize += entry.size;
acc[entry.type].queries.push(entry);
return acc;
}, {});
}
/**
* Logs the total size, adds it to the report, and returns its numerical value.
*/
function logTotalSize(querySizes: QueryInfo[], report: Report | null): number {
const totalSize = querySizes.reduce((sum, q) => sum + q.size, 0);
const sizeMbString = (totalSize / (1024 * 1024)).toFixed(2);
devLog(`\nTotal Size: ${sizeMbString} MB`);
if (report) report.totalSize = { title: `Total Size: ${sizeMbString} MB`, message: '' };
return totalSize;
}
/**
* Logs size by query type, sorted descending by total size.
*/
function logSizeByType(sizeByType: Record<string, { totalSize: number; queries: QueryInfo[] }>, report: Record<string, LogEntry> | null) {
devLog('\nSize by Query Type:');
if (report) report.sizeByType = { title: 'Size by Query Type', message: '' };
Object.entries(sizeByType)
.sort(([, a], [, b]) => b.totalSize - a.totalSize)
.forEach(([type, { totalSize }]) => {
const sizeMb = (totalSize / (1024 * 1024)).toFixed(2);
devLog(`${type}: ${sizeMb} MB`);
if (report) report.sizeByType.message += `${type}: ${sizeMb} MB\n`;
});
}
/**
* Logs query size distribution in defined buckets (<10KB, 10-100KB, etc.).
*/
function logQuerySizeDistribution(querySizes: QueryInfo[], report: Report | null) {
const sizeRanges = {
'1MB+': 0,
'500KB-1MB': 0,
'100KB-500KB': 0,
'10KB-100KB': 0,
'<10KB': 0,
};
querySizes.forEach(q => {
const sizeKB = q.size / 1024;
if (sizeKB >= 1024) sizeRanges['1MB+'] += 1;
else if (sizeKB >= 500) sizeRanges['500KB-1MB'] += 1;
else if (sizeKB >= 100) sizeRanges['100KB-500KB'] += 1;
else if (sizeKB >= 10) sizeRanges['10KB-100KB'] += 1;
else sizeRanges['<10KB'] += 1;
});
devLog('\nQuery Size Distribution:');
const logEntry: LogEntry = { title: 'Query Size Distribution', message: '' };
Object.entries(sizeRanges).forEach(([range, count]) => {
devLog(`${range}: ${count} ${count === 1 ? 'query' : 'queries'}`);
if (report) logEntry.message += `${range}: ${count} ${count === 1 ? 'query' : 'queries'}\n`;
});
if (report) report.querySizeDistribution = logEntry;
}
/**
* Logs the top 10 queries by size per query type.
*/
function logTop10BySize(sizeByType: Record<string, { totalSize: number; queries: QueryInfo[] }>, report: Report | null) {
devLog('\nTop 10 Largest Queries:');
const logEntry: LogEntry = { title: 'Top 10 Largest Queries', message: '' };
const sorted = Object.entries(sizeByType)
.sort(([, a], [, b]) => b.totalSize - a.totalSize)
.slice(0, 10);
sorted.forEach(([type, { totalSize, queries }], index) => {
// Find most recently updated query for this type
const mostRecentQuery = queries.reduce((latest, current) => {
if (!latest.lastUpdated) return current;
if (!current.lastUpdated) return latest;
return current.lastUpdated > latest.lastUpdated ? current : latest;
}, queries[0]);
devLog(`${index === 0 ? '' : '\n'}#${index + 1}:`);
if (report) logEntry.message += `${index === 0 ? '' : '\n'}#${index + 1}:\n`;
devLog(`Type: ${type}`);
if (report) logEntry.message += `Type: ${type}\n`;
const sizeStr = totalSize >= 1024 * 1024 ? (totalSize / (1024 * 1024)).toFixed(2) + ' MB' : (totalSize / 1024).toFixed(2) + ' KB';
devLog(`Total Size: ${sizeStr}`);
if (report) logEntry.message += `Total Size: ${sizeStr}\n`;
devLog(`Query Key: ${JSON.stringify(mostRecentQuery.queryKey)}`);
if (report) logEntry.message += `Example Query Key: ${JSON.stringify(mostRecentQuery.queryKey)}\n`;
if (mostRecentQuery.lastUpdated) {
const updatedAt = getDateFormatter('en-US', {
day: '2-digit',
hour: '2-digit',
hour12: true,
minute: '2-digit',
month: 'short',
year: 'numeric',
}).format(new Date(mostRecentQuery.lastUpdated));
devLog(`Last Updated: ${updatedAt}`);
if (report) logEntry.message += `Last Updated: ${updatedAt}\n`;
}
});
if (report) report.top10BySize = logEntry;
}
/**
* Logs the top 10 queries by number of cache entries per query type.
*/
function logTop10ByEntries(querySizes: QueryInfo[], report: Report | null) {
const queryCountByType = querySizes.reduce<Record<string, number>>((acc, entry) => {
acc[entry.type] = (acc[entry.type] ?? 0) + 1;
return acc;
}, {});
const sortedTypes = Object.entries(queryCountByType)
.sort(([, a], [, b]) => b - a)
.slice(0, 10);
let logEntry: LogEntry;
devLog('\nTop 10 Queries by Entries:');
if (report) logEntry = { title: 'Top 10 Queries by Entries', message: '' };
sortedTypes.forEach(([type, count], index) => {
devLog(`\n#${index + 1}:`);
if (report) logEntry.message += `${index === 0 ? '' : '\n'}#${index + 1}:\n`;
devLog(`Type: ${type}`);
if (report) logEntry.message += `Type: ${type}\n`;
devLog(`Number of Cache Entries: ${count}`);
if (report) logEntry.message += `Number of Cache Entries: ${count}\n`;
const exampleQuery = querySizes.find(q => q.type === type);
if (exampleQuery) {
devLog(`Example Query Key: ${JSON.stringify(exampleQuery.queryKey)}`);
if (report) logEntry.message += `Example Query Key: ${JSON.stringify(exampleQuery.queryKey)}\n`;
}
if (report) report.top10ByEntries = logEntry;
});
}
// ============ Utilities ====================================================== //
function isFavoritesQuery(query: CachedQuery): boolean {
return (
query.queryKey[1] === favoritesQueryKey[1] &&
typeof query.queryKey[2] === 'object' &&
'persisterVersion' in query.queryKey[2] &&
query.queryKey[2].persisterVersion === favoritesQueryKey[2].persisterVersion
);
}
/**
* Logs messages to the console in dev mode only.
*/
function devLog(...args: Parameters<typeof console.log>) {
if (IS_DEV) console.log(...args);
}
type Report = Partial<Record<ReportKeys, LogEntry>>;
/**
* Ordered keys that may be present in the report.
*/
enum ReportKeys {
title = 'title',
numberOfQueries = 'numberOfQueries',
totalSize = 'totalSize',
querySizeDistribution = 'querySizeDistribution',
sizeByType = 'sizeByType',
top10BySize = 'top10BySize',
top10ByEntries = 'top10ByEntries',
}
/**
* Formats a report object into an ordered array based on REPORT_ORDER.
*/
function formatReport(report: Report): LogEntry[] {
return Object.values(ReportKeys)
.map(key => report[key])
.filter(Boolean);
}