Rxjs: fromEventPattern() should not magically return different results based on arguments.length

Created on 26 Apr 2019  路  2Comments  路  Source: ReactiveX/rxjs

Bug Report

Current Behavior
fromEventPattern currently checks args.length and if it's 1, emits the first item, otherwise it emits an array of all:

https://github.com/ReactiveX/rxjs/blob/d3e7e3f299e277b077602d26c59dab40ef0e1dba/src/internal/observable/fromEventPattern.ts#L153

This is problematic because it is not type safe and unpredictable.
It could be made slightly more type safe with a conditional type, but even then, if the parameter is optional TS would accept calling the listener with undefined or no parameter at all, which are different args.length and would result in one case in the emission of an array [undefined] and in the other case in undefined, which would not be checked by the type checker.

Checking arguments.length is generally considered bad practice.

What's worse is that you can't even reliably use resultSelector anymore to ensure it is always an array, because the magic switching is _always_ done first, and resultSelector is just a shortcut now that checks if the element is an array. Which means that if the listener was _actually_ called with an array as a single parameter, the resultSelector logic would _think_ it was instead called with multiple parameters and spread the array out.

Expected behavior
fromEventPattern() should just emit a tuple of all parameters. It's trivial to destructure that array or map it to just the first parameter if desired.

Environment

  • RxJS version: 6
discussion

Most helpful comment

Yeah. I agree that this behaviour is not ideal. This is something that could be looked at for v7, but not for v6 where it would be a breaking change.

All 2 comments

Yeah. I agree that this behaviour is not ideal. This is something that could be looked at for v7, but not for v6 where it would be a breaking change.

How about simply replacing e.length === 1 with e.length <= 1 for now? I'd call it a bug fix, not a breaking change.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benlesh picture benlesh  路  3Comments

marcusradell picture marcusradell  路  4Comments

Agraphie picture Agraphie  路  3Comments

haf picture haf  路  3Comments

matthewwithanm picture matthewwithanm  路  4Comments