Skip to content
Open
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
16 changes: 11 additions & 5 deletions modules/operators/spec/tap-response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ describe('tapResponse', () => {
it('should invoke next callback on next', () => {
const nextCallback = vi.fn<(value: [number]) => void>();

of(1, 2, 3).pipe(tapResponse(nextCallback, noop)).subscribe();
of(1, 2, 3)
.pipe(tapResponse({ next: nextCallback, error: noop }))
.subscribe();

expect(nextCallback.mock.calls).toEqual([[1], [2], [3]]);
});
Expand All @@ -17,7 +19,7 @@ describe('tapResponse', () => {
const error = { message: 'error' };

throwError(() => error)
.pipe(tapResponse(noop, errorCallback))
.pipe(tapResponse({ next: noop, error: errorCallback }))
.subscribe();

expect(errorCallback).toHaveBeenCalledWith(error);
Expand All @@ -31,15 +33,19 @@ describe('tapResponse', () => {
throw error;
}

of(1).pipe(tapResponse(producesError, errorCallback)).subscribe();
of(1)
.pipe(tapResponse({ next: producesError, error: errorCallback }))
.subscribe();

expect(errorCallback).toHaveBeenCalledWith(error);
});

it('should invoke complete callback on complete', () => {
const completeCallback = vi.fn<() => void>();

EMPTY.pipe(tapResponse(noop, noop, completeCallback)).subscribe();
EMPTY.pipe(
tapResponse({ next: noop, error: noop, complete: completeCallback })
).subscribe();

expect(completeCallback).toHaveBeenCalledWith();
});
Expand Down Expand Up @@ -105,7 +111,7 @@ describe('tapResponse', () => {
.pipe(
concatMap(() =>
throwError(() => 'error').pipe(
tapResponse(noop, noop),
tapResponse({ next: noop, error: noop }),
finalize(innerCompleteCallback)
)
),
Expand Down
23 changes: 16 additions & 7 deletions modules/operators/spec/types/tap-response.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ describe('tapResponse types', () => {
it('should infer next type', () => {
expectSnippet(`
of(1).pipe(
tapResponse((next) => {
const num = next;
}, noop)
tapResponse({
next: (next) => {
const num = next;
},
error: noop,
})
);
`).toInfer('num', 'number');
});

it('should accept error type', () => {
expectSnippet(`
of(true).pipe(
tapResponse(noop, (error: { message: string }) => {
const err = error;
tapResponse({
next: noop,
error: (error: { message: string }) => {
const err = error;
},
})
);
`).toInfer('err', '{ message: string; }');
Expand All @@ -33,8 +39,11 @@ describe('tapResponse types', () => {
it('should use unknown as default error type', () => {
expectSnippet(`
of(true).pipe(
tapResponse(noop, (error) => {
const err = error;
tapResponse({
next: noop,
error: (error) => {
const err = error;
},
})
);
`).toInfer('err', 'unknown');
Expand Down
28 changes: 2 additions & 26 deletions modules/operators/src/tap-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ type TapResponseObserver<T, E> = {
finalize?: () => void;
};

export function tapResponse<T, E = unknown>(
observer: TapResponseObserver<T, E>
): (source$: Observable<T>) => Observable<T>;
/**
* @deprecated Instead of passing a sequence of callbacks, use an observer
* object. For more info see: https://github.com/ngrx/platform/issues/4840
*/
export function tapResponse<T, E = unknown>(
next: (value: T) => void,
error: (error: E) => void,
complete?: () => void
): (source$: Observable<T>) => Observable<T>;
/**
* Handles the response in ComponentStore effects in a safe way, without
* additional boilerplate. It enforces that the error case is handled and
Expand All @@ -46,21 +34,9 @@ export function tapResponse<T, E = unknown>(
* );
* ```
*/
export function tapResponse<T, E>(
observerOrNext: TapResponseObserver<T, E> | ((value: T) => void),
error?: (error: E) => void,
complete?: () => void
export function tapResponse<T, E = unknown>(
observer: TapResponseObserver<T, E>
): (source$: Observable<T>) => Observable<T> {
const observer: TapResponseObserver<T, E> =
typeof observerOrNext === 'function'
? {
next: observerOrNext,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
error: error!,
complete,
}
: observerOrNext;

return (source) =>
source.pipe(
tap({ next: observer.next, complete: observer.complete }),
Expand Down
Loading