diff --git a/spec/operators/let-spec.js b/spec/operators/let-spec.js new file mode 100644 index 0000000000..405a4dd811 --- /dev/null +++ b/spec/operators/let-spec.js @@ -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); + }); +}); diff --git a/src/Observable.ts b/src/Observable.ts index b5500db710..3f61663add 100644 --- a/src/Observable.ts +++ b/src/Observable.ts @@ -219,6 +219,8 @@ export class Observable implements CoreOperators { last: (predicate?: (value: T, index: number) => boolean, resultSelector?: (value: T, index: number) => R, defaultValue?: any) => Observable | Observable; + let: (func: (selector: Observable) => Observable) => Observable; + letBind: (func: (selector: Observable) => Observable) => Observable; every: (predicate: (value: T, index: number) => boolean, thisArg?: any) => Observable; map: (project: (x: T, ix?: number) => R, thisArg?: any) => Observable; mapTo: (value: R) => Observable; diff --git a/src/Rx.ts b/src/Rx.ts index 51ef1deffa..1b450334d1 100644 --- a/src/Rx.ts +++ b/src/Rx.ts @@ -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'; diff --git a/src/add/operator/let.ts b/src/add/operator/let.ts new file mode 100644 index 0000000000..cc4e0f4b2d --- /dev/null +++ b/src/add/operator/let.ts @@ -0,0 +1,4 @@ +import {Observable} from '../../Observable'; +import {letProto} from '../../operator/let'; +Observable.prototype.let = letProto; +Observable.prototype.letBind = letProto; diff --git a/src/operator/let.ts b/src/operator/let.ts new file mode 100644 index 0000000000..e97023c675 --- /dev/null +++ b/src/operator/let.ts @@ -0,0 +1,5 @@ +import {Observable} from '../Observable'; + +export function letProto(func: (selector: Observable) => Observable): Observable { + return func(this); +} diff --git a/tools/generate-operator-patches.ts b/tools/generate-operator-patches.ts index d90a939419..51c39be9ce 100644 --- a/tools/generate-operator-patches.ts +++ b/tools/generate-operator-patches.ts @@ -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 = {