-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rx): add flattening operators with default error handling strate…
…gies
- Loading branch information
1 parent
9a7213a
commit af3af82
Showing
10 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {catchError, concatMap, Observable, of, OperatorFunction} from "rxjs"; | ||
|
||
/** | ||
* RxJs concatMap operator with error handling | ||
*/ | ||
export function rxConcatmap<T, R>(project: (value: T, index: number) => Observable<R>): OperatorFunction<T, R> { | ||
return (source: Observable<T>) => source.pipe( | ||
concatMap((value, index) => project(value, index).pipe( | ||
catchError(error => of(error)) | ||
)), | ||
); | ||
|
||
} | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {catchError, exhaustMap, Observable, of, OperatorFunction} from "rxjs"; | ||
|
||
/** | ||
* RxJs exhaustMap operator with error handling | ||
*/ | ||
export function rxExhaustMap<T, R>(project: (value: T, index: number) => Observable<R>): OperatorFunction<T, R> { | ||
return (source: Observable<T>) => source.pipe( | ||
exhaustMap((value, index) => project(value, index).pipe( | ||
catchError(error => of(error)) | ||
)), | ||
); | ||
|
||
} | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {catchError, mergeMap, Observable, of, OperatorFunction} from "rxjs"; | ||
|
||
/** | ||
* RxJs mergeMap operator with error handling | ||
*/ | ||
export function rxMergeMap<T, R>(project: (value: T, index: number) => Observable<R>): OperatorFunction<T, R> { | ||
return (source: Observable<T>) => source.pipe( | ||
mergeMap((value, index) => project(value, index).pipe( | ||
catchError(error => of(error)) | ||
)), | ||
); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// create marble tests for rxSwitchMap operator | ||
|
||
|
||
import {rxSwitchMap} from "./rx-switchmap"; | ||
import {TestScheduler} from "rxjs/internal/testing/TestScheduler"; | ||
import {observableMatcher} from "@angular-kit/test-helpers"; | ||
import {map} from "rxjs"; | ||
|
||
describe('rxSwitchMap', () => { | ||
|
||
let testScheduler: TestScheduler; | ||
|
||
beforeEach(() => { | ||
testScheduler = new TestScheduler(observableMatcher); | ||
}); | ||
|
||
it('should map-and-flatten each item to an Observable', () => { | ||
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => { | ||
const e1 = hot(' --1-----3--5-------|'); | ||
const e1subs = ' ^------------------!'; | ||
const e2 = cold(' x-x-x| ', { x: 10 }); | ||
// x-x-x| | ||
// x-x-x| | ||
const expected = ' --x-x-x-y-yz-z-z---|'; | ||
const values = { x: 10, y: 30, z: 50 }; | ||
|
||
const result = e1.pipe(rxSwitchMap((x) => e2.pipe(map((i) => i * +x)))); | ||
|
||
expectObservable(result).toBe(expected, values); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
// todo | ||
/*describe('default error handling', () => { | ||
it('should catch and return the raised error', () => { | ||
testScheduler.run(({ hot, cold, expectObservable }) => { | ||
const e1 = hot(' --1-1--|'); | ||
const e2 = cold(' y-x--| ', { x: 10, y: 1 }); | ||
// x-x-x| | ||
// x-x-x| | ||
const expected = ' --e-x--|'; | ||
const values = { x: 10, e: 'error' }; | ||
const result = e1.pipe(rxSwitchMap((x) => e2.pipe(map((i) => { | ||
if(i === 2) { | ||
throw ('error'); | ||
} | ||
return i * +x | ||
})))); | ||
expectObservable(result).toBe(expected, values); | ||
}); | ||
}); | ||
});*/ | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {catchError, Observable, of, OperatorFunction, switchMap} from "rxjs"; | ||
|
||
/** | ||
* RxJs switchMap operator with error handling | ||
*/ | ||
export function rxSwitchMap<T, R>(project: (value: T, index: number) => Observable<R>): OperatorFunction<T, R> { | ||
return (source: Observable<T>) => source.pipe( | ||
switchMap((value, index) => project(value, index).pipe( | ||
catchError(error => of(error)) | ||
)), | ||
); | ||
|
||
} | ||
|