11import { Operator } from '../Operator' ;
22import { Observable } from '../Observable' ;
33import { Subscriber } from '../Subscriber' ;
4- import { OperatorFunction , MonoTypeOperatorFunction } from '../types' ;
4+ import { OperatorFunction , TeardownLogic } from '../types' ;
55
66/* tslint:disable:max-line-length */
7- export function scan < T , R > ( accumulator : ( acc : R , value : T , index : number ) => R , seed : R ) : OperatorFunction < T , R > ;
8- export function scan < T > ( accumulator : ( acc : T , value : T , index : number ) => T , seed ?: T ) : MonoTypeOperatorFunction < T > ;
9- export function scan < T , R > ( accumulator : ( acc : R , value : T , index : number ) => R ) : OperatorFunction < T , R > ;
7+ export function scan < V , A = V > ( accumulator : ( acc : A | V , value : V , index : number ) => A ) : OperatorFunction < V , V | A > ;
8+ export function scan < V , A > ( accumulator : ( acc : A , value : V , index : number ) => A , seed : A ) : OperatorFunction < V , A > ;
9+ export function scan < V , A , S > ( accumulator : ( acc : A | S , value : V , index : number ) => A , seed : S ) : OperatorFunction < V , A > ;
1010/* tslint:enable:max-line-length */
1111
1212/**
@@ -45,14 +45,14 @@ export function scan<T, R>(accumulator: (acc: R, value: T, index: number) => R):
4545 * @see {@link mergeScan }
4646 * @see {@link reduce }
4747 *
48- * @param {function(acc: R , value: T , index: number): R } accumulator
48+ * @param {function(acc: A , value: V , index: number): A } accumulator
4949 * The accumulator function called on each source value.
50- * @param {T|R } [seed] The initial accumulation value.
51- * @return {Observable<R > } An observable of the accumulated values.
50+ * @param {V|A } [seed] The initial accumulation value.
51+ * @return {Observable<A > } An observable of the accumulated values.
5252 * @method scan
5353 * @owner Observable
5454 */
55- export function scan < T , R > ( accumulator : ( acc : R , value : T , index : number ) => R , seed ?: T | R ) : OperatorFunction < T , R > {
55+ export function scan < V , A , S > ( accumulator : ( acc : V | A | S , value : V , index : number ) => A , seed ?: S ) : OperatorFunction < V , V | A > {
5656 let hasSeed = false ;
5757 // providing a seed of `undefined` *should* be valid and trigger
5858 // hasSeed! so don't use `seed !== undefined` checks!
@@ -63,15 +63,15 @@ export function scan<T, R>(accumulator: (acc: R, value: T, index: number) => R,
6363 hasSeed = true ;
6464 }
6565
66- return function scanOperatorFunction ( source : Observable < T > ) : Observable < R > {
66+ return function scanOperatorFunction ( source : Observable < V > ) {
6767 return source . lift ( new ScanOperator ( accumulator , seed , hasSeed ) ) ;
6868 } ;
6969}
7070
71- class ScanOperator < T , R > implements Operator < T , R > {
72- constructor ( private accumulator : ( acc : R , value : T , index : number ) => R , private seed ?: T | R , private hasSeed : boolean = false ) { }
71+ class ScanOperator < V , A , S > implements Operator < V , A > {
72+ constructor ( private accumulator : ( acc : V | A | S , value : V , index : number ) => A , private seed ?: S , private hasSeed : boolean = false ) { }
7373
74- call ( subscriber : Subscriber < R > , source : any ) : any {
74+ call ( subscriber : Subscriber < A > , source : any ) : TeardownLogic {
7575 return source . subscribe ( new ScanSubscriber ( subscriber , this . accumulator , this . seed , this . hasSeed ) ) ;
7676 }
7777}
@@ -81,41 +81,31 @@ class ScanOperator<T, R> implements Operator<T, R> {
8181 * @ignore
8282 * @extends {Ignored }
8383 */
84- class ScanSubscriber < T , R > extends Subscriber < T > {
84+ class ScanSubscriber < V , A > extends Subscriber < V > {
8585 private index : number = 0 ;
8686
87- get seed ( ) : T | R {
88- return this . _seed ;
89- }
90-
91- set seed ( value : T | R ) {
92- this . hasSeed = true ;
93- this . _seed = value ;
94- }
95-
96- constructor ( destination : Subscriber < R > , private accumulator : ( acc : R , value : T , index : number ) => R , private _seed : T | R ,
97- private hasSeed : boolean ) {
87+ constructor ( destination : Subscriber < A > , private accumulator : ( acc : V | A , value : V , index : number ) => A , private _state : any ,
88+ private _hasState : boolean ) {
9889 super ( destination ) ;
9990 }
10091
101- protected _next ( value : T ) : void {
102- if ( ! this . hasSeed ) {
103- this . seed = value ;
104- this . destination . next ( value ) ;
92+ protected _next ( value : V ) : void {
93+ const { destination } = this ;
94+ if ( ! this . _hasState ) {
95+ this . _state = value ;
96+ this . _hasState = true ;
97+ destination . next ( value ) ;
10598 } else {
106- return this . _tryNext ( value ) ;
107- }
108- }
109-
110- private _tryNext ( value : T ) : void {
111- const index = this . index ++ ;
112- let result : any ;
113- try {
114- result = this . accumulator ( < R > this . seed , value , index ) ;
115- } catch ( err ) {
116- this . destination . error ( err ) ;
99+ const index = this . index ++ ;
100+ let result : A ;
101+ try {
102+ result = this . accumulator ( this . _state , value , index ) ;
103+ } catch ( err ) {
104+ destination . error ( err ) ;
105+ return ;
106+ }
107+ this . _state = result ;
108+ destination . next ( result ) ;
117109 }
118- this . seed = result ;
119- this . destination . next ( result ) ;
120110 }
121111}
0 commit comments