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

expirable-item

v1.0.1

Published

Expirable items for when you must do something to an item after a certain amount of time

Downloads

10

Readme

expirable-item

Expirable items allow you to do stuff with items and set a timeout to do something after the timeout expires. This is written in TypeScript and uses native Promises.

Install

npm i expirable-item

Examples

Basic usage

import { Expirable } from "expirable-item";

console.log("Making expirable...");
let e = new Expirable<number>(13, 5000);
e.expire.then((n) => {
    console.log("The number " + n + " has expired!");
});

Output:

Making expirable...
The number 13 has expired!

Changing expire time

let e = new Expirable<string>("Stringy strings", 50000); //Expire time is 50 seconds
console.log("e expire time: " + e.expire_time);

e.expire.then((s) => {// Don't have to "re-then" to the promise if you change expire time before it expires
    console.log("e has expired: " + s);
});

setTimeout(() => {
    console.log("Expire time changing for e");
    e.expire_time = new Date(Date.now() + 2000); //Change expire time to 2 seconds from the moment this line runs, instead of 50 initially
    //e.set_expire_time(2000); //This will do the same thing as above, but this function recognizes Date and number types.
    console.log("e expire new time: " + e.expire_time);
}, 1000);

Output:

e expire time: Sat Jul 18 2020 16:19:02 GMT-0400 (Eastern Daylight Time)
Expire time changing for e
e expire new time: Sat Jul 18 2020 16:18:15 GMT-0400 (Eastern Daylight Time)
e has expired: Stringy strings

Restarting expired item

async function example(){
    let e = new Expirable<string>("Stringy strings", 1000); //Expire time is 1 second

    let s = await e.expire;
    console.log("e has expired: " + s);
    e.set_expire_time(1000); //Reset expire time to 1 second

    s = await e.expire; //You must "re-await" or "re-then" the expire promise, as a new one is created after the Expirable has initially expired.
                        //Promises by design can only return one value. If you're looking for a way around this, check out `expirable-item-rxjs`.
    console.log("e has expired again: " + s);
}

example();

Output:

e has expired: Stringy strings
e has expired again: Stringy strings

Usage ideas

  • Refreshing items after a certain amount of time
  • Removing items after a certain amount of time
  • Dynamically changing a timeout for an item

Inspiration

This package was really created to just help focus on a functionality idea I had instead of making it a non-removable part of a larger project. This idea seemed generic enough to be usable elsewhere.

It's important to note that this library doesn't offer a bunch of over complicated features. You could probably make the exact same thing in little time, or even argue that this library isn't necessary. I wouldn't disagree, but I would argue that it is convenient to have. I didn't have anything that specifically fit my needs, so I made this and decided to make it a small package.