Class: JunkPocket\<T>¶
Named key-value cache backed by Map<string, T>.
Extends the built-in Map with lazy value creation via getOrCreate,
conditional deletion via clearIf, and a named clearAll alias.
Used throughout gaslamp for temporary in-memory caching of GAS objects,
type guards, timers, and counters.
Remarks¶
Each instance carries a name string for identification in logs and
error messages. The name does not affect caching behavior.
Example¶
const pocket = new JunkPocket<string>("labels");
// Retrieve or create on first access
const v1 = pocket.getOrCreate("greeting", () => "hello");
const v2 = pocket.getOrCreate("greeting", () => "ignored"); // returns "hello"
// Remove only stale entries
pocket.clearIf((key) => key.startsWith("temp_"));
// Remove everything
pocket.clearAll();
Since¶
0.14.0
Extends¶
Map\<string,T>
Type Parameters¶
T¶
T
Constructors¶
Constructor¶
new JunkPocket\<
T>(name):JunkPocket\<T>
Creates a new JunkPocket instance.
Parameters¶
name¶
string
Label to identify this cache (used in logs and error messages).
Returns¶
JunkPocket\<T>
Example¶
const timers = new JunkPocket<Clock>("timers");
const loggers = new JunkPocket<AfterGlow>("loggers");
Since¶
0.14.0
Overrides¶
Map<string, T>.constructor
Gears¶
getOrCreate()¶
getOrCreate\<
U>(key,creator):U
Returns the cached value for key, or creates and stores it if absent.
The creator function is called only when the key is not present.
The created value is stored immediately so subsequent calls return the
same instance.
Type Parameters¶
U¶
U = T
Parameters¶
key¶
string
Cache key to look up.
creator¶
() => U
Factory function called when the key is missing.
Returns¶
U
The existing or newly created value.
Example¶
const pocket = new JunkPocket<string>("labels");
const a = pocket.getOrCreate("msg", () => "hello"); // creates "hello"
const b = pocket.getOrCreate("msg", () => "world"); // returns "hello"
// a === b === "hello"
Since¶
0.14.0
clearIf()¶
clearIf(
predicate):void
Deletes all entries for which predicate returns true.
Iterates over a snapshot of the current entries, so adding or removing
items inside predicate does not affect which entries are evaluated.
Parameters¶
predicate¶
(key, value) => boolean
Called with each (key, value) pair.
Return true to delete the entry.
Returns¶
void
Example¶
const pocket = new JunkPocket<number>("scores");
pocket.set("alice", 80);
pocket.set("bob", 40);
pocket.set("carol", 95);
// Remove entries with a score below 50
pocket.clearIf((key, value) => value < 50);
// pocket now contains only "alice" and "carol"
Since¶
0.14.0
clearAll()¶
clearAll():
void
Removes all entries from the cache.
Equivalent to calling Map.clear() directly.
Returns¶
void
Example¶
const pocket = new JunkPocket<string>("temp");
pocket.set("a", "1");
pocket.set("b", "2");
pocket.clearAll();
pocket.size; // 0
Since¶
0.14.0
Other¶
name¶
readonlyname:string
Name used to identify this cache instance.