Skip to content

Commit

Permalink
fix(effects): resubscribe every time an error occurs (ngrx#2165)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored and jordanpowell88 committed Nov 14, 2019
1 parent d52dd48 commit 9a53b6f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
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);
})
);
}

0 comments on commit 9a53b6f

Please sign in to comment.