@mhjuma/traverse 0.1.0
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/package.json +46 -0
- package/src/bootstrap.spec.tsx +290 -0
- package/src/bootstrap.tsx +195 -0
- package/src/colors.ts +23 -0
- package/src/components/ChartPanel/ChartPanel.logic.spec.ts +358 -0
- package/src/components/ChartPanel/ChartPanel.logic.ts +318 -0
- package/src/components/ChartPanel/ChartPanel.spec.tsx +331 -0
- package/src/components/ChartPanel/ChartPanel.styles.ts +14 -0
- package/src/components/ChartPanel/ChartPanel.tsx +151 -0
- package/src/components/ConnectionDialog/ConnectionDialog.spec.tsx +767 -0
- package/src/components/ConnectionDialog/ConnectionDialog.tsx +652 -0
- package/src/components/ConnectionDialog/ConnectionRow.spec.tsx +104 -0
- package/src/components/ConnectionDialog/ConnectionRow.tsx +177 -0
- package/src/components/EditorToolbar/EditorToolbar.spec.tsx +599 -0
- package/src/components/EditorToolbar/EditorToolbar.tsx +259 -0
- package/src/components/Explorer/Explorer.spec.tsx +615 -0
- package/src/components/Explorer/Explorer.tsx +49 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.spec.ts +715 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +324 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +450 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.styles.ts +478 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +648 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +321 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.ts +155 -0
- package/src/components/ResultsPanel/ElapsedTimer.spec.tsx +51 -0
- package/src/components/ResultsPanel/ElapsedTimer.tsx +15 -0
- package/src/components/ResultsPanel/HistoryItem.spec.tsx +123 -0
- package/src/components/ResultsPanel/HistoryItem.tsx +56 -0
- package/src/components/ResultsPanel/ResultSetTab.tsx +18 -0
- package/src/components/ResultsPanel/ResultsPanel.spec.tsx +1194 -0
- package/src/components/ResultsPanel/ResultsPanel.tsx +321 -0
- package/src/components/ResultsPanel/TabButton.tsx +11 -0
- package/src/components/ResultsTable/ColumnHeader.tsx +42 -0
- package/src/components/ResultsTable/ColumnPickerItem.tsx +16 -0
- package/src/components/ResultsTable/FilterCell.tsx +24 -0
- package/src/components/ResultsTable/ResultsTable.logic.spec.ts +655 -0
- package/src/components/ResultsTable/ResultsTable.logic.ts +264 -0
- package/src/components/ResultsTable/ResultsTable.spec.tsx +930 -0
- package/src/components/ResultsTable/ResultsTable.styles.ts +108 -0
- package/src/components/ResultsTable/ResultsTable.tsx +501 -0
- package/src/components/ResultsTable/VirtualCell.spec.tsx +55 -0
- package/src/components/ResultsTable/VirtualCell.tsx +32 -0
- package/src/components/ResultsTable/VirtualRow.tsx +55 -0
- package/src/components/SchemaSidebar/FolderRow.tsx +35 -0
- package/src/components/SchemaSidebar/SchemaColumnItem.tsx +41 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.spec.tsx +148 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.tsx +49 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.spec.ts +230 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.ts +74 -0
- package/src/components/SchemaSidebar/SchemaSidebar.spec.tsx +1111 -0
- package/src/components/SchemaSidebar/SchemaSidebar.tsx +244 -0
- package/src/components/SchemaSidebar/SchemaTableItem.tsx +77 -0
- package/src/components/TabBar/TabBar.logic.spec.ts +127 -0
- package/src/components/TabBar/TabBar.logic.ts +35 -0
- package/src/components/TabBar/TabBar.spec.tsx +929 -0
- package/src/components/TabBar/TabBar.tsx +284 -0
- package/src/components/TabBar/TabItem.tsx +166 -0
- package/src/components/VegaChart/VegaChart.spec.tsx +126 -0
- package/src/components/VegaChart/VegaChart.tsx +224 -0
- package/src/config.spec.ts +44 -0
- package/src/config.ts +47 -0
- package/src/context/ExplorerColorContext.ts +14 -0
- package/src/context/ExplorerStateContext.ts +49 -0
- package/src/context/KustoClientContext.ts +12 -0
- package/src/context/context.spec.tsx +183 -0
- package/src/index.ts +14 -0
- package/src/services/cache.spec.ts +31 -0
- package/src/services/cache.ts +16 -0
- package/src/services/concurrency.spec.ts +139 -0
- package/src/services/concurrency.ts +46 -0
- package/src/services/kusto.spec.ts +919 -0
- package/src/services/kusto.ts +472 -0
- package/src/services/queryHistory.spec.ts +287 -0
- package/src/services/queryHistory.ts +146 -0
- package/src/services/schema.spec.ts +618 -0
- package/src/services/schema.ts +294 -0
- package/src/services/state-service.spec.ts +670 -0
- package/src/services/state-service.ts +602 -0
- package/src/services/useKustoQuery.spec.ts +234 -0
- package/src/services/useKustoQuery.ts +124 -0
- package/src/state/actions.ts +50 -0
- package/src/state/index.ts +3 -0
- package/src/state/reducer.spec.ts +644 -0
- package/src/state/reducer.ts +207 -0
- package/src/state/types.ts +54 -0
- package/tsconfig.json +7 -0
- package/tsconfig.lint.json +4 -0
- package/tsconfig.spec.json +7 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized state service — two-tier architecture:
|
|
3
|
+
* Memory layer (all reads, instant) + IndexedDB (persistence, write-behind).
|
|
4
|
+
*
|
|
5
|
+
* Config store additionally mirrors to localStorage for synchronous bootstrap
|
|
6
|
+
* on app startup (avoids theme flash).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Types
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
export type StoreName = 'config' | 'queryCache' | 'explorerCache' | 'explorerConnections';
|
|
14
|
+
|
|
15
|
+
export interface StoreEntry<T = unknown> {
|
|
16
|
+
value: T;
|
|
17
|
+
timestamp: number;
|
|
18
|
+
/** Per-entry TTL override in ms. Falls back to store default when absent. */
|
|
19
|
+
ttl?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface StoreEntryInfo {
|
|
23
|
+
key: string;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
ttl?: number;
|
|
26
|
+
/** Approximate byte size of the serialized value. */
|
|
27
|
+
size: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface StoreStats {
|
|
31
|
+
stores: Record<StoreName, { entries: number; totalSize: number; oldestTimestamp: number | null }>;
|
|
32
|
+
totalEntries: number;
|
|
33
|
+
totalSize: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface StoreConfig {
|
|
37
|
+
/** Default TTL in ms. `undefined` = no expiry. */
|
|
38
|
+
defaultTtlMs?: number;
|
|
39
|
+
/** Maximum entries. Oldest evicted on write. `undefined` = unlimited. */
|
|
40
|
+
maxEntries?: number;
|
|
41
|
+
/** Whether to persist to IndexedDB. */
|
|
42
|
+
persist: boolean;
|
|
43
|
+
/** Whether to mirror writes to localStorage for synchronous reads on next startup. */
|
|
44
|
+
syncBootstrap: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// Store configuration
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
const STORE_CONFIGS: Record<StoreName, StoreConfig> = {
|
|
52
|
+
config: { persist: true, syncBootstrap: true },
|
|
53
|
+
queryCache: { defaultTtlMs: 15 * 60 * 1000, persist: true, syncBootstrap: false },
|
|
54
|
+
explorerCache: { maxEntries: 200, persist: true, syncBootstrap: false },
|
|
55
|
+
explorerConnections: { persist: true, syncBootstrap: true },
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const STORE_NAMES = Object.keys(STORE_CONFIGS) as StoreName[];
|
|
59
|
+
|
|
60
|
+
const IDB_NAME = 'traverse-state';
|
|
61
|
+
const IDB_VERSION = 1;
|
|
62
|
+
const LS_PREFIX = 'traverse-state:';
|
|
63
|
+
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
// Helpers
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
function lsKey(store: StoreName, key: string): string {
|
|
69
|
+
return `${LS_PREFIX}${store}:${key}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function estimateSize(value: unknown): number {
|
|
73
|
+
try {
|
|
74
|
+
return JSON.stringify(value).length * 2; // rough byte estimate (UTF-16)
|
|
75
|
+
} catch {
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function isExpired(entry: StoreEntry, config: StoreConfig): boolean {
|
|
81
|
+
const ttl = entry.ttl ?? config.defaultTtlMs;
|
|
82
|
+
if (ttl === undefined) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
return Date.now() - entry.timestamp > ttl;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// IndexedDB helpers
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
function openIDB(): Promise<IDBDatabase> {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const request = indexedDB.open(IDB_NAME, IDB_VERSION);
|
|
95
|
+
request.onupgradeneeded = () => {
|
|
96
|
+
const db = request.result;
|
|
97
|
+
for (const name of STORE_NAMES) {
|
|
98
|
+
if (!db.objectStoreNames.contains(name)) {
|
|
99
|
+
db.createObjectStore(name);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
request.onsuccess = () => resolve(request.result);
|
|
104
|
+
request.onerror = () => reject(request.error);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function idbReadAll(db: IDBDatabase, storeName: StoreName): Promise<Map<string, StoreEntry>> {
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
const tx = db.transaction(storeName, 'readonly');
|
|
111
|
+
const store = tx.objectStore(storeName);
|
|
112
|
+
const result = new Map<string, StoreEntry>();
|
|
113
|
+
const cursor = store.openCursor();
|
|
114
|
+
cursor.onsuccess = () => {
|
|
115
|
+
const c = cursor.result;
|
|
116
|
+
if (c) {
|
|
117
|
+
result.set(c.key as string, c.value as StoreEntry);
|
|
118
|
+
c.continue();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
tx.oncomplete = () => resolve(result);
|
|
122
|
+
tx.onerror = () => reject(tx.error);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function idbPut(db: IDBDatabase, storeName: StoreName, key: string, entry: StoreEntry): void {
|
|
127
|
+
try {
|
|
128
|
+
const tx = db.transaction(storeName, 'readwrite');
|
|
129
|
+
tx.objectStore(storeName).put(entry, key);
|
|
130
|
+
} catch {
|
|
131
|
+
// IndexedDB unavailable — memory layer still works
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function idbDelete(db: IDBDatabase, storeName: StoreName, key: string): void {
|
|
136
|
+
try {
|
|
137
|
+
const tx = db.transaction(storeName, 'readwrite');
|
|
138
|
+
tx.objectStore(storeName).delete(key);
|
|
139
|
+
} catch {
|
|
140
|
+
// silently skip
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function idbClear(db: IDBDatabase, storeName: StoreName): void {
|
|
145
|
+
try {
|
|
146
|
+
const tx = db.transaction(storeName, 'readwrite');
|
|
147
|
+
tx.objectStore(storeName).clear();
|
|
148
|
+
} catch {
|
|
149
|
+
// silently skip
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
// StateService
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
export class StateService {
|
|
158
|
+
private memory = new Map<StoreName, Map<string, StoreEntry>>();
|
|
159
|
+
private listeners = new Map<StoreName, Map<string, Set<() => void>>>();
|
|
160
|
+
private db: IDBDatabase | null = null;
|
|
161
|
+
private hydratePromise: Promise<void> | null = null;
|
|
162
|
+
private _hydrated = false;
|
|
163
|
+
|
|
164
|
+
// Snapshot caching — ensures useSyncExternalStore gets stable references
|
|
165
|
+
private storeVersions = new Map<StoreName, number>();
|
|
166
|
+
private entriesCache = new Map<StoreName, { version: number; value: StoreEntryInfo[] }>();
|
|
167
|
+
private statsCache: { version: number; value: StoreStats } | null = null;
|
|
168
|
+
private globalVersion = 0;
|
|
169
|
+
|
|
170
|
+
constructor() {
|
|
171
|
+
for (const name of STORE_NAMES) {
|
|
172
|
+
this.memory.set(name, new Map());
|
|
173
|
+
this.listeners.set(name, new Map());
|
|
174
|
+
this.storeVersions.set(name, 0);
|
|
175
|
+
}
|
|
176
|
+
// Synchronous bootstrap: read config and explorerConnections from localStorage
|
|
177
|
+
this.bootstrapFromLocalStorage('config');
|
|
178
|
+
this.bootstrapFromLocalStorage('explorerConnections');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// --- Bootstrap (sync) ---
|
|
182
|
+
|
|
183
|
+
private bootstrapFromLocalStorage(store: StoreName): void {
|
|
184
|
+
try {
|
|
185
|
+
const prefix = `${LS_PREFIX}${store}:`;
|
|
186
|
+
const storeMap = this.memory.get(store)!;
|
|
187
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
188
|
+
const k = localStorage.key(i);
|
|
189
|
+
if (k?.startsWith(prefix)) {
|
|
190
|
+
try {
|
|
191
|
+
const entry = JSON.parse(localStorage.getItem(k)!) as StoreEntry;
|
|
192
|
+
const storeKey = k.slice(prefix.length);
|
|
193
|
+
if (!isExpired(entry, STORE_CONFIGS[store])) {
|
|
194
|
+
storeMap.set(storeKey, entry);
|
|
195
|
+
}
|
|
196
|
+
} catch { /* skip corrupt entries */ }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} catch {
|
|
200
|
+
// localStorage inaccessible (e.g. in some test environments)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// --- Hydration (async) ---
|
|
205
|
+
|
|
206
|
+
get hydrated(): boolean {
|
|
207
|
+
return this._hydrated;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
hydrate(): Promise<void> {
|
|
211
|
+
if (this.hydratePromise) {
|
|
212
|
+
return this.hydratePromise;
|
|
213
|
+
}
|
|
214
|
+
this.hydratePromise = this.doHydrate();
|
|
215
|
+
return this.hydratePromise;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private async doHydrate(): Promise<void> {
|
|
219
|
+
// Phase 1: Try to load persisted state from IndexedDB
|
|
220
|
+
try {
|
|
221
|
+
this.db = await openIDB();
|
|
222
|
+
for (const name of STORE_NAMES) {
|
|
223
|
+
try {
|
|
224
|
+
const entries = await idbReadAll(this.db, name);
|
|
225
|
+
const storeMap = this.memory.get(name)!;
|
|
226
|
+
const config = STORE_CONFIGS[name];
|
|
227
|
+
for (const [key, entry] of entries) {
|
|
228
|
+
if (isExpired(entry, config)) {
|
|
229
|
+
idbDelete(this.db, name, key);
|
|
230
|
+
} else if (!storeMap.has(key)) {
|
|
231
|
+
// Don't override bootstrap values — they may be newer (user changed theme before hydrate)
|
|
232
|
+
storeMap.set(key, entry);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
} catch {
|
|
236
|
+
// Skip individual store failures — memory layer still works
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
} catch {
|
|
240
|
+
// IndexedDB completely unavailable — app runs with memory-only
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Phase 2: Persist any bootstrap entries to IndexedDB
|
|
244
|
+
if (this.db) {
|
|
245
|
+
for (const name of STORE_NAMES) {
|
|
246
|
+
const storeMap = this.memory.get(name)!;
|
|
247
|
+
for (const [key, entry] of storeMap) {
|
|
248
|
+
idbPut(this.db, name, key, entry);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
this._hydrated = true;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// --- Read ---
|
|
257
|
+
|
|
258
|
+
get<T>(store: StoreName, key: string): T | null {
|
|
259
|
+
const storeMap = this.memory.get(store);
|
|
260
|
+
if (!storeMap) {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
const entry = storeMap.get(key);
|
|
264
|
+
if (!entry) {
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
if (isExpired(entry, STORE_CONFIGS[store])) {
|
|
268
|
+
this.delete(store, key);
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
return entry.value as T;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
has(store: StoreName, key: string): boolean {
|
|
275
|
+
return this.get(store, key) !== null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// --- Write ---
|
|
279
|
+
|
|
280
|
+
set<T>(store: StoreName, key: string, value: T, options?: { ttl?: number }): void {
|
|
281
|
+
const storeMap = this.memory.get(store);
|
|
282
|
+
if (!storeMap) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const config = STORE_CONFIGS[store];
|
|
286
|
+
const entry: StoreEntry = {
|
|
287
|
+
value,
|
|
288
|
+
timestamp: Date.now(),
|
|
289
|
+
ttl: options?.ttl,
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
storeMap.set(key, entry);
|
|
293
|
+
|
|
294
|
+
// Max-entry eviction (LRU by timestamp)
|
|
295
|
+
if (config.maxEntries !== undefined && storeMap.size > config.maxEntries) {
|
|
296
|
+
this.evictOldest(store, storeMap, storeMap.size - config.maxEntries);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Notify listeners
|
|
300
|
+
this.notify(store, key);
|
|
301
|
+
|
|
302
|
+
// Write-behind to IndexedDB
|
|
303
|
+
if (config.persist && this.db) {
|
|
304
|
+
idbPut(this.db, store, key, entry);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Sync bootstrap to localStorage
|
|
308
|
+
if (config.syncBootstrap) {
|
|
309
|
+
try {
|
|
310
|
+
localStorage.setItem(lsKey(store, key), JSON.stringify(entry));
|
|
311
|
+
} catch { /* quota exceeded — memory + IDB still work */ }
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// --- Delete ---
|
|
316
|
+
|
|
317
|
+
delete(store: StoreName, key: string): void {
|
|
318
|
+
const storeMap = this.memory.get(store);
|
|
319
|
+
if (!storeMap) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
const existed = storeMap.delete(key);
|
|
323
|
+
if (existed) {
|
|
324
|
+
this.notify(store, key);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const config = STORE_CONFIGS[store];
|
|
328
|
+
if (config.persist && this.db) {
|
|
329
|
+
idbDelete(this.db, store, key);
|
|
330
|
+
}
|
|
331
|
+
if (config.syncBootstrap) {
|
|
332
|
+
try { localStorage.removeItem(lsKey(store, key)); } catch { /* ignore */ }
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// --- Clear ---
|
|
337
|
+
|
|
338
|
+
clear(store: StoreName): void {
|
|
339
|
+
const storeMap = this.memory.get(store);
|
|
340
|
+
if (!storeMap || storeMap.size === 0) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const keys = [...storeMap.keys()];
|
|
344
|
+
storeMap.clear();
|
|
345
|
+
// Notify all listeners in this store
|
|
346
|
+
for (const key of keys) {
|
|
347
|
+
this.notify(store, key);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const config = STORE_CONFIGS[store];
|
|
351
|
+
if (config.persist && this.db) {
|
|
352
|
+
idbClear(this.db, store);
|
|
353
|
+
}
|
|
354
|
+
if (config.syncBootstrap) {
|
|
355
|
+
this.clearLocalStoragePrefix(store);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// --- Enumeration ---
|
|
360
|
+
|
|
361
|
+
keys(store: StoreName): string[] {
|
|
362
|
+
const storeMap = this.memory.get(store);
|
|
363
|
+
if (!storeMap) {
|
|
364
|
+
return [];
|
|
365
|
+
}
|
|
366
|
+
// Filter out expired entries
|
|
367
|
+
const result: string[] = [];
|
|
368
|
+
const config = STORE_CONFIGS[store];
|
|
369
|
+
for (const [key, entry] of storeMap) {
|
|
370
|
+
if (!isExpired(entry, config)) {
|
|
371
|
+
result.push(key);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return result;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
entries(store: StoreName): StoreEntryInfo[] {
|
|
378
|
+
const version = this.storeVersions.get(store) ?? 0;
|
|
379
|
+
const cached = this.entriesCache.get(store);
|
|
380
|
+
if (cached && cached.version === version) {
|
|
381
|
+
return cached.value;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const storeMap = this.memory.get(store);
|
|
385
|
+
if (!storeMap) {
|
|
386
|
+
return [];
|
|
387
|
+
}
|
|
388
|
+
const config = STORE_CONFIGS[store];
|
|
389
|
+
const result: StoreEntryInfo[] = [];
|
|
390
|
+
for (const [key, entry] of storeMap) {
|
|
391
|
+
if (!isExpired(entry, config)) {
|
|
392
|
+
result.push({
|
|
393
|
+
key,
|
|
394
|
+
timestamp: entry.timestamp,
|
|
395
|
+
ttl: entry.ttl ?? config.defaultTtlMs,
|
|
396
|
+
size: estimateSize(entry.value),
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const sorted = result.sort((a, b) => b.timestamp - a.timestamp);
|
|
401
|
+
this.entriesCache.set(store, { version, value: sorted });
|
|
402
|
+
return sorted;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
entryCount(store: StoreName): number {
|
|
406
|
+
return this.keys(store).length;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
stats(): StoreStats {
|
|
410
|
+
if (this.statsCache && this.statsCache.version === this.globalVersion) {
|
|
411
|
+
return this.statsCache.value;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
let totalEntries = 0;
|
|
415
|
+
let totalSize = 0;
|
|
416
|
+
const stores = {} as StoreStats['stores'];
|
|
417
|
+
|
|
418
|
+
for (const name of STORE_NAMES) {
|
|
419
|
+
const infos = this.entries(name);
|
|
420
|
+
const storeSize = infos.reduce((sum, e) => sum + e.size, 0);
|
|
421
|
+
const oldest = infos.length > 0 ? Math.min(...infos.map((e) => e.timestamp)) : null;
|
|
422
|
+
stores[name] = { entries: infos.length, totalSize: storeSize, oldestTimestamp: oldest };
|
|
423
|
+
totalEntries += infos.length;
|
|
424
|
+
totalSize += storeSize;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const value: StoreStats = { stores, totalEntries, totalSize };
|
|
428
|
+
this.statsCache = { version: this.globalVersion, value };
|
|
429
|
+
return value;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// --- Subscribe ---
|
|
433
|
+
|
|
434
|
+
subscribe(store: StoreName, key: string, callback: () => void): () => void {
|
|
435
|
+
const storeListeners = this.listeners.get(store);
|
|
436
|
+
if (!storeListeners) {
|
|
437
|
+
return () => {};
|
|
438
|
+
}
|
|
439
|
+
let keyListeners = storeListeners.get(key);
|
|
440
|
+
if (!keyListeners) {
|
|
441
|
+
keyListeners = new Set();
|
|
442
|
+
storeListeners.set(key, keyListeners);
|
|
443
|
+
}
|
|
444
|
+
const listeners = keyListeners;
|
|
445
|
+
listeners.add(callback);
|
|
446
|
+
return () => {
|
|
447
|
+
listeners.delete(callback);
|
|
448
|
+
if (listeners.size === 0) {
|
|
449
|
+
storeListeners.delete(key);
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Subscribe to all changes in a store (for admin UI). */
|
|
455
|
+
subscribeStore(store: StoreName, callback: () => void): () => void {
|
|
456
|
+
const storeListeners = this.listeners.get(store);
|
|
457
|
+
if (!storeListeners) {
|
|
458
|
+
return () => {};
|
|
459
|
+
}
|
|
460
|
+
const STORE_KEY = '__store__';
|
|
461
|
+
let keyListeners = storeListeners.get(STORE_KEY);
|
|
462
|
+
if (!keyListeners) {
|
|
463
|
+
keyListeners = new Set();
|
|
464
|
+
storeListeners.set(STORE_KEY, keyListeners);
|
|
465
|
+
}
|
|
466
|
+
const listeners = keyListeners;
|
|
467
|
+
listeners.add(callback);
|
|
468
|
+
return () => {
|
|
469
|
+
listeners.delete(callback);
|
|
470
|
+
if (listeners.size === 0) {
|
|
471
|
+
storeListeners.delete(STORE_KEY);
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// --- Debug ---
|
|
477
|
+
|
|
478
|
+
dump(): Record<string, Record<string, unknown>> {
|
|
479
|
+
const result: Record<string, Record<string, unknown>> = {};
|
|
480
|
+
for (const name of STORE_NAMES) {
|
|
481
|
+
const storeMap = this.memory.get(name);
|
|
482
|
+
if (!storeMap) {
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
const entries: Record<string, unknown> = {};
|
|
486
|
+
for (const [key, entry] of storeMap) {
|
|
487
|
+
entries[key] = entry.value;
|
|
488
|
+
}
|
|
489
|
+
result[name] = entries;
|
|
490
|
+
}
|
|
491
|
+
return result;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** Export full state as JSON (for backup/debugging). */
|
|
495
|
+
exportState(): string {
|
|
496
|
+
const data: Record<string, Record<string, StoreEntry>> = {};
|
|
497
|
+
for (const name of STORE_NAMES) {
|
|
498
|
+
const storeMap = this.memory.get(name);
|
|
499
|
+
if (!storeMap) {
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
const entries: Record<string, StoreEntry> = {};
|
|
503
|
+
for (const [key, entry] of storeMap) {
|
|
504
|
+
entries[key] = entry;
|
|
505
|
+
}
|
|
506
|
+
if (Object.keys(entries).length > 0) {
|
|
507
|
+
data[name] = entries;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return JSON.stringify(data, null, 2);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/** Import state from a previously exported JSON string. */
|
|
514
|
+
importState(json: string): void {
|
|
515
|
+
let data: unknown;
|
|
516
|
+
try {
|
|
517
|
+
data = JSON.parse(json);
|
|
518
|
+
} catch {
|
|
519
|
+
throw new Error('importState: invalid JSON');
|
|
520
|
+
}
|
|
521
|
+
if (typeof data !== 'object' || data === null || Array.isArray(data)) {
|
|
522
|
+
throw new Error('importState: expected a JSON object');
|
|
523
|
+
}
|
|
524
|
+
for (const [storeName, entries] of Object.entries(data as Record<string, unknown>)) {
|
|
525
|
+
if (!STORE_NAMES.includes(storeName as StoreName)) {
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
if (typeof entries !== 'object' || entries === null || Array.isArray(entries)) {
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
const store = storeName as StoreName;
|
|
532
|
+
for (const [key, entry] of Object.entries(entries as Record<string, unknown>)) {
|
|
533
|
+
if (typeof entry !== 'object' || entry === null) {
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
const e = entry as Record<string, unknown>;
|
|
537
|
+
if (!('value' in e)) {
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
this.set(store, key, e['value'], { ttl: typeof e['ttl'] === 'number' ? e['ttl'] : undefined });
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// --- Internals ---
|
|
546
|
+
|
|
547
|
+
private notify(store: StoreName, key: string): void {
|
|
548
|
+
// Bump version counters to invalidate snapshot caches
|
|
549
|
+
this.storeVersions.set(store, (this.storeVersions.get(store) ?? 0) + 1);
|
|
550
|
+
this.globalVersion++;
|
|
551
|
+
|
|
552
|
+
const storeListeners = this.listeners.get(store);
|
|
553
|
+
if (!storeListeners) {
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
// Notify key-specific listeners
|
|
557
|
+
const keyListeners = storeListeners.get(key);
|
|
558
|
+
if (keyListeners) {
|
|
559
|
+
for (const cb of keyListeners) {
|
|
560
|
+
cb();
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
// Notify store-wide listeners
|
|
564
|
+
const storeWide = storeListeners.get('__store__');
|
|
565
|
+
if (storeWide) {
|
|
566
|
+
for (const cb of storeWide) {
|
|
567
|
+
cb();
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
private evictOldest(store: StoreName, storeMap: Map<string, StoreEntry>, count: number): void {
|
|
573
|
+
const sorted = [...storeMap.entries()].sort((a, b) => a[1].timestamp - b[1].timestamp);
|
|
574
|
+
for (let i = 0; i < count && i < sorted.length; i++) {
|
|
575
|
+
const [key] = sorted[i];
|
|
576
|
+
storeMap.delete(key);
|
|
577
|
+
if (this.db) {
|
|
578
|
+
idbDelete(this.db, store, key);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
private clearLocalStoragePrefix(store: StoreName): void {
|
|
584
|
+
try {
|
|
585
|
+
const prefix = `${LS_PREFIX}${store}:`;
|
|
586
|
+
const keysToRemove: string[] = [];
|
|
587
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
588
|
+
const k = localStorage.key(i);
|
|
589
|
+
if (k?.startsWith(prefix)) {
|
|
590
|
+
keysToRemove.push(k);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
keysToRemove.forEach((k) => localStorage.removeItem(k));
|
|
594
|
+
} catch { /* ignore */ }
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// ---------------------------------------------------------------------------
|
|
599
|
+
// Singleton
|
|
600
|
+
// ---------------------------------------------------------------------------
|
|
601
|
+
|
|
602
|
+
export const stateService = new StateService();
|