fromEvent

Rx.Node.fromEvent(eventEmitter, eventName)

#

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

参数

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

返回值

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

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