fromEvent

Rx.Node.fromEvent(eventEmitter, eventName)

#

Handles an event from the given EventEmitter as an observable sequence.

Arguments

  1. eventEmitter (EventEmitter): The EventEmitter to subscribe to the given event.
  2. eventName (String): The event name to subscribe.

Returns

(Observable): An observable sequence generated from the named event from the given EventEmitter.

Example

var EventEmitter = require('events').EventEmitter;
var Rx = require('Rx');

var emitter = new EventEmitter();

var source = Rx.Node.fromEvent(emitter, 'data');

var observer = Rx.Observer.create(
    function (x) {
        console.log('Next: ' + x[0]);
    },
    function (err) {
        console.log('Error: ' + err);   
    },
    function () {
        console.log('Completed');   
    }
);

var subscription = source.subscribe(observer);

emitter.emit('data', 'foo');

// => Next: foo

Location

  • rx.node.js