npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@openally/timestore

v1.5.1

Published

An abstract class designed to manage the Time To Live (TTL) of a given list of identifiers.

Downloads

325

Readme

[!NOTE] Internally it uses a Node.js timer. This library does not guarantee that the timers doesn't drift.

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @openally/timestore
# or
$ yarn add @openally/timestore

Usage example

import { TimeStore } from "@openally/timestore";

const store = new TimeStore({ ttl: 10_000 })
  .add("foo")
  .add("bar", { ttl: 500 })
  .add("bar", { ttl: 200, keepIdentifierBirthTTL: true }); // will be ignored!

console.log(store.ttl); // 10000

store.on(
  TimeStore.Expired,
  (id) => console.log(`identifier '${id}' has expired!`)
);

[!IMPORTANT] By default the internal timer we use in unreferenced to allow the event loop to properly stop. You can modify this behaviour by enabling the keepEventLoopAlive options.

API

Identifier are often described with the following type:

export type TimeStoreIdentifier = string | symbol | number | boolean | bigint | object | null;

constructor(options?: ITimeStoreConstructorOptions)

The constructor options payload is described by the following TS interface:

interface ITimeStoreConstructorOptions {
  /**
   * Time To Live (Lifetime of stored identifiers).
   */
  ttl?: number;
  /**
   * Automatically expire identifiers when Node.js process "exit" event is triggered.
   *
   * @see https://nodejs.org/api/process.html#event-exit
   * @default false
   */
  expireIdentifiersOnProcessExit?: boolean;
  /**
   * Provide an additional EventEmitter to use for broadcasting events
   */
  eventEmitter?: EventEmitter;

  /**
   * If enabled the internal timer will not be unreferenced
   *  
   * @see https://nodejs.org/dist/latest-v18.x/docs/api/timers.html#timeoutunref
   * @default false
   */
  keepEventLoopAlive?: boolean;
}

If the ttl option is not provided all identifiers will remain active. The default class ttl will be equal zero.

add(identifier: TimeStoreIdentifier, options?: ITimeStoreAddOptions): this

The options payload is described by the following TS interface:

interface ITimeStoreAddOptions {
  /**
   * Time To Live for the given identifier.
   * If no value provided it will take the class TTL value.
   */
  ttl?: number;

  /**
   * If identifier exist then keep is original timestamp and ttl.
   *
   * @default false
   */
  keepIdentifierBirthTTL?: boolean;
}

[!NOTE] Adding an existing ID will reset its previous TTL/timestamp except if the keepIdentifierBirthTTL option is set to true.

addTsv(data: tSvResponse): this

Add a value using a TimeStoreValue:

import { TimeStore, tSv } from "@openally/timestore";

const tSvFactory = tSv({ ttl: 500 });

const store = new TimeStore({ ttl: 10_000 })
  .addTsv(tSvFactory("key"))
  .addTsv(tSvFactory(["key", "value"])); // value will be ignored here

TimeStoreValue are useful to build higher abstraction using TimeStore. Those values all embed a Global symbol Symbol.for("TimeStoreValue").

The module also export it as TSV_SYMBOL.

delete(identifier: TimeStoreIdentifier): this

Remove a given identifier from the store.

clear(): this

Calling this method will remove all stored identifiers and clear the internal Node.js Timeout. The instance basically returns to its initial state.

get ttl(): number

Read-only TTL. Return 0 if the class has no ttl.

get size(): number

Read-only store size.

has(): boolean

Return true if the key exists in the store otherwise it will return false.

Events

The TimeStore class broadcast two distinct events:

  • TimeStore.Expired (when a given identifier expire)
  • TimeStore.Renewed (when an identifier TTL is Renewed with add() method)

[!WARNING] Both value are JavaScript Symbols primitive

License

MIT