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
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-ttlUsage
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 // trueExisting wrappers can be reset with new values:
userID.value = 34;
// Timer starts again for 30 seconds of inactivityChanging 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 // falseTouch 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 // falseAfter setting no-touch:
const item = new TTLValue(123, 1000, { touchOnRead: false });
// 998ms later
item.value;
// 3ms later
item.expired // true