Skip to content

feat(component-store): resubscribe on errors #3963

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions modules/component-store/spec/component-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
of,
queueScheduler,
scheduled,
Subject,
Subscription,
switchMap,
throwError,
timer,
} from 'rxjs';
Expand Down Expand Up @@ -1776,6 +1778,65 @@ describe('Component Store', () => {
jest.advanceTimersByTime(20);
});
});

describe('Resubscribes on errors', () => {
let eff: ReturnType<typeof componentStore.effect<string>>;
let lastResult: string | undefined;
let lastError: string | undefined;

beforeEach(() => {
lastResult = undefined;

eff = componentStore.effect<string>((_) =>
_.pipe(
tap((r) => (lastResult = r)),
switchMap((v) => {
if (v === 'error') {
lastError = v;
return throwError(() => 'err');
}
lastError = undefined;
return of(v);
})
)
);
});

it('resubscribes on error in generator', () => {
expect(lastError).toEqual(undefined);
eff('error');
expect(lastResult).toEqual('error');
expect(lastError).toEqual('error');

eff('next');
expect(lastResult).toEqual('next');
expect(lastError).toEqual(undefined);
});

it('resubscribes when value$ throws an error', () => {
expect(lastError).toEqual(undefined);
const s = new Subject<string>();
const m = s.pipe(
switchMap((v) => {
if (v === 'error') {
return throwError(() => 'err');
}
return of(v);
})
);
eff(m);
s.next('a');
expect(lastResult).toEqual('a');
s.next('error');
expect(lastResult).toEqual('a');
s.next('b');
expect(lastResult).toEqual('b');

eff('next');
expect(lastResult).toEqual('next');
expect(lastError).toEqual(undefined);
});
});
});

describe('get', () => {
Expand Down
5 changes: 3 additions & 2 deletions modules/component-store/src/component-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
asapScheduler,
EMPTY,
ObservedValueOf,
retry,
} from 'rxjs';
import {
takeUntil,
Expand Down Expand Up @@ -403,14 +404,14 @@ export class ComponentStore<T extends object> implements OnDestroy {
const origin$ = new Subject<ObservableType>();
generator(origin$ as OriginType)
// tied to the lifecycle 👇 of ComponentStore
.pipe(takeUntil(this.destroy$))
.pipe(retry(), takeUntil(this.destroy$))
.subscribe();

return ((
observableOrValue?: ObservableType | Observable<ObservableType>
): Subscription => {
const observable$ = isObservable(observableOrValue)
? observableOrValue
? observableOrValue.pipe(retry())
: of(observableOrValue);
return observable$.pipe(takeUntil(this.destroy$)).subscribe((value) => {
// any new 👇 value is pushed into a stream
Expand Down