Skip to content

Commit c8294b6

Browse files
author
Liza K
committed
Handle exceptions
1 parent 1483a41 commit c8294b6

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/plugins/data/common/utils/tap_once.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,22 @@ describe('tapOnce', () => {
5353
},
5454
});
5555
});
56+
57+
test('fires once even when there is an error', async (done) => {
58+
const tester = jest.fn().mockImplementation(() => {
59+
throw new Error();
60+
});
61+
62+
const source = from([1, 2, 3, 4]).pipe(tapOnce(tester));
63+
64+
source.subscribe(() => {});
65+
source.subscribe({
66+
next: () => {},
67+
complete: () => {
68+
expect(tester).toBeCalledWith(1);
69+
expect(tester).toBeCalledTimes(1);
70+
done();
71+
},
72+
});
73+
});
5674
});

src/plugins/data/common/utils/tap_once.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ export function tapOnce<T>(fn: (v: T) => any) {
2929
let first = true;
3030
return source$.pipe(
3131
tap((payload) => {
32-
if (first) {
33-
fn(payload);
32+
try {
33+
if (first) {
34+
fn(payload);
35+
}
36+
first = false;
37+
} catch (e) {
38+
first = false;
3439
}
35-
first = false;
3640
})
3741
);
3842
};

0 commit comments

Comments
 (0)