ES6 Map-Like implementation with keys that have a defined timelife (using TimeStore under the hood)
- Node.js v20 or higher
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @openally/ephemeral-map
# or
$ yarn add @openally/ephemeral-map
import EphemeralMap, { tSv } from "@openally/ephemeral-map";
const data = [
["hello", "world"]
];
// Note: ttl is not mandatory
const em = new EphemeralMap(data, { ttl: 500 });
em.on(EphemeralMap.Expired, (key, value) => {
console.log(`Identifier '${key}' with value '${value}' has expired!`);
});
em.set(tSv({ ttl: 200 })("key"), "value");
EphemeralMap extend from a normal Map. By default the inner TimeStore set his ttl to 0 (which mean that no keys expire).
Read-only TTL. Return 0
if the class has no ttl.
Method inspired from the TC39 proposal to add an upsert method on Map.
Add a pair (key, value) to a Map or EphemeralMap. If the first argument is a Map then the third argument is ignored.
const em = new EphemeralMap();
EphemeralMap.set(em, ["foo", "bar"], {
ttl: 400
});
The EphemeralMap EventEmitter broadcast two distinct events:
- EphemeralMap.Expired (when a given identifier expire)
- EphemeralMap.Renewed (when an identifier TTL is Renewed with add() method)
Warning
Both value are JavaScript Symbols primitive
MIT