@@ -19,6 +19,7 @@ declare let define: any;
19
19
}
20
20
} ( typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global ,
21
21
( Rx : any ) => {
22
+ 'use strict' ;
22
23
Zone . __load_patch ( 'rxjs' , ( global : any , Zone : ZoneType , api : _ZonePrivate ) => {
23
24
const subscribeSource = 'rxjs.subscribe' ;
24
25
const nextSource = 'rxjs.Subscriber.next' ;
@@ -33,6 +34,14 @@ declare let define: any;
33
34
Rx . Observable = function ( ) {
34
35
Observable . apply ( this , arguments ) ;
35
36
this . _zone = Zone . current ;
37
+
38
+ // patch inner function this._subscribe to check
39
+ // SubscriptionZone is same with ConstuctorZone or not
40
+ if ( this . _subscribe && typeof this . _subscribe === 'function' && ! this . _originalSubscribe ) {
41
+ this . _originalSubscribe = this . _subscribe ;
42
+ this . _subscribe = _patchedSubscribe ;
43
+ }
44
+
36
45
return this ;
37
46
} ;
38
47
@@ -42,6 +51,39 @@ declare let define: any;
42
51
const lift = Observable . prototype . lift ;
43
52
const create = Observable . create ;
44
53
54
+ const _patchedSubscribe = function ( ) {
55
+ const currentZone = Zone . current ;
56
+ const _zone = this . _zone ;
57
+
58
+ const args = Array . prototype . slice . call ( arguments ) ;
59
+ const subscriber = args . length > 0 ? args [ 0 ] : undefined ;
60
+ // also keep currentZone in Subscriber
61
+ // for later Subscriber.next/error/complete method
62
+ if ( subscriber && ! subscriber . _zone ) {
63
+ subscriber . _zone = currentZone ;
64
+ }
65
+ // _subscribe should run in ConstructorZone
66
+ // but for performance concern, we should check
67
+ // whether ConsturctorZone === Zone.current here
68
+ const tearDownLogic = _zone !== Zone . current ?
69
+ _zone . run ( this . _originalSubscribe , this , args ) :
70
+ this . _originalSubscribe . apply ( this , args ) ;
71
+ if ( tearDownLogic && typeof tearDownLogic === 'function' ) {
72
+ const patchedTearDownLogic = function ( ) {
73
+ // tearDownLogic should also run in ConstructorZone
74
+ // but for performance concern, we should check
75
+ // whether ConsturctorZone === Zone.current here
76
+ if ( _zone && _zone !== Zone . current ) {
77
+ return _zone . run ( tearDownLogic , this , arguments ) ;
78
+ } else {
79
+ return tearDownLogic . apply ( this , arguments ) ;
80
+ }
81
+ } ;
82
+ return patchedTearDownLogic ;
83
+ }
84
+ return tearDownLogic ;
85
+ } ;
86
+
45
87
// patch Observable.prototype.subscribe
46
88
// if SubscripitionZone is different with ConstructorZone
47
89
// we should run _subscribe in ConstructorZone and
@@ -51,43 +93,6 @@ declare let define: any;
51
93
const _zone = this . _zone ;
52
94
const currentZone = Zone . current ;
53
95
54
- // patch inner function this._subscribe to check
55
- // SubscriptionZone is same with ConstuctorZone or not
56
- if ( this . _subscribe && typeof this . _subscribe === 'function' ) {
57
- this . _subscribe . _zone = this . _zone ;
58
- const _subscribe = this . _subscribe ;
59
- if ( _zone ) {
60
- this . _subscribe = function ( ) {
61
- const args = Array . prototype . slice . call ( arguments ) ;
62
- const subscriber = args . length > 0 ? args [ 0 ] : undefined ;
63
- // also keep currentZone in Subscriber
64
- // for later Subscriber.next/error/complete method
65
- if ( subscriber && ! subscriber . _zone ) {
66
- subscriber . _zone = currentZone ;
67
- }
68
- // _subscribe should run in ConstructorZone
69
- // but for performance concern, we should check
70
- // whether ConsturctorZone === Zone.current here
71
- const tearDownLogic = _zone !== Zone . current ? _zone . run ( _subscribe , this , args ) :
72
- _subscribe . apply ( this , args ) ;
73
- if ( tearDownLogic && typeof tearDownLogic === 'function' ) {
74
- const patchedTearDownLogic = function ( ) {
75
- // tearDownLogic should also run in ConstructorZone
76
- // but for performance concern, we should check
77
- // whether ConsturctorZone === Zone.current here
78
- if ( _zone && _zone !== Zone . current ) {
79
- return _zone . run ( tearDownLogic , this , arguments ) ;
80
- } else {
81
- return tearDownLogic . apply ( this , arguments ) ;
82
- }
83
- } ;
84
- return patchedTearDownLogic ;
85
- }
86
- return tearDownLogic ;
87
- } ;
88
- }
89
- }
90
-
91
96
// if operator is involved, we should also
92
97
// patch the call method to save the Subscription zone
93
98
if ( this . operator && _zone && _zone !== currentZone ) {
@@ -102,12 +107,6 @@ declare let define: any;
102
107
} ;
103
108
}
104
109
const result = subscribe . apply ( this , arguments ) ;
105
- // clean up _subscribe._zone to prevent
106
- // the same _subscribe being used in multiple
107
- // Observable instances.
108
- if ( this . _subscribe ) {
109
- this . _subscribe . _zone = undefined ;
110
- }
111
110
// the result is the subscriber sink,
112
111
// we save the current Zone here
113
112
result . _zone = currentZone ;
@@ -118,13 +117,27 @@ declare let define: any;
118
117
Observable . prototype . lift = function ( ) {
119
118
const observable = lift . apply ( this , arguments ) ;
120
119
observable . _zone = Zone . current ;
120
+ // patch inner function this._subscribe to check
121
+ // SubscriptionZone is same with ConstuctorZone or not
122
+ if ( this . _subscribe && typeof this . _subscribe === 'function' && ! this . _originalSubscribe ) {
123
+ this . _originalSubscribe = this . _subscribe ;
124
+ this . _subscribe = _patchedSubscribe ;
125
+ }
126
+
121
127
return observable ;
122
128
} ;
123
129
124
130
// patch create method to save ConstructorZone of Observable
125
131
Rx . Observable . create = function ( ) {
126
132
const observable = create . apply ( this , arguments ) ;
127
133
observable . _zone = Zone . current ;
134
+ // patch inner function this._subscribe to check
135
+ // SubscriptionZone is same with ConstuctorZone or not
136
+ if ( this . _subscribe && typeof this . _subscribe === 'function' && ! this . _originalSubscribe ) {
137
+ this . _originalSubscribe = this . _subscribe ;
138
+ this . _subscribe = _patchedSubscribe ;
139
+ }
140
+
128
141
return observable ;
129
142
} ;
130
143
0 commit comments