a zip code crypto-currency system good for red ONLY

browser.ts 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /**
  9. * @fileoverview
  10. * @suppress {missingRequire}
  11. */
  12. import {findEventTasks} from '../common/events';
  13. import {patchTimer} from '../common/timers';
  14. import {bindArguments, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, scheduleMacroTaskWithCurrentZone, ZONE_SYMBOL_ADD_EVENT_LISTENER, ZONE_SYMBOL_REMOVE_EVENT_LISTENER, zoneSymbol} from '../common/utils';
  15. import {propertyPatch} from './define-property';
  16. import {eventTargetPatch, patchEvent} from './event-target';
  17. import {propertyDescriptorPatch} from './property-descriptor';
  18. import {registerElementPatch} from './register-element';
  19. Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  20. api.patchOnProperties = patchOnProperties;
  21. api.patchMethod = patchMethod;
  22. api.bindArguments = bindArguments;
  23. });
  24. Zone.__load_patch('timers', (global: any) => {
  25. const set = 'set';
  26. const clear = 'clear';
  27. patchTimer(global, set, clear, 'Timeout');
  28. patchTimer(global, set, clear, 'Interval');
  29. patchTimer(global, set, clear, 'Immediate');
  30. });
  31. Zone.__load_patch('requestAnimationFrame', (global: any) => {
  32. patchTimer(global, 'request', 'cancel', 'AnimationFrame');
  33. patchTimer(global, 'mozRequest', 'mozCancel', 'AnimationFrame');
  34. patchTimer(global, 'webkitRequest', 'webkitCancel', 'AnimationFrame');
  35. });
  36. Zone.__load_patch('blocking', (global: any, Zone: ZoneType) => {
  37. const blockingMethods = ['alert', 'prompt', 'confirm'];
  38. for (let i = 0; i < blockingMethods.length; i++) {
  39. const name = blockingMethods[i];
  40. patchMethod(global, name, (delegate, symbol, name) => {
  41. return function(s: any, args: any[]) {
  42. return Zone.current.run(delegate, global, args, name);
  43. };
  44. });
  45. }
  46. });
  47. Zone.__load_patch('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  48. // load blackListEvents from global
  49. const SYMBOL_BLACK_LISTED_EVENTS = Zone.__symbol__('BLACK_LISTED_EVENTS');
  50. if (global[SYMBOL_BLACK_LISTED_EVENTS]) {
  51. (Zone as any)[SYMBOL_BLACK_LISTED_EVENTS] = global[SYMBOL_BLACK_LISTED_EVENTS];
  52. }
  53. patchEvent(global, api);
  54. eventTargetPatch(global, api);
  55. // patch XMLHttpRequestEventTarget's addEventListener/removeEventListener
  56. const XMLHttpRequestEventTarget = (global as any)['XMLHttpRequestEventTarget'];
  57. if (XMLHttpRequestEventTarget && XMLHttpRequestEventTarget.prototype) {
  58. api.patchEventTarget(global, [XMLHttpRequestEventTarget.prototype]);
  59. }
  60. patchClass('MutationObserver');
  61. patchClass('WebKitMutationObserver');
  62. patchClass('IntersectionObserver');
  63. patchClass('FileReader');
  64. });
  65. Zone.__load_patch('on_property', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  66. propertyDescriptorPatch(api, global);
  67. propertyPatch();
  68. registerElementPatch(global);
  69. });
  70. Zone.__load_patch('canvas', (global: any) => {
  71. const HTMLCanvasElement = global['HTMLCanvasElement'];
  72. if (typeof HTMLCanvasElement !== 'undefined' && HTMLCanvasElement.prototype &&
  73. HTMLCanvasElement.prototype.toBlob) {
  74. patchMacroTask(HTMLCanvasElement.prototype, 'toBlob', (self: any, args: any[]) => {
  75. return {name: 'HTMLCanvasElement.toBlob', target: self, cbIdx: 0, args: args};
  76. });
  77. }
  78. });
  79. Zone.__load_patch('XHR', (global: any, Zone: ZoneType) => {
  80. // Treat XMLHttpRequest as a macrotask.
  81. patchXHR(global);
  82. const XHR_TASK = zoneSymbol('xhrTask');
  83. const XHR_SYNC = zoneSymbol('xhrSync');
  84. const XHR_LISTENER = zoneSymbol('xhrListener');
  85. const XHR_SCHEDULED = zoneSymbol('xhrScheduled');
  86. const XHR_URL = zoneSymbol('xhrURL');
  87. interface XHROptions extends TaskData {
  88. target: any;
  89. url: string;
  90. args: any[];
  91. aborted: boolean;
  92. }
  93. function patchXHR(window: any) {
  94. const XMLHttpRequestPrototype: any = XMLHttpRequest.prototype;
  95. function findPendingTask(target: any) {
  96. return target[XHR_TASK];
  97. }
  98. let oriAddListener = XMLHttpRequestPrototype[ZONE_SYMBOL_ADD_EVENT_LISTENER];
  99. let oriRemoveListener = XMLHttpRequestPrototype[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
  100. if (!oriAddListener) {
  101. const XMLHttpRequestEventTarget = window['XMLHttpRequestEventTarget'];
  102. if (XMLHttpRequestEventTarget) {
  103. const XMLHttpRequestEventTargetPrototype = XMLHttpRequestEventTarget.prototype;
  104. oriAddListener = XMLHttpRequestEventTargetPrototype[ZONE_SYMBOL_ADD_EVENT_LISTENER];
  105. oriRemoveListener = XMLHttpRequestEventTargetPrototype[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
  106. }
  107. }
  108. const READY_STATE_CHANGE = 'readystatechange';
  109. const SCHEDULED = 'scheduled';
  110. function scheduleTask(task: Task) {
  111. (XMLHttpRequest as any)[XHR_SCHEDULED] = false;
  112. const data = <XHROptions>task.data;
  113. const target = data.target;
  114. // remove existing event listener
  115. const listener = target[XHR_LISTENER];
  116. if (!oriAddListener) {
  117. oriAddListener = target[ZONE_SYMBOL_ADD_EVENT_LISTENER];
  118. oriRemoveListener = target[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
  119. }
  120. if (listener) {
  121. oriRemoveListener.call(target, READY_STATE_CHANGE, listener);
  122. }
  123. const newListener = target[XHR_LISTENER] = () => {
  124. if (target.readyState === target.DONE) {
  125. // sometimes on some browsers XMLHttpRequest will fire onreadystatechange with
  126. // readyState=4 multiple times, so we need to check task state here
  127. if (!data.aborted && (XMLHttpRequest as any)[XHR_SCHEDULED] && task.state === SCHEDULED) {
  128. task.invoke();
  129. }
  130. }
  131. };
  132. oriAddListener.call(target, READY_STATE_CHANGE, newListener);
  133. const storedTask: Task = target[XHR_TASK];
  134. if (!storedTask) {
  135. target[XHR_TASK] = task;
  136. }
  137. sendNative.apply(target, data.args);
  138. (XMLHttpRequest as any)[XHR_SCHEDULED] = true;
  139. return task;
  140. }
  141. function placeholderCallback() {}
  142. function clearTask(task: Task) {
  143. const data = <XHROptions>task.data;
  144. // Note - ideally, we would call data.target.removeEventListener here, but it's too late
  145. // to prevent it from firing. So instead, we store info for the event listener.
  146. data.aborted = true;
  147. return abortNative.apply(data.target, data.args);
  148. }
  149. const openNative: Function =
  150. patchMethod(XMLHttpRequestPrototype, 'open', () => function(self: any, args: any[]) {
  151. self[XHR_SYNC] = args[2] == false;
  152. self[XHR_URL] = args[1];
  153. return openNative.apply(self, args);
  154. });
  155. const XMLHTTPREQUEST_SOURCE = 'XMLHttpRequest.send';
  156. const sendNative: Function =
  157. patchMethod(XMLHttpRequestPrototype, 'send', () => function(self: any, args: any[]) {
  158. if (self[XHR_SYNC]) {
  159. // if the XHR is sync there is no task to schedule, just execute the code.
  160. return sendNative.apply(self, args);
  161. } else {
  162. const options: XHROptions = {
  163. target: self,
  164. url: self[XHR_URL],
  165. isPeriodic: false,
  166. delay: null,
  167. args: args,
  168. aborted: false
  169. };
  170. return scheduleMacroTaskWithCurrentZone(
  171. XMLHTTPREQUEST_SOURCE, placeholderCallback, options, scheduleTask, clearTask);
  172. }
  173. });
  174. const abortNative = patchMethod(XMLHttpRequestPrototype, 'abort', () => function(self: any) {
  175. const task: Task = findPendingTask(self);
  176. if (task && typeof task.type == 'string') {
  177. // If the XHR has already completed, do nothing.
  178. // If the XHR has already been aborted, do nothing.
  179. // Fix #569, call abort multiple times before done will cause
  180. // macroTask task count be negative number
  181. if (task.cancelFn == null || (task.data && (<XHROptions>task.data).aborted)) {
  182. return;
  183. }
  184. task.zone.cancelTask(task);
  185. }
  186. // Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
  187. // task
  188. // to cancel. Do nothing.
  189. });
  190. }
  191. });
  192. Zone.__load_patch('geolocation', (global: any) => {
  193. /// GEO_LOCATION
  194. if (global['navigator'] && global['navigator'].geolocation) {
  195. patchPrototype(global['navigator'].geolocation, ['getCurrentPosition', 'watchPosition']);
  196. }
  197. });
  198. Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType) => {
  199. // handle unhandled promise rejection
  200. function findPromiseRejectionHandler(evtName: string) {
  201. return function(e: any) {
  202. const eventTasks = findEventTasks(global, evtName);
  203. eventTasks.forEach(eventTask => {
  204. // windows has added unhandledrejection event listener
  205. // trigger the event listener
  206. const PromiseRejectionEvent = global['PromiseRejectionEvent'];
  207. if (PromiseRejectionEvent) {
  208. const evt = new PromiseRejectionEvent(evtName, {promise: e.promise, reason: e.rejection});
  209. eventTask.invoke(evt);
  210. }
  211. });
  212. };
  213. }
  214. if (global['PromiseRejectionEvent']) {
  215. (Zone as any)[zoneSymbol('unhandledPromiseRejectionHandler')] =
  216. findPromiseRejectionHandler('unhandledrejection');
  217. (Zone as any)[zoneSymbol('rejectionHandledHandler')] =
  218. findPromiseRejectionHandler('rejectionhandled');
  219. }
  220. });