a zip code crypto-currency system good for red ONLY

zone.js.d.ts 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. * Suppress closure compiler errors about unknown 'global' variable
  10. * @fileoverview
  11. * @suppress {undefinedVars}
  12. */
  13. /**
  14. * Zone is a mechanism for intercepting and keeping track of asynchronous work.
  15. *
  16. * A Zone is a global object which is configured with rules about how to intercept and keep track
  17. * of the asynchronous callbacks. Zone has these responsibilities:
  18. *
  19. * 1. Intercept asynchronous task scheduling
  20. * 2. Wrap callbacks for error-handling and zone tracking across async operations.
  21. * 3. Provide a way to attach data to zones
  22. * 4. Provide a context specific last frame error handling
  23. * 5. (Intercept blocking methods)
  24. *
  25. * A zone by itself does not do anything, instead it relies on some other code to route existing
  26. * platform API through it. (The zone library ships with code which monkey patches all of the
  27. * browsers's asynchronous API and redirects them through the zone for interception.)
  28. *
  29. * In its simplest form a zone allows one to intercept the scheduling and calling of asynchronous
  30. * operations, and execute additional code before as well as after the asynchronous task. The rules
  31. * of interception are configured using [ZoneConfig]. There can be many different zone instances in
  32. * a system, but only one zone is active at any given time which can be retrieved using
  33. * [Zone#current].
  34. *
  35. *
  36. *
  37. * ## Callback Wrapping
  38. *
  39. * An important aspect of the zones is that they should persist across asynchronous operations. To
  40. * achieve this, when a future work is scheduled through async API, it is necessary to capture, and
  41. * subsequently restore the current zone. For example if a code is running in zone `b` and it
  42. * invokes `setTimeout` to scheduleTask work later, the `setTimeout` method needs to 1) capture the
  43. * current zone and 2) wrap the `wrapCallback` in code which will restore the current zone `b` once
  44. * the wrapCallback executes. In this way the rules which govern the current code are preserved in
  45. * all future asynchronous tasks. There could be a different zone `c` which has different rules and
  46. * is associated with different asynchronous tasks. As these tasks are processed, each asynchronous
  47. * wrapCallback correctly restores the correct zone, as well as preserves the zone for future
  48. * asynchronous callbacks.
  49. *
  50. * Example: Suppose a browser page consist of application code as well as third-party
  51. * advertisement code. (These two code bases are independent, developed by different mutually
  52. * unaware developers.) The application code may be interested in doing global error handling and
  53. * so it configures the `app` zone to send all of the errors to the server for analysis, and then
  54. * executes the application in the `app` zone. The advertising code is interested in the same
  55. * error processing but it needs to send the errors to a different third-party. So it creates the
  56. * `ads` zone with a different error handler. Now both advertising as well as application code
  57. * create many asynchronous operations, but the [Zone] will ensure that all of the asynchronous
  58. * operations created from the application code will execute in `app` zone with its error
  59. * handler and all of the advertisement code will execute in the `ads` zone with its error handler.
  60. * This will not only work for the async operations created directly, but also for all subsequent
  61. * asynchronous operations.
  62. *
  63. * If you think of chain of asynchronous operations as a thread of execution (bit of a stretch)
  64. * then [Zone#current] will act as a thread local variable.
  65. *
  66. *
  67. *
  68. * ## Asynchronous operation scheduling
  69. *
  70. * In addition to wrapping the callbacks to restore the zone, all operations which cause a
  71. * scheduling of work for later are routed through the current zone which is allowed to intercept
  72. * them by adding work before or after the wrapCallback as well as using different means of
  73. * achieving the request. (Useful for unit testing, or tracking of requests). In some instances
  74. * such as `setTimeout` the wrapping of the wrapCallback and scheduling is done in the same
  75. * wrapCallback, but there are other examples such as `Promises` where the `then` wrapCallback is
  76. * wrapped, but the execution of `then` is triggered by `Promise` scheduling `resolve` work.
  77. *
  78. * Fundamentally there are three kinds of tasks which can be scheduled:
  79. *
  80. * 1. [MicroTask] used for doing work right after the current task. This is non-cancelable which is
  81. * guaranteed to run exactly once and immediately.
  82. * 2. [MacroTask] used for doing work later. Such as `setTimeout`. This is typically cancelable
  83. * which is guaranteed to execute at least once after some well understood delay.
  84. * 3. [EventTask] used for listening on some future event. This may execute zero or more times, with
  85. * an unknown delay.
  86. *
  87. * Each asynchronous API is modeled and routed through one of these APIs.
  88. *
  89. *
  90. * ### [MicroTask]
  91. *
  92. * [MicroTask]s represent work which will be done in current VM turn as soon as possible, before VM
  93. * yielding.
  94. *
  95. *
  96. * ### [TimerTask]
  97. *
  98. * [TimerTask]s represent work which will be done after some delay. (Sometimes the delay is
  99. * approximate such as on next available animation frame). Typically these methods include:
  100. * `setTimeout`, `setImmediate`, `setInterval`, `requestAnimationFrame`, and all browser specific
  101. * variants.
  102. *
  103. *
  104. * ### [EventTask]
  105. *
  106. * [EventTask]s represent a request to create a listener on an event. Unlike the other task
  107. * events they may never be executed, but typically execute more than once. There is no queue of
  108. * events, rather their callbacks are unpredictable both in order and time.
  109. *
  110. *
  111. * ## Global Error Handling
  112. *
  113. *
  114. * ## Composability
  115. *
  116. * Zones can be composed together through [Zone.fork()]. A child zone may create its own set of
  117. * rules. A child zone is expected to either:
  118. *
  119. * 1. Delegate the interception to a parent zone, and optionally add before and after wrapCallback
  120. * hooks.
  121. * 2. Process the request itself without delegation.
  122. *
  123. * Composability allows zones to keep their concerns clean. For example a top most zone may choose
  124. * to handle error handling, while child zones may choose to do user action tracking.
  125. *
  126. *
  127. * ## Root Zone
  128. *
  129. * At the start the browser will run in a special root zone, which is configured to behave exactly
  130. * like the platform, making any existing code which is not zone-aware behave as expected. All
  131. * zones are children of the root zone.
  132. *
  133. */
  134. interface Zone {
  135. /**
  136. *
  137. * @returns {Zone} The parent Zone.
  138. */
  139. parent: Zone;
  140. /**
  141. * @returns {string} The Zone name (useful for debugging)
  142. */
  143. name: string;
  144. /**
  145. * Returns a value associated with the `key`.
  146. *
  147. * If the current zone does not have a key, the request is delegated to the parent zone. Use
  148. * [ZoneSpec.properties] to configure the set of properties associated with the current zone.
  149. *
  150. * @param key The key to retrieve.
  151. * @returns {any} The value for the key, or `undefined` if not found.
  152. */
  153. get(key: string): any;
  154. /**
  155. * Returns a Zone which defines a `key`.
  156. *
  157. * Recursively search the parent Zone until a Zone which has a property `key` is found.
  158. *
  159. * @param key The key to use for identification of the returned zone.
  160. * @returns {Zone} The Zone which defines the `key`, `null` if not found.
  161. */
  162. getZoneWith(key: string): Zone;
  163. /**
  164. * Used to create a child zone.
  165. *
  166. * @param zoneSpec A set of rules which the child zone should follow.
  167. * @returns {Zone} A new child zone.
  168. */
  169. fork(zoneSpec: ZoneSpec): Zone;
  170. /**
  171. * Wraps a callback function in a new function which will properly restore the current zone upon
  172. * invocation.
  173. *
  174. * The wrapped function will properly forward `this` as well as `arguments` to the `callback`.
  175. *
  176. * Before the function is wrapped the zone can intercept the `callback` by declaring
  177. * [ZoneSpec.onIntercept].
  178. *
  179. * @param callback the function which will be wrapped in the zone.
  180. * @param source A unique debug location of the API being wrapped.
  181. * @returns {function(): *} A function which will invoke the `callback` through [Zone.runGuarded].
  182. */
  183. wrap<F extends Function>(callback: F, source: string): F;
  184. /**
  185. * Invokes a function in a given zone.
  186. *
  187. * The invocation of `callback` can be intercepted by declaring [ZoneSpec.onInvoke].
  188. *
  189. * @param callback The function to invoke.
  190. * @param applyThis
  191. * @param applyArgs
  192. * @param source A unique debug location of the API being invoked.
  193. * @returns {any} Value from the `callback` function.
  194. */
  195. run<T>(callback: Function, applyThis?: any, applyArgs?: any[], source?: string): T;
  196. /**
  197. * Invokes a function in a given zone and catches any exceptions.
  198. *
  199. * Any exceptions thrown will be forwarded to [Zone.HandleError].
  200. *
  201. * The invocation of `callback` can be intercepted by declaring [ZoneSpec.onInvoke]. The
  202. * handling of exceptions can be intercepted by declaring [ZoneSpec.handleError].
  203. *
  204. * @param callback The function to invoke.
  205. * @param applyThis
  206. * @param applyArgs
  207. * @param source A unique debug location of the API being invoked.
  208. * @returns {any} Value from the `callback` function.
  209. */
  210. runGuarded<T>(callback: Function, applyThis?: any, applyArgs?: any[], source?: string): T;
  211. /**
  212. * Execute the Task by restoring the [Zone.currentTask] in the Task's zone.
  213. *
  214. * @param task to run
  215. * @param applyThis
  216. * @param applyArgs
  217. * @returns {*}
  218. */
  219. runTask(task: Task, applyThis?: any, applyArgs?: any): any;
  220. /**
  221. * Schedule a MicroTask.
  222. *
  223. * @param source
  224. * @param callback
  225. * @param data
  226. * @param customSchedule
  227. */
  228. scheduleMicroTask(source: string, callback: Function, data?: TaskData, customSchedule?: (task: Task) => void): MicroTask;
  229. /**
  230. * Schedule a MacroTask.
  231. *
  232. * @param source
  233. * @param callback
  234. * @param data
  235. * @param customSchedule
  236. * @param customCancel
  237. */
  238. scheduleMacroTask(source: string, callback: Function, data: TaskData, customSchedule: (task: Task) => void, customCancel: (task: Task) => void): MacroTask;
  239. /**
  240. * Schedule an EventTask.
  241. *
  242. * @param source
  243. * @param callback
  244. * @param data
  245. * @param customSchedule
  246. * @param customCancel
  247. */
  248. scheduleEventTask(source: string, callback: Function, data: TaskData, customSchedule: (task: Task) => void, customCancel: (task: Task) => void): EventTask;
  249. /**
  250. * Schedule an existing Task.
  251. *
  252. * Useful for rescheduling a task which was already canceled.
  253. *
  254. * @param task
  255. */
  256. scheduleTask<T extends Task>(task: T): T;
  257. /**
  258. * Allows the zone to intercept canceling of scheduled Task.
  259. *
  260. * The interception is configured using [ZoneSpec.onCancelTask]. The default canceler invokes
  261. * the [Task.cancelFn].
  262. *
  263. * @param task
  264. * @returns {any}
  265. */
  266. cancelTask(task: Task): any;
  267. }
  268. interface ZoneType {
  269. /**
  270. * @returns {Zone} Returns the current [Zone]. The only way to change
  271. * the current zone is by invoking a run() method, which will update the current zone for the
  272. * duration of the run method callback.
  273. */
  274. current: Zone;
  275. /**
  276. * @returns {Task} The task associated with the current execution.
  277. */
  278. currentTask: Task;
  279. /**
  280. * Verify that Zone has been correctly patched. Specifically that Promise is zone aware.
  281. */
  282. assertZonePatched(): void;
  283. /**
  284. * Return the root zone.
  285. */
  286. root: Zone;
  287. }
  288. interface UncaughtPromiseError extends Error {
  289. zone: Zone;
  290. task: Task;
  291. promise: Promise<any>;
  292. rejection: any;
  293. }
  294. /**
  295. * Provides a way to configure the interception of zone events.
  296. *
  297. * Only the `name` property is required (all other are optional).
  298. */
  299. interface ZoneSpec {
  300. /**
  301. * The name of the zone. Useful when debugging Zones.
  302. */
  303. name: string;
  304. /**
  305. * A set of properties to be associated with Zone. Use [Zone.get] to retrieve them.
  306. */
  307. properties?: {
  308. [key: string]: any;
  309. };
  310. /**
  311. * Allows the interception of zone forking.
  312. *
  313. * When the zone is being forked, the request is forwarded to this method for interception.
  314. *
  315. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  316. * @param currentZone The current [Zone] where the current interceptor has been declared.
  317. * @param targetZone The [Zone] which originally received the request.
  318. * @param zoneSpec The argument passed into the `fork` method.
  319. */
  320. onFork?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, zoneSpec: ZoneSpec) => Zone;
  321. /**
  322. * Allows interception of the wrapping of the callback.
  323. *
  324. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  325. * @param currentZone The current [Zone] where the current interceptor has been declared.
  326. * @param targetZone The [Zone] which originally received the request.
  327. * @param delegate The argument passed into the `wrap` method.
  328. * @param source The argument passed into the `wrap` method.
  329. */
  330. onIntercept?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function, source: string) => Function;
  331. /**
  332. * Allows interception of the callback invocation.
  333. *
  334. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  335. * @param currentZone The current [Zone] where the current interceptor has been declared.
  336. * @param targetZone The [Zone] which originally received the request.
  337. * @param delegate The argument passed into the `run` method.
  338. * @param applyThis The argument passed into the `run` method.
  339. * @param applyArgs The argument passed into the `run` method.
  340. * @param source The argument passed into the `run` method.
  341. */
  342. onInvoke?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function, applyThis: any, applyArgs: any[], source: string) => any;
  343. /**
  344. * Allows interception of the error handling.
  345. *
  346. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  347. * @param currentZone The current [Zone] where the current interceptor has been declared.
  348. * @param targetZone The [Zone] which originally received the request.
  349. * @param error The argument passed into the `handleError` method.
  350. */
  351. onHandleError?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any) => boolean;
  352. /**
  353. * Allows interception of task scheduling.
  354. *
  355. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  356. * @param currentZone The current [Zone] where the current interceptor has been declared.
  357. * @param targetZone The [Zone] which originally received the request.
  358. * @param task The argument passed into the `scheduleTask` method.
  359. */
  360. onScheduleTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) => Task;
  361. onInvokeTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task, applyThis: any, applyArgs: any) => any;
  362. /**
  363. * Allows interception of task cancellation.
  364. *
  365. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  366. * @param currentZone The current [Zone] where the current interceptor has been declared.
  367. * @param targetZone The [Zone] which originally received the request.
  368. * @param task The argument passed into the `cancelTask` method.
  369. */
  370. onCancelTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) => any;
  371. /**
  372. * Notifies of changes to the task queue empty status.
  373. *
  374. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  375. * @param currentZone The current [Zone] where the current interceptor has been declared.
  376. * @param targetZone The [Zone] which originally received the request.
  377. * @param hasTaskState
  378. */
  379. onHasTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, hasTaskState: HasTaskState) => void;
  380. }
  381. /**
  382. * A delegate when intercepting zone operations.
  383. *
  384. * A ZoneDelegate is needed because a child zone can't simply invoke a method on a parent zone. For
  385. * example a child zone wrap can't just call parent zone wrap. Doing so would create a callback
  386. * which is bound to the parent zone. What we are interested in is intercepting the callback before
  387. * it is bound to any zone. Furthermore, we also need to pass the targetZone (zone which received
  388. * the original request) to the delegate.
  389. *
  390. * The ZoneDelegate methods mirror those of Zone with an addition of extra targetZone argument in
  391. * the method signature. (The original Zone which received the request.) Some methods are renamed
  392. * to prevent confusion, because they have slightly different semantics and arguments.
  393. *
  394. * - `wrap` => `intercept`: The `wrap` method delegates to `intercept`. The `wrap` method returns
  395. * a callback which will run in a given zone, where as intercept allows wrapping the callback
  396. * so that additional code can be run before and after, but does not associate the callback
  397. * with the zone.
  398. * - `run` => `invoke`: The `run` method delegates to `invoke` to perform the actual execution of
  399. * the callback. The `run` method switches to new zone; saves and restores the `Zone.current`;
  400. * and optionally performs error handling. The invoke is not responsible for error handling,
  401. * or zone management.
  402. *
  403. * Not every method is usually overwritten in the child zone, for this reason the ZoneDelegate
  404. * stores the closest zone which overwrites this behavior along with the closest ZoneSpec.
  405. *
  406. * NOTE: We have tried to make this API analogous to Event bubbling with target and current
  407. * properties.
  408. *
  409. * Note: The ZoneDelegate treats ZoneSpec as class. This allows the ZoneSpec to use its `this` to
  410. * store internal state.
  411. */
  412. interface ZoneDelegate {
  413. zone: Zone;
  414. fork(targetZone: Zone, zoneSpec: ZoneSpec): Zone;
  415. intercept(targetZone: Zone, callback: Function, source: string): Function;
  416. invoke(targetZone: Zone, callback: Function, applyThis: any, applyArgs: any[], source: string): any;
  417. handleError(targetZone: Zone, error: any): boolean;
  418. scheduleTask(targetZone: Zone, task: Task): Task;
  419. invokeTask(targetZone: Zone, task: Task, applyThis: any, applyArgs: any): any;
  420. cancelTask(targetZone: Zone, task: Task): any;
  421. hasTask(targetZone: Zone, isEmpty: HasTaskState): void;
  422. }
  423. declare type HasTaskState = {
  424. microTask: boolean;
  425. macroTask: boolean;
  426. eventTask: boolean;
  427. change: TaskType;
  428. };
  429. /**
  430. * Task type: `microTask`, `macroTask`, `eventTask`.
  431. */
  432. declare type TaskType = 'microTask' | 'macroTask' | 'eventTask';
  433. /**
  434. * Task type: `notScheduled`, `scheduling`, `scheduled`, `running`, `canceling`, 'unknown'.
  435. */
  436. declare type TaskState = 'notScheduled' | 'scheduling' | 'scheduled' | 'running' | 'canceling' | 'unknown';
  437. /**
  438. */
  439. interface TaskData {
  440. /**
  441. * A periodic [MacroTask] is such which get automatically rescheduled after it is executed.
  442. */
  443. isPeriodic?: boolean;
  444. /**
  445. * Delay in milliseconds when the Task will run.
  446. */
  447. delay?: number;
  448. /**
  449. * identifier returned by the native setTimeout.
  450. */
  451. handleId?: number;
  452. }
  453. /**
  454. * Represents work which is executed with a clean stack.
  455. *
  456. * Tasks are used in Zones to mark work which is performed on clean stack frame. There are three
  457. * kinds of task. [MicroTask], [MacroTask], and [EventTask].
  458. *
  459. * A JS VM can be modeled as a [MicroTask] queue, [MacroTask] queue, and [EventTask] set.
  460. *
  461. * - [MicroTask] queue represents a set of tasks which are executing right after the current stack
  462. * frame becomes clean and before a VM yield. All [MicroTask]s execute in order of insertion
  463. * before VM yield and the next [MacroTask] is executed.
  464. * - [MacroTask] queue represents a set of tasks which are executed one at a time after each VM
  465. * yield. The queue is ordered by time, and insertions can happen in any location.
  466. * - [EventTask] is a set of tasks which can at any time be inserted to the end of the [MacroTask]
  467. * queue. This happens when the event fires.
  468. *
  469. */
  470. interface Task {
  471. /**
  472. * Task type: `microTask`, `macroTask`, `eventTask`.
  473. */
  474. type: TaskType;
  475. /**
  476. * Task state: `notScheduled`, `scheduling`, `scheduled`, `running`, `canceling`, `unknown`.
  477. */
  478. state: TaskState;
  479. /**
  480. * Debug string representing the API which requested the scheduling of the task.
  481. */
  482. source: string;
  483. /**
  484. * The Function to be used by the VM upon entering the [Task]. This function will delegate to
  485. * [Zone.runTask] and delegate to `callback`.
  486. */
  487. invoke: Function;
  488. /**
  489. * Function which needs to be executed by the Task after the [Zone.currentTask] has been set to
  490. * the current task.
  491. */
  492. callback: Function;
  493. /**
  494. * Task specific options associated with the current task. This is passed to the `scheduleFn`.
  495. */
  496. data: TaskData;
  497. /**
  498. * Represents the default work which needs to be done to schedule the Task by the VM.
  499. *
  500. * A zone may choose to intercept this function and perform its own scheduling.
  501. */
  502. scheduleFn: (task: Task) => void;
  503. /**
  504. * Represents the default work which needs to be done to un-schedule the Task from the VM. Not all
  505. * Tasks are cancelable, and therefore this method is optional.
  506. *
  507. * A zone may chose to intercept this function and perform its own un-scheduling.
  508. */
  509. cancelFn: (task: Task) => void;
  510. /**
  511. * @type {Zone} The zone which will be used to invoke the `callback`. The Zone is captured
  512. * at the time of Task creation.
  513. */
  514. readonly zone: Zone;
  515. /**
  516. * Number of times the task has been executed, or -1 if canceled.
  517. */
  518. runCount: number;
  519. /**
  520. * Cancel the scheduling request. This method can be called from `ZoneSpec.onScheduleTask` to
  521. * cancel the current scheduling interception. Once canceled the task can be discarded or
  522. * rescheduled using `Zone.scheduleTask` on a different zone.
  523. */
  524. cancelScheduleRequest(): void;
  525. }
  526. interface MicroTask extends Task {
  527. type: 'microTask';
  528. }
  529. interface MacroTask extends Task {
  530. type: 'macroTask';
  531. }
  532. interface EventTask extends Task {
  533. type: 'eventTask';
  534. }
  535. declare const Zone: ZoneType;