6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import 'rxjs/add/observable/bindCallback' ;
10
- import 'rxjs/add/observable/bindNodeCallback' ;
11
- import 'rxjs/add/observable/defer' ;
12
- import 'rxjs/add/observable/forkJoin' ;
13
- import 'rxjs/add/observable/fromEventPattern' ;
14
- import 'rxjs/add/operator/multicast' ;
9
+ import { Observable , Subscriber , Subscription } from 'rxjs' ;
15
10
16
- import { Observable } from 'rxjs/Observable' ;
17
- import { asap } from 'rxjs/scheduler/asap' ;
18
- import { Subscriber } from 'rxjs/Subscriber' ;
19
- import { Subscription } from 'rxjs/Subscription' ;
20
- import { rxSubscriber } from 'rxjs/symbol/rxSubscriber' ;
21
-
22
- ( Zone as any ) . __load_patch ( 'rxjs' , ( global : any , Zone : ZoneType ) => {
11
+ ( Zone as any ) . __load_patch ( 'rxjs' , ( global : any , Zone : ZoneType , api : _ZonePrivate ) => {
23
12
const symbol : ( symbolString : string ) => string = ( Zone as any ) . __symbol__ ;
24
13
const nextSource = 'rxjs.Subscriber.next' ;
25
14
const errorSource = 'rxjs.Subscriber.error' ;
26
15
const completeSource = 'rxjs.Subscriber.complete' ;
27
16
28
17
const ObjectDefineProperties = Object . defineProperties ;
29
18
30
- const empty = {
31
- closed : true ,
32
- next ( value : any ) : void { } ,
33
- error ( err : any ) : void {
34
- throw err ;
35
- } ,
36
- complete ( ) : void { }
37
- } ;
38
-
39
- function toSubscriber < T > (
40
- nextOrObserver ?: any , error ?: ( error : any ) => void , complete ?: ( ) => void ) : Subscriber < T > {
41
- if ( nextOrObserver ) {
42
- if ( nextOrObserver instanceof Subscriber ) {
43
- return ( < Subscriber < T > > nextOrObserver ) ;
44
- }
45
-
46
- if ( nextOrObserver [ rxSubscriber ] ) {
47
- return nextOrObserver [ rxSubscriber ] ( ) ;
48
- }
49
- }
50
-
51
- if ( ! nextOrObserver && ! error && ! complete ) {
52
- return new Subscriber ( empty ) ;
53
- }
54
-
55
- return new Subscriber ( nextOrObserver , error , complete ) ;
56
- }
57
-
58
19
const patchObservable = function ( ) {
59
20
const ObservablePrototype : any = Observable . prototype ;
60
- const symbolSubscribe = symbol ( 'subscribe' ) ;
61
21
const _symbolSubscribe = symbol ( '_subscribe' ) ;
62
22
const _subscribe = ObservablePrototype [ _symbolSubscribe ] = ObservablePrototype . _subscribe ;
63
- const subscribe = ObservablePrototype [ symbolSubscribe ] = ObservablePrototype . subscribe ;
64
23
65
24
ObjectDefineProperties ( Observable . prototype , {
66
25
_zone : { value : null , writable : true , configurable : true } ,
@@ -89,30 +48,58 @@ import {rxSubscriber} from 'rxjs/symbol/rxSubscriber';
89
48
} ,
90
49
set : function ( this : Observable < any > , subscribe : any ) {
91
50
( this as any ) . _zone = Zone . current ;
92
- ( this as any ) . _zoneSubscribe = subscribe ;
51
+ ( this as any ) . _zoneSubscribe = function ( ) {
52
+ if ( this . _zone && this . _zone !== Zone . current ) {
53
+ const tearDown = this . _zone . run ( subscribe , this , arguments ) ;
54
+ if ( tearDown && typeof tearDown === 'function' ) {
55
+ const zone = this . _zone ;
56
+ return function ( ) {
57
+ if ( zone !== Zone . current ) {
58
+ return zone . run ( tearDown , this , arguments ) ;
59
+ }
60
+ return tearDown . apply ( this , arguments ) ;
61
+ } ;
62
+ }
63
+ return tearDown ;
64
+ }
65
+ return subscribe . apply ( this , arguments ) ;
66
+ } ;
93
67
}
94
68
} ,
95
- subscribe : {
96
- writable : true ,
97
- configurable : true ,
98
- value : function ( this : Observable < any > , observerOrNext : any , error : any , complete : any ) {
99
- // Only grab a zone if we Zone exists and it is different from the current zone.
100
- const _zone = ( this as any ) . _zone ;
101
- if ( _zone && _zone !== Zone . current ) {
102
- // Current Zone is different from the intended zone.
103
- // Restore the zone before invoking the subscribe callback.
104
- return _zone . run ( subscribe , this , [ toSubscriber ( observerOrNext , error , complete ) ] ) ;
105
- }
106
- return subscribe . call ( this , observerOrNext , error , complete ) ;
69
+ subjectFactory : {
70
+ get : function ( ) {
71
+ return ( this as any ) . _zoneSubjectFactory ;
72
+ } ,
73
+ set : function ( factory : any ) {
74
+ const zone = this . _zone ;
75
+ this . _zoneSubjectFactory = function ( ) {
76
+ if ( zone && zone !== Zone . current ) {
77
+ return zone . run ( factory , this , arguments ) ;
78
+ }
79
+ return factory . apply ( this , arguments ) ;
80
+ } ;
107
81
}
108
82
}
109
83
} ) ;
110
84
} ;
111
85
86
+ api . patchMethod ( Observable . prototype , 'lift' , ( delegate : any ) => ( self : any , args : any [ ] ) => {
87
+ const observable : any = delegate . apply ( self , args ) ;
88
+ if ( observable . operator ) {
89
+ observable . operator . _zone = Zone . current ;
90
+ api . patchMethod (
91
+ observable . operator , 'call' ,
92
+ ( operatorDelegate : any ) => ( operatorSelf : any , operatorArgs : any [ ] ) => {
93
+ if ( operatorSelf . _zone && operatorSelf . _zone !== Zone . current ) {
94
+ return operatorSelf . _zone . run ( operatorDelegate , operatorSelf , operatorArgs ) ;
95
+ }
96
+ return operatorDelegate . apply ( operatorSelf , operatorArgs ) ;
97
+ } ) ;
98
+ }
99
+ return observable ;
100
+ } ) ;
101
+
112
102
const patchSubscription = function ( ) {
113
- const unsubscribeSymbol = symbol ( 'unsubscribe' ) ;
114
- const unsubscribe = ( Subscription . prototype as any ) [ unsubscribeSymbol ] =
115
- Subscription . prototype . unsubscribe ;
116
103
ObjectDefineProperties ( Subscription . prototype , {
117
104
_zone : { value : null , writable : true , configurable : true } ,
118
105
_zoneUnsubscribe : { value : null , writable : true , configurable : true } ,
@@ -126,22 +113,12 @@ import {rxSubscriber} from 'rxjs/symbol/rxSubscriber';
126
113
} ,
127
114
set : function ( this : Subscription , unsubscribe : any ) {
128
115
( this as any ) . _zone = Zone . current ;
129
- ( this as any ) . _zoneUnsubscribe = unsubscribe ;
130
- }
131
- } ,
132
- unsubscribe : {
133
- writable : true ,
134
- configurable : true ,
135
- value : function ( this : Subscription ) {
136
- // Only grab a zone if we Zone exists and it is different from the current zone.
137
- const _zone : Zone = ( this as any ) . _zone ;
138
- if ( _zone && _zone !== Zone . current ) {
139
- // Current Zone is different from the intended zone.
140
- // Restore the zone before invoking the subscribe callback.
141
- _zone . run ( unsubscribe , this ) ;
142
- } else {
143
- unsubscribe . apply ( this ) ;
144
- }
116
+ ( this as any ) . _zoneUnsubscribe = function ( ) {
117
+ if ( this . _zone && this . _zone !== Zone . current ) {
118
+ return this . _zone . run ( unsubscribe , this , arguments ) ;
119
+ }
120
+ return unsubscribe . apply ( this , arguments ) ;
121
+ } ;
145
122
}
146
123
}
147
124
} ) ;
@@ -205,158 +182,7 @@ import {rxSubscriber} from 'rxjs/symbol/rxSubscriber';
205
182
} ;
206
183
} ;
207
184
208
- const patchObservableInstance = function ( observable : any ) {
209
- observable . _zone = Zone . current ;
210
- } ;
211
-
212
- const patchObservableFactoryCreator = function ( obj : any , factoryName : string ) {
213
- const symbolFactory : string = symbol ( factoryName ) ;
214
- if ( obj [ symbolFactory ] ) {
215
- return ;
216
- }
217
- const factoryCreator : any = obj [ symbolFactory ] = obj [ factoryName ] ;
218
- if ( ! factoryCreator ) {
219
- return ;
220
- }
221
- obj [ factoryName ] = function ( ) {
222
- const factory : any = factoryCreator . apply ( this , arguments ) ;
223
- return function ( ) {
224
- const observable = factory . apply ( this , arguments ) ;
225
- patchObservableInstance ( observable ) ;
226
- return observable ;
227
- } ;
228
- } ;
229
- } ;
230
-
231
- const patchObservableFactory = function ( obj : any , factoryName : string ) {
232
- const symbolFactory : string = symbol ( factoryName ) ;
233
- if ( obj [ symbolFactory ] ) {
234
- return ;
235
- }
236
- const factory : any = obj [ symbolFactory ] = obj [ factoryName ] ;
237
- if ( ! factory ) {
238
- return ;
239
- }
240
- obj [ factoryName ] = function ( ) {
241
- const observable = factory . apply ( this , arguments ) ;
242
- patchObservableInstance ( observable ) ;
243
- return observable ;
244
- } ;
245
- } ;
246
-
247
- const patchObservableFactoryArgs = function ( obj : any , factoryName : string ) {
248
- const symbolFactory : string = symbol ( factoryName ) ;
249
- if ( obj [ symbolFactory ] ) {
250
- return ;
251
- }
252
- const factory : any = obj [ symbolFactory ] = obj [ factoryName ] ;
253
- if ( ! factory ) {
254
- return ;
255
- }
256
- obj [ factoryName ] = function ( ) {
257
- const initZone = Zone . current ;
258
- const args = Array . prototype . slice . call ( arguments ) ;
259
- for ( let i = 0 ; i < args . length ; i ++ ) {
260
- const arg = args [ i ] ;
261
- if ( typeof arg === 'function' ) {
262
- args [ i ] = function ( ) {
263
- const argArgs = Array . prototype . slice . call ( arguments ) ;
264
- const runningZone = Zone . current ;
265
- if ( initZone && runningZone && initZone !== runningZone ) {
266
- return initZone . run ( arg , this , argArgs ) ;
267
- } else {
268
- return arg . apply ( this , argArgs ) ;
269
- }
270
- } ;
271
- }
272
- }
273
-
274
- const observable = factory . apply ( this , args ) ;
275
- patchObservableInstance ( observable ) ;
276
- return observable ;
277
- } ;
278
- } ;
279
-
280
- const patchMulticast = function ( ) {
281
- const obj : any = Observable . prototype ;
282
- const factoryName : string = 'multicast' ;
283
- const symbolFactory : string = symbol ( factoryName ) ;
284
- if ( obj [ symbolFactory ] ) {
285
- return ;
286
- }
287
- const factory : any = obj [ symbolFactory ] = obj [ factoryName ] ;
288
- if ( ! factory ) {
289
- return ;
290
- }
291
- obj [ factoryName ] = function ( ) {
292
- const _zone : any = Zone . current ;
293
- const args = Array . prototype . slice . call ( arguments ) ;
294
- let subjectOrSubjectFactory : any = args . length > 0 ? args [ 0 ] : undefined ;
295
- if ( typeof subjectOrSubjectFactory !== 'function' ) {
296
- const originalFactory : any = subjectOrSubjectFactory ;
297
- subjectOrSubjectFactory = function ( ) {
298
- return originalFactory ;
299
- } ;
300
- }
301
- args [ 0 ] = function ( ) {
302
- let subject : any ;
303
- if ( _zone && _zone !== Zone . current ) {
304
- subject = _zone . run ( subjectOrSubjectFactory , this , arguments ) ;
305
- } else {
306
- subject = subjectOrSubjectFactory . apply ( this , arguments ) ;
307
- }
308
- if ( subject && _zone ) {
309
- subject . _zone = _zone ;
310
- }
311
- return subject ;
312
- } ;
313
- const observable = factory . apply ( this , args ) ;
314
- patchObservableInstance ( observable ) ;
315
- return observable ;
316
- } ;
317
- } ;
318
-
319
- const patchImmediate = function ( asap : any ) {
320
- if ( ! asap ) {
321
- return ;
322
- }
323
-
324
- const scheduleSymbol = symbol ( 'scheduleSymbol' ) ;
325
- const zoneSymbol = symbol ( 'zone' ) ;
326
- if ( asap [ scheduleSymbol ] ) {
327
- return ;
328
- }
329
-
330
- const schedule = asap [ scheduleSymbol ] = asap . schedule ;
331
- asap . schedule = function ( ) {
332
- const args = Array . prototype . slice . call ( arguments ) ;
333
- const work = args . length > 0 ? args [ 0 ] : undefined ;
334
- const delay = args . length > 1 ? args [ 1 ] : 0 ;
335
- const state = ( args . length > 2 ? args [ 2 ] : undefined ) || { } ;
336
- state [ zoneSymbol ] = Zone . current ;
337
-
338
- const patchedWork = function ( ) {
339
- const workArgs = Array . prototype . slice . call ( arguments ) ;
340
- const action = workArgs . length > 0 ? workArgs [ 0 ] : undefined ;
341
- const scheduleZone = action && action [ zoneSymbol ] ;
342
- if ( scheduleZone && scheduleZone !== Zone . current ) {
343
- return scheduleZone . runGuarded ( work , this , arguments ) ;
344
- } else {
345
- return work . apply ( this , arguments ) ;
346
- }
347
- } ;
348
- return schedule . call ( this , patchedWork , delay , state ) ;
349
- } ;
350
- } ;
351
-
352
185
patchObservable ( ) ;
353
186
patchSubscription ( ) ;
354
187
patchSubscriber ( ) ;
355
- patchObservableFactoryCreator ( Observable , 'bindCallback' ) ;
356
- patchObservableFactoryCreator ( Observable , 'bindNodeCallback' ) ;
357
- patchObservableFactory ( Observable , 'defer' ) ;
358
- patchObservableFactory ( Observable , 'forkJoin' ) ;
359
- patchObservableFactoryArgs ( Observable , 'fromEventPattern' ) ;
360
- patchMulticast ( ) ;
361
- patchImmediate ( asap ) ;
362
188
} ) ;
0 commit comments