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

Unexpected false default value for leading when only trailing is specified in config parameter #7146

Closed
tobiiasl opened this issue Jan 11, 2023 · 0 comments · Fixed by #7176

Comments

@tobiiasl
Copy link

tobiiasl commented Jan 11, 2023

Describe the bug

The config object to throttle and throttleTime behaves in an unexpected way regarding leading.

The documentation states that the config parameter "Defaults to { leading: true, trailing: false }" but if a config parameter is specified the default value for leading becomes false.

In other words, if someone wants to change the behaviour for trailing to true and therefore adds a config parameter { trailing: true } this also changes the behavior of leading to be false.

I was first tricked by this but then realised I have to specify both leading and trailing even if I just want to change the behavior of trailing. But then I got PR review comments on the updated code from two independent people that I did not have to specify leading: true since that is the default value.

So it seems like the current behaviour is quite unexpected since they also thought that default value for leading would not be affected by specifying trailing.

Expected behavior

I would expect that leading is set to the default value (true) but this code would cause leading: false:

  throttle(1000, undefined, { trailing: true }

I expect the reproduction code to print the same as if I do not specify trailing, ie:

0
6
13
20
26

but it prints

6
13
20
26

Reproduction code

timer(0, 150)
  .pipe(throttleTime(1000, undefined, { trailing: true }))
  .subscribe((v) => console.log(v));

Reproduction URL

https://stackblitz.com/edit/rxjs-wehnfb?file=index.ts

Version

7.5.5

Environment

No response

Additional context

I believe the last line in this code defines the current behavior:

* @param durationSelector A function
 * that receives a value from the source Observable, for computing the silencing
 * duration for each source value, returned as an Observable or a Promise.
 * @param config a configuration object to define `leading` and `trailing` behavior. Defaults
 * to `{ leading: true, trailing: false }`.
 * @return A function that returns an Observable that performs the throttle
 * operation to limit the rate of emissions from the source.
 */
export function throttle<T>(
  durationSelector: (value: T) => ObservableInput<any>,
  config: ThrottleConfig = defaultThrottleConfig
): MonoTypeOperatorFunction<T> {
  return operate((source, subscriber) => {
    const { leading, trailing } = config;
@tobiiasl tobiiasl changed the title Unexpected false default value for leading when only trailing is specified in config parameter Unexpected false default value for leading when only trailing is specified in config parameter Jan 11, 2023
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 a pull request may close this issue.

1 participant