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

feat(fromEvent): Aggregate multiple arguments #89

Merged
merged 1 commit into from
Jul 22, 2016

Conversation

xtianjohns
Copy link
Contributor

If provided with an EventEmitter, fromEvent streams will emit an array of values when the EventEmitter provides listeners with multiple arguments.

Description

The previous implementation naively provided only the first argument, which made fromEvent incompatible with event interfaces that rely on providing multiple arguments to their listeners, such as Node's http.Server.

An alternative solution involved providing selector support. This wasn't implemented for several reasons.

  1. Modifying the function signature for the extra would be a breaking change.
  2. An expanded function signature would become 'crowded', and one of the architectural goals of xstream is to keep interfaces as small as they need to be to achieve their goals.
  3. Aggregating emitted events into an array is consistent with the implementation in Most.js, and achieves similar flexibility with Rx when providing a selector function to .map() on a result stream.

fromEvent will now emit mixed-types. If consumers are not responsible for calling .emit() on the source emitter, they should implement appropriate guards to ensure they are dealing with an intended type.

I think that it would be great if fromEvent could support typed EventEmitters so that downstream consumers could rely on TypeScript to warn them about guards, but EventEmitter isn't a generic type and that would take some rejiggering that I didn't think was warranted to implement this feature.

Motivation and Context

This should resolve #84.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@@ -29,7 +29,9 @@ export class NodeEventProducer implements InternalProducer<any> {
constructor(private node: EventEmitter, private eventName: string) { }

_start(out: InternalListener<any>) {
this.listener = (e: any) => out._n(e);
this.listener = (...args: Array<any>) => {
return (args.length > 1) ? out._n([...args]) : out._n(args[0]);
Copy link
Contributor Author

@xtianjohns xtianjohns Jul 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably don't need to expand [...args] here. @staltz, if this bothers you I'll fix it.

Edit: it bothers me, I'll probably fix this today unless it gets merged.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, [...array] is the same as array. It's like x + 1 - 1. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also be good information to have in the future, do you prefer amended/rebased stuff for changes like this, or smaller fix commits?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amended is better

If provided with an EventEmitter, `fromEvent` streams will emit an array
of values when the EventEmitter provides listeners with multiple
arguments.

The previous implementation naively provided only the first argument,
which made `fromEvent` incompatible with event interfaces that rely on
providing multiple arguments to their listeners, such as Node's
`http.Server`.

An alternative solution involved providing selector support. This wasn't
implemented for several reasons.

  1. Modifying the function signature for the extra would be a
     breaking change.
  2. An expanded function signature would become 'crowded', and one of
     the architectural goals of xstream is to keep interfaces as small
     as they need to be to achieve their goals.
  3. Aggregating emitted events into an array is consistent with the
     implementation in Most.js, and achieves similar flexibility with Rx
     when providing a selector function to `.map()` on a result stream.

`fromEvent` will now emit mixed-types. If consumers are not responsible
for calling `.emit()` on the source emitter, they should implement
appropriate guards to ensure they are dealing with an intended type.

Should resolve staltz#84.
@staltz staltz merged commit 714dd01 into staltz:master Jul 22, 2016
@staltz
Copy link
Owner

staltz commented Jul 22, 2016

Thanks! Merged

@staltz
Copy link
Owner

staltz commented Jul 22, 2016

Will be released in v5.3.0

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

Successfully merging this pull request may close these issues.

Events with more than one argument
2 participants