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

Observable.bufferWhen emits data before trigger if source observable completes #2361

Closed
Jason3S opened this issue Feb 12, 2017 · 3 comments
Closed

Comments

@Jason3S
Copy link

Jason3S commented Feb 12, 2017

RxJS version:
5.1.0

Code to reproduce:

import * as Rx from 'rxjs/Rx';
import {ISuiteCallbackContext} from 'mocha';

describe('Experiment with Observable buffer', function (this: ISuiteCallbackContext) {
    this.timeout(10000);

    it('Demonstrates a bug with bufferWhen', () => {
        const src = Rx.Observable.interval(5).take(10);
        const interval = Rx.Observable.interval(1000);
        const trigger = new Rx.Subject<number>();

        interval
            .do(n => console.log('Trigger'))
            .subscribe(trigger);

        return src
            .bufferWhen(() => trigger)
            .do(v => console.log(v))
            .toArray()
            .toPromise()
            .then(values => {
                console.log(values);
                console.log('done.');
            });
        // Actual output:
        // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
        // [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ]
        // done.

        // Expected output:
        // Trigger
        // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
        // [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ]
        // done.
    });
});

Expected behavior:

src         ---1---2---3--|
trigger     --------------------s-------------->
bufferWhen
            --------------------[1,2,3]-|

I would expect that bufferWhen should only emit when the closingSelect (trigger) emits a value.

Actual behavior:

src         ---1---2---3--|
trigger     ----------------------------s------->
bufferWhen
            ---------------[1,2,3]-|

bufferWhen emits as soon as src is complete. That is unexpected and undesired.

Additional information:
This is an attempt at working around #2154. The issue there is that it doesn't emit at all.

@JaminFarr
Copy link

Thought I should put the workaround in this issue too where it's more relevant.

Zip with the trigger after the buffer gives the expected behaviour.

import * as Rx from 'rxjs/Rx';
import {ISuiteCallbackContext} from 'mocha';

describe('Experiment with Observable buffer', function (this: ISuiteCallbackContext) {
    this.timeout(10000);

    it('Demonstrates a bug with bufferWhen', () => {
        const src = Rx.Observable.interval(5).take(10);
        const interval = Rx.Observable.interval(1000);
        const trigger = new Rx.Subject<number>();

        interval
            .do(n => console.log('Trigger'))
            .subscribe(trigger);

        return src
            .bufferWhen(() => trigger)
            .zip(trigger, src => src)
            .do(v => console.log(v))
            .toArray()
            .toPromise()
            .then(values => {
                console.log(values);
                console.log('done.');
            });
        // Actual output:
        // Trigger
        // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
        // [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ]
        // done.

        // Expected output:
        // Trigger
        // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
        // [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ]
        // done.
    });
});

@Jason3S
Copy link
Author

Jason3S commented Feb 12, 2017

Nice workaround. Cool idea with the zip.

@benlesh
Copy link
Member

benlesh commented May 21, 2018

Closing as stale

@benlesh benlesh closed this as completed May 21, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 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

No branches or pull requests

3 participants