MegaMap Personal Project
MegaMap is a TypeScript library that extends the native JavaScript Map object and provides additional features such as caching, loadability, automatic expiry of entries, and more. MegaMap is ideal for situations where you need a sophisticated mapping solution with enhanced control over data storage and retrieval.
Features
- Caching: Store and retrieve data from a local or remote cache.
- Loadability: Load data from a function or a promise when the map does not have the requested key.
- Expiry: Set a time-to-live for each entry and automatically remove them when they expire.
- Events: Listen to events such as
set
,get
,delete
,clear
,load
, andexpire
. - Customization: Configure various options such as the default cache, the default loader, the default expiry, and the default event handler.
How It Works
MegaMap inherits from the native Map object and overrides some of its methods to add the extra features. MegaMap also
exposes some new methods and properties to access and manipulate the data. MegaMap uses a WeakMap
to store the
metadata for each entry, such as the cache, the loader, the expiry, and the event handler. MegaMap also uses a Set
to
keep track of the expired keys and a setTimeout
function to schedule the removal of the expired entries.