@@ -56,82 +56,63 @@ export class ForkJoinObservable<T> extends Observable<T> {
56
56
}
57
57
}
58
58
59
- interface ForkJoinContext {
60
- completed : number ;
61
- total : number ;
62
- values : Array < any > ;
63
- haveValues : Array < boolean > ;
64
- selector : Function ;
65
- }
66
-
67
59
class ForkJoinSubscriber < T > extends OuterSubscriber < T , T > {
68
- private context : ForkJoinContext = null ;
60
+ private completed = 0 ;
61
+ private total : number ;
62
+ private values : any [ ] ;
63
+ private haveValues = 0 ;
69
64
70
65
constructor ( destination : Subscriber < T > ,
71
66
private sources : Array < SubscribableOrPromise < any > > ,
72
- resultSelector ?: ( ...values : Array < any > ) => T ) {
67
+ private resultSelector ?: ( ...values : Array < any > ) => T ) {
73
68
super ( destination ) ;
74
69
75
70
const len = sources . length ;
76
- this . context = { completed : 0 ,
77
- total : len ,
78
- values : new Array ( len ) ,
79
- haveValues : new Array ( len ) ,
80
- selector : resultSelector } ;
71
+ this . total = len ;
72
+ this . values = new Array ( len ) ;
73
+
74
+ for ( let i = 0 ; i < len ; i ++ ) {
75
+ const source = sources [ i ] ;
76
+ const innerSubscription = subscribeToResult ( this , source , null , i ) ;
81
77
82
- this . tryForkJoin ( ) ;
78
+ if ( innerSubscription ) {
79
+ ( < any > innerSubscription ) . outerIndex = i ;
80
+ this . add ( innerSubscription ) ;
81
+ }
82
+ }
83
83
}
84
84
85
85
notifyNext ( outerValue : any , innerValue : T ,
86
86
outerIndex : number , innerIndex : number ,
87
87
innerSub : InnerSubscriber < T , T > ) : void {
88
- const context = this . context ;
89
-
90
- context . values [ outerIndex ] = innerValue ;
91
- context . haveValues [ outerIndex ] = true ;
88
+ this . values [ outerIndex ] = innerValue ;
89
+ if ( ! ( < any > innerSub ) . _hasValue ) {
90
+ ( < any > innerSub ) . _hasValue = true ;
91
+ this . haveValues ++ ;
92
+ }
92
93
}
93
94
94
95
notifyComplete ( innerSub : InnerSubscriber < T , T > ) : void {
95
- const outerIndex = ( < any > innerSub ) . outerIndex ;
96
- this . tryComplete ( outerIndex ) ;
97
- }
98
-
99
- private tryComplete ( index : number ) : void {
100
96
const destination = this . destination ;
101
- const context = this . context ;
97
+ const { haveValues, resultSelector, values } = this ;
98
+ const len = values . length ;
102
99
103
- context . completed ++ ;
104
-
105
- if ( ! context . haveValues [ index ] ) {
100
+ if ( ! ( < any > innerSub ) . _hasValue ) {
106
101
destination . complete ( ) ;
102
+ return ;
107
103
}
108
104
109
- const values = context . values ;
110
- if ( context . completed !== values . length ) {
105
+ this . completed ++ ;
106
+
107
+ if ( this . completed !== len ) {
111
108
return ;
112
109
}
113
110
114
- if ( context . haveValues . every ( x => x === true ) ) {
115
- const value = context . selector ? context . selector . apply ( this , values ) :
116
- values ;
111
+ if ( haveValues === len ) {
112
+ const value = resultSelector ? resultSelector . apply ( this , values ) : values ;
117
113
destination . next ( value ) ;
118
114
}
119
115
120
116
destination . complete ( ) ;
121
117
}
122
-
123
- private tryForkJoin ( ) : void {
124
- const sources = this . sources ;
125
- const len = sources . length ;
126
-
127
- for ( let i = 0 ; i < len ; i ++ ) {
128
- const source = sources [ i ] ;
129
- const innerSubscription = subscribeToResult ( this , source , null , i ) ;
130
-
131
- if ( innerSubscription ) {
132
- ( < any > innerSubscription ) . outerIndex = i ;
133
- this . add ( innerSubscription ) ;
134
- }
135
- }
136
- }
137
118
}
0 commit comments