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 behaves differently when wrapped #4095

Closed
OliverJAsh opened this issue Sep 5, 2018 · 3 comments
Closed

Observable behaves differently when wrapped #4095

OliverJAsh opened this issue Sep 5, 2018 · 3 comments
Assignees
Labels
bug Confirmed bug

Comments

@OliverJAsh
Copy link
Contributor

Bug Report

Current Behavior
Given this code:

import { Observable, of, OperatorFunction, timer } from 'rxjs';
import { catchError, concat, mergeMap, mergeMapTo, startWith } from 'rxjs/operators';

enum State {
    loading = 'loading',
    error = 'error',
}

const ajax$ = new Observable<State>(subscriber => {
    setTimeout(() => {
        subscriber.error('foo');
        subscriber.complete();
    }, 3000);
}).pipe(
    catchError(_error => of(State.error)),
    startWith(State.loading),
);

const delayedRepeatWhenError = (): OperatorFunction<State, State> => state$ =>
    state$.pipe(
        mergeMap(state => {
            const currentState$ = of(state);
            return state === State.error
                ? currentState$.pipe(concat(timer(3000).pipe(mergeMapTo(state$))))
                : currentState$;
        }),
    );

ajax$.pipe(delayedRepeatWhenError()).subscribe(console.log);

The output is as expected:

loading
error
loading
error

However, if the observable is wrapped and then subscribed to, the output is different and not as expected.

Code:

const wrappedAjax$ = new Observable<State>(subscriber => {
    const subscription = ajax$.subscribe(subscriber);
    return () => {
        subscription.unsubscribe();
    };
});

wrappedAjax$.pipe(delayedRepeatWhenError()).subscribe(console.log);

Output:

loading
error

This seems to be a regression from one of the releases after 6.2.2, as that's what I just upgraded from.

Reproduction

@cartant
Copy link
Collaborator

cartant commented Sep 5, 2018

Thanks for the repro of the bug. I've confirmed that this is due to #3963 (which was a refactor of #2457).

@cartant
Copy link
Collaborator

cartant commented Sep 5, 2018

The problem appears to be related to the returning of a teardown function instead of returning the subscription itself.

If the observable is wrapped without the intermediate teardown function, it works fine:

const wrappedAjax$ = new Observable<State>(subscriber => ajax$.subscribe(subscriber));

Wrapping the unsubscribe call in a teardown function see this test pass and the subscription's unsubscription is added to itself. Clearly, that test is inadequate and an alternative mechanism for handling this is required.

@cartant cartant added the bug Confirmed bug label Sep 5, 2018
@cartant cartant self-assigned this Sep 6, 2018
@cartant
Copy link
Collaborator

cartant commented Sep 6, 2018

I have a solution for this. I will submit a PR tomorrow.

cartant added a commit to cartant/rxjs that referenced this issue Sep 6, 2018
When unsubscribing a subscriber's parent, make sure that the subscriber
itself is not unsubscribed.

Closes ReactiveX#4095
cartant added a commit to cartant/rxjs that referenced this issue Sep 6, 2018
When unsubscribing a subscriber's parent, make sure that the subscriber
itself is not unsubscribed.

Closes ReactiveX#4095
cartant added a commit to cartant/rxjs that referenced this issue Sep 14, 2018
When unsubscribing a subscriber's parent, make sure that the subscriber
itself is not unsubscribed.

Closes ReactiveX#4095
@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

2 participants