-
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.
Merge pull request #3250 from benlesh/iif-fn
Refactor iif impl to be defer-based (plus some file moves/cleanup)
- Loading branch information
Showing
8 changed files
with
116 additions
and
166 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
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,4 +1,5 @@ | ||
import { Observable } from '../../internal/Observable'; | ||
import { _if } from '../../internal/observable/if'; | ||
import { iif } from '../../internal/observable/iif'; | ||
|
||
Observable.if = _if; | ||
//tslint:disable-next-line:no-any TypeScript doesn't like `if` | ||
(Observable as any).if = iif; |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,93 @@ | ||
import { Observable } from '../Observable'; | ||
import { defer } from './defer'; | ||
import { EMPTY } from './empty'; | ||
import { SubscribableOrPromise } from '../types'; | ||
|
||
/** | ||
* Decides at subscription time which Observable will actually be subscribed. | ||
* | ||
* <span class="informal">`If` statement for Observables.</span> | ||
* | ||
* `if` accepts a condition function and two Observables. When | ||
* an Observable returned by the operator is subscribed, condition function will be called. | ||
* Based on what boolean it returns at that moment, consumer will subscribe either to | ||
* the first Observable (if condition was true) or to the second (if condition was false). Condition | ||
* function may also not return anything - in that case condition will be evaluated as false and | ||
* second Observable will be subscribed. | ||
* | ||
* Note that Observables for both cases (true and false) are optional. If condition points to an Observable that | ||
* was left undefined, resulting stream will simply complete immediately. That allows you to, rather | ||
* then controlling which Observable will be subscribed, decide at runtime if consumer should have access | ||
* to given Observable or not. | ||
* | ||
* If you have more complex logic that requires decision between more than two Observables, {@link defer} | ||
* will probably be a better choice. Actually `if` can be easily implemented with {@link defer} | ||
* and exists only for convenience and readability reasons. | ||
* | ||
* | ||
* @example <caption>Change at runtime which Observable will be subscribed</caption> | ||
* let subscribeToFirst; | ||
* const firstOrSecond = Rx.Observable.if( | ||
* () => subscribeToFirst, | ||
* Rx.Observable.of('first'), | ||
* Rx.Observable.of('second') | ||
* ); | ||
* | ||
* subscribeToFirst = true; | ||
* firstOrSecond.subscribe(value => console.log(value)); | ||
* | ||
* // Logs: | ||
* // "first" | ||
* | ||
* subscribeToFirst = false; | ||
* firstOrSecond.subscribe(value => console.log(value)); | ||
* | ||
* // Logs: | ||
* // "second" | ||
* | ||
* | ||
* @example <caption>Control an access to an Observable</caption> | ||
* let accessGranted; | ||
* const observableIfYouHaveAccess = Rx.Observable.if( | ||
* () => accessGranted, | ||
* Rx.Observable.of('It seems you have an access...') // Note that only one Observable is passed to the operator. | ||
* ); | ||
* | ||
* accessGranted = true; | ||
* observableIfYouHaveAccess.subscribe( | ||
* value => console.log(value), | ||
* err => {}, | ||
* () => console.log('The end') | ||
* ); | ||
* | ||
* // Logs: | ||
* // "It seems you have an access..." | ||
* // "The end" | ||
* | ||
* accessGranted = false; | ||
* observableIfYouHaveAccess.subscribe( | ||
* value => console.log(value), | ||
* err => {}, | ||
* () => console.log('The end') | ||
* ); | ||
* | ||
* // Logs: | ||
* // "The end" | ||
* | ||
* @see {@link defer} | ||
* | ||
* @param {function(): boolean} condition Condition which Observable should be chosen. | ||
* @param {Observable} [trueObservable] An Observable that will be subscribed if condition is true. | ||
* @param {Observable} [falseObservable] An Observable that will be subscribed if condition is false. | ||
* @return {Observable} Either first or second Observable, depending on condition. | ||
* @static true | ||
* @name iif | ||
* @owner Observable | ||
*/ | ||
export function iif<T, F>( | ||
condition: () => boolean, | ||
trueResult: SubscribableOrPromise<T> = EMPTY, | ||
falseResult: SubscribableOrPromise<F> = EMPTY | ||
): Observable<T|F> { | ||
return defer<T|F>(() => condition() ? trueResult : falseResult); | ||
} |