zone-patch-rxjs.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2022 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. import { Observable, Subscription, Subscriber } from 'rxjs';
  8. Zone.__load_patch('rxjs', (global, Zone, api) => {
  9. const symbol = Zone.__symbol__;
  10. const nextSource = 'rxjs.Subscriber.next';
  11. const errorSource = 'rxjs.Subscriber.error';
  12. const completeSource = 'rxjs.Subscriber.complete';
  13. const ObjectDefineProperties = Object.defineProperties;
  14. const patchObservable = function () {
  15. const ObservablePrototype = Observable.prototype;
  16. const _symbolSubscribe = symbol('_subscribe');
  17. const _subscribe = ObservablePrototype[_symbolSubscribe] = ObservablePrototype._subscribe;
  18. ObjectDefineProperties(Observable.prototype, {
  19. _zone: { value: null, writable: true, configurable: true },
  20. _zoneSource: { value: null, writable: true, configurable: true },
  21. _zoneSubscribe: { value: null, writable: true, configurable: true },
  22. source: {
  23. configurable: true,
  24. get: function () {
  25. return this._zoneSource;
  26. },
  27. set: function (source) {
  28. this._zone = Zone.current;
  29. this._zoneSource = source;
  30. }
  31. },
  32. _subscribe: {
  33. configurable: true,
  34. get: function () {
  35. if (this._zoneSubscribe) {
  36. return this._zoneSubscribe;
  37. }
  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 (subscribe) {
  45. this._zone = Zone.current;
  46. if (!subscribe) {
  47. this._zoneSubscribe = subscribe;
  48. }
  49. else {
  50. this._zoneSubscribe = function () {
  51. if (this._zone && this._zone !== Zone.current) {
  52. const tearDown = this._zone.run(subscribe, this, arguments);
  53. if (typeof tearDown === 'function') {
  54. const zone = this._zone;
  55. return function () {
  56. if (zone !== Zone.current) {
  57. return zone.run(tearDown, this, arguments);
  58. }
  59. return tearDown.apply(this, arguments);
  60. };
  61. }
  62. else {
  63. return tearDown;
  64. }
  65. }
  66. else {
  67. return subscribe.apply(this, arguments);
  68. }
  69. };
  70. }
  71. }
  72. },
  73. subjectFactory: {
  74. get: function () {
  75. return this._zoneSubjectFactory;
  76. },
  77. set: function (factory) {
  78. const zone = this._zone;
  79. this._zoneSubjectFactory = function () {
  80. if (zone && zone !== Zone.current) {
  81. return zone.run(factory, this, arguments);
  82. }
  83. return factory.apply(this, arguments);
  84. };
  85. }
  86. }
  87. });
  88. };
  89. api.patchMethod(Observable.prototype, 'lift', (delegate) => (self, args) => {
  90. const observable = delegate.apply(self, args);
  91. if (observable.operator) {
  92. observable.operator._zone = Zone.current;
  93. api.patchMethod(observable.operator, 'call', (operatorDelegate) => (operatorSelf, operatorArgs) => {
  94. if (operatorSelf._zone && operatorSelf._zone !== Zone.current) {
  95. return operatorSelf._zone.run(operatorDelegate, operatorSelf, operatorArgs);
  96. }
  97. return operatorDelegate.apply(operatorSelf, operatorArgs);
  98. });
  99. }
  100. return observable;
  101. });
  102. const patchSubscription = function () {
  103. ObjectDefineProperties(Subscription.prototype, {
  104. _zone: { value: null, writable: true, configurable: true },
  105. _zoneUnsubscribe: { value: null, writable: true, configurable: true },
  106. _unsubscribe: {
  107. get: function () {
  108. if (this._zoneUnsubscribe || this._zoneUnsubscribeCleared) {
  109. return this._zoneUnsubscribe;
  110. }
  111. const proto = Object.getPrototypeOf(this);
  112. return proto && proto._unsubscribe;
  113. },
  114. set: function (unsubscribe) {
  115. this._zone = Zone.current;
  116. if (!unsubscribe) {
  117. this._zoneUnsubscribe = unsubscribe;
  118. // In some operator such as `retryWhen`, the _unsubscribe
  119. // method will be set to null, so we need to set another flag
  120. // to tell that we should return null instead of finding
  121. // in the prototype chain.
  122. this._zoneUnsubscribeCleared = true;
  123. }
  124. else {
  125. this._zoneUnsubscribeCleared = false;
  126. this._zoneUnsubscribe = function () {
  127. if (this._zone && this._zone !== Zone.current) {
  128. return this._zone.run(unsubscribe, this, arguments);
  129. }
  130. else {
  131. return unsubscribe.apply(this, arguments);
  132. }
  133. };
  134. }
  135. }
  136. }
  137. });
  138. };
  139. const patchSubscriber = function () {
  140. const next = Subscriber.prototype.next;
  141. const error = Subscriber.prototype.error;
  142. const complete = Subscriber.prototype.complete;
  143. Object.defineProperty(Subscriber.prototype, 'destination', {
  144. configurable: true,
  145. get: function () {
  146. return this._zoneDestination;
  147. },
  148. set: function (destination) {
  149. this._zone = Zone.current;
  150. this._zoneDestination = destination;
  151. }
  152. });
  153. // patch Subscriber.next to make sure it run
  154. // into SubscriptionZone
  155. Subscriber.prototype.next = function () {
  156. const currentZone = Zone.current;
  157. const subscriptionZone = this._zone;
  158. // for performance concern, check Zone.current
  159. // equal with this._zone(SubscriptionZone) or not
  160. if (subscriptionZone && subscriptionZone !== currentZone) {
  161. return subscriptionZone.run(next, this, arguments, nextSource);
  162. }
  163. else {
  164. return next.apply(this, arguments);
  165. }
  166. };
  167. Subscriber.prototype.error = function () {
  168. const currentZone = Zone.current;
  169. const subscriptionZone = this._zone;
  170. // for performance concern, check Zone.current
  171. // equal with this._zone(SubscriptionZone) or not
  172. if (subscriptionZone && subscriptionZone !== currentZone) {
  173. return subscriptionZone.run(error, this, arguments, errorSource);
  174. }
  175. else {
  176. return error.apply(this, arguments);
  177. }
  178. };
  179. Subscriber.prototype.complete = function () {
  180. const currentZone = Zone.current;
  181. const subscriptionZone = this._zone;
  182. // for performance concern, check Zone.current
  183. // equal with this._zone(SubscriptionZone) or not
  184. if (subscriptionZone && subscriptionZone !== currentZone) {
  185. return subscriptionZone.run(complete, this, arguments, completeSource);
  186. }
  187. else {
  188. return complete.call(this);
  189. }
  190. };
  191. };
  192. patchObservable();
  193. patchSubscription();
  194. patchSubscriber();
  195. });