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

harken

v1.2.20

Published

a simple event library

Downloads

29

Readme

Harken!

Greenkeeper badge Harken is a drop in replacement for the built in eventemitter in nodejs/iojs with a few extre nice pieces. It also works in clients because it is pretty much just simple javascript. So now you can take the node event goodness and have it everywhere.

Codeship Status for ansble/harken

API

This is how you use this thing.

.on / .addListener

The most basic usage looks like this:

    harken.on('some-event', function () {
        //do something
    });

    harken.addListener('some-event', function () {
        //do something
    });

Pass in an event to listen for, and a function to execute when the event happens. Simple.

.on and .addListener are the same function so from here on out we'll just be using .on in the examples. But know that you can change the name of the function to .addListener and it will work exactly the same.

A .on allows you to pass it the following options either as positionals or as an object hash:

    harken.on(eventName, handler, scope, once);

    harkent.on({
        eventName: 'event'
        , handler: function () {}
        , scope: {}
        , once: true
    });
Required parameters

eventName is the event that you want to listen for. handler is the function to be executed, it recieves the payload of the event.

Optional parameters

scope is the scope applied to the execution of the function. Can be useful at times and is optional once is a boolean that indicates if the listener should be un-bound after it has been called.

.off / .removeListener

The most basic usage looks like this:

    harken.off('some-event', function () {
        //do something
    });

    harken.removeListener('some-event', function () {
        //do something
    });

Pass in an event to stop listening too, and the function that you want to stop triggering.

.off and .removeListener are the same function so from here on out we'll just be using .off in the examples. But know that you can change the name of the function to .removeListener and it will work exactly the same.

A .off allows you to pass it the following options either as positionals or as an object hash:

    harken.off(eventName, handler, scope, once);

    harkent.off({
        eventName: 'event'
        , handler: function () {}
        , scope: {}
        , once: true
    });
Required parameters

eventName is the event that you want to stop listening to.

Optional parameters

handler is the function that was executing. scope is the scope applied to the execution of the function. once is a boolean that indicates if the listener should be un-bound after it has been called.

.off looks for an exact combination of the parameters passed into it. This allows for either precise unbinding of listeners, or a more heavy-handed approach that turns off all the listeners for a given event. It's all up to you and how you use it.

.emit

    harken.emit('some-event', {payload: 'is cool and awesome'});

Emit triggers the listeners for a given event and can accept an optional payload. The payload can be any valid javascript value, so objects, strings, arrays, you name it.

.cleanup

    harken.cleanup();

.cleanup removes any old listeners that are hanging around for no good reason. It executes .off on any listener in the store that is over 120000ms old. It's arbitrary... but this will be an area of improvement going forward.

.listeners

    var listenersArray = harken.listeners('some-event');

Returns all of the listeners for a given event.

.once

This is a convenience function for creating one-time-use event listeners. Any listener created with it will be unbound after executing.

    harken.once('some-event', function () {
        //do something
    });

Pass in an event to listen for, and a function to execute when the event happens.

A .once allows you to pass it the following options either as positionals or as an object hash:

    harken.once(eventName, handler, scope);

    harkent.once({
        eventName: 'event'
        , handler: function () {}
        , scope: {}
    });

.removeAllListeners

    harken.removeAllListeners('some-event');

This is a convenience function for harken.off('some-event') which is already pretty convenient. It removes all the listeners for a given event.

.required

    harken.required(['event-1', 'event-2', 'event-3'], function (dataArray) {
    //do something here when all three events have triggered
  });

Harken uses event-state to provide stateful eventing. Above is a simple example but you probably want to read the documentation on event-state if you are going to use this extensively. It's API is pretty simple.