-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(if): add static Observable.if creation operator.
- Loading branch information
Showing
6 changed files
with
80 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as Rx from '../../dist/cjs/Rx.KitchenSink'; | ||
import {expectObservable} from '../helpers/marble-testing'; | ||
import {it} from '../helpers/test-helper'; | ||
|
||
const Observable = Rx.Observable; | ||
|
||
describe('Observable.if', () => { | ||
it('should subscribe to thenSource when the conditional returns true', () => { | ||
const e1 = Observable.if(() => true, Observable.of('a')); | ||
const expected = '(a|)'; | ||
|
||
expectObservable(e1).toBe(expected); | ||
}); | ||
|
||
it('should subscribe to elseSource when the conditional returns false', () => { | ||
const e1 = Observable.if(() => false, Observable.of('a'), Observable.of('b')); | ||
const expected = '(b|)'; | ||
|
||
expectObservable(e1).toBe(expected); | ||
}); | ||
|
||
it('should complete without an elseSource when the conditional returns false', () => { | ||
const e1 = Observable.if(() => false, Observable.of('a')); | ||
const expected = '|'; | ||
|
||
expectObservable(e1).toBe(expected); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './Rx'; | ||
|
||
// statics | ||
import './add/observable/if'; | ||
import './add/observable/using'; | ||
|
||
// Operators | ||
|
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,4 @@ | ||
import {Observable} from '../../Observable'; | ||
import {IfObservable} from '../../observable/IfObservable'; | ||
|
||
Observable.if = IfObservable.create; |
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,42 @@ | ||
import {Observable} from '../Observable'; | ||
import {Subscriber} from '../Subscriber'; | ||
import {Subscription} from '../Subscription'; | ||
|
||
export class IfObservable<T, R> extends Observable<T> { | ||
|
||
static create<T, R>(condition: () => boolean, | ||
thenSource?: Observable<T>, | ||
elseSource?: Observable<R>): Observable<T|R> { | ||
return new IfObservable(condition, thenSource, elseSource); | ||
} | ||
|
||
constructor(private condition: () => boolean, | ||
private thenSource?: Observable<T>, | ||
private elseSource?: Observable<R>) { | ||
super(); | ||
} | ||
|
||
protected _subscribe(subscriber: Subscriber<T|R>): Subscription | Function | void { | ||
|
||
const { condition, thenSource, elseSource } = this; | ||
|
||
let result: boolean, error: any, errorHappened = false; | ||
|
||
try { | ||
result = condition(); | ||
} catch (e) { | ||
error = e; | ||
errorHappened = true; | ||
} | ||
|
||
if (errorHappened) { | ||
subscriber.error(error); | ||
} else if (result && thenSource) { | ||
return thenSource.subscribe(subscriber); | ||
} else if (elseSource) { | ||
return elseSource.subscribe(subscriber); | ||
} else { | ||
subscriber.complete(); | ||
} | ||
} | ||
} |