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

cycle-events

v0.1.0

Published

Provides event-based pub/sub to cycle.js applications.

Downloads

7

Readme

cycle-events

Provides event-based pub/sub to cycle.js applications.

Installation

npm i cycle-events --save

Scripts

NOTE: Make sure you've installed all dependencies using npm install first.

To generate documentation: npm run doc. This will create documentation in the build/docs folder.

To run unit tests: npm test

API

Broker

Kind: global class

new Broker()

Provides pub/sub functionality to Cycle.js applications.

Example

var broker = require('cycle-events')(); // create instance
var off = broker.on('my-custom-event', // register handler
  function myCallback(arg1, arg2) { ... }
);
broker.emit('my-custom-event', 'arg1', 'arg2'); // fire event
off(); // remove the event handler

Example

// add pub/sub functionality to a class and
// automatically log any errors caused by
// subscribers of the class:
function MyClass() {
  Broker.call(this); // mixin pub/sub
  Rx.Observable.fromEvent(this, 'error')
    .subscribe(logger.error);
}

// now use the class:
var myClass = new MyClass();
myClass.on('some-custom-event', function() {
  methodDoesNotExist(); // error logged automatically
});

broker.on(event, callback) ⇒ function

Registers a listener for the specified event.

Kind: instance method of Broker
Returns: function - A method to invoke to remove the listener from the specified event.
Throws:

  • TypeError Parameter event must be a non-empty string.
  • TypeError Parameter callback must be a function.

Emits: listenerAdded

| Param | Type | Description | | --- | --- | --- | | event | String | The event to subscribe to. | | callback | function | The listener to invoke when the specified event is emitted. |

Example

// register an event handler:
broker.on('my-custom-event', myEventHandler(arg1, arg2) { ... };
// invoke the event handler (any any others registered):
broker.emit('my-custom-event', 'arg1', 'arg2');

broker.one(event, callback) ⇒ function

Registers a listener for the specified event, but ensures the listener will only be fired at most 1 time. Once a listener has been invoked, it will automatically be removed.

Kind: instance method of Broker
Returns: function - A method to invoke to remove the listener from the specified event.
Throws:

  • TypeError Parameter event must be a non-empty string.
  • TypeError Parameter callback must be a function.

Emits: listenerAdded

| Param | Type | Description | | --- | --- | --- | | event | String | The event to subscribe to. | | callback | function | The listener to invoke when the specified event is emitted. |

Example

// register a handler to only run once:
broker.one('my-custom-event', myEventHandler(arg1, arg2) { ... };
// the handler will be removed after being invoked:
broker.emit('my-custom-event', 'arg1', 'arg2');

broker.off(event, callback)

Removes the specified listener for the specified event.

Kind: instance method of Broker
Throws:

  • TypeError Parameter event must be a non-empty string.
  • TypeError Parameter callback must be a function.

Emits: listenerRemoved

| Param | Type | Description | | --- | --- | --- | | event | String | The event whose listener should be removed. | | callback | function | The listener to remove. |

Example

function myHandler() { ... }
broker.on('my-custom-event', myHandler);
broker.off('my-custom-event', myHandler);

broker.removeAllListeners(event)

Removes all listeners registered for the specified event.

Kind: instance method of Broker
Throws:

  • TypeError Parameter event must be a non-empty string.
  • TypeError Parameter callback must be a function.

Emits: listenerRemoved

| Param | Type | Description | | --- | --- | --- | | event | String | The event whose listeners should all be removed. |

Example

broker.on('custom-event', function myHandler1() { ... });
broker.on('custom-event', function myHandler2() { ... });
broker.emit('custom-event'); // both handlers invoked
broker.removeAllListeners('custom-event');
broker.emit('custom-event'); // no handlers invoked

broker.emit(event, args)

Invokes any listeners for the specified event--in the order they were registered--and passes any provided arguments to those listeners. If a listener throws an exception, the error event will be emitted but subsequent listeners will still be invoked.

Kind: instance method of Broker
Throws:

  • TypeError Parameter event must be a non-empty string.

Emits: error

| Param | Type | Description | | --- | --- | --- | | event | String | The event to emit. | | args | * | Any additional arguments to pass to listeners. |

Example

broker.on('my-custom-event', function() { ... });
broker.emit('my-custom-event'); // handler fired

Example

broker.on('log', function(msg, ...args) {
   log.write(msg, args);
});
broker.emit('log', 'Today is %s', new Date());

Example

broker.on('sum', function(...nums) {
   var sum = nums.reduce(function(result, num) {
       return result + num;
   }, 0);
   log.info('The sum of', nums, 'is', sum);
});
broker.emit('add', 1, 2, 3, 4, 5);

"error"

An error occurred in an event listener while firing an event.

Kind: event emitted by Broker
Properties

| Name | Type | Description | | --- | --- | --- | | event | String | The event the listener was registered for. | | callback | function | The listener that caused the error. | | error | Error | The error that occurred while invoking the listener. |

Example

broker.on(Broker.Events.ERROR, function(data) {
  log.error('An error occurred:', data.error);
});

"listenerAdded"

A listener was added. Examine the event properties for details.

Kind: event emitted by Broker
Properties

| Name | Type | Description | | --- | --- | --- | | event | String | The event the listener was registered for. | | callback | function | The listener registered for the event. |

Example

broker.on(Broker.Events.ADDED, function(data) {
  log(data.event); // 'my-custom-event'
  log(data.callback); // function callback() { ... }
});
broker.on('my-custom-event', function callback() { ... });

"listenerRemoved"

A listener was removed. Examine the event properties for details.

Kind: event emitted by Broker
Properties

| Name | Type | Description | | --- | --- | --- | | event | String | The event the listener was removed from. | | callback | function | The listener removed from the event. |

Example

broker.on(Broker.Events.REMOVED, function(data) {
  log(data.event); // 'my-custom-event'
  log(data.callback); // function callback() { ... }
});
var off = broker.on('my-custom-event', function callback() { ... });
off(); // remove the event handler

Broker.Events : Events

An enumeration of event names used internally that external callers can also subscribe to.

Kind: static property of Broker
Example

broker.on(Broker.Events.ADDED, function listenerAdded() { ... });
broker.on(Broker.Events.ERROR, function errorOccurred() { ... });

Broker~Events : Object

Kind: inner typedef of Broker
Properties

| Name | Type | Description | | --- | --- | --- | | ERROR | String | 'error' - An error occurred in an event listener. | | ADDED | String | 'listenerAdded' - An event listener was registered. | | REMOVED | String | 'listenerRemoved' - An event listener was removed. |