@omni-tools/map-age-cleaner

Automatically cleanup expired items in a Map

Usage no npm install needed!

<script type="module">
  import omniToolsMapAgeCleaner from 'https://cdn.skypack.dev/@omni-tools/map-age-cleaner';
</script>

README

map-age-cleaner

Npm Version Build Status codecov

Automatically cleanup expired items in a Map

:loudspeaker: This is a fork of @SamVerschueren map-age-cleaner, to be used for @omni-tools/mem

So far its still kind of experiment, but It might end up to be merged :slightly_smiling_face:

Install

$ npm install @omni-tools/map-age-cleaner

Usage

import mapAgeCleaner from '@omni-tools/map-age-cleaner';

const map = new Map([
    ['unicorn', {data: '🦄', maxAge: Date.now() + 1000}]
]);

mapAgeCleaner(map);

map.has('unicorn'); //=> true

// Wait for 1 second... 😴

map.has('unicorn'); //=> false

API

mapAgeCleaner(map, [options={property?,onExpire?}])

Returns the Map instance.

  • map: Map instance which should be cleaned up. (Type: Map)
  • options: object containing eventualy the two options possible
    • property: Name of the property which olds the expiry timestamp. (Type: string, Default: maxAge)
    • onExpire: Handler to be called with expired object on expiration. (Type: (key, value) => void, Default: undefined) takes the key and value of expired item as argument

Related

  • expiry-map - A Map implementation with expirable items
  • expiry-set - A Set implementation with expirable keys
  • mem - Memoize functions

License

MIT © Sam Verschueren