1
1
import { Observable , ObservableInput } from '../Observable' ;
2
- import { ArrayObservable } from '../observable/ArrayObservable' ;
3
- import { isArray } from '../util/isArray' ;
4
- import { Operator } from '../Operator' ;
5
- import { Subscriber } from '../Subscriber' ;
6
- import { OuterSubscriber } from '../OuterSubscriber' ;
7
- import { InnerSubscriber } from '../InnerSubscriber' ;
8
- import { subscribeToResult } from '../util/subscribeToResult' ;
9
- const none = { } ;
2
+ import { combineLatest as higherOrder } from '../operators' ;
10
3
11
4
/* tslint:disable:max-line-length */
12
5
export function combineLatest < T , R > ( this : Observable < T > , project : ( v1 : T ) => R ) : Observable < R > ;
@@ -71,99 +64,5 @@ export function combineLatest<T, TOther, R>(this: Observable<T>, array: Observab
71
64
export function combineLatest < T , R > ( this : Observable < T > , ...observables : Array < ObservableInput < any > |
72
65
Array < ObservableInput < any > > |
73
66
( ( ...values : Array < any > ) => R ) > ) : Observable < R > {
74
- let project : ( ...values : Array < any > ) => R = null ;
75
- if ( typeof observables [ observables . length - 1 ] === 'function' ) {
76
- project = < ( ...values : Array < any > ) => R > observables . pop ( ) ;
77
- }
78
-
79
- // if the first and only other argument besides the resultSelector is an array
80
- // assume it's been called with `combineLatest([obs1, obs2, obs3], project)`
81
- if ( observables . length === 1 && isArray ( observables [ 0 ] ) ) {
82
- observables = ( < any > observables [ 0 ] ) . slice ( ) ;
83
- }
84
-
85
- observables . unshift ( this ) ;
86
-
87
- return this . lift . call ( new ArrayObservable ( observables ) , new CombineLatestOperator ( project ) ) ;
88
- }
89
-
90
- export class CombineLatestOperator < T , R > implements Operator < T , R > {
91
- constructor ( private project ?: ( ...values : Array < any > ) => R ) {
92
- }
93
-
94
- call ( subscriber : Subscriber < R > , source : any ) : any {
95
- return source . subscribe ( new CombineLatestSubscriber ( subscriber , this . project ) ) ;
96
- }
97
- }
98
-
99
- /**
100
- * We need this JSDoc comment for affecting ESDoc.
101
- * @ignore
102
- * @extends {Ignored }
103
- */
104
- export class CombineLatestSubscriber < T , R > extends OuterSubscriber < T , R > {
105
- private active : number = 0 ;
106
- private values : any [ ] = [ ] ;
107
- private observables : any [ ] = [ ] ;
108
- private toRespond : number ;
109
-
110
- constructor ( destination : Subscriber < R > , private project ?: ( ...values : Array < any > ) => R ) {
111
- super ( destination ) ;
112
- }
113
-
114
- protected _next ( observable : any ) {
115
- this . values . push ( none ) ;
116
- this . observables . push ( observable ) ;
117
- }
118
-
119
- protected _complete ( ) {
120
- const observables = this . observables ;
121
- const len = observables . length ;
122
- if ( len === 0 ) {
123
- this . destination . complete ( ) ;
124
- } else {
125
- this . active = len ;
126
- this . toRespond = len ;
127
- for ( let i = 0 ; i < len ; i ++ ) {
128
- const observable = observables [ i ] ;
129
- this . add ( subscribeToResult ( this , observable , observable , i ) ) ;
130
- }
131
- }
132
- }
133
-
134
- notifyComplete ( unused : Subscriber < R > ) : void {
135
- if ( ( this . active -= 1 ) === 0 ) {
136
- this . destination . complete ( ) ;
137
- }
138
- }
139
-
140
- notifyNext ( outerValue : T , innerValue : R ,
141
- outerIndex : number , innerIndex : number ,
142
- innerSub : InnerSubscriber < T , R > ) : void {
143
- const values = this . values ;
144
- const oldVal = values [ outerIndex ] ;
145
- const toRespond = ! this . toRespond
146
- ? 0
147
- : oldVal === none ? -- this . toRespond : this . toRespond ;
148
- values [ outerIndex ] = innerValue ;
149
-
150
- if ( toRespond === 0 ) {
151
- if ( this . project ) {
152
- this . _tryProject ( values ) ;
153
- } else {
154
- this . destination . next ( values . slice ( ) ) ;
155
- }
156
- }
157
- }
158
-
159
- private _tryProject ( values : any [ ] ) {
160
- let result : any ;
161
- try {
162
- result = this . project . apply ( this , values ) ;
163
- } catch ( err ) {
164
- this . destination . error ( err ) ;
165
- return ;
166
- }
167
- this . destination . next ( result ) ;
168
- }
169
- }
67
+ return higherOrder ( ...observables ) ( this ) ;
68
+ }
0 commit comments