Skip to content

Commit

Permalink
feat(operator): implement startWith().
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuharuohzeki authored and benlesh committed Aug 13, 2015
1 parent 6447f43 commit 1f36d99
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
20 changes: 20 additions & 0 deletions spec/operators/startWith-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* globals describe, it, expect */
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('Observable.prototype.startWith()', function () {
it('should start an obsevable with given value', function (done) {
var source = 'source'
var init = 'init';
var expected = [init, source];

var i = 0;
Observable.just(source)
.startWith(init)
.subscribe(function (x) {
expect(x).toBe(expected[i++]);
}, null, function () {
done();
});
});
});
3 changes: 2 additions & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default class Observable<T> {
toArray: () => Observable<T[]>;
scan: <R>(project: (acc: R, x: T) => R, acc?: R) => Observable<R>;
reduce: <R>(project: (acc: R, x: T) => R, acc?: R) => Observable<R>;
startWith: <T>(x: T) => Observable<T>;

filter: (predicate: (x: T) => boolean, ix?: number, thisArg?: any) => Observable<T>;
skip: (count: number) => Observable<T>;
Expand All @@ -134,6 +135,6 @@ export default class Observable<T> {

publish: () => ConnectableObservable<T>;
multicast: (subjectFactory: () => Subject<T>) => ConnectableObservable<T>;

catch: (selector: (err: any, source: Observable<T>, caught: Observable<any>) => Observable<any>) => Observable<T>;
}
2 changes: 2 additions & 0 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ import mapTo from './operators/mapTo';
import toArray from './operators/toArray';
import scan from './operators/scan';
import reduce from './operators/reduce';
import startWith from './operators/startWith';

observableProto.map = map;
observableProto.mapTo = mapTo;
observableProto.toArray = toArray;
observableProto.scan = scan;
observableProto.reduce = reduce;
observableProto.startWith = startWith;

import filter from './operators/filter';
import take from './operators/take';
Expand Down
8 changes: 8 additions & 0 deletions src/operators/startWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Observable from '../Observable';
import ScalarObservable from '../observables/ScalarObservable';
import concat from './concat';

export default function startWith<T>(x: T): Observable<T> {
const init = new ScalarObservable(x);
return concat.call(init, null, <Observable<T>>this);
}

0 comments on commit 1f36d99

Please sign in to comment.