-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat(RecoverWith): add recoverWith Operator #260
base: master
Are you sure you want to change the base?
Changes from all commits
d5b43a4
4437bc5
4ee3dc6
632ef74
3d65266
561bafb
048782d
5c5dc7a
b6155bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Created by srijan02420 on 12/10/18. | ||
*/ | ||
|
||
import {CompleteMixin, NextMixin, Virgin} from '../internal/Mixins' | ||
import {IObservable} from '../internal/Observable' | ||
import {IObserver} from '../internal/Observer' | ||
import {ISubscription} from '../internal/Subscription' | ||
import {curry} from '../internal/Utils' | ||
import {flatMap, just} from '../main' | ||
import {IScheduler} from '../schedulers/Scheduler' | ||
|
||
export type TSource<T> = IObservable<T> | ||
export type TResult<R> = IObservable<R> | ||
export type TMapper<T> = (err: Error) => IObservable<T> | ||
|
||
class RecoverWithObserver<T> extends NextMixin(CompleteMixin(Virgin)) | ||
implements IObserver<T | Error> { | ||
constructor(public sink: IObserver<T | Error>) { | ||
super() | ||
} | ||
|
||
error(err: Error): void { | ||
this.sink.next(err) | ||
} | ||
} | ||
|
||
class RecoverWithObservable<T> implements TResult<T | Error> { | ||
constructor(private source: IObservable<T>) {} | ||
|
||
subscribe(observer: IObserver<T>, scheduler: IScheduler): ISubscription { | ||
return this.source.subscribe(new RecoverWithObserver(observer), scheduler) | ||
} | ||
} | ||
|
||
export const recoverWith = curry(function<T>( | ||
mapFunction: TMapper<T>, | ||
source: TSource<T> | ||
) { | ||
return flatMap( | ||
val => (val instanceof Error ? mapFunction(val) : just(val)), | ||
new RecoverWithObservable(source) | ||
) | ||
}) as { | ||
<T>(mapper: TMapper<T>, source: TSource<T>): TResult<T> | ||
<T>(): {mapper: TMapper<T>; (source: TSource<T>): TResult<T>} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as t from 'assert' | ||
import {EVENT} from '../src/internal/Events' | ||
import {fromMarble} from '../src/internal/Marble' | ||
import {just} from '../src/main' | ||
import {recoverWith} from '../src/operators/RecoverWith' | ||
import {createTestScheduler} from '../src/schedulers/TestScheduler' | ||
|
||
const {next, complete, error} = EVENT | ||
|
||
describe('recoverWith()', () => { | ||
it('should emit errors as data', () => { | ||
const sh = createTestScheduler() | ||
const $ = sh.Cold<number>([ | ||
next(200, 10), | ||
error(201, new Error('err1')), | ||
error(202, new Error('err2')), | ||
next(203, 10), | ||
complete(205) | ||
]) | ||
const {results} = sh.start(() => recoverWith((err: Error) => just(-1), $)) | ||
t.deepEqual(results, [ | ||
next(401, 10), | ||
next(402, -1), | ||
next(403, -1), | ||
next(404, 10), | ||
complete(405) | ||
]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you write a marble test instead. |
||
}) | ||
it('(marble): should emit errors as data', () => { | ||
const SH = createTestScheduler() | ||
const source$ = SH.Cold(fromMarble('a-b-#-c-#-d|')) | ||
const o$ = SH.Cold(fromMarble('z-x-y-w-|')) | ||
const {results} = SH.start(() => { | ||
return recoverWith((err: Error) => o$, source$) | ||
}) | ||
t.deepEqual(results, [ | ||
EVENT.next(401, 'a'), | ||
EVENT.next(403, 'b'), | ||
EVENT.next(407, 'c'), | ||
EVENT.next(411, 'd'), | ||
EVENT.next(604, 'z'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As soon as an error is dispatched. The subscription should end. So even though the stream is firing |
||
EVENT.next(606, 'x'), | ||
EVENT.next(608, 'y'), | ||
EVENT.next(608, 'z'), | ||
EVENT.next(610, 'w'), | ||
EVENT.next(610, 'x'), | ||
EVENT.next(612, 'y'), | ||
EVENT.next(614, 'w'), | ||
EVENT.complete(616) | ||
]) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need tests for when the subscription starts and when it ends.