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