JSPM

mini-ttl

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q24688F
  • License MIT

Miniature temporary value creator

Package Exports

  • mini-ttl

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mini-ttl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Mini-TTL

Miniature temorary value creator

Build Status

About

A tiny library to manage temporary or expiring values. Wraps values in a helper instance which expires values after a given time. Extends an event emitter for easy watching.

Installation

Install by running:

npm install mini-ttl

Usage

Usage is simple, and is best explained by example:

const TTLValue = require("mini-ttl");

// Create a user ID wrapper that expires in 30 seconds
const userID = new TTLValue(18, "30s");

// 15 seconds later
console.log(userID.value); // 18
// Value touched, timer is reset

// 29 seconds later
// Value is still valid

// Another 2 seconds later
console.log(userID.value); // null
userID.expired // true

Existing wrappers can be reset with new values:

userID.value = 34;
// Timer starts again for 30 seconds of inactivity

Changing the expiry value

You can set the value that the wrapper uses when it expires by providing it in the options:

const alive = new TTLValue(true, 1500, { expiryValue: false });

// 2000ms later
alive.value // false

Touch on read

Typically wrapper expiry timers are reset (touched) when reading the value. This can be disabled by setting the option.

Before:

const item = new TTLValue(123, 1000);

// 998ms later
item.value;

// 900ms later
item.expired // false

After setting no-touch:

const item = new TTLValue(123, 1000, { touchOnRead: false });

// 998ms later
item.value;

// 3ms later
item.expired // true