@@ -5,27 +5,11 @@ import Subscriber from '../Subscriber';
55
66import  ArrayObservable  from  '../observables/ArrayObservable' ; 
77import  EmptyObservable  from  '../observables/EmptyObservable' ; 
8- import  { ZipSubscriber ,  ZipInnerSubscriber }  from  './zip-support' ; 
98
109import  tryCatch  from  '../util/tryCatch' ; 
1110import  { errorObject }  from  '../util/errorObject' ; 
12- 
13- export  function  combineLatest < T ,  R > ( ...observables : ( Observable < any >  |  ( ( ...values : Array < any > )  =>  R ) ) [ ] ) : Observable < R >  { 
14-   const  project  =  < ( ( ...ys : Array < any > )  =>  R ) >  observables [ observables . length  -  1 ] ; 
15-   if  ( typeof  project  ===  "function" )  { 
16-     observables . pop ( ) ; 
17-   } 
18-   return  new  ArrayObservable ( observables ) . lift ( new  CombineLatestOperator ( project ) ) ; 
19- } 
20- 
21- export  function  combineLatestProto < R > ( ...observables : ( Observable < any > | ( ( ...values : any [ ] )  =>  R ) ) [ ] ) : Observable < R >  { 
22-   const  project  =  < ( ( ...ys : Array < any > )  =>  R ) >  observables [ observables . length  -  1 ] ; 
23-   if  ( typeof  project  ===  "function" )  { 
24-     observables . pop ( ) ; 
25-   } 
26-   observables . unshift ( this ) ; 
27-   return  new  ArrayObservable ( observables ) . lift ( new  CombineLatestOperator ( project ) ) ; 
28- } 
11+ import  OuterSubscriber  from  '../OuterSubscriber' ; 
12+ import  subscribeToResult  from  '../util/subscribeToResult' ; 
2913
3014export  class  CombineLatestOperator < T ,  R >  implements  Operator < T ,  R >  { 
3115
@@ -40,51 +24,68 @@ export class CombineLatestOperator<T, R> implements Operator<T, R> {
4024  } 
4125} 
4226
43- export  class  CombineLatestSubscriber < T ,  R >  extends  ZipSubscriber < T ,  R >  { 
44- 
45-   project : ( ...values : Array < any > )  =>  R ; 
46-   limit : number  =  0 ; 
47- 
48-   constructor ( destination : Subscriber < R > ,  project ?: ( ...values : Array < any > )  =>  R )  { 
49-     super ( destination ,  project ,  [ ] ) ; 
27+ export  class  CombineLatestSubscriber < T ,  R >  extends  OuterSubscriber < T ,  R >  { 
28+   private  active : number  =  0 ; 
29+   private  values : any [ ]  =  [ ] ; 
30+   private  observables : any [ ]  =  [ ] ; 
31+   private  toRespond : number [ ]  =  [ ] ; 
32+   
33+   constructor ( destination : Subscriber < R > ,  private  project ?: ( ...values : Array < any > )  =>  R )  { 
34+     super ( destination ) ; 
5035  } 
5136
52-   _subscribeInner ( observable ,  values ,  index ,  total )  { 
53-     return  observable . _subscribe ( new  CombineLatestInnerSubscriber ( this . destination ,  this ,  values ,  index ,  total ) ) ; 
37+   _next ( observable : any )  { 
38+     const  toRespond  =  this . toRespond ; 
39+     toRespond . push ( toRespond . length ) ; 
40+     this . observables . push ( observable ) ; 
5441  } 
55- 
56-   _innerComplete ( innerSubscriber )  { 
57-     if ( ( this . active  -=  1 )  ===  0 )  { 
42+   
43+   _complete ( )  { 
44+     const  observables  =  this . observables ; 
45+     const  len  =  observables . length ; 
46+     if ( len  ===  0 )  { 
5847      this . destination . complete ( ) ; 
48+     }  else  { 
49+       this . active  =  len ; 
50+       for ( let  i  =  0 ;  i  <  len ;  i ++ )  { 
51+         let  observable  =  observables [ i ] ; 
52+         this . add ( subscribeToResult ( this ,  observable ,  observable ,  i ) ) ; 
53+       } 
5954    } 
6055  } 
61- } 
62- 
63- export  class  CombineLatestInnerSubscriber < T ,  R >  extends  ZipInnerSubscriber < T ,  R >  { 
6456
65-   constructor ( destination : Observer < T > ,  parent : ZipSubscriber < T ,  R > ,  values : any ,  index  : number ,  total  : number )  { 
66-     super ( destination ,  parent ,  values ,  index ,  total ) ; 
57+   notifyComplete ( innerSubscriber )  { 
58+     if ( ( this . active  -=  1 )  ===  0 )  { 
59+       this . destination . complete ( ) ; 
60+     } 
6761  } 
68- 
69-   _next ( x )  { 
70- 
71-     const  index  =  this . index ; 
72-     const  total  =  this . total ; 
73-     const  parent  =  this . parent ; 
62+   
63+   notifyNext ( value : R ,  observable : any ,  innerIndex : number ,  outerIndex : number )  { 
7464    const  values  =  this . values ; 
75-     const  valueBox  =  values [ index ] ; 
76-     let  limit ; 
77- 
78-     if ( valueBox )  { 
79-       valueBox [ 0 ]  =  x ; 
80-       limit  =  parent . limit ; 
81-     }  else  { 
82-       limit  =  parent . limit  +=  1 ; 
83-       values [ index ]  =  [ x ] ; 
65+     values [ outerIndex ]  =  value ; 
66+     const  toRespond  =  this . toRespond ; 
67+     
68+     if ( toRespond . length  >  0 )  { 
69+       const  found  =  toRespond . indexOf ( outerIndex ) ; 
70+       if ( found  !==  - 1 )  { 
71+         toRespond . splice ( found ,  1 ) ; 
72+       } 
8473    } 
85- 
86-     if ( limit  >=  total )  { 
87-       this . _projectNext ( values ,  parent . project ) ; 
74+     
75+     if ( toRespond . length  ===  0 )  { 
76+       const  project  =  this . project ; 
77+       const  destination  =  this . destination ; 
78+       
79+       if ( project )  { 
80+         let  result  =  tryCatch ( project ) . apply ( this ,  values ) ; 
81+         if ( result  ===  errorObject )  { 
82+           destination . error ( errorObject . e ) ; 
83+         }  else  { 
84+           destination . next ( result ) ; 
85+         } 
86+       }  else  { 
87+         destination . next ( values ) ; 
88+       } 
8889    } 
8990  } 
9091} 
0 commit comments