Defer Map
A small Map
wrapper with defer and expiry.
Table of Contents
Features
- 💥 Configure expiry to auto remove items.
- 🐙 Uses promises to defer values.
- 🚀 Drop in replacement for native
Map
. - 💪 Written in TypeScript.
Installation
npm install defer-map --save
Usage
import { DeferMap } from 'defer-map';
const expiry = 60 * 60 * 1000;
const map = new DeferMap({ expiry });
map.set('hero', 'Luke Skywalker');
const deferred = map.defer('villain');
console.log(map.size);
console.log(await map.get('hero').result);
deferred.done('Darth Vadar');
console.log(await map.get('villain').result);
console.log(map.get('hero'), map.get('villain'));
The defer-map
API follows the native JS Map, with a few key additions.
The constructor (new DeferMap()
) takes an optional configuration object with the following properties:
Property | Type | Description | Default |
---|
expiry | number | Number of milliseconds before items will expire. | n/a |
If expiry
is set, expired items are not instantly removed. If you call get
with a key for an expired item, it will
be removed and undefined
will be returned. Alternatively, you can call cleanup
to remove all expired items.
The get
method returns an object, instead of the set value directly. The result
property on the object returned
by get
is a Promise
that is resolved with the set value.
Development
npm install
npm run build