How do I get event name of Observable created with .fromEvent

954 views Asked by At

I'm trying to restrict what events coming from common message bus should be emitted to server. Allowed events are on outgoingEvents array. Problem is that to properly send them th server I need event name.

var Events = new EventEmitter();
var outgoingEvents = [
    'messageSubmit'
];

var outgoingMessages = Observable.fromArray(outgoingEvents)
.flatMap(function(eventName) {
    return Observable.fromEvent(Events, eventName);
}).subscribe(function() {
    // need event name here to properly encode message:

    // var eventName = ... should be 'messageSubmit'
    // var args = [].slice.call(arguments, 1);
    // var encoded = JSON.stringify({ name: eventName, args: args });
    // socket.send(encoded);
});

Events.emit('messageSubmit', { message: 'Submitted!' }, {flags: 'ABC'});
1

There are 1 answers

0
Yury Solovyov On BEST ANSWER

It is possible to reshape objects that get emmited to inservable in function that comes third parameter in .fromEvent

return Observable.fromEvent(Events, eventName, function() {
    var args = [].slice.call(arguments[0]);
    return { name: eventName, args: args };
});

So it will be arguments of .subscribe:

var eventName = event.name;
var args = event.args;