Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 594x 594x 594x 594x 594x 594x 594x 594x 594x 594x 594x 594x 594x 594x 35x 35x 35x 18x 35x 17x 17x 35x 35x 594x 36x 36x 36x 36x 36x 36x 594x 8x 8x 8x 8x 8x 8x 8x 8x 2x 6x 8x 8x 8x 594x 594x 594x 594x 594x 27x 27x 27x 25x 25x 27x 27x 27x 594x | import { Logger } from "./logger";
/**
* Generic resource state management class
*
* This class provides a generic key-value state management system with:
* - Automatic cleanup (keeps last N items per resource type)
* - Key to object mapping for fast lookups
* - Ordered key tracking for proper cleanup
*/
export class ResourceState<T> {
private resourceMap: Map<string | number, T> = new Map();
private keyArray: (string | number)[] = [];
private readonly maxItems: number;
private readonly resourceType: string;
constructor(resourceType: string, maxItems: number = 100) {
const logger = Logger.getInstance();
this.resourceType = resourceType;
this.maxItems = maxItems;
logger.debug(
`ResourceState initialized for ${resourceType} with max ${maxItems} items`
);
}
/**
* Update resources - saves items with key mapping and manages item limit
* @param items Array of items to save
* @param keyExtractor Function to extract key from item
*/
updateResources(
items: T[],
keyExtractor: (item: T) => string | number
): void {
const logger = Logger.getInstance();
logger.debug(
`Updating ${this.resourceType}: ${items.length} items received`
);
// Add new items to the map and track their keys
const newKeys: (string | number)[] = [];
for (const item of items) {
const key = keyExtractor(item);
this.resourceMap.set(key, item);
newKeys.push(key);
// Add to array if not already present
if (!this.keyArray.includes(key)) {
this.keyArray.push(key);
}
}
logger.debug(
`Added ${newKeys.length} new ${this.resourceType} keys: [${newKeys.join(", ")}]`
);
// Clean up old items if we exceed the limit
if (this.keyArray.length > this.maxItems) {
const itemsToRemove = this.keyArray.length - this.maxItems;
const removedKeys = this.keyArray.splice(0, itemsToRemove);
// Remove from map as well
for (const removedKey of removedKeys) {
this.resourceMap.delete(removedKey);
}
logger.debug(
`Cleaned up ${itemsToRemove} old ${this.resourceType}. Removed keys: [${removedKeys.join(", ")}]`
);
}
logger.info(
`${this.resourceType} state updated. Total items in cache: ${this.resourceMap.size}, Key array length: ${this.keyArray.length}`
);
}
/**
* Get a resource by key
* @param key The key of the resource to retrieve
* @returns The resource object or undefined if not found
*/
getResource(key: string | number): T | undefined {
const logger = Logger.getInstance();
const resource = this.resourceMap.get(key);
if (resource) {
logger.debug(`Retrieved ${this.resourceType} ${key} from cache`);
} else {
logger.debug(`${this.resourceType} ${key} not found in cache`);
}
return resource;
}
/**
* Clear all cached resources
*/
clearResources(): void {
const logger = Logger.getInstance();
const clearedCount = this.resourceMap.size;
this.resourceMap.clear();
this.keyArray.length = 0;
logger.info(`Cleared ${clearedCount} ${this.resourceType} from cache`);
}
/**
* Get state statistics for debugging
* @returns Object with state statistics
*/
getStats(): {
resourceType: string;
itemCount: number;
maxItems: number;
oldestKey: string | number | null;
newestKey: string | number | null;
keys: (string | number)[];
} {
return {
resourceType: this.resourceType,
itemCount: this.resourceMap.size,
maxItems: this.maxItems,
oldestKey: this.keyArray.length > 0 ? this.keyArray[0] : null,
newestKey:
this.keyArray.length > 0
? this.keyArray[this.keyArray.length - 1]
: null,
keys: [...this.keyArray],
};
}
/**
* Check if a resource exists in cache
* @param key The key of the resource to check
* @returns True if resource exists, false otherwise
*/
hasResource(key: string | number): boolean {
return this.resourceMap.has(key);
}
/**
* Get all cached keys
* @returns Array of all cached keys
*/
getAllKeys(): (string | number)[] {
return [...this.keyArray];
}
/**
* Get all cached resources
* @returns Array of all cached resources
*/
getAllResources(): T[] {
return Array.from(this.resourceMap.values());
}
/**
* Get the number of cached resources
* @returns Number of resources in cache
*/
getCount(): number {
return this.resourceMap.size;
}
/**
* Set a single resource by key (simpler alternative to updateResources for single items)
* @param key The key for the resource
* @param resource The resource to store
*/
setResource(key: string | number, resource: T): void {
const logger = Logger.getInstance();
// Add to map
this.resourceMap.set(key, resource);
// Add to array if not already present
if (!this.keyArray.includes(key)) {
this.keyArray.push(key);
}
// Clean up old items if we exceed the limit
if (this.keyArray.length > this.maxItems) {
const itemsToRemove = this.keyArray.length - this.maxItems;
const removedKeys = this.keyArray.splice(0, itemsToRemove);
// Remove from map as well
for (const removedKey of removedKeys) {
this.resourceMap.delete(removedKey);
}
logger.debug(
`Cleaned up ${itemsToRemove} old ${this.resourceType}. Removed keys: [${removedKeys.join(", ")}]`
);
}
logger.debug(`Set ${this.resourceType} ${key} in cache`);
}
}
|