jopi-toolkit 3.1.22 → 3.1.32
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/dist/jk_fs/jBundler_ifServer.js +15 -9
- package/dist/jk_fs/jBundler_ifServer.js.map +1 -1
- package/dist/jk_logs/index.js +2 -2
- package/dist/jk_logs/index.js.map +1 -1
- package/dist/jk_memcache/_logs.d.ts +1 -0
- package/dist/jk_memcache/_logs.js +3 -0
- package/dist/jk_memcache/_logs.js.map +1 -0
- package/dist/jk_memcache/index.d.ts +127 -0
- package/dist/jk_memcache/index.js +415 -0
- package/dist/jk_memcache/index.js.map +1 -0
- package/dist/jk_tools/common.d.ts +6 -0
- package/dist/jk_tools/common.js +6 -0
- package/dist/jk_tools/common.js.map +1 -1
- package/package.json +8 -4
- package/src/jk_fs/jBundler_ifServer.ts +8 -2
- package/src/jk_logs/index.ts +2 -2
- package/src/jk_memcache/README.md +66 -0
- package/src/jk_memcache/_logs.ts +3 -0
- package/src/jk_memcache/index.ts +498 -0
- package/src/jk_tools/common.ts +6 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import { logMemCache } from "./_logs.ts";
|
|
2
|
+
|
|
3
|
+
export interface CacheEntryOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Importance level. Higher value means less likely to be evicted by GC.
|
|
6
|
+
* Default: 1
|
|
7
|
+
*/
|
|
8
|
+
importance?: number;
|
|
9
|
+
/**
|
|
10
|
+
* Time to live in milliseconds.
|
|
11
|
+
*/
|
|
12
|
+
ttl?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Absolute expiration date.
|
|
15
|
+
*/
|
|
16
|
+
expiresAt?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Arbitrary metadata (optional).
|
|
19
|
+
*/
|
|
20
|
+
meta?: any;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface InternalCacheEntry {
|
|
24
|
+
key: string;
|
|
25
|
+
value: string | ArrayBuffer | Uint8Array;
|
|
26
|
+
type: 'string' | 'buffer' | 'json';
|
|
27
|
+
size: number;
|
|
28
|
+
createdAt: number;
|
|
29
|
+
expiresAt: number | null;
|
|
30
|
+
accessCount: number;
|
|
31
|
+
importance: number;
|
|
32
|
+
meta: any;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface JkMemCacheOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Name of the cache instance (used for logs).
|
|
38
|
+
*/
|
|
39
|
+
name: string;
|
|
40
|
+
/**
|
|
41
|
+
* Maximum number of items in cache.
|
|
42
|
+
* Default: Infinity
|
|
43
|
+
*/
|
|
44
|
+
maxCount?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Maximum size in bytes.
|
|
47
|
+
* Default: 50MB (50 * 1024 * 1024)
|
|
48
|
+
*/
|
|
49
|
+
maxSize?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Interval in milliseconds for the recurrent GC.
|
|
52
|
+
* Default: 60000 (1 minute)
|
|
53
|
+
*/
|
|
54
|
+
cleanupInterval?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class JkMemCache {
|
|
58
|
+
private _storage = new Map<string, InternalCacheEntry>();
|
|
59
|
+
private _currentSize = 0;
|
|
60
|
+
private _options: Required<JkMemCacheOptions>;
|
|
61
|
+
private _intervalId: any = null;
|
|
62
|
+
private _name: string;
|
|
63
|
+
|
|
64
|
+
constructor(options: JkMemCacheOptions) {
|
|
65
|
+
this._options = {
|
|
66
|
+
name: options.name,
|
|
67
|
+
maxCount: options.maxCount ?? Infinity,
|
|
68
|
+
maxSize: options.maxSize ?? 50 * 1024 * 1024,
|
|
69
|
+
cleanupInterval: options.cleanupInterval ?? 60000,
|
|
70
|
+
};
|
|
71
|
+
this._name = options.name;
|
|
72
|
+
|
|
73
|
+
logMemCache.info(`Cache [${this._name}] initialized`);
|
|
74
|
+
|
|
75
|
+
if (this._options.cleanupInterval > 0) {
|
|
76
|
+
this.startAutoCleanup();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Start the automatic cleanup interval.
|
|
82
|
+
*/
|
|
83
|
+
public startAutoCleanup() {
|
|
84
|
+
if (this._intervalId) clearInterval(this._intervalId);
|
|
85
|
+
|
|
86
|
+
this._intervalId = setInterval(() => {
|
|
87
|
+
this.performCleanup();
|
|
88
|
+
}, this._options.cleanupInterval);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Stop the automatic cleanup interval.
|
|
93
|
+
*/
|
|
94
|
+
public stopAutoCleanup() {
|
|
95
|
+
if (this._intervalId) {
|
|
96
|
+
clearInterval(this._intervalId);
|
|
97
|
+
this._intervalId = null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Add or update an item in the cache.
|
|
103
|
+
*/
|
|
104
|
+
public set(key: string, value: string | ArrayBuffer | Uint8Array | object, options: CacheEntryOptions = {}) {
|
|
105
|
+
// 1. Prepare new entry details
|
|
106
|
+
let storedValue: string | ArrayBuffer | Uint8Array;
|
|
107
|
+
let type: 'string' | 'buffer' | 'json';
|
|
108
|
+
let size = 0;
|
|
109
|
+
|
|
110
|
+
// Meta size calculation
|
|
111
|
+
const metaSize = options.meta ? JSON.stringify(options.meta).length * 2 : 0;
|
|
112
|
+
|
|
113
|
+
if (value instanceof ArrayBuffer) {
|
|
114
|
+
storedValue = value;
|
|
115
|
+
type = 'buffer';
|
|
116
|
+
size = value.byteLength;
|
|
117
|
+
} else if (value instanceof Uint8Array) {
|
|
118
|
+
storedValue = value;
|
|
119
|
+
type = 'buffer';
|
|
120
|
+
size = value.byteLength;
|
|
121
|
+
} else if (typeof value === 'string') {
|
|
122
|
+
storedValue = value;
|
|
123
|
+
type = 'string';
|
|
124
|
+
size = value.length * 2; // Approximation for JS string memory
|
|
125
|
+
} else {
|
|
126
|
+
storedValue = JSON.stringify(value);
|
|
127
|
+
type = 'json';
|
|
128
|
+
size = (storedValue as string).length * 2;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Overhead for object structure (approximate) + Meta size
|
|
132
|
+
size += 100 + metaSize;
|
|
133
|
+
|
|
134
|
+
// 2. Check overlap
|
|
135
|
+
if (this._storage.has(key)) {
|
|
136
|
+
this.delete(key);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 3. Calculate Expiration
|
|
140
|
+
|
|
141
|
+
let expiresAt: number | null = null;
|
|
142
|
+
|
|
143
|
+
if (options.expiresAt) {
|
|
144
|
+
expiresAt = options.expiresAt;
|
|
145
|
+
} else if (options.ttl) {
|
|
146
|
+
expiresAt = Date.now() + options.ttl;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const entry: InternalCacheEntry = {
|
|
150
|
+
key,
|
|
151
|
+
value: storedValue,
|
|
152
|
+
type,
|
|
153
|
+
size,
|
|
154
|
+
createdAt: Date.now(),
|
|
155
|
+
expiresAt,
|
|
156
|
+
accessCount: 0,
|
|
157
|
+
importance: options.importance ?? 1,
|
|
158
|
+
meta: options.meta,
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// 4. Check if item is too big for the cache entirely
|
|
162
|
+
if (entry.size > this._options.maxSize) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 5. Check if we need to make space
|
|
167
|
+
if (this.needsEviction(entry.size)) {
|
|
168
|
+
this.evictFor(entry.size);
|
|
169
|
+
|
|
170
|
+
if (this.needsEviction(entry.size)) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
this._storage.set(key, entry);
|
|
176
|
+
this._currentSize += size;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Retrieve an item from the cache.
|
|
181
|
+
*/
|
|
182
|
+
public get<T = any>(key: string, peek = false): T | null {
|
|
183
|
+
const entry = this._storage.get(key);
|
|
184
|
+
if (!entry) return null;
|
|
185
|
+
|
|
186
|
+
if (entry.expiresAt && entry.expiresAt < Date.now()) {
|
|
187
|
+
this.delete(key);
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!peek) entry.accessCount++;
|
|
192
|
+
|
|
193
|
+
if (entry.type === 'buffer') {
|
|
194
|
+
return entry.value as T;
|
|
195
|
+
} else if (entry.type === 'json') {
|
|
196
|
+
return JSON.parse(entry.value as string) as T;
|
|
197
|
+
} else {
|
|
198
|
+
return entry.value as T;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Retrieve an item from the cache with its metadata.
|
|
204
|
+
*/
|
|
205
|
+
public getWithMeta<T = any>(key: string, peek = false): { value: T; meta: any } | null {
|
|
206
|
+
const entry = this._storage.get(key);
|
|
207
|
+
if (!entry) return null;
|
|
208
|
+
|
|
209
|
+
if (entry.expiresAt && entry.expiresAt < Date.now()) {
|
|
210
|
+
this.delete(key);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (!peek) entry.accessCount++;
|
|
215
|
+
|
|
216
|
+
let value: T;
|
|
217
|
+
if (entry.type === 'buffer') {
|
|
218
|
+
value = entry.value as T;
|
|
219
|
+
} else if (entry.type === 'json') {
|
|
220
|
+
value = JSON.parse(entry.value as string) as T;
|
|
221
|
+
} else {
|
|
222
|
+
value = entry.value as T;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return { value, meta: entry.meta };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Check if an item exists in the cache and is not expired.
|
|
230
|
+
* Does not increment accessCount.
|
|
231
|
+
*/
|
|
232
|
+
public has(key: string): boolean {
|
|
233
|
+
const entry = this._storage.get(key);
|
|
234
|
+
if (!entry) return false;
|
|
235
|
+
|
|
236
|
+
if (entry.expiresAt && entry.expiresAt < Date.now()) {
|
|
237
|
+
this.delete(key);
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Manually delete an item.
|
|
246
|
+
*/
|
|
247
|
+
public delete(key: string) {
|
|
248
|
+
const entry = this._storage.get(key);
|
|
249
|
+
|
|
250
|
+
if (entry) {
|
|
251
|
+
this._currentSize -= entry.size;
|
|
252
|
+
this._storage.delete(key);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Clear all items.
|
|
258
|
+
*/
|
|
259
|
+
public clear() {
|
|
260
|
+
this._storage.clear();
|
|
261
|
+
this._currentSize = 0;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Get current cache stats.
|
|
266
|
+
*/
|
|
267
|
+
public getStats() {
|
|
268
|
+
return {
|
|
269
|
+
count: this._storage.size,
|
|
270
|
+
size: this._currentSize,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Iterate over all valid keys.
|
|
276
|
+
*/
|
|
277
|
+
public *keys(): Generator<string> {
|
|
278
|
+
const now = Date.now();
|
|
279
|
+
for (const [key, entry] of this._storage) {
|
|
280
|
+
if (entry.expiresAt && now > entry.expiresAt) {
|
|
281
|
+
this.delete(key);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
yield key;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Iterate over keys starting with a prefix.
|
|
290
|
+
*/
|
|
291
|
+
public *keysStartingWith(prefix: string): Generator<string> {
|
|
292
|
+
for (const key of this.keys()) {
|
|
293
|
+
if (key.startsWith(prefix)) yield key;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Iterate over keys ending with a suffix.
|
|
299
|
+
*/
|
|
300
|
+
public *keysEndingWith(suffix: string): Generator<string> {
|
|
301
|
+
for (const key of this.keys()) {
|
|
302
|
+
if (key.endsWith(suffix)) yield key;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Iterate over keys containing a specific text.
|
|
308
|
+
*/
|
|
309
|
+
public *keysContaining(text: string): Generator<string> {
|
|
310
|
+
for (const key of this.keys()) {
|
|
311
|
+
if (key.includes(text)) yield key;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Check if we need to evict entries to fit a new one (or if limits are exceeded).
|
|
317
|
+
*/
|
|
318
|
+
private needsEviction(incomingSize: number): boolean {
|
|
319
|
+
return (
|
|
320
|
+
(this._storage.size + 1 > this._options.maxCount) ||
|
|
321
|
+
(this._currentSize + incomingSize > this._options.maxSize)
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Perform recurrent cleanup (expiration only).
|
|
327
|
+
*/
|
|
328
|
+
private performCleanup() {
|
|
329
|
+
const now = Date.now();
|
|
330
|
+
let removedCount = 0;
|
|
331
|
+
|
|
332
|
+
logMemCache.info(`Cache [${this._name}] Recurrent GC started - ${JSON.stringify({
|
|
333
|
+
count: this._storage.size,
|
|
334
|
+
sizeMB: (this._currentSize / 1024 / 1024).toFixed(2)
|
|
335
|
+
})}`);
|
|
336
|
+
|
|
337
|
+
for (const [key, entry] of this._storage) {
|
|
338
|
+
if (entry.expiresAt && now > entry.expiresAt) {
|
|
339
|
+
this.delete(key);
|
|
340
|
+
removedCount++;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (removedCount > 0) {
|
|
345
|
+
logMemCache.info(`Cache [${this._name}] Recurrent GC finished - ${JSON.stringify({
|
|
346
|
+
removed: removedCount,
|
|
347
|
+
remaining: this._storage.size,
|
|
348
|
+
sizeMB: (this._currentSize / 1024 / 1024).toFixed(2)
|
|
349
|
+
})}`);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Evict items to make space/reduce count.
|
|
355
|
+
* Strategy: Calculate a score. Lower score = evicted first.
|
|
356
|
+
* Factors:
|
|
357
|
+
* - expired (immediate kill)
|
|
358
|
+
* - importance (higher = keep)
|
|
359
|
+
* - accessCount (higher = keep)
|
|
360
|
+
*
|
|
361
|
+
* Score = (importance * 1000) + (accessCount)
|
|
362
|
+
* (Simplified logic)
|
|
363
|
+
*/
|
|
364
|
+
private evictFor(requiredSpace: number) {
|
|
365
|
+
const now = Date.now();
|
|
366
|
+
|
|
367
|
+
logMemCache.info(`Cache [${this._name}] GC Eviction started (Memory Pressure) - ${JSON.stringify({
|
|
368
|
+
requiredSpace,
|
|
369
|
+
count: this._storage.size,
|
|
370
|
+
sizeMB: (this._currentSize / 1024 / 1024).toFixed(2)
|
|
371
|
+
})}`);
|
|
372
|
+
|
|
373
|
+
// 1. Remove expired first (In-place, no allocation)
|
|
374
|
+
for (const [key, entry] of this._storage) {
|
|
375
|
+
if (entry.expiresAt && now > entry.expiresAt) {
|
|
376
|
+
this.delete(key);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Optimization: Low Water Mark.
|
|
381
|
+
// To avoid frequent GC, when we evict, we try to go down to 90% capacity,
|
|
382
|
+
// creating a temporary buffer.
|
|
383
|
+
//
|
|
384
|
+
const safeSize = this._options.maxSize * 0.9;
|
|
385
|
+
const safeCount = this._options.maxCount * 0.9;
|
|
386
|
+
|
|
387
|
+
// Helper: Have we reached our goal?
|
|
388
|
+
// If strict is true, we aim for the buffer (90%).
|
|
389
|
+
// If strict is false, we just aim to fit the item (100%).
|
|
390
|
+
const isTargetReached = (strict: boolean) => {
|
|
391
|
+
// 1. Absolute requirement: Must fit the new item within MAX limits.
|
|
392
|
+
// needsEviction returns TRUE if we are OVER limits.
|
|
393
|
+
if (this.needsEviction(requiredSpace)) {
|
|
394
|
+
return false; // We are over max, so target is definitely NOT reached.
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// 2. Buffer requirement: specific to strict mode
|
|
398
|
+
if (strict) {
|
|
399
|
+
// We are under MAX, but are we under SAFE limits?
|
|
400
|
+
return (this._currentSize + requiredSpace <= safeSize) && (this._storage.size <= safeCount);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// If not strict, and needsEviction was false, we are good.
|
|
404
|
+
return true;
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
// Helper to format log message
|
|
408
|
+
const logGC = (step: string, extra: object = {}) => {
|
|
409
|
+
logMemCache.info(`Cache [${this._name}] [GC] ${step} - ${JSON.stringify({
|
|
410
|
+
...extra,
|
|
411
|
+
count: this._storage.size,
|
|
412
|
+
sizeMB: (this._currentSize / 1024 / 1024).toFixed(2)
|
|
413
|
+
})}`);
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
if (isTargetReached(true)) {
|
|
417
|
+
logGC("Step 1 (Expired) sufficient");
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// 2. Multi-step Eviction by Importance
|
|
422
|
+
const MAX_SEARCH_LEVEL = 10;
|
|
423
|
+
// We treat levels 1-5 as "recyclable" to build buffer.
|
|
424
|
+
// Levels 6-10 are "protected" -> only evicted if absolutely necessary to fit the item.
|
|
425
|
+
const BUFFER_TARGET_LEVEL = 5;
|
|
426
|
+
|
|
427
|
+
for (let level = 1; level <= MAX_SEARCH_LEVEL; level++) {
|
|
428
|
+
const candidates: InternalCacheEntry[] = [];
|
|
429
|
+
|
|
430
|
+
for (const entry of this._storage.values()) {
|
|
431
|
+
if (entry.importance === level) {
|
|
432
|
+
candidates.push(entry);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (candidates.length === 0) continue;
|
|
437
|
+
|
|
438
|
+
candidates.sort((a, b) => {
|
|
439
|
+
if (a.accessCount !== b.accessCount) {
|
|
440
|
+
return a.accessCount - b.accessCount;
|
|
441
|
+
}
|
|
442
|
+
return a.createdAt - b.createdAt;
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
const aimForBuffer = level <= BUFFER_TARGET_LEVEL;
|
|
446
|
+
|
|
447
|
+
for (const candidate of candidates) {
|
|
448
|
+
this.delete(candidate.key);
|
|
449
|
+
// If we reached our target (buffer or just space), we stop.
|
|
450
|
+
if (isTargetReached(aimForBuffer)) {
|
|
451
|
+
logGC("Step 2 (Importance) finished", { level });
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// 3. Strategy: Delete biggest items
|
|
458
|
+
// If we are still here, it means we have high importance items filling the cache.
|
|
459
|
+
// We try to free space by removing the largest items first.
|
|
460
|
+
if (!isTargetReached(false)) {
|
|
461
|
+
let b1: InternalCacheEntry | null = null;
|
|
462
|
+
let b2: InternalCacheEntry | null = null;
|
|
463
|
+
let b3: InternalCacheEntry | null = null;
|
|
464
|
+
|
|
465
|
+
for (const entry of this._storage.values()) {
|
|
466
|
+
if (!b1 || entry.size > b1.size) {
|
|
467
|
+
b3 = b2;
|
|
468
|
+
b2 = b1;
|
|
469
|
+
b1 = entry;
|
|
470
|
+
} else if (!b2 || entry.size > b2.size) {
|
|
471
|
+
b3 = b2;
|
|
472
|
+
b2 = entry;
|
|
473
|
+
} else if (!b3 || entry.size > b3.size) {
|
|
474
|
+
b3 = entry;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (b1) { this.delete(b1.key); if (isTargetReached(false)) { logGC("Step 3 (Biggest) finished"); return; } }
|
|
479
|
+
if (b2) { this.delete(b2.key); if (isTargetReached(false)) { logGC("Step 3 (Biggest) finished"); return; } }
|
|
480
|
+
if (b3) { this.delete(b3.key); if (isTargetReached(false)) { logGC("Step 3 (Biggest) finished"); return; } }
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// 4. Fallback / Emergency
|
|
484
|
+
if (!isTargetReached(false)) {
|
|
485
|
+
const iterator = this._storage.keys();
|
|
486
|
+
let result = iterator.next();
|
|
487
|
+
|
|
488
|
+
while (!result.done) {
|
|
489
|
+
this.delete(result.value);
|
|
490
|
+
if (isTargetReached(false)) {
|
|
491
|
+
logGC("Step 4 (Fallback) finished");
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
result = iterator.next();
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
package/src/jk_tools/common.ts
CHANGED
|
@@ -69,6 +69,12 @@ export interface ValueWithPriority<T> {
|
|
|
69
69
|
priority: PriorityLevel;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Sorts a list of items based on their priority level in ascending order.
|
|
74
|
+
* Items with lower priority values (e.g., -200) will appear first in the returned array.
|
|
75
|
+
* @param values The list of items to sort, each containing a value and a priority.
|
|
76
|
+
* @returns An array of just the values, sorted by priority. Returns undefined if the input is undefined.
|
|
77
|
+
*/
|
|
72
78
|
export function sortByPriority<T>(values: undefined|ValueWithPriority<T>[]): undefined|(T[]) {
|
|
73
79
|
if (values === undefined) return undefined;
|
|
74
80
|
|