Skip to content

Commit

Permalink
feat(if): add static Observable.if creation operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
trxcllnt committed Mar 10, 2016
1 parent 3b5453b commit f7ff7ec
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"commit": "git-cz",
"compile_dist_amd": "tsc ./typings/main.d.ts ./dist/amd/src/Rx.ts ./dist/amd/src/Rx.KitchenSink.ts ./dist/amd/src/Rx.DOM.ts ./dist/amd/src/add/observable/of.ts -m amd --sourceMap --outDir ./dist/amd --target ES5 --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"compile_dist_cjs": "tsc ./typings/main.d.ts ./dist/cjs/src/Rx.ts ./dist/cjs/src/Rx.KitchenSink.ts ./dist/cjs/src/Rx.DOM.ts ./dist/cjs/src/add/observable/of.ts -m commonjs --sourceMap --outDir ./dist/cjs --target ES5 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"compile_dist_es6": "tsc ./dist/es6/src/Rx.ts ./dist/es6/src/Rx.KitchenSink.ts ./dist/es6/src/Rx.DOM.ts ./dist/es6/src/add/observable/of.ts -m es2015 --sourceMap --outDir ./dist/es6 --target ES6 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"compile_dist_es6": "tsc ./dist/es6/src/Rx.ts ./dist/es6/src/Rx.KitchenSink.ts ./dist/es6/src/Rx.DOM.ts ./dist/es6/src/add/observable/of.ts -m es2015 --sourceMap --outDir ./dist/es6 --target ES6 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"cover": "npm run cover_test && npm run cover_remapping",
"cover_test": "istanbul cover -x \"*-spec.js index.js *-helper.js spec-js/helpers/*\" ./node_modules/jasmine/bin/jasmine.js",
"cover_remapping": "remap-istanbul -i coverage/coverage.json -o coverage/coverage-remapped.json && remap-istanbul -i coverage/coverage.json -o coverage/coverage-remapped.lcov -t lcovonly && remap-istanbul -i coverage/coverage.json -o coverage/coverage-remapped -t html",
Expand Down
28 changes: 28 additions & 0 deletions spec/observables/if-spec.ts
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);
});
});
6 changes: 4 additions & 2 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {root} from './util/root';
import {SymbolShim} from './util/SymbolShim';
import {toSubscriber} from './util/toSubscriber';

import {IfObservable} from './observable/IfObservable';
import {ErrorObservable} from './observable/ErrorObservable';

export type ObservableOrPromise<T> = Observable<T> | Promise<T>;
Expand Down Expand Up @@ -150,7 +151,8 @@ export class Observable<T> {
return this.source.subscribe(subscriber);
}

// Throw is the special snow flake, the compiler sees it as a reserved word
// `if` and `throw` are special snow flakes, the compiler sees them as reserved words
static if: typeof IfObservable.create;
static throw: typeof ErrorObservable.create;

/**
Expand All @@ -161,4 +163,4 @@ export class Observable<T> {
[SymbolShim.observable]() {
return this;
}
}
}
1 change: 1 addition & 0 deletions src/Rx.KitchenSink.ts
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
Expand Down
4 changes: 4 additions & 0 deletions src/add/observable/if.ts
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;
42 changes: 42 additions & 0 deletions src/observable/IfObservable.ts
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();
}
}
}

0 comments on commit f7ff7ec

Please sign in to comment.