Giter Club home page Giter Club logo

rubidium's Introduction

rubidium

Build Status David

npm npm

Rubidium is a one-time task emitter, inspired by the unix at utility. This module is a minimal implementation. It exports a constructor. Instances of the constructor are event emitters, with methods to add and remove job specifications. A job consists of a due time and a message.

Rubidium is built upon the use of setTimeout, so the usual warnings apply. That said, Rubidium creates a fresh timeout after each job is emitted, so it is self-correcting to within the resolution of a timer.

Rubidium is built on vetebrate-event-emitter, which makes it robust against memory leaks. It has been bundled for you with its dependencies, so Rubidium has no dependencies in production.

Both UMD (for your CommonJS, AMD, and browser global needs) and ES2015 flavour modules have been provided, so you can use this module easily with all major module systems.

Usage

A brief example is given below. See the examples directory for some more interesting examples, including wrapping a rubidium instance in an HTTP server, and persisting a rubidium instance over app restarts.

var Rubidium = require('rubidium');

var rb = new Rubidium();

rb.on('job', job => console.log(job.message));

// Add a job to be emitted in 5 seconds time.
rb.add({ time: Date.now() + 5000, message: '5 seconds have passed.' });

// Add a job to be emitted in one day.
rb.add({ time: Date.now() + 1000 * 60 * 60 * 24, 'One day as passed.' });

You could of course do this with vanilla timeouts. A rubidium instance allows you to do it all with a single timeout. It also works around the maximum timeout length, so you can have a job more than about 25 days into the future.

API

Rubidium extends vertebrate-event-emitter, so please see the docs for that for the base API around events (methods like on, off etc.)

var rb = new Rubidium()

Construct a new Rubidium instance.

var hasJobs = rb.hasPendingJobs

A boolean property. True when there are jobs remaining in the queue.

var job = rb.add({ time, message, [uuid] }, silent)

Add a job. The time must be a Date instance or a timestamp integer (like Date.now()) representing the time for the job to be emitted. This method returns a job with a uuid property that may be used to find or remove the job from the queue.

When uuid is in the spec, it will be copied into the job. This is useful for reviving a persisted queue (see the section on persistence below). In general, you should not be setting this.

When silent is true, this addJob event will not be emitted. This is useful for reviving a persisted queue.

var job = rb.remove(uuid, silent)

Remove a job from a Rubidium instance with the job uuid. This function returns a job if the job existed and was removed, or undefined if the job did not exist. When silent is true, the removeJob event will not be emitted.

var job = rb.find(uuid)

Get a job from the Rubidium instance with the job uuid. Returns undefined when no matching job is found.

rb.clear(silent)

Clear all pending jobs. When silent is true, the clearJobs event will not be emitted.

Event: 'job'

Listeners on the 'job' event receive a job object containing time and message fields, where time is an integer.

Event: 'addJob'

This event is emitted when a new job is added. The job object is passed as an argument to listeners on this event.

Event: 'removeJob'

This event is emitted when when a job is removed. The job object is passed as an argument to listeners on this event. When the clear method is used, this event is emitted for every job.

Event: 'clearJobs'

This event is emitted when all jobs for a Rubidium instance are cleared.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.