Skip to content

Commit

Permalink
docs(operators): write comprehensive JSDoc for delayWhen()
Browse files Browse the repository at this point in the history
Also tweak harmless indentation in delayWhen specs.
  • Loading branch information
staltz authored and Andre Medeiros committed Apr 26, 2016
1 parent a526e35 commit 95e96d1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
8 changes: 4 additions & 4 deletions spec/operators/delayWhen-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ describe('Observable.prototype.delayWhen', () => {
const expected = '-----a------c----(b|)';
const subs = '^ !';
const selector = [cold( '--x--|'),
cold( '----------x-|'),
cold( '-x--|')];
cold( '----------(x|)'),
cold( '-x--|')];
const selectorSubs = [' ^ ! ',
' ^ !',
' ^! '];
' ^ !',
' ^! '];

let idx = 0;
function durationSelector(x) {
Expand Down
46 changes: 41 additions & 5 deletions src/operator/delayWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,47 @@ import {InnerSubscriber} from '../InnerSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';

/**
* Returns an Observable that delays the emission of items from the source Observable
* by a subscription delay and a delay selector function for each element.
* @param {Function} selector function to retrieve a sequence indicating the delay for each given element.
* @param {Observable} sequence indicating the delay for the subscription to the source.
* @return {Observable} an Observable that delays the emissions of the source Observable by the specified timeout or Date.
* Delays the emission of items from the source Observable by a given time span
* determined by the emissions of another Observable.
*
* <span class="informal">It's like {@link delay}, but the time span of the
* delay duration is determined by a second Observable.</span>
*
* <img src="./img/delayWhen.png" width="100%">
*
* `delayWhen` time shifts each emitted value from the source Observable by a
* time span determined by another Observable. When the source emits a value,
* the `delayDurationSelector` function is called with the source value as
* argument, and should return an Observable, called the "duration" Observable.
* The source value is emitted on the output Observable only when the duration
* Observable emits a value or completes.
*
* Optionally, `delayWhen` takes a second argument, `subscriptionDelay`, which
* is an Observable. When `subscriptionDelay` emits its first value or
* completes, the source Observable is subscribed to and starts behaving like
* described in the previous paragraph. If `subscriptionDelay` is not provided,
* `delayWhen` will subscribe to the source Observable as soon as the output
* Observable is subscribed.
*
* @example <caption>Delay each click by a random amount of time, between 0 and 5 seconds</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var delayedClicks = clicks.delayWhen(event =>
* Rx.Observable.interval(Math.random() * 5000)
* );
* delayedClicks.subscribe(x => console.log(x));
*
* @see {@link debounce}
* @see {@link delay}
*
* @param {function(value: T): Observable} delayDurationSelector A function that
* returns an Observable for each value emitted by the source Observable, which
* is then used to delay the emission of that item on the output Observable
* until the Observable returned from this function emits a value.
* @param {Observable} subscriptionDelay An Observable that triggers the
* subscription to the source Observable once it emits any value.
* @return {Observable} An Observable that delays the emissions of the source
* Observable by an amount of time specified by the Observable returned by
* `delayDurationSelector`.
* @method delayWhen
* @owner Observable
*/
Expand Down

0 comments on commit 95e96d1

Please sign in to comment.