Skip to content

Commit 19f4362

Browse files
authored
Merge pull request #1803 from staltz/docs-expand
docs(operators): write comprehensive JSDoc for expand()
2 parents 4971993 + 6a21e0f commit 19f4362

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

spec/operators/expand-spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ const Observable = Rx.Observable;
77

88
/** @test {expand} */
99
describe('Observable.prototype.expand', () => {
10+
asDiagram('expand(x => x === 8 ? empty : \u2014\u20142*x\u2014| )')
11+
('should recursively map-and-flatten each item to an Observable', () => {
12+
const e1 = hot('--x----| ', {x: 1});
13+
const e1subs = '^ !';
14+
const e2 = cold('--c| ', {c: 2});
15+
const expected = '--a-b-c-d|';
16+
const values = {a: 1, b: 2, c: 4, d: 8};
17+
18+
const result = e1.expand(x => x === 8 ? Observable.empty() : e2.mapTo(2 * x));
19+
20+
expectObservable(result).toBe(expected, values);
21+
expectSubscriptions(e1.subscriptions).toBe(e1subs);
22+
});
23+
1024
it('should map and recursively flatten', () => {
1125
const values = {
1226
a: 1,

src/operator/expand.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,47 @@ import {InnerSubscriber} from '../InnerSubscriber';
1010
import {subscribeToResult} from '../util/subscribeToResult';
1111

1212
/**
13-
* Returns an Observable where for each item in the source Observable, the supplied function is applied to each item,
14-
* resulting in a new value to then be applied again with the function.
15-
* @param {function} project the function for projecting the next emitted item of the Observable.
16-
* @param {number} [concurrent] the max number of observables that can be created concurrently. defaults to infinity.
17-
* @param {Scheduler} [scheduler] The Scheduler to use for managing the expansions.
18-
* @return {Observable} an Observable containing the expansions of the source Observable.
13+
* Recursively projects each source value to an Observable which is merged in
14+
* the output Observable.
15+
*
16+
* <span class="informal">It's similar to {@link mergeMap}, but applies the
17+
* projection function to every source value as well as every output value.
18+
* It's recursive.</span>
19+
*
20+
* <img src="./img/expand.png" width="100%">
21+
*
22+
* Returns an Observable that emits items based on applying a function that you
23+
* supply to each item emitted by the source Observable, where that function
24+
* returns an Observable, and then merging those resulting Observables and
25+
* emitting the results of this merger. *Expand* will re-emit on the output
26+
* Observable every source value. Then, each output value is given to the
27+
* `project` function which returns an inner Observable to be merged on the
28+
* output Observable. Those output values resulting from the projection are also
29+
* given to the `project` function to produce new output values. This is how
30+
* *expand* behaves recursively.
31+
*
32+
* @example <caption>Start emitting the powers of two on every click, at most 10 of them</caption>
33+
* var clicks = Rx.Observable.fromEvent(document, 'click');
34+
* var powersOfTwo = clicks
35+
* .mapTo(1)
36+
* .expand(x => Rx.Observable.of(2 * x).delay(1000))
37+
* .take(10);
38+
* powersOfTwo.subscribe(x => console.log(x));
39+
*
40+
* @see {@link mergeMap}
41+
* @see {@link mergeScan}
42+
*
43+
* @param {function(value: T, index: number) => Observable} project A function
44+
* that, when applied to an item emitted by the source or the output Observable,
45+
* returns an Observable.
46+
* @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
47+
* Observables being subscribed to concurrently.
48+
* @param {Scheduler} [scheduler=null] The Scheduler to use for subscribing to
49+
* each projected inner Observable.
50+
* @return {Observable} An Observable that emits the source values and also
51+
* result of applying the projection function to each value emitted on the
52+
* output Observable and and merging the results of the Observables obtained
53+
* from this transformation.
1954
* @method expand
2055
* @owner Observable
2156
*/

0 commit comments

Comments
 (0)