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

Throttle fix #2864

Closed
wants to merge 1 commit into from
Closed

Throttle fix #2864

wants to merge 1 commit into from

Conversation

benlesh
Copy link
Member

@benlesh benlesh commented Sep 25, 2017

To address #2859

@rxjs-bot
Copy link

rxjs-bot commented Sep 25, 2017

Messages
📖

CJS: 1341.7KB, global: 736.8KB (gzipped: 137.9KB), min: 144.4KB (gzipped: 30.7KB)

Generated by 🚫 dangerJS

Copy link
Member

@kwonoj kwonoj left a comment

Choose a reason for hiding this comment

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

do we need test cases for { leading: true, trailing: false }? seems not needed maybe.

@sod
Copy link

sod commented Sep 26, 2017

Was something wrong with #2749?

@benlesh
Copy link
Member Author

benlesh commented Sep 26, 2017

@sod nope... I just didn't see it. sorry.

@benlesh benlesh closed this Sep 26, 2017
@benlesh benlesh reopened this Sep 26, 2017
@benlesh
Copy link
Member Author

benlesh commented Sep 26, 2017

Okay, @sod I've reviewed your PR. there were a lot of changes I requested, if you think you can knock those out, that would be great. I'll just ween this PR down to only be the throttle operator, as your fixes were only to throttleTime.

BREAKING CHANGES: This changes the behavior of throttle, in particular
throttling with both leading and trailing behaviors set to true, to more
closely match the throttling behavior of lodash and other libraries.
Throttling now starts immediately after any emission from the
observable, and values will not be double emitted for both leading and
trailing values.

fixes #2859
@Parakleta
Copy link

This has the same problem that #2749 had initially in that it defers the setup of the throttle observable until after emitting the value, and this can lead to drift in the throttling. I think it needs to look something like:

protected _next(value: T): void {
    this._hasValue = true;
    this._sendValue = value;

    if (!this._throttled) {
        this.throttle(this._leading);
    }
}
private throttle(doSend: boolean) {
    const { _hasValue, _sendValue } = this;
    if (!_hasValue) return;

    let duration, err;
    try {
        duration = this.durationSelector(_sendValue);
    } catch (e) {
        err = e;
    }

    if (doSend) {
        this.destination.next(_sendValue);
        this._hasValue = false;
        this._sendValue = null;
    }

    if (err != null) {
        this.destination.error(err);
    } else {
        this.add(this._throttled = subscribeToResult(this, duration));
    }
}
private throttlingDone() {
    const { _throttled, _trailing } = this;
    if (_throttled) {
        _throttled.unsubscribe();
    }
    this._throttled = null;

    if (_trailing) {
        this.throttle(true);
    }
}

The throttle() method feels untidy to me, but I'm not sure how to make it nicer.

@micha149
Copy link

Whats up with this PR?

@benlesh
Copy link
Member Author

benlesh commented Feb 23, 2018

@micha149

it needs to be rebased, and I haven't gotten back around to it.

@micha149
Copy link

@benlesh thanks for the response. will it be possible to get this into Version 6.0.0? Can I support by rebasing your commit?

@benlesh
Copy link
Member Author

benlesh commented Feb 26, 2018

@micha149 Yeah, we're going to get this in. Feel free to rebase if you have the time.

@micha149
Copy link

@benlesh I rebased the commit. As far as I know I'm not able to get it into this PR. You can pull from here: micha149@df689e5

@Parakleta
Copy link

This still doesn't deal with the issue that a slow next() emission will cause the throttling to drift (or have inconsistent duration) as discussed in #2749. I think swapping lines https://github.com/micha149/rxjs/blob/df689e5550bef58f534d8779b2c7c27d35440089/src/internal/operators/throttle.ts#L113-L114 should fix it.

benlesh added a commit to benlesh/rxjs that referenced this pull request Mar 30, 2018
BREAKING CHANGES: This changes the behavior of throttle, in particular
throttling with both leading and trailing behaviors set to true, to more
closely match the throttling behavior of lodash and other libraries.
Throttling now starts immediately after any emission from the
observable, and values will not be double emitted for both leading and
trailing values

closes ReactiveX#2864
@benlesh
Copy link
Member Author

benlesh commented Mar 30, 2018

Changes coped to #3505

@benlesh benlesh closed this Mar 30, 2018
benlesh added a commit that referenced this pull request Mar 30, 2018
…3505)

BREAKING CHANGES: This changes the behavior of throttle, in particular
throttling with both leading and trailing behaviors set to true, to more
closely match the throttling behavior of lodash and other libraries.
Throttling now starts immediately after any emission from the
observable, and values will not be double emitted for both leading and
trailing values

closes #2864
@coveralls
Copy link

coveralls commented Apr 11, 2018

Coverage Status

Coverage remained the same at ?% when pulling ccca46c on benlesh:throttle_fix into d24f5b9 on ReactiveX:master.

@lock
Copy link

lock bot commented Jun 5, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants