Socket
Socket
Sign inDemoInstall

co-events

Package Overview
Dependencies
1
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    co-events

Wrapper for EventEmitter for using coroutines.


Version published
Weekly downloads
1
Maintainers
1
Install size
27.7 kB
Created
Weekly downloads
 

Readme

Source

TOC

  • Co-events

Co-events

API

Listeners can be anything that co supports viz. promises, thunks, generators, arrays or objects..

var events, fs, read;
events = new CoEvents;
fs = require('fs');
read = function(filename) {
  return function(cb) {
    return fs.readFile(filename, cb);
  };
};
events.on('readFile', function*(filename) {
  return callback(null, yield read(filename));
});
return events.emit('readFile', 'LICENSE');

Good old EventEmitter style functions.

var events, fs;
events = new CoEvents;
fs = require('fs');
events.on('readFile', function(filename) {
  return callback(null, fs.readFileSync(filename));
});
return events.emit('readFile', 'LICENSE');

Listener called when event emitted.

var events;
events = new CoEvents;
events.on('event', function*() {
  return callback(null);
});
return events.emit('event');

Arguments sent by emit are applied to listener.

var events;
events = new CoEvents;
events.on('wait', function*(time) {
  yield wait(time);
  return callback(null);
});
return events.emit('wait', 500);

RemoveListener can remove attached listeners.

var events, fn;
events = new CoEvents;
fn = function*() {
  return 'Hello';
};
events.on('hello', fn);
events.removeListener('hello', fn);
assert.equal(false, events.emit('hello'));
return callback(null);

Events registered using .once are removed once they are fired.

var events;
events = new CoEvents;
events.once('hello', function*() {
  return 'Hello';
});
events.emit('hello');
assert.equal(false, events.emit('hello'));
return callback(null);

Aliases

.off is an alias of .removeListener and .trigger is an alias of .emit.

var events, fn;
events = new CoEvents;
fn = function*() {
  return 'Hello';
};
events.on('hello', fn);
events.trigger('hello');
events.off('hello', fn);
assert.equal(false, events.trigger('hello'));
return callback(null);

Keywords

FAQs

Last updated on 01 Feb 2014

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc