New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

cycle-events

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cycle-events

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

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

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

  • Broker

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

ParamTypeDescription
eventStringThe event to subscribe to.
callbackfunctionThe 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

ParamTypeDescription
eventStringThe event to subscribe to.
callbackfunctionThe 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

ParamTypeDescription
eventStringThe event whose listener should be removed.
callbackfunctionThe 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

ParamTypeDescription
eventStringThe 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

ParamTypeDescription
eventStringThe 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

NameTypeDescription
eventStringThe event the listener was registered for.
callbackfunctionThe listener that caused the error.
errorErrorThe 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

NameTypeDescription
eventStringThe event the listener was registered for.
callbackfunctionThe 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

NameTypeDescription
eventStringThe event the listener was removed from.
callbackfunctionThe 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

NameTypeDescription
ERRORString'error' - An error occurred in an event listener.
ADDEDString'listenerAdded' - An event listener was registered.
REMOVEDString'listenerRemoved' - An event listener was removed.

Keywords

cycle

FAQs

Package last updated on 22 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts