rxjs.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import {Observable, Subscriber, Subscription} from 'rxjs';
  9. (Zone as any).__load_patch('rxjs', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  10. const symbol: (symbolString: string) => string = (Zone as any).__symbol__;
  11. const nextSource = 'rxjs.Subscriber.next';
  12. const errorSource = 'rxjs.Subscriber.error';
  13. const completeSource = 'rxjs.Subscriber.complete';
  14. const ObjectDefineProperties = Object.defineProperties;
  15. const patchObservable = function() {
  16. const ObservablePrototype: any = Observable.prototype;
  17. const _symbolSubscribe = symbol('_subscribe');
  18. const _subscribe = ObservablePrototype[_symbolSubscribe] = ObservablePrototype._subscribe;
  19. ObjectDefineProperties(Observable.prototype, {
  20. _zone: {value: null, writable: true, configurable: true},
  21. _zoneSource: {value: null, writable: true, configurable: true},
  22. _zoneSubscribe: {value: null, writable: true, configurable: true},
  23. source: {
  24. configurable: true,
  25. get: function(this: Observable<any>) {
  26. return (this as any)._zoneSource;
  27. },
  28. set: function(this: Observable<any>, source: any) {
  29. (this as any)._zone = Zone.current;
  30. (this as any)._zoneSource = source;
  31. }
  32. },
  33. _subscribe: {
  34. configurable: true,
  35. get: function(this: Observable<any>) {
  36. if ((this as any)._zoneSubscribe) {
  37. return (this as any)._zoneSubscribe;
  38. } else if (this.constructor === Observable) {
  39. return _subscribe;
  40. }
  41. const proto = Object.getPrototypeOf(this);
  42. return proto && proto._subscribe;
  43. },
  44. set: function(this: Observable<any>, subscribe: any) {
  45. (this as any)._zone = Zone.current;
  46. (this as any)._zoneSubscribe = function() {
  47. if (this._zone && this._zone !== Zone.current) {
  48. const tearDown = this._zone.run(subscribe, this, arguments);
  49. if (tearDown && typeof tearDown === 'function') {
  50. const zone = this._zone;
  51. return function() {
  52. if (zone !== Zone.current) {
  53. return zone.run(tearDown, this, arguments);
  54. }
  55. return tearDown.apply(this, arguments);
  56. };
  57. }
  58. return tearDown;
  59. }
  60. return subscribe.apply(this, arguments);
  61. };
  62. }
  63. },
  64. subjectFactory: {
  65. get: function() {
  66. return (this as any)._zoneSubjectFactory;
  67. },
  68. set: function(factory: any) {
  69. const zone = this._zone;
  70. this._zoneSubjectFactory = function() {
  71. if (zone && zone !== Zone.current) {
  72. return zone.run(factory, this, arguments);
  73. }
  74. return factory.apply(this, arguments);
  75. };
  76. }
  77. }
  78. });
  79. };
  80. api.patchMethod(Observable.prototype, 'lift', (delegate: any) => (self: any, args: any[]) => {
  81. const observable: any = delegate.apply(self, args);
  82. if (observable.operator) {
  83. observable.operator._zone = Zone.current;
  84. api.patchMethod(
  85. observable.operator, 'call',
  86. (operatorDelegate: any) => (operatorSelf: any, operatorArgs: any[]) => {
  87. if (operatorSelf._zone && operatorSelf._zone !== Zone.current) {
  88. return operatorSelf._zone.run(operatorDelegate, operatorSelf, operatorArgs);
  89. }
  90. return operatorDelegate.apply(operatorSelf, operatorArgs);
  91. });
  92. }
  93. return observable;
  94. });
  95. const patchSubscription = function() {
  96. ObjectDefineProperties(Subscription.prototype, {
  97. _zone: {value: null, writable: true, configurable: true},
  98. _zoneUnsubscribe: {value: null, writable: true, configurable: true},
  99. _unsubscribe: {
  100. get: function(this: Subscription) {
  101. if ((this as any)._zoneUnsubscribe) {
  102. return (this as any)._zoneUnsubscribe;
  103. }
  104. const proto = Object.getPrototypeOf(this);
  105. return proto && proto._unsubscribe;
  106. },
  107. set: function(this: Subscription, unsubscribe: any) {
  108. (this as any)._zone = Zone.current;
  109. (this as any)._zoneUnsubscribe = function() {
  110. if (this._zone && this._zone !== Zone.current) {
  111. return this._zone.run(unsubscribe, this, arguments);
  112. }
  113. return unsubscribe.apply(this, arguments);
  114. };
  115. }
  116. }
  117. });
  118. };
  119. const patchSubscriber = function() {
  120. const next = Subscriber.prototype.next;
  121. const error = Subscriber.prototype.error;
  122. const complete = Subscriber.prototype.complete;
  123. Object.defineProperty(Subscriber.prototype, 'destination', {
  124. configurable: true,
  125. get: function(this: Subscriber<any>) {
  126. return (this as any)._zoneDestination;
  127. },
  128. set: function(this: Subscriber<any>, destination: any) {
  129. (this as any)._zone = Zone.current;
  130. (this as any)._zoneDestination = destination;
  131. }
  132. });
  133. // patch Subscriber.next to make sure it run
  134. // into SubscriptionZone
  135. Subscriber.prototype.next = function() {
  136. const currentZone = Zone.current;
  137. const subscriptionZone = this._zone;
  138. // for performance concern, check Zone.current
  139. // equal with this._zone(SubscriptionZone) or not
  140. if (subscriptionZone && subscriptionZone !== currentZone) {
  141. return subscriptionZone.run(next, this, arguments, nextSource);
  142. } else {
  143. return next.apply(this, arguments as any);
  144. }
  145. };
  146. Subscriber.prototype.error = function() {
  147. const currentZone = Zone.current;
  148. const subscriptionZone = this._zone;
  149. // for performance concern, check Zone.current
  150. // equal with this._zone(SubscriptionZone) or not
  151. if (subscriptionZone && subscriptionZone !== currentZone) {
  152. return subscriptionZone.run(error, this, arguments, errorSource);
  153. } else {
  154. return error.apply(this, arguments as any);
  155. }
  156. };
  157. Subscriber.prototype.complete = function() {
  158. const currentZone = Zone.current;
  159. const subscriptionZone = this._zone;
  160. // for performance concern, check Zone.current
  161. // equal with this._zone(SubscriptionZone) or not
  162. if (subscriptionZone && subscriptionZone !== currentZone) {
  163. return subscriptionZone.run(complete, this, arguments, completeSource);
  164. } else {
  165. return complete.call(this);
  166. }
  167. };
  168. };
  169. patchObservable();
  170. patchSubscription();
  171. patchSubscriber();
  172. });