From 688ea189a13bf0272588a15c8916490f289d8e73 Mon Sep 17 00:00:00 2001 From: Martin Sikora Date: Thu, 23 Feb 2017 16:39:08 +0100 Subject: [PATCH] feat(delayWhen): add index to the selector function #2388 --- spec/operators/delayWhen-spec.ts | 19 +++++++++++++++++++ src/operator/delayWhen.ts | 12 +++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/spec/operators/delayWhen-spec.ts b/spec/operators/delayWhen-spec.ts index 850b98af84b..af08318d22b 100644 --- a/spec/operators/delayWhen-spec.ts +++ b/spec/operators/delayWhen-spec.ts @@ -1,3 +1,4 @@ +import {expect} from 'chai'; import * as Rx from '../../dist/cjs/Rx'; import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports @@ -215,4 +216,22 @@ describe('Observable.prototype.delayWhen', () => { expectSubscriptions(selector.subscriptions).toBe([]); expectSubscriptions(subDelay.subscriptions).toBe(subDelaySub); }); + + it('should call predicate with indices starting at 0', () => { + const e1 = hot('--a--b--c--|'); + const expected = '--a--b--c--|'; + const selector = cold('(x|)'); + + let indices = []; + const predicate = (value, index) => { + indices.push(index); + return selector; + }; + + const result = e1.delayWhen(predicate); + + expectObservable(result.do(null, null, () => { + expect(indices).to.deep.equal([0, 1, 2]); + })).toBe(expected); + }); }); \ No newline at end of file diff --git a/src/operator/delayWhen.ts b/src/operator/delayWhen.ts index af43e343d6a..24b8b10ba77 100644 --- a/src/operator/delayWhen.ts +++ b/src/operator/delayWhen.ts @@ -40,7 +40,7 @@ import { subscribeToResult } from '../util/subscribeToResult'; * @see {@link debounce} * @see {@link delay} * - * @param {function(value: T): Observable} delayDurationSelector A function that + * @param {function(value: T, index: number): 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. @@ -52,7 +52,7 @@ import { subscribeToResult } from '../util/subscribeToResult'; * @method delayWhen * @owner Observable */ -export function delayWhen(this: Observable, delayDurationSelector: (value: T) => Observable, +export function delayWhen(this: Observable, delayDurationSelector: (value: T, index: number) => Observable, subscriptionDelay?: Observable): Observable { if (subscriptionDelay) { return new SubscriptionDelayObservable(this, subscriptionDelay) @@ -62,7 +62,7 @@ export function delayWhen(this: Observable, delayDurationSelector: (value: } class DelayWhenOperator implements Operator { - constructor(private delayDurationSelector: (value: T) => Observable) { + constructor(private delayDurationSelector: (value: T, index: number) => Observable) { } call(subscriber: Subscriber, source: any): TeardownLogic { @@ -79,9 +79,10 @@ class DelayWhenSubscriber extends OuterSubscriber { private completed: boolean = false; private delayNotifierSubscriptions: Array = []; private values: Array = []; + private index: number = 0; constructor(destination: Subscriber, - private delayDurationSelector: (value: T) => Observable) { + private delayDurationSelector: (value: T, index: number) => Observable) { super(destination); } @@ -106,8 +107,9 @@ class DelayWhenSubscriber extends OuterSubscriber { } protected _next(value: T): void { + const index = this.index++; try { - const delayNotifier = this.delayDurationSelector(value); + const delayNotifier = this.delayDurationSelector(value, index); if (delayNotifier) { this.tryDelay(delayNotifier, value); }