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

pauseable

v0.3.2

Published

Create event emitters, intervals, and timeouts that can be paused and resumed.

Downloads

5,534

Readme

pauseable.js

Pauseable allows you to pause event emitters, timeouts, and intervals. It can group multiple of these pauseable objects and pause entire groups.

Dependency Status codecov

Usage

Using pauseable with EventEmitter

const pauseable = require('pauseable');
const EventEmitter = require('events').EventEmitter;

let ee = new EventEmitter();

ee.on('foo', () => { ... });

// ...later
pauseable.pause(ee);

// this event will not be immediately fired
// instead, it will be buffered
ee.emit('foo', 'hellow');

// ...much later
// the 'foo' event will be fired at this point
pauseable.resume(ee);

Comes with pauseable setTimeout and setInterval too

let timeout = pauseable.setTimeout(() => {
  // this will take 8 seconds total to execute
  // not 5
}, 5000);

// pause timeout after 2 secs
setTimeout(() => {
  timeout.pause();
  timeout.isPaused(); // true
  
  // resume after 3
  setTimeout(() => {
    timeout.resume();
  }, 3000);
}, 2000);

The function and ms arguments are interchangeable. Use whichever way you prefer!

let interval = pauseable.setInterval(5000, () => {
  // this is called after 5 seconds
  // then paused for 2 seconds
  interval.pause(2000);
});

Grouping

// create a group
let g = pauseable.createGroup();

// make and add emitters to group
let ee1 = g.add(new EventEmitter());
let ee2 = g.add(new EventEmitter());

ee1.on('forth', () => {
  // pause entire group (that means ee1 and ee2) for 500 ms
  // timeout is out of the group by the time this executes
  g.pause(500);
  console.log('forth');
  ee2.emit('back');
});

ee2.on('back', () => {
  console.log('back');
  ee1.emit('forth');
});

let timeout = g.setTimeout(() => {
  ee2.emit('back', 'poop');
}, 1000);

Motive

Javascript is event based by nature. When developing large scale applications that are completely event based, it becomes complicated to pause the streaming of events, because Javascript never "sleeps". It becomes even more complicated to pause timeouts and intervals, having to keep track of when they were paused so they can be resumed with the correct time again.

That's where this module comes in. Pauseable helps manage pausing and resuming your application or part of it. It works with EventEmitter, with setInterval and setTimeout.

API

Events

pauseable.pause(ee, [ms])

Pauses an instance of EventEmitter. All events get buffered and emitted once the emitter is resumed. Currently only works with EventEmitters. Optional ms will pause only for that long.

pauseable.resume(ee, [ms])

Resumes the emitter. Events can be emitted again. Optional ms will resume only for that long.

Timers

pauseable.setTimeout(fn, ms) and pauseable.setInterval(fn, ms)

Creates a pauseable timeout or interval. fn and ms are interchangeabale. Returns an instance of timer.

timer.pause([ms])

Pauses timer. Optional ms will pause the timer only for that amount.

timer.resume([ms])

Resumes timer. Optional ms will resume the timer only for that amount.

timer.next()

Returns the number of ms left until the fn function in the constructor gets called again.

timer.clear()

Clears timeout. Can no longer be resumed.

timer.isPaused()

Returns true if timer is currently paused.

Groups

pauseable.createGroup()

Creates a group where emitters and timers can be easily managed.

group.add()

Add an emitter or a timer to the group. Returns added emitter/timer.

group.setTimeout(fn, ms)

group.setInterval(fn, ms)

Shortcut to create an instance of a timer and add it to the group.

group.pause([ms])

Pauses all emitters and timers in the group.

group.resume([ms])

Resumes all emitters and timers in the group.

group.clear()

Clears timers in the group.

group.isPaused()

Returns true is group is paused.

group.isDone()

Returns true if all timers currently in the group are timeouts and their original function has been called or all timers have been cleared.

group.timers()

Contains both emitters and timers. For if you want to micro manage more.

Install

npm install pauseable

Tests

Tests are written with mocha

npm test