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

fix(effects): resubscribe every time an error occurs #2165

Merged
merged 3 commits into from
Oct 19, 2019
Merged
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
18 changes: 9 additions & 9 deletions modules/effects/spec/effect_sources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,18 @@ describe('EffectSources', () => {
it('should resubscribe on error by default', () => {
class Eff {
@Effect()
b$ = hot('a--b--c--d').pipe(
b$ = hot('a--e--b--e--c--e--d').pipe(
map(v => {
if (v == 'b') throw new Error('An Error');
if (v == 'e') throw new Error('An Error');
return v;
})
);
}

const sources$ = of(new Eff());

// 👇 'b' is ignored.
const expected = cold('a-----c--d');

// 👇 'e' is ignored.
const expected = cold('a-----b-----c-----d');
expect(toActions(sources$)).toBeObservable(expected);
});

Expand Down Expand Up @@ -516,17 +515,18 @@ describe('EffectSources', () => {
const sources$ = of(
new class {
b$ = createEffect(() =>
hot('a--b--c--d').pipe(
hot('a--e--b--e--c--e--d').pipe(
map(v => {
if (v == 'b') throw new Error('An Error');
if (v == 'e') throw new Error('An Error');
return v;
})
)
);
}()
);
// 👇 'b' is ignored.
const expected = cold('a-----c--d');

// 👇 'e' is ignored.
const expected = cold('a-----b-----c-----d');

expect(toActions(sources$)).toBeObservable(expected);
});
Expand Down
21 changes: 14 additions & 7 deletions modules/effects/src/effects_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ export function mergeEffects(
: sourceInstance[propertyName];

const resubscribable$ = resubscribeOnError
? observable$.pipe(
catchError(error => {
if (errorHandler) errorHandler.handleError(error);
// Return observable that produces this particular effect
return observable$;
})
)
? resubscribeInCaseOfError(observable$, errorHandler)
: observable$;

if (dispatch === false) {
Expand All @@ -56,3 +50,16 @@ export function mergeEffects(

return merge(...observables$);
}

function resubscribeInCaseOfError<T extends Action>(
observable$: Observable<T>,
errorHandler?: ErrorHandler
): Observable<T> {
return observable$.pipe(
catchError(error => {
if (errorHandler) errorHandler.handleError(error);
// Return observable that produces this particular effect
return resubscribeInCaseOfError(observable$, errorHandler);
})
);
}