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

@eugbyte/timed-map

v0.0.10

Published

Like a regular JS `Map`, but with the ability to execute callbacks at the specified time.

Downloads

9

Readme

About

Like a regular JS Map, but with the ability to execute callbacks at the specified time.

A possible use case might be to stop displaying the gps position of vehicles on a map should data stop emitting after a specified time.

Quick start example

Install:

npm i @eugbyte/timed-maps

Code demo:

import { timedMapFactory, type TimedMap } from "@eugbyte/timed-maps";

const cache: TimedMap<string, number> = timedMapFactory();
cache.set("one", 1);

// 5000 ms from now, execute a callback w.r.t the key "one" that will delete the key.
cache.setTimer("one", Date.now() + 5000, () => {
    console.log("5 seconds has passed, callback triggered");
    cache.delete("one");
});

// cache inherits all methods from the native JS Map object, like `get`, `has`, `set` etc.
const value = cache.get("one");
console.log(value); // "1"

console.log(cache.has("one"));    // true

// demo purpose - create a sleep function for the time to pass
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(6000);
console.log(cache.has("one")) // false

// remember to call `dispose` once you are done with the cache, since the cache uses `setTimeout` / `setInterval` internally and needs to clear it.
cache.dispose();

More info

Strategies

There are two strategies for the timed map. Strategy.TIMEOUT AND Strategy.TICKER (default).

import { timedMapFactory, Strategy, type TimedMap } from "@eugbyte/timed-maps";

let cache: TimedMap<string, number> = timedMapFactory(Strategy.TIMEOUT);
cache = timedMapFactory(Strategy.TICKER);
cache = timedMapFactory(); // defaults to `Strategy.TICKER`

| | TICKER | TIMEOUT | | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | Default if not specified in factory | Yes | No | | How it works | Uses setInterval internally to check the specified timestamps periodically every second. The callbacks are stored in a priority queue, ordered by the timestamp. | Uses setTimeout internally to create multiple timeouts | | Use case | Cache is expected to be used perpetually, e.g. in a backend server | Cache is expected to not be used perpetually, e.g. in a browser tab that will eventually close. The setTimeout id must be unique. Since there is a maximum number the computer can handle, it is possible to run out of timeout ids. | | Precision | Since setInterval is used to check the specified timestamps every second, delays might be increased by a second. | High. However, see more below on "Precision". |

Precision

The timeout value represents the minimum delay after which the message will be pushed into the queue. Recall that JS uses an event loop and message queue, the latter which can be enqueued with intensive tasks. To elaborate from MDN:

"If there is no other message in the queue, and the stack is empty, the message is processed right after the delay. However, if there are messages, the setTimeout message will have to wait for other messages to be processed. For this reason, the second argument indicates a minimum time — not a guaranteed time."

To improve performance, ensure that the callback specified in setTimer is not CPU intensive.

Additionally, note that if the TICKER strategy is used, the minimum delay might increased by a second, since setInterval is used internally to check the provided timestamps every second.

Additional methods

getTimeout(key)

Gets the timeout information w.r.t the specified key.

import { timedMapFactory, Strategy, type TimedMap, type TimeoutInfo } from "@eugbyte/timed-maps";

const cache: TimedMap<string, number> = timedMapFactory();
cache.set("one", 1);
cache.setTimer("one", Date.now() + 5000, () => {
    console.log("5 seconds has passed, callback triggered");
});

const timeoutInfo: TimeoutInfo | undefined = cache.getTimeout("one");
console.log(timeoutInfo); // { ttl: 5000, timestamp: 1722165571956, callback: [Function (anonymous)] }

getTimeouts()

Returns a deep copy of all timeout informations.

const infos: Map<string, TimeoutInfo> = cache.getTimeouts();

Development

Test:

pnpm run test

Build

pnpm run build

This project use Vite library mode with vite-plugin-dts to bundle the .ts files to generate .js and d.ts files.