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

guarantee-events

v1.0.6

Published

Guarantee that every event handler gets every event...

Downloads

3

Readme

guaranteeEvents

This module exports a single function that when passed an event(s) and an EventEmitter compatible object will register a couple of handlers that will provide the following functionality:

  • Cache event data for each event of the given type that gets emitted.

  • Call new handlers of the specified event with each of the prior event data sets in order of event occurrence.

  • Add a .clearGuaranteedQueue(<event>) method to the emitter to facilitate event cache cleaning.

This is useful for modules like glob that use the EventEmitter model to pass data to the user (see examples below).

This is similar to how state change handlers work in jQuery.Deferred or in Promise objects.

Install

$ npm install guarantee-events

Basic Examples

A synthetic example illustrating the basic functionality:


var emitter = new (require('events').EventEmitter)
var guaranteeEvents = require('guarantee-events')

guaranteeEvents('event', emitter)

// emit some events...
emitter.emit('event', 'some data')

emitter.emit('event', 'some', 'more', 'data')


// Here the handler will be called for each event it missed...
emitter.on('event', function(){ console.log([].slice.apply(argumnts).join(' ')) })


emitter.emit('event', 'some data')

A real-life use-case using the excellent glob utility:

var glob = require('glob')
var guaranteeEvents = require('guarantee-events')


// build a glob object with cached 'match' and 'end' events...
var results = guaranteeEvents('match end', glob('**/*js'))


// Do stuff for some time...


// This will not miss a single result, regardless of how long it 
// took to do stuff...
results.on('match', function(path){ console.log('found: '+path) })

Cache cleaning and use for long running emitters

One of the dangers in using this in long running event emitters is cache buildup -- the data for each event emitted will get stored and this might get quite large, this, if not managed, is a potential memory leak.

To deal with this issue a .clearGuaranteedQueue(<event>) method is added to the emitter, this will clear the cache for a specific event. This and has a shorthand form .clearGuaranteedQueue('*') that will clear the cache for all wrapped events.

So for the above example:

// This this will drop all the prior match data, so newly registred handlers
// will not see them...
// NOTE: this will not affect the underlaying glob object in any way. 
results.clearGuaranteedQueue('match')