Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscribe ignores second argument of event emitter #2431

Closed
benjaminhon opened this issue Mar 1, 2017 · 13 comments
Closed

subscribe ignores second argument of event emitter #2431

benjaminhon opened this issue Mar 1, 2017 · 13 comments

Comments

@benjaminhon
Copy link

I am using redis, which exposes the event emitter as follows with 2 arguments, without rxjs, i can get both arguments, however with rxjs, the second argument is missing.

r.on 'message', (channel, message) ->
  console.log channel, message

# => Greet, Hello

Rx.Observable.fromEvent(r, 'message').subscribe (channel, message) ->
  console.log channel, message

# => Greet, undefined

is this supposed to be the case?

@mpodlasin
Copy link
Contributor

That is how it works at the moment. You can get access to the second parameter by using project function:

Rx.Observable.fromEvent(r, 'message', (channel, message) => [channel, message])
.subscribe(arr => {
  console.log(arr[0]); // logs value of channel
  console.log(arr[1]); // logs value of message
});

That being said it seems like a strong case to make fromEvent behave like bindCallback which emits an array if there are many arguments in callback.

@mpodlasin
Copy link
Contributor

Does redis have an off function for canceling subscriptions? How do you stop listening to events?

@benjaminhon
Copy link
Author

benjaminhon commented Mar 2, 2017 via email

@mpodlasin
Copy link
Contributor

mpodlasin commented Mar 2, 2017

That's weird. If there is no off (I literally mean method that is named "off") function on redis, then fromEvent should throw an Error, since it is not supported...

@trxcllnt
Copy link
Member

trxcllnt commented Mar 3, 2017

We should probably update fromEvent to work with EventEmitters, as I constantly have this problem too. From what I remember, EventEmitters have on and removeListener methods. Does that sound right?

@kwonoj
Copy link
Member

kwonoj commented Mar 3, 2017

, EventEmitters have on and removeListener methods.

doesn't node evemtEmitter works already via addListener (https://nodejs.org/api/events.html#events_emitter_addlistener_eventname_listener) /removeListener ?(

export type NodeStyleEventEmitter = {
addListener: (eventName: string, handler: Function) => void;
removeListener: (eventName: string, handler: Function) => void;
};
)

@benjaminhon
Copy link
Author

@Podlas29 that does the job

@kwonoj
Copy link
Member

kwonoj commented Apr 16, 2017

I'm not sure where this issue has landed - is there specific we need to resolve in this issue?

@felixfbecker
Copy link
Contributor

felixfbecker commented May 6, 2017

It would probably be a more ergonomic API to default to an array if multiple args are provided, but it would also be a breaking change. At the same time, the workaround is very concise:

Observable.fromEvent(r, 'message', Array.of)
  .subscribe(([channel, message]) => {
    // ...
  })

@felixfbecker
Copy link
Contributor

or you could even create an object with lodash:

Observable.from(r, 'message', partial(zipObject, ['channel', 'message']))
  .subscribe(event => {
    console.log(event.channel)
    console.log(event.message)
  })

@benjaminhon
Copy link
Author

zeromq for node alse use multipart replies which requires the above

@raycarter
Copy link

raycarter commented Nov 5, 2017

A simple use case about this issue and more about #3048 and PR #3049:

const Observable = require('rxjs').Observable;
const Oboe = require('oboe');

// create oboe instance
const oboe = Oboe({
  url: 'http://test-streaming.shengsoft.net',
  method: 'GET'
});

// original usage of oboe node event
oboe.on('node', '!.*', function() {
  console.log('from original oboe node event callback');
  console.log(arguments); // here 3 arguments are passed in
  console.log('');
});

// create another oboe instance
const oboe1 = Oboe({
  url: 'http://test-streaming.shengsoft.net',
  method: 'GET'
});

// use fromEventPattern to convert oboe event in Observable
const oboeObs = Observable.fromEventPattern((handler) => {
  oboe1.on('node', '!.*', handler);
});

oboeObs.subscribe(function() {
  console.log('from Observable handler');
  console.log(arguments); // !!! only the first argument is passed in
  console.log('');
});

In this simple use case Observable combines Oboe to consume a json-streaming server. The server returns only a part of a whole json object every second. Observable is introduced to process the data returned from the json-streaming server. But it got only the first argument while the original Oboe event passed 3 arguments in event handler. I have created a simple repo for you to retry what I mean.

@benlesh
Copy link
Member

benlesh commented Aug 18, 2020

This is no longer an issue. fromEvent will emit an array of arguments if it gets more than one argument back from the handler.

@benlesh benlesh closed this as completed Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants