a zip code crypto-currency system good for red ONLY

rxjs.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 'rxjs/add/observable/bindCallback';
  9. import 'rxjs/add/observable/bindNodeCallback';
  10. import 'rxjs/add/observable/defer';
  11. import 'rxjs/add/observable/forkJoin';
  12. import 'rxjs/add/observable/fromEventPattern';
  13. import 'rxjs/add/operator/multicast';
  14. import {Observable} from 'rxjs/Observable';
  15. import {asap} from 'rxjs/scheduler/asap';
  16. import {Subscriber} from 'rxjs/Subscriber';
  17. import {Subscription} from 'rxjs/Subscription';
  18. import {rxSubscriber} from 'rxjs/symbol/rxSubscriber';
  19. (Zone as any).__load_patch('rxjs', (global: any, Zone: ZoneType) => {
  20. const symbol: (symbolString: string) => string = (Zone as any).__symbol__;
  21. const nextSource = 'rxjs.Subscriber.next';
  22. const errorSource = 'rxjs.Subscriber.error';
  23. const completeSource = 'rxjs.Subscriber.complete';
  24. const ObjectDefineProperties = Object.defineProperties;
  25. const empty = {
  26. closed: true,
  27. next(value: any): void{},
  28. error(err: any): void{throw err;},
  29. complete(): void{}
  30. };
  31. function toSubscriber<T>(
  32. nextOrObserver?: any, error?: (error: any) => void, complete?: () => void): Subscriber<T> {
  33. if (nextOrObserver) {
  34. if (nextOrObserver instanceof Subscriber) {
  35. return (<Subscriber<T>>nextOrObserver);
  36. }
  37. if (nextOrObserver[rxSubscriber]) {
  38. return nextOrObserver[rxSubscriber]();
  39. }
  40. }
  41. if (!nextOrObserver && !error && !complete) {
  42. return new Subscriber(empty);
  43. }
  44. return new Subscriber(nextOrObserver, error, complete);
  45. }
  46. const patchObservable = function() {
  47. const ObservablePrototype: any = Observable.prototype;
  48. const symbolSubscribe = symbol('subscribe');
  49. const _symbolSubscribe = symbol('_subscribe');
  50. const _subscribe = ObservablePrototype[_symbolSubscribe] = ObservablePrototype._subscribe;
  51. const subscribe = ObservablePrototype[symbolSubscribe] = ObservablePrototype.subscribe;
  52. ObjectDefineProperties(Observable.prototype, {
  53. _zone: {value: null, writable: true, configurable: true},
  54. _zoneSource: {value: null, writable: true, configurable: true},
  55. _zoneSubscribe: {value: null, writable: true, configurable: true},
  56. source: {
  57. configurable: true,
  58. get: function(this: Observable<any>) {
  59. return (this as any)._zoneSource;
  60. },
  61. set: function(this: Observable<any>, source: any) {
  62. (this as any)._zone = Zone.current;
  63. (this as any)._zoneSource = source;
  64. }
  65. },
  66. _subscribe: {
  67. configurable: true,
  68. get: function(this: Observable<any>) {
  69. if ((this as any)._zoneSubscribe) {
  70. return (this as any)._zoneSubscribe;
  71. } else if (this.constructor === Observable) {
  72. return _subscribe;
  73. }
  74. const proto = Object.getPrototypeOf(this);
  75. return proto && proto._subscribe;
  76. },
  77. set: function(this: Observable<any>, subscribe: any) {
  78. (this as any)._zone = Zone.current;
  79. (this as any)._zoneSubscribe = subscribe;
  80. }
  81. },
  82. subscribe: {
  83. writable: true,
  84. configurable: true,
  85. value: function(this: Observable<any>, observerOrNext: any, error: any, complete: any) {
  86. // Only grab a zone if we Zone exists and it is different from the current zone.
  87. const _zone = (this as any)._zone;
  88. if (_zone && _zone !== Zone.current) {
  89. // Current Zone is different from the intended zone.
  90. // Restore the zone before invoking the subscribe callback.
  91. return _zone.run(subscribe, this, [toSubscriber(observerOrNext, error, complete)]);
  92. }
  93. return subscribe.call(this, observerOrNext, error, complete);
  94. }
  95. }
  96. });
  97. };
  98. const patchSubscription = function() {
  99. const unsubscribeSymbol = symbol('unsubscribe');
  100. const unsubscribe = (Subscription.prototype as any)[unsubscribeSymbol] =
  101. Subscription.prototype.unsubscribe;
  102. ObjectDefineProperties(Subscription.prototype, {
  103. _zone: {value: null, writable: true, configurable: true},
  104. _zoneUnsubscribe: {value: null, writable: true, configurable: true},
  105. _unsubscribe: {
  106. get: function(this: Subscription) {
  107. if ((this as any)._zoneUnsubscribe) {
  108. return (this as any)._zoneUnsubscribe;
  109. }
  110. const proto = Object.getPrototypeOf(this);
  111. return proto && proto._unsubscribe;
  112. },
  113. set: function(this: Subscription, unsubscribe: any) {
  114. (this as any)._zone = Zone.current;
  115. (this as any)._zoneUnsubscribe = unsubscribe;
  116. }
  117. },
  118. unsubscribe: {
  119. writable: true,
  120. configurable: true,
  121. value: function(this: Subscription) {
  122. // Only grab a zone if we Zone exists and it is different from the current zone.
  123. const _zone: Zone = (this as any)._zone;
  124. if (_zone && _zone !== Zone.current) {
  125. // Current Zone is different from the intended zone.
  126. // Restore the zone before invoking the subscribe callback.
  127. _zone.run(unsubscribe, this);
  128. } else {
  129. unsubscribe.apply(this);
  130. }
  131. }
  132. }
  133. });
  134. };
  135. const patchSubscriber = function() {
  136. const next = Subscriber.prototype.next;
  137. const error = Subscriber.prototype.error;
  138. const complete = Subscriber.prototype.complete;
  139. Object.defineProperty(Subscriber.prototype, 'destination', {
  140. configurable: true,
  141. get: function(this: Subscriber<any>) {
  142. return (this as any)._zoneDestination;
  143. },
  144. set: function(this: Subscriber<any>, destination: any) {
  145. (this as any)._zone = Zone.current;
  146. (this as any)._zoneDestination = destination;
  147. }
  148. });
  149. // patch Subscriber.next to make sure it run
  150. // into SubscriptionZone
  151. Subscriber.prototype.next = function() {
  152. const currentZone = Zone.current;
  153. const subscriptionZone = this._zone;
  154. // for performance concern, check Zone.current
  155. // equal with this._zone(SubscriptionZone) or not
  156. if (subscriptionZone && subscriptionZone !== currentZone) {
  157. return subscriptionZone.run(next, this, arguments, nextSource);
  158. } else {
  159. return next.apply(this, arguments);
  160. }
  161. };
  162. Subscriber.prototype.error = function() {
  163. const currentZone = Zone.current;
  164. const subscriptionZone = this._zone;
  165. // for performance concern, check Zone.current
  166. // equal with this._zone(SubscriptionZone) or not
  167. if (subscriptionZone && subscriptionZone !== currentZone) {
  168. return subscriptionZone.run(error, this, arguments, errorSource);
  169. } else {
  170. return error.apply(this, arguments);
  171. }
  172. };
  173. Subscriber.prototype.complete = function() {
  174. const currentZone = Zone.current;
  175. const subscriptionZone = this._zone;
  176. // for performance concern, check Zone.current
  177. // equal with this._zone(SubscriptionZone) or not
  178. if (subscriptionZone && subscriptionZone !== currentZone) {
  179. return subscriptionZone.run(complete, this, arguments, completeSource);
  180. } else {
  181. return complete.apply(this, arguments);
  182. }
  183. };
  184. };
  185. const patchObservableInstance = function(observable: any) {
  186. observable._zone = Zone.current;
  187. };
  188. const patchObservableFactoryCreator = function(obj: any, factoryName: string) {
  189. const symbolFactory: string = symbol(factoryName);
  190. if (obj[symbolFactory]) {
  191. return;
  192. }
  193. const factoryCreator: any = obj[symbolFactory] = obj[factoryName];
  194. if (!factoryCreator) {
  195. return;
  196. }
  197. obj[factoryName] = function() {
  198. const factory: any = factoryCreator.apply(this, arguments);
  199. return function() {
  200. const observable = factory.apply(this, arguments);
  201. patchObservableInstance(observable);
  202. return observable;
  203. };
  204. };
  205. };
  206. const patchObservableFactory = function(obj: any, factoryName: string) {
  207. const symbolFactory: string = symbol(factoryName);
  208. if (obj[symbolFactory]) {
  209. return;
  210. }
  211. const factory: any = obj[symbolFactory] = obj[factoryName];
  212. if (!factory) {
  213. return;
  214. }
  215. obj[factoryName] = function() {
  216. const observable = factory.apply(this, arguments);
  217. patchObservableInstance(observable);
  218. return observable;
  219. };
  220. };
  221. const patchObservableFactoryArgs = function(obj: any, factoryName: string) {
  222. const symbolFactory: string = symbol(factoryName);
  223. if (obj[symbolFactory]) {
  224. return;
  225. }
  226. const factory: any = obj[symbolFactory] = obj[factoryName];
  227. if (!factory) {
  228. return;
  229. }
  230. obj[factoryName] = function() {
  231. const initZone = Zone.current;
  232. const args = Array.prototype.slice.call(arguments);
  233. for (let i = 0; i < args.length; i++) {
  234. const arg = args[i];
  235. if (typeof arg === 'function') {
  236. args[i] = function() {
  237. const argArgs = Array.prototype.slice.call(arguments);
  238. const runningZone = Zone.current;
  239. if (initZone && runningZone && initZone !== runningZone) {
  240. return initZone.run(arg, this, argArgs);
  241. } else {
  242. return arg.apply(this, argArgs);
  243. }
  244. };
  245. }
  246. }
  247. const observable = factory.apply(this, args);
  248. patchObservableInstance(observable);
  249. return observable;
  250. };
  251. };
  252. const patchMulticast = function() {
  253. const obj: any = Observable.prototype;
  254. const factoryName: string = 'multicast';
  255. const symbolFactory: string = symbol(factoryName);
  256. if (obj[symbolFactory]) {
  257. return;
  258. }
  259. const factory: any = obj[symbolFactory] = obj[factoryName];
  260. if (!factory) {
  261. return;
  262. }
  263. obj[factoryName] = function() {
  264. const _zone: any = Zone.current;
  265. const args = Array.prototype.slice.call(arguments);
  266. let subjectOrSubjectFactory: any = args.length > 0 ? args[0] : undefined;
  267. if (typeof subjectOrSubjectFactory !== 'function') {
  268. const originalFactory: any = subjectOrSubjectFactory;
  269. subjectOrSubjectFactory = function() {
  270. return originalFactory;
  271. };
  272. }
  273. args[0] = function() {
  274. let subject: any;
  275. if (_zone && _zone !== Zone.current) {
  276. subject = _zone.run(subjectOrSubjectFactory, this, arguments);
  277. } else {
  278. subject = subjectOrSubjectFactory.apply(this, arguments);
  279. }
  280. if (subject && _zone) {
  281. subject._zone = _zone;
  282. }
  283. return subject;
  284. };
  285. const observable = factory.apply(this, args);
  286. patchObservableInstance(observable);
  287. return observable;
  288. };
  289. };
  290. const patchImmediate = function(asap: any) {
  291. if (!asap) {
  292. return;
  293. }
  294. const scheduleSymbol = symbol('scheduleSymbol');
  295. const zoneSymbol = symbol('zone');
  296. if (asap[scheduleSymbol]) {
  297. return;
  298. }
  299. const schedule = asap[scheduleSymbol] = asap.schedule;
  300. asap.schedule = function() {
  301. const args = Array.prototype.slice.call(arguments);
  302. const work = args.length > 0 ? args[0] : undefined;
  303. const delay = args.length > 1 ? args[1] : 0;
  304. const state = (args.length > 2 ? args[2] : undefined) || {};
  305. state[zoneSymbol] = Zone.current;
  306. const patchedWork = function() {
  307. const workArgs = Array.prototype.slice.call(arguments);
  308. const action = workArgs.length > 0 ? workArgs[0] : undefined;
  309. const scheduleZone = action && action[zoneSymbol];
  310. if (scheduleZone && scheduleZone !== Zone.current) {
  311. return scheduleZone.runGuarded(work, this, arguments);
  312. } else {
  313. return work.apply(this, arguments);
  314. }
  315. };
  316. return schedule.call(this, patchedWork, delay, state);
  317. };
  318. };
  319. patchObservable();
  320. patchSubscription();
  321. patchSubscriber();
  322. patchObservableFactoryCreator(Observable, 'bindCallback');
  323. patchObservableFactoryCreator(Observable, 'bindNodeCallback');
  324. patchObservableFactory(Observable, 'defer');
  325. patchObservableFactory(Observable, 'forkJoin');
  326. patchObservableFactoryArgs(Observable, 'fromEventPattern');
  327. patchMulticast();
  328. patchImmediate(asap);
  329. });