Skip to content

Commit

Permalink
feat(Observable): add let to allow fluent style query building
Browse files Browse the repository at this point in the history
Add let (and letBind for legacy browsers) operators, exactly like currently available in RxJS 4
(https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/let.md)
  • Loading branch information
Kevin van der Vlist authored and kwonoj committed Jan 11, 2016
1 parent db7f0b1 commit 5a2014c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
24 changes: 24 additions & 0 deletions spec/operators/let-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* globals describe, it, expect */
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('let', function () {
it('should be able to compose with let', function (done) {
var expected = ['aa', 'bb'];
var i = 0;

var foo = function (observable) {
return observable
.map(function (x) {
return x + x;
});
};

Observable
.fromArray(['a','b'])
.let(foo)
.subscribe(function (x) {
expect(x).toBe(expected[i++]);
}, null, done);
});
});
2 changes: 2 additions & 0 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ export class Observable<T> implements CoreOperators<T> {
last: <R>(predicate?: (value: T, index: number) => boolean,
resultSelector?: (value: T, index: number) => R,
defaultValue?: any) => Observable<T> | Observable<R>;
let: <T, R>(func: (selector: Observable<T>) => Observable<R>) => Observable<R>;
letBind: <T, R>(func: (selector: Observable<T>) => Observable<R>) => Observable<R>;
every: (predicate: (value: T, index: number) => boolean, thisArg?: any) => Observable<T>;
map: <R>(project: (x: T, ix?: number) => R, thisArg?: any) => Observable<R>;
mapTo: <R>(value: R) => Observable<R>;
Expand Down
1 change: 1 addition & 0 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import './add/operator/groupBy';
import './add/operator/ignoreElements';
import './add/operator/every';
import './add/operator/last';
import './add/operator/let';
import './add/operator/map';
import './add/operator/mapTo';
import './add/operator/materialize';
Expand Down
4 changes: 4 additions & 0 deletions src/add/operator/let.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Observable} from '../../Observable';
import {letProto} from '../../operator/let';
Observable.prototype.let = letProto;
Observable.prototype.letBind = letProto;
5 changes: 5 additions & 0 deletions src/operator/let.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Observable} from '../Observable';

export function letProto<T, R>(func: (selector: Observable<T>) => Observable<R>): Observable<R> {
return func(this);
}
6 changes: 4 additions & 2 deletions tools/generate-operator-patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ const SpecialCasePrototypes = {
'switch.ts': 'switch',
'do.ts': 'do',
'finally.ts': 'finally',
'catch.ts': 'catch'
'catch.ts': 'catch',
'let.ts': 'let'
};

const AdditionalAliases = {
'mergeMap.ts': ['flatMap'],
'fromArray.ts': ['of']
'fromArray.ts': ['of'],
'let.ts': ['letBind']
};

const AliasMethodOverrides = {
Expand Down

0 comments on commit 5a2014c

Please sign in to comment.