1// src/services/query-cache.js
2
3const queries = new Map();
4
5export const queryCache = {
6 /**
7 * Get cached query result
8 * @param {string} queryId - Query identifier (e.g., "timeline", "profile:handle")
9 * @returns {{ uris: string[], cursor: string|null, hasMore: boolean }|undefined}
10 */
11 get(queryId) {
12 return queries.get(queryId);
13 },
14
15 /**
16 * Set query result (replaces existing)
17 * @param {string} queryId - Query identifier
18 * @param {{ uris: string[], cursor: string|null, hasMore: boolean }} result
19 */
20 set(queryId, result) {
21 queries.set(queryId, {
22 uris: result.uris || [],
23 cursor: result.cursor || null,
24 hasMore: result.hasMore ?? true,
25 fetchedAt: Date.now()
26 });
27 },
28
29 /**
30 * Append to existing query result (for pagination)
31 * @param {string} queryId - Query identifier
32 * @param {{ uris: string[], cursor: string|null, hasMore: boolean }} result
33 */
34 append(queryId, result) {
35 const existing = queries.get(queryId);
36 if (existing) {
37 queries.set(queryId, {
38 uris: [...existing.uris, ...(result.uris || [])],
39 cursor: result.cursor || null,
40 hasMore: result.hasMore ?? true,
41 fetchedAt: existing.fetchedAt // preserve original fetch time
42 });
43 } else {
44 this.set(queryId, result);
45 }
46 },
47
48 /**
49 * Check if query is cached
50 * @param {string} queryId - Query identifier
51 * @returns {boolean}
52 */
53 has(queryId) {
54 return queries.has(queryId);
55 },
56
57 /**
58 * Clear all query cache (useful for logout or refresh)
59 */
60 clear() {
61 queries.clear();
62 }
63};