1.2.20 • Published 5 years ago

harken v1.2.20

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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.

1.2.20

5 years ago

1.2.19

6 years ago

1.2.18

6 years ago

1.2.17

6 years ago

1.2.16

6 years ago

1.2.15

6 years ago

1.2.14

6 years ago

1.2.13

6 years ago

1.2.12

6 years ago

1.2.11

6 years ago

1.2.10

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.6

6 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago