TimeMap
ECMAScript 6 Map-Like implementation with keys that have a defined timelife.
Requirements
- Node.js v12 or higher
Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @slimio/timemap# or $ yarn add @slimio/timemap
Usage example
const strictEqual = ;const TimeMap = ; const col = 1000;col; col;col;; ; ;
Events
TimeMap class is extended by a Node.js EventEmitter. The class can trigger several events:
event name | description |
---|---|
expiration | Expired key are emitted before deletion |
const map = 100;map; map;
API
Following methods are members of TimeMap class. The type TimeMap.key
is declared as follow:
;
constructor(timeLifeMs: number)
Create a new TimeMap Object. Take an argument which is the time that a key stay alive within the class.
const map = 5000;map; // foo will live for the next 5,000 milliseconds
The default timeLifeMs is equal to the value of static member TimeMap.DEFAULT_TIMELIFE_MS
(equal to 1000 by default).
const strictEqual = ; const map = ;;
set(key: TimeMap.key, value: any): void
Set a new key in the Collection. Inner timer will be initialized by the first key. The key must be a string or a symbol (no other primitive are accepted).
const strictEqual = ; const map = ;const sym = Symbol"foo";map;;
has(key: TimeMap.key): boolean
Similar to Map.has
method. Return true if the key exist within.
const strictEqual = ; const map = 100;map;; ;
delete(key: TimeMap.key): void
Delete a given key from TimeMap. The key must be a string or a symbol.
const strictEqual = ; const map = 100;map;map;map;
get< T >(key: TimeMap.key, refreshTimestamp?: boolean): T
Get a given key from the Class. Throw an Error if the key doesn't exist in the Collection (use .has() before).
const assert = ; const map = 100;map; assert;assert;
clear(): void
Clear internal timer and internal data. Everything will be reset.
keys(): IterableIterator< TimeMap.key >
The keys() method returns a new Iterator object that contains the keys for each element in the TimeMap object in insertion order.
const deepEqual = ; const map = ;map;map; ;
Properties
All following properties are readonly
.size
The size accessor property returns the number of elements in the TimeMap.
const strictEqual = ; const map = ;map;;
.timeLife
The timeLife accessor property return the configured time life for keys
const strictEqual = ; const map = 2000;;
Dependencies
This project have no dependencies.
License
MIT