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

longterm

v1.3.0

Published

A tool to schedule events persistently across multiple app restarts

Downloads

3

Readme

longterm.js

Longterm is a zero-dependency tool to schedule events persistently across multiple app restarts. It is designed to handle a large volume of events and to allow great flexibility in its use.

The API

Define your event listeners on app startup, and then you can schedule events whenever you want.

init(options)

Before scheduling events, you will want to set up longterm with the init function. This will let you choose the data store you want to use, among other options.

var longterm = require('longterm')
longterm.init({
  queue: new MongoQueue()
})

schedule(what, when, data[, callback])

The primary export of longterm.js is the longterm function, which allows you to schedule events.

longterm.schedule('party', Date.now() + 10000, {
  inviteList: [
    'Aaron',
    'Christy',
    'Aji',
    'Nancy'
  ]
});

Event Binding

Longterm is an EventEmitter. To handle events, use on(event, handler). When the event handler is called, longterm will pass to it the data that was originally provided when the event was scheduled.

longterm.on('party', function(data) {
  for (guest in data.inviteList) {
    console.log('Welcome to the party, ' + guest + '!');
  }
})

You may want to set up an error handler. If you don't specify one, errors will be printed to console.error.

longterm.on('error', function(err) {
  handle(err);
})

For more examples in an actual app environment, check out the demo directory.

Getting a Queue

Longterm needs to be connected to a queue to work correctly. The default queue is stored in-memory, which doesn't actually work for persisting across app restarts, so you'll need a queue backed by a more persistent data store. You can plug in an existing implementation, or roll your own. Set the queue option in longterm.init to use it.

Existing Queue Implementations

Implementing your own Queue

To implement a queue, you'll need to export a constructor with a few methods. All the methods in this interface use callbacks but they aren't listed in the definitions; you're smart enough to figure out where they go. To test your queue (with mocha), use longterm-queue-test. All irrecoverable errors should be bubbled up to the callback.

If you wrote a queue and you want it mentioned on this readme, please submit a pull request.

peek()

Fetches the soonest event (Earliest possible date) and supplies it to the callback. Sends null if there are no events queued.

enqueue(when, data)

Stores an event at time when (when is a Date object), containing data, and assigns it a unique string ID (You can store it as whatever you want internally, but front-facing it should be a string). Sends the scheduled object to the callback in the form:

{
  id: "string",
  when: when, // unchanged
  data: data // unchanged
}

This is the form all scheduled events will take throughout the queue interface.

update(id, data)

Changes the data of the event with the given id. data will replace the event's previous data value. Sends the full event (with the updated data) to the callback in the form specified in enqueue. If the event does not exist in the queue, no changes are made and callback is invoked with null.

remove(id)

Removes the event with the given id entirely. Sends the number of events removed to the callback (should be 1), 0 if the event didn't exist.

find(id)

Sends the full event object with the given id to the callback. Makes no changes to the event. Sends null if the event is not found.

count()

Sends the total number of events in the queue to the callback.

clear()

Removes all events from the queue, and sends the total number removed to the callback.

For a more detailed look at what's required from a queue, check out the tests.