-
Notifications
You must be signed in to change notification settings - Fork 3k
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
fix(throttleTime): leading and trailing true should only emit a single value per time frame #2749
Changes from all commits
ade1c0e
9cda512
9fc78aa
15701a3
1b6c92a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,30 +85,37 @@ class ThrottleTimeSubscriber<T> extends Subscriber<T> { | |
} | ||
|
||
protected _next(value: T) { | ||
if (this.throttled) { | ||
if (this.trailing) { | ||
this._trailingValue = value; | ||
this._hasTrailingValue = true; | ||
} | ||
} else { | ||
this.add(this.throttled = this.scheduler.schedule(dispatchNext, this.duration, { subscriber: this })); | ||
if (this.leading) { | ||
this.destination.next(value); | ||
} | ||
if (!this.throttled && this.leading) { | ||
this.throttle(); | ||
this.destination.next(value); | ||
} else if (this.trailing) { | ||
this._trailingValue = value; | ||
this._hasTrailingValue = true; | ||
} | ||
|
||
if (!this.throttled) { | ||
this.throttle(); | ||
} | ||
} | ||
|
||
private throttle(): void { | ||
this.add(this.throttled = this.scheduler.schedule(dispatchNext, this.duration, { subscriber: this })); | ||
} | ||
|
||
clearThrottle() { | ||
const throttled = this.throttled; | ||
|
||
if (throttled) { | ||
throttled.unsubscribe(); | ||
this.throttled = null; | ||
|
||
if (this.trailing && this._hasTrailingValue) { | ||
this.destination.next(this._trailingValue); | ||
const trailingValue = this._trailingValue; | ||
this._trailingValue = null; | ||
this._hasTrailingValue = false; | ||
this.throttle(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is unnecessary and changes the behavior of the operator. It's not supposed to start throttling again when the throttle is cleared. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm.. I think there's a disconnect here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I okay, I actually see what you're saying now. This is to start the throttle again as soon as there's another emission (due to trailing behavior) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay... the more I look at this, the more I think it will have to be a change that lands in v 6.0, because it will be a breaking change for someone. (Albeit a fix) |
||
this.destination.next(trailingValue); | ||
} | ||
throttled.unsubscribe(); | ||
this.remove(throttled); | ||
this.throttled = null; | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor this a little to be:
I'm not sure in it's current configuration it will work as desired, because of that
else if
. If it's trailing, we always want to record the value, not only if we've already started throttling.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then
throttleTime
would emit the same input twice on{leading:true, trailing: true}
. I guess in the end it doesn't matter when throttleTime is used in conjunction withdistinctUntilChanged
, but it feels wrong regardless.vs. lodash