zone.js.d.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /**
  2. * @license
  3. * Copyright Google LLC 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. * ### [MacroTask]
  97. *
  98. * [MacroTask]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 | null;
  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 | null;
  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 {any} Value from the `task.callback` function.
  218. */
  219. runTask<T>(task: Task, applyThis?: any, applyArgs?: any): T;
  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 | null;
  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. * load patch for specified native module, allow user to
  289. * define their own patch, user can use this API after loading zone.js
  290. */
  291. __load_patch(name: string, fn: _PatchFn, ignoreDuplicate?: boolean): void;
  292. /**
  293. * Zone symbol API to generate a string with __zone_symbol__ prefix
  294. */
  295. __symbol__(name: string): string;
  296. }
  297. /**
  298. * Patch Function to allow user define their own monkey patch module.
  299. */
  300. type _PatchFn = (global: Window, Zone: ZoneType, api: _ZonePrivate) => void;
  301. /**
  302. * _ZonePrivate interface to provide helper method to help user implement
  303. * their own monkey patch module.
  304. */
  305. interface _ZonePrivate {
  306. currentZoneFrame: () => _ZoneFrame;
  307. symbol: (name: string) => string;
  308. scheduleMicroTask: (task?: MicroTask) => void;
  309. onUnhandledError: (error: Error) => void;
  310. microtaskDrainDone: () => void;
  311. showUncaughtError: () => boolean;
  312. patchEventTarget: (global: any, api: _ZonePrivate, apis: any[], options?: any) => boolean[];
  313. patchOnProperties: (obj: any, properties: string[] | null, prototype?: any) => void;
  314. patchThen: (ctro: Function) => void;
  315. patchMethod: (target: any, name: string, patchFn: (delegate: Function, delegateName: string, name: string) => (self: any, args: any[]) => any) => Function | null;
  316. bindArguments: (args: any[], source: string) => any[];
  317. patchMacroTask: (obj: any, funcName: string, metaCreator: (self: any, args: any[]) => any) => void;
  318. patchEventPrototype: (_global: any, api: _ZonePrivate) => void;
  319. isIEOrEdge: () => boolean;
  320. ObjectDefineProperty: (o: any, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => any;
  321. ObjectGetOwnPropertyDescriptor: (o: any, p: PropertyKey) => PropertyDescriptor | undefined;
  322. ObjectCreate(o: object | null, properties?: PropertyDescriptorMap & ThisType<any>): any;
  323. ArraySlice(start?: number, end?: number): any[];
  324. patchClass: (className: string) => void;
  325. wrapWithCurrentZone: (callback: any, source: string) => any;
  326. filterProperties: (target: any, onProperties: string[], ignoreProperties: any[]) => string[];
  327. attachOriginToPatched: (target: any, origin: any) => void;
  328. _redefineProperty: (target: any, callback: string, desc: any) => void;
  329. nativeScheduleMicroTask: (func: Function) => void;
  330. patchCallbacks: (api: _ZonePrivate, target: any, targetName: string, method: string, callbacks: string[]) => void;
  331. getGlobalObjects: () => {
  332. globalSources: any;
  333. zoneSymbolEventNames: any;
  334. eventNames: string[];
  335. isBrowser: boolean;
  336. isMix: boolean;
  337. isNode: boolean;
  338. TRUE_STR: string;
  339. FALSE_STR: string;
  340. ZONE_SYMBOL_PREFIX: string;
  341. ADD_EVENT_LISTENER_STR: string;
  342. REMOVE_EVENT_LISTENER_STR: string;
  343. } | undefined;
  344. }
  345. /**
  346. * _ZoneFrame represents zone stack frame information
  347. */
  348. interface _ZoneFrame {
  349. parent: _ZoneFrame | null;
  350. zone: Zone;
  351. }
  352. interface UncaughtPromiseError extends Error {
  353. zone: Zone;
  354. task: Task;
  355. promise: Promise<any>;
  356. rejection: any;
  357. throwOriginal?: boolean;
  358. }
  359. /**
  360. * Provides a way to configure the interception of zone events.
  361. *
  362. * Only the `name` property is required (all other are optional).
  363. */
  364. interface ZoneSpec {
  365. /**
  366. * The name of the zone. Useful when debugging Zones.
  367. */
  368. name: string;
  369. /**
  370. * A set of properties to be associated with Zone. Use [Zone.get] to retrieve them.
  371. */
  372. properties?: {
  373. [key: string]: any;
  374. };
  375. /**
  376. * Allows the interception of zone forking.
  377. *
  378. * When the zone is being forked, the request is forwarded to this method for interception.
  379. *
  380. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  381. * @param currentZone The current [Zone] where the current interceptor has been declared.
  382. * @param targetZone The [Zone] which originally received the request.
  383. * @param zoneSpec The argument passed into the `fork` method.
  384. */
  385. onFork?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, zoneSpec: ZoneSpec) => Zone;
  386. /**
  387. * Allows interception of the wrapping of the callback.
  388. *
  389. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  390. * @param currentZone The current [Zone] where the current interceptor has been declared.
  391. * @param targetZone The [Zone] which originally received the request.
  392. * @param delegate The argument passed into the `wrap` method.
  393. * @param source The argument passed into the `wrap` method.
  394. */
  395. onIntercept?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function, source: string) => Function;
  396. /**
  397. * Allows interception of the callback invocation.
  398. *
  399. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  400. * @param currentZone The current [Zone] where the current interceptor has been declared.
  401. * @param targetZone The [Zone] which originally received the request.
  402. * @param delegate The argument passed into the `run` method.
  403. * @param applyThis The argument passed into the `run` method.
  404. * @param applyArgs The argument passed into the `run` method.
  405. * @param source The argument passed into the `run` method.
  406. */
  407. onInvoke?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function, applyThis: any, applyArgs?: any[], source?: string) => any;
  408. /**
  409. * Allows interception of the error handling.
  410. *
  411. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  412. * @param currentZone The current [Zone] where the current interceptor has been declared.
  413. * @param targetZone The [Zone] which originally received the request.
  414. * @param error The argument passed into the `handleError` method.
  415. */
  416. onHandleError?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any) => boolean;
  417. /**
  418. * Allows interception of task scheduling.
  419. *
  420. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  421. * @param currentZone The current [Zone] where the current interceptor has been declared.
  422. * @param targetZone The [Zone] which originally received the request.
  423. * @param task The argument passed into the `scheduleTask` method.
  424. */
  425. onScheduleTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) => Task;
  426. onInvokeTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task, applyThis: any, applyArgs?: any[]) => any;
  427. /**
  428. * Allows interception of task cancellation.
  429. *
  430. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  431. * @param currentZone The current [Zone] where the current interceptor has been declared.
  432. * @param targetZone The [Zone] which originally received the request.
  433. * @param task The argument passed into the `cancelTask` method.
  434. */
  435. onCancelTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) => any;
  436. /**
  437. * Notifies of changes to the task queue empty status.
  438. *
  439. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  440. * @param currentZone The current [Zone] where the current interceptor has been declared.
  441. * @param targetZone The [Zone] which originally received the request.
  442. * @param hasTaskState
  443. */
  444. onHasTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, hasTaskState: HasTaskState) => void;
  445. }
  446. /**
  447. * A delegate when intercepting zone operations.
  448. *
  449. * A ZoneDelegate is needed because a child zone can't simply invoke a method on a parent zone. For
  450. * example a child zone wrap can't just call parent zone wrap. Doing so would create a callback
  451. * which is bound to the parent zone. What we are interested in is intercepting the callback before
  452. * it is bound to any zone. Furthermore, we also need to pass the targetZone (zone which received
  453. * the original request) to the delegate.
  454. *
  455. * The ZoneDelegate methods mirror those of Zone with an addition of extra targetZone argument in
  456. * the method signature. (The original Zone which received the request.) Some methods are renamed
  457. * to prevent confusion, because they have slightly different semantics and arguments.
  458. *
  459. * - `wrap` => `intercept`: The `wrap` method delegates to `intercept`. The `wrap` method returns
  460. * a callback which will run in a given zone, where as intercept allows wrapping the callback
  461. * so that additional code can be run before and after, but does not associate the callback
  462. * with the zone.
  463. * - `run` => `invoke`: The `run` method delegates to `invoke` to perform the actual execution of
  464. * the callback. The `run` method switches to new zone; saves and restores the `Zone.current`;
  465. * and optionally performs error handling. The invoke is not responsible for error handling,
  466. * or zone management.
  467. *
  468. * Not every method is usually overwritten in the child zone, for this reason the ZoneDelegate
  469. * stores the closest zone which overwrites this behavior along with the closest ZoneSpec.
  470. *
  471. * NOTE: We have tried to make this API analogous to Event bubbling with target and current
  472. * properties.
  473. *
  474. * Note: The ZoneDelegate treats ZoneSpec as class. This allows the ZoneSpec to use its `this` to
  475. * store internal state.
  476. */
  477. interface ZoneDelegate {
  478. zone: Zone;
  479. fork(targetZone: Zone, zoneSpec: ZoneSpec): Zone;
  480. intercept(targetZone: Zone, callback: Function, source: string): Function;
  481. invoke(targetZone: Zone, callback: Function, applyThis?: any, applyArgs?: any[], source?: string): any;
  482. handleError(targetZone: Zone, error: any): boolean;
  483. scheduleTask(targetZone: Zone, task: Task): Task;
  484. invokeTask(targetZone: Zone, task: Task, applyThis?: any, applyArgs?: any[]): any;
  485. cancelTask(targetZone: Zone, task: Task): any;
  486. hasTask(targetZone: Zone, isEmpty: HasTaskState): void;
  487. }
  488. type HasTaskState = {
  489. microTask: boolean;
  490. macroTask: boolean;
  491. eventTask: boolean;
  492. change: TaskType;
  493. };
  494. /**
  495. * Task type: `microTask`, `macroTask`, `eventTask`.
  496. */
  497. type TaskType = 'microTask' | 'macroTask' | 'eventTask';
  498. /**
  499. * Task type: `notScheduled`, `scheduling`, `scheduled`, `running`, `canceling`, 'unknown'.
  500. */
  501. type TaskState = 'notScheduled' | 'scheduling' | 'scheduled' | 'running' | 'canceling' | 'unknown';
  502. /**
  503. */
  504. interface TaskData {
  505. /**
  506. * A periodic [MacroTask] is such which get automatically rescheduled after it is executed.
  507. */
  508. isPeriodic?: boolean;
  509. /**
  510. * Delay in milliseconds when the Task will run.
  511. */
  512. delay?: number;
  513. /**
  514. * identifier returned by the native setTimeout.
  515. */
  516. handleId?: number;
  517. }
  518. /**
  519. * Represents work which is executed with a clean stack.
  520. *
  521. * Tasks are used in Zones to mark work which is performed on clean stack frame. There are three
  522. * kinds of task. [MicroTask], [MacroTask], and [EventTask].
  523. *
  524. * A JS VM can be modeled as a [MicroTask] queue, [MacroTask] queue, and [EventTask] set.
  525. *
  526. * - [MicroTask] queue represents a set of tasks which are executing right after the current stack
  527. * frame becomes clean and before a VM yield. All [MicroTask]s execute in order of insertion
  528. * before VM yield and the next [MacroTask] is executed.
  529. * - [MacroTask] queue represents a set of tasks which are executed one at a time after each VM
  530. * yield. The queue is ordered by time, and insertions can happen in any location.
  531. * - [EventTask] is a set of tasks which can at any time be inserted to the end of the [MacroTask]
  532. * queue. This happens when the event fires.
  533. *
  534. */
  535. interface Task {
  536. /**
  537. * Task type: `microTask`, `macroTask`, `eventTask`.
  538. */
  539. type: TaskType;
  540. /**
  541. * Task state: `notScheduled`, `scheduling`, `scheduled`, `running`, `canceling`, `unknown`.
  542. */
  543. state: TaskState;
  544. /**
  545. * Debug string representing the API which requested the scheduling of the task.
  546. */
  547. source: string;
  548. /**
  549. * The Function to be used by the VM upon entering the [Task]. This function will delegate to
  550. * [Zone.runTask] and delegate to `callback`.
  551. */
  552. invoke: Function;
  553. /**
  554. * Function which needs to be executed by the Task after the [Zone.currentTask] has been set to
  555. * the current task.
  556. */
  557. callback: Function;
  558. /**
  559. * Task specific options associated with the current task. This is passed to the `scheduleFn`.
  560. */
  561. data?: TaskData;
  562. /**
  563. * Represents the default work which needs to be done to schedule the Task by the VM.
  564. *
  565. * A zone may choose to intercept this function and perform its own scheduling.
  566. */
  567. scheduleFn?: (task: Task) => void;
  568. /**
  569. * Represents the default work which needs to be done to un-schedule the Task from the VM. Not all
  570. * Tasks are cancelable, and therefore this method is optional.
  571. *
  572. * A zone may chose to intercept this function and perform its own un-scheduling.
  573. */
  574. cancelFn?: (task: Task) => void;
  575. /**
  576. * @type {Zone} The zone which will be used to invoke the `callback`. The Zone is captured
  577. * at the time of Task creation.
  578. */
  579. readonly zone: Zone;
  580. /**
  581. * Number of times the task has been executed, or -1 if canceled.
  582. */
  583. runCount: number;
  584. /**
  585. * Cancel the scheduling request. This method can be called from `ZoneSpec.onScheduleTask` to
  586. * cancel the current scheduling interception. Once canceled the task can be discarded or
  587. * rescheduled using `Zone.scheduleTask` on a different zone.
  588. */
  589. cancelScheduleRequest(): void;
  590. }
  591. interface MicroTask extends Task {
  592. type: 'microTask';
  593. }
  594. interface MacroTask extends Task {
  595. type: 'macroTask';
  596. }
  597. interface EventTask extends Task {
  598. type: 'eventTask';
  599. }
  600. declare const Zone: ZoneType;