@@ -44,7 +44,7 @@ export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T
44
44
* @see {@link mergeScan }
45
45
* @see {@link scan }
46
46
*
47
- * @param {function(acc: R, value: T): R } accumulator The accumulator function
47
+ * @param {function(acc: R, value: T, index: number ): R } accumulator The accumulator function
48
48
* called on each source value.
49
49
* @param {R } [seed] The initial accumulation value.
50
50
* @return {Observable<R> } An observable of the accumulated values.
@@ -53,7 +53,7 @@ export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T
53
53
* @method reduce
54
54
* @owner Observable
55
55
*/
56
- export function reduce < T , R > ( this : Observable < T > , accumulator : ( acc : R , value : T ) => R , seed ?: R ) : Observable < R > {
56
+ export function reduce < T , R > ( this : Observable < T > , accumulator : ( acc : R , value : T , index ?: number ) => R , seed ?: R ) : Observable < R > {
57
57
let hasSeed = false ;
58
58
// providing a seed of `undefined` *should* be valid and trigger
59
59
// hasSeed! so don't use `seed !== undefined` checks!
@@ -68,7 +68,7 @@ export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T
68
68
}
69
69
70
70
export class ReduceOperator < T , R > implements Operator < T , R > {
71
- constructor ( private accumulator : ( acc : R , value : T ) => R , private seed ?: R , private hasSeed : boolean = false ) { }
71
+ constructor ( private accumulator : ( acc : R , value : T , index ?: number ) => R , private seed ?: R , private hasSeed : boolean = false ) { }
72
72
73
73
call ( subscriber : Subscriber < R > , source : any ) : any {
74
74
return source . subscribe ( new ReduceSubscriber ( subscriber , this . accumulator , this . seed , this . hasSeed ) ) ;
@@ -81,15 +81,20 @@ export class ReduceOperator<T, R> implements Operator<T, R> {
81
81
* @extends {Ignored }
82
82
*/
83
83
export class ReduceSubscriber < T , R > extends Subscriber < T > {
84
- acc : T | R ;
85
- hasValue : boolean = false ;
84
+ private index : number = 0 ;
85
+ private acc : T | R ;
86
+ private hasValue : boolean = false ;
86
87
87
88
constructor ( destination : Subscriber < R > ,
88
- private accumulator : ( acc : R , value : T ) => R ,
89
+ private accumulator : ( acc : R , value : T , index ?: number ) => R ,
89
90
seed : R ,
90
91
private hasSeed : boolean ) {
91
92
super ( destination ) ;
92
93
this . acc = seed ;
94
+
95
+ if ( ! this . hasSeed ) {
96
+ this . index ++ ;
97
+ }
93
98
}
94
99
95
100
protected _next ( value : T ) {
@@ -104,7 +109,7 @@ export class ReduceSubscriber<T, R> extends Subscriber<T> {
104
109
private _tryReduce ( value : T ) {
105
110
let result : any ;
106
111
try {
107
- result = this . accumulator ( < R > this . acc , value ) ;
112
+ result = this . accumulator ( < R > this . acc , value , this . index ++ ) ;
108
113
} catch ( err ) {
109
114
this . destination . error ( err ) ;
110
115
return ;
0 commit comments