zone-impl.d.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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.dev/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
  31. * rules of interception are configured using [ZoneConfig]. There can be many different zone
  32. * instances in a system, but only one zone is active at any given time which can be retrieved
  33. * using [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,
  41. * and 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
  43. * the current zone and 2) wrap the `wrapCallback` in code which will restore the current zone `b`
  44. * once the wrapCallback executes. In this way the rules which govern the current code are
  45. * preserved in all future asynchronous tasks. There could be a different zone `c` which has
  46. * different rules and is associated with different asynchronous tasks. As these tasks are
  47. * processed, each asynchronous wrapCallback correctly restores the correct zone, as well as
  48. * preserves the zone for future 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
  60. * handler. This will not only work for the async operations created directly, but also for all
  61. * subsequent 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
  81. * is 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,
  85. * with 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
  93. * VM 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. export declare 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
  182. * [Zone.runGuarded].
  183. */
  184. wrap<F extends Function>(callback: F, source: string): F;
  185. /**
  186. * Invokes a function in a given zone.
  187. *
  188. * The invocation of `callback` can be intercepted by declaring [ZoneSpec.onInvoke].
  189. *
  190. * @param callback The function to invoke.
  191. * @param applyThis
  192. * @param applyArgs
  193. * @param source A unique debug location of the API being invoked.
  194. * @returns {any} Value from the `callback` function.
  195. */
  196. run<T>(callback: Function, applyThis?: any, applyArgs?: any[], source?: string): T;
  197. /**
  198. * Invokes a function in a given zone and catches any exceptions.
  199. *
  200. * Any exceptions thrown will be forwarded to [Zone.HandleError].
  201. *
  202. * The invocation of `callback` can be intercepted by declaring [ZoneSpec.onInvoke]. The
  203. * handling of exceptions can be intercepted by declaring [ZoneSpec.handleError].
  204. *
  205. * @param callback The function to invoke.
  206. * @param applyThis
  207. * @param applyArgs
  208. * @param source A unique debug location of the API being invoked.
  209. * @returns {any} Value from the `callback` function.
  210. */
  211. runGuarded<T>(callback: Function, applyThis?: any, applyArgs?: any[], source?: string): T;
  212. /**
  213. * Execute the Task by restoring the [Zone.currentTask] in the Task's zone.
  214. *
  215. * @param task to run
  216. * @param applyThis
  217. * @param applyArgs
  218. * @returns {any} Value from the `task.callback` function.
  219. */
  220. runTask<T>(task: Task, applyThis?: any, applyArgs?: any): T;
  221. /**
  222. * Schedule a MicroTask.
  223. *
  224. * @param source
  225. * @param callback
  226. * @param data
  227. * @param customSchedule
  228. */
  229. scheduleMicroTask(source: string, callback: Function, data?: TaskData, customSchedule?: (task: Task) => void): MicroTask;
  230. /**
  231. * Schedule a MacroTask.
  232. *
  233. * @param source
  234. * @param callback
  235. * @param data
  236. * @param customSchedule
  237. * @param customCancel
  238. */
  239. scheduleMacroTask(source: string, callback: Function, data?: TaskData, customSchedule?: (task: Task) => void, customCancel?: (task: Task) => void): MacroTask;
  240. /**
  241. * Schedule an EventTask.
  242. *
  243. * @param source
  244. * @param callback
  245. * @param data
  246. * @param customSchedule
  247. * @param customCancel
  248. */
  249. scheduleEventTask(source: string, callback: Function, data?: TaskData, customSchedule?: (task: Task) => void, customCancel?: (task: Task) => void): EventTask;
  250. /**
  251. * Schedule an existing Task.
  252. *
  253. * Useful for rescheduling a task which was already canceled.
  254. *
  255. * @param task
  256. */
  257. scheduleTask<T extends Task>(task: T): T;
  258. /**
  259. * Allows the zone to intercept canceling of scheduled Task.
  260. *
  261. * The interception is configured using [ZoneSpec.onCancelTask]. The default canceler invokes
  262. * the [Task.cancelFn].
  263. *
  264. * @param task
  265. * @returns {any}
  266. */
  267. cancelTask(task: Task): any;
  268. }
  269. export declare interface ZoneType {
  270. /**
  271. * @returns {Zone} Returns the current [Zone]. The only way to change
  272. * the current zone is by invoking a run() method, which will update the current zone for the
  273. * duration of the run method callback.
  274. */
  275. current: Zone;
  276. /**
  277. * @returns {Task} The task associated with the current execution.
  278. */
  279. currentTask: Task | null;
  280. /**
  281. * Verify that Zone has been correctly patched. Specifically that Promise is zone aware.
  282. */
  283. assertZonePatched(): void;
  284. /**
  285. * Return the root zone.
  286. */
  287. root: Zone;
  288. /**
  289. * load patch for specified native module, allow user to
  290. * define their own patch, user can use this API after loading zone.js
  291. */
  292. __load_patch(name: string, fn: PatchFn, ignoreDuplicate?: boolean): void;
  293. /**
  294. * Zone symbol API to generate a string with __zone_symbol__ prefix
  295. */
  296. __symbol__(name: string): string;
  297. }
  298. /**
  299. * Patch Function to allow user define their own monkey patch module.
  300. */
  301. export type PatchFn = (global: Window, Zone: ZoneType, api: ZonePrivate) => void;
  302. /**
  303. * ZonePrivate interface to provide helper method to help user implement
  304. * their own monkey patch module.
  305. */
  306. export declare interface ZonePrivate {
  307. currentZoneFrame: () => ZoneFrame;
  308. symbol: (name: string) => string;
  309. scheduleMicroTask: (task?: MicroTask) => void;
  310. onUnhandledError: (error: Error) => void;
  311. microtaskDrainDone: () => void;
  312. showUncaughtError: () => boolean;
  313. patchEventTarget: (global: any, api: ZonePrivate, apis: any[], options?: any) => boolean[];
  314. patchOnProperties: (obj: any, properties: string[] | null, prototype?: any) => void;
  315. patchThen: (ctro: Function) => void;
  316. patchMethod: (target: any, name: string, patchFn: (delegate: Function, delegateName: string, name: string) => (self: any, args: any[]) => any) => Function | null;
  317. bindArguments: (args: any[], source: string) => any[];
  318. patchMacroTask: (obj: any, funcName: string, metaCreator: (self: any, args: any[]) => any) => void;
  319. patchEventPrototype: (_global: any, api: ZonePrivate) => void;
  320. isIEOrEdge: () => boolean;
  321. ObjectDefineProperty: (o: any, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => any;
  322. ObjectGetOwnPropertyDescriptor: (o: any, p: PropertyKey) => PropertyDescriptor | undefined;
  323. ObjectCreate(o: object | null, properties?: PropertyDescriptorMap & ThisType<any>): any;
  324. ArraySlice(start?: number, end?: number): any[];
  325. patchClass: (className: string) => void;
  326. wrapWithCurrentZone: (callback: any, source: string) => any;
  327. filterProperties: (target: any, onProperties: string[], ignoreProperties: any[]) => string[];
  328. attachOriginToPatched: (target: any, origin: any) => void;
  329. _redefineProperty: (target: any, callback: string, desc: any) => void;
  330. nativeScheduleMicroTask: (func: Function) => void;
  331. patchCallbacks: (api: ZonePrivate, target: any, targetName: string, method: string, callbacks: string[]) => void;
  332. getGlobalObjects: () => {
  333. globalSources: any;
  334. zoneSymbolEventNames: any;
  335. eventNames: string[];
  336. isBrowser: boolean;
  337. isMix: boolean;
  338. isNode: boolean;
  339. TRUE_STR: string;
  340. FALSE_STR: string;
  341. ZONE_SYMBOL_PREFIX: string;
  342. ADD_EVENT_LISTENER_STR: string;
  343. REMOVE_EVENT_LISTENER_STR: string;
  344. } | undefined;
  345. }
  346. /**
  347. * ZoneFrame represents zone stack frame information
  348. */
  349. export declare interface ZoneFrame {
  350. parent: ZoneFrame | null;
  351. zone: Zone;
  352. }
  353. export declare interface UncaughtPromiseError extends Error {
  354. zone: Zone;
  355. task: Task;
  356. promise: Promise<any>;
  357. rejection: any;
  358. throwOriginal?: boolean;
  359. }
  360. /**
  361. * Provides a way to configure the interception of zone events.
  362. *
  363. * Only the `name` property is required (all other are optional).
  364. */
  365. export declare interface ZoneSpec {
  366. /**
  367. * The name of the zone. Useful when debugging Zones.
  368. */
  369. name: string;
  370. /**
  371. * A set of properties to be associated with Zone. Use [Zone.get] to retrieve them.
  372. */
  373. properties?: {
  374. [key: string]: any;
  375. };
  376. /**
  377. * Allows the interception of zone forking.
  378. *
  379. * When the zone is being forked, the request is forwarded to this method for interception.
  380. *
  381. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  382. * @param currentZone The current [Zone] where the current interceptor has been declared.
  383. * @param targetZone The [Zone] which originally received the request.
  384. * @param zoneSpec The argument passed into the `fork` method.
  385. */
  386. onFork?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, zoneSpec: ZoneSpec) => Zone;
  387. /**
  388. * Allows interception of the wrapping of the callback.
  389. *
  390. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  391. * @param currentZone The current [Zone] where the current interceptor has been declared.
  392. * @param targetZone The [Zone] which originally received the request.
  393. * @param delegate The argument passed into the `wrap` method.
  394. * @param source The argument passed into the `wrap` method.
  395. */
  396. onIntercept?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function, source: string) => Function;
  397. /**
  398. * Allows interception of the callback invocation.
  399. *
  400. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  401. * @param currentZone The current [Zone] where the current interceptor has been declared.
  402. * @param targetZone The [Zone] which originally received the request.
  403. * @param delegate The argument passed into the `run` method.
  404. * @param applyThis The argument passed into the `run` method.
  405. * @param applyArgs The argument passed into the `run` method.
  406. * @param source The argument passed into the `run` method.
  407. */
  408. onInvoke?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function, applyThis: any, applyArgs?: any[], source?: string) => any;
  409. /**
  410. * Allows interception of the error handling.
  411. *
  412. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  413. * @param currentZone The current [Zone] where the current interceptor has been declared.
  414. * @param targetZone The [Zone] which originally received the request.
  415. * @param error The argument passed into the `handleError` method.
  416. */
  417. onHandleError?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any) => boolean;
  418. /**
  419. * Allows interception of task scheduling.
  420. *
  421. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  422. * @param currentZone The current [Zone] where the current interceptor has been declared.
  423. * @param targetZone The [Zone] which originally received the request.
  424. * @param task The argument passed into the `scheduleTask` method.
  425. */
  426. onScheduleTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) => Task;
  427. onInvokeTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task, applyThis: any, applyArgs?: any[]) => any;
  428. /**
  429. * Allows interception of task cancellation.
  430. *
  431. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  432. * @param currentZone The current [Zone] where the current interceptor has been declared.
  433. * @param targetZone The [Zone] which originally received the request.
  434. * @param task The argument passed into the `cancelTask` method.
  435. */
  436. onCancelTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) => any;
  437. /**
  438. * Notifies of changes to the task queue empty status.
  439. *
  440. * @param parentZoneDelegate Delegate which performs the parent [ZoneSpec] operation.
  441. * @param currentZone The current [Zone] where the current interceptor has been declared.
  442. * @param targetZone The [Zone] which originally received the request.
  443. * @param hasTaskState
  444. */
  445. onHasTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, hasTaskState: HasTaskState) => void;
  446. }
  447. /**
  448. * A delegate when intercepting zone operations.
  449. *
  450. * A ZoneDelegate is needed because a child zone can't simply invoke a method on a parent zone.
  451. * For example a child zone wrap can't just call parent zone wrap. Doing so would create a
  452. * callback which is bound to the parent zone. What we are interested in is intercepting the
  453. * callback before it is bound to any zone. Furthermore, we also need to pass the targetZone (zone
  454. * which received the original request) to the delegate.
  455. *
  456. * The ZoneDelegate methods mirror those of Zone with an addition of extra targetZone argument in
  457. * the method signature. (The original Zone which received the request.) Some methods are renamed
  458. * to prevent confusion, because they have slightly different semantics and arguments.
  459. *
  460. * - `wrap` => `intercept`: The `wrap` method delegates to `intercept`. The `wrap` method returns
  461. * a callback which will run in a given zone, where as intercept allows wrapping the callback
  462. * so that additional code can be run before and after, but does not associate the callback
  463. * with the zone.
  464. * - `run` => `invoke`: The `run` method delegates to `invoke` to perform the actual execution of
  465. * the callback. The `run` method switches to new zone; saves and restores the `Zone.current`;
  466. * and optionally performs error handling. The invoke is not responsible for error handling,
  467. * or zone management.
  468. *
  469. * Not every method is usually overwritten in the child zone, for this reason the ZoneDelegate
  470. * stores the closest zone which overwrites this behavior along with the closest ZoneSpec.
  471. *
  472. * NOTE: We have tried to make this API analogous to Event bubbling with target and current
  473. * properties.
  474. *
  475. * Note: The ZoneDelegate treats ZoneSpec as class. This allows the ZoneSpec to use its `this` to
  476. * store internal state.
  477. */
  478. export declare interface ZoneDelegate {
  479. zone: Zone;
  480. fork(targetZone: Zone, zoneSpec: ZoneSpec): Zone;
  481. intercept(targetZone: Zone, callback: Function, source: string): Function;
  482. invoke(targetZone: Zone, callback: Function, applyThis?: any, applyArgs?: any[], source?: string): any;
  483. handleError(targetZone: Zone, error: any): boolean;
  484. scheduleTask(targetZone: Zone, task: Task): Task;
  485. invokeTask(targetZone: Zone, task: Task, applyThis?: any, applyArgs?: any[]): any;
  486. cancelTask(targetZone: Zone, task: Task): any;
  487. hasTask(targetZone: Zone, isEmpty: HasTaskState): void;
  488. }
  489. export type HasTaskState = {
  490. microTask: boolean;
  491. macroTask: boolean;
  492. eventTask: boolean;
  493. change: TaskType;
  494. };
  495. /**
  496. * Task type: `microTask`, `macroTask`, `eventTask`.
  497. */
  498. export type TaskType = 'microTask' | 'macroTask' | 'eventTask';
  499. /**
  500. * Task type: `notScheduled`, `scheduling`, `scheduled`, `running`, `canceling`, 'unknown'.
  501. */
  502. export type TaskState = 'notScheduled' | 'scheduling' | 'scheduled' | 'running' | 'canceling' | 'unknown';
  503. /**
  504. */
  505. export declare interface TaskData {
  506. /**
  507. * A periodic [MacroTask] is such which get automatically rescheduled after it is executed.
  508. */
  509. isPeriodic?: boolean;
  510. /**
  511. * A [MacroTask] that can be manually rescheduled.
  512. */
  513. isRefreshable?: boolean;
  514. /**
  515. * Delay in milliseconds when the Task will run.
  516. */
  517. delay?: number;
  518. /**
  519. * identifier returned by the native setTimeout.
  520. */
  521. handleId?: number;
  522. /** The target handler. */
  523. handle?: any;
  524. }
  525. /**
  526. * Represents work which is executed with a clean stack.
  527. *
  528. * Tasks are used in Zones to mark work which is performed on clean stack frame. There are three
  529. * kinds of task. [MicroTask], [MacroTask], and [EventTask].
  530. *
  531. * A JS VM can be modeled as a [MicroTask] queue, [MacroTask] queue, and [EventTask] set.
  532. *
  533. * - [MicroTask] queue represents a set of tasks which are executing right after the current stack
  534. * frame becomes clean and before a VM yield. All [MicroTask]s execute in order of insertion
  535. * before VM yield and the next [MacroTask] is executed.
  536. * - [MacroTask] queue represents a set of tasks which are executed one at a time after each VM
  537. * yield. The queue is ordered by time, and insertions can happen in any location.
  538. * - [EventTask] is a set of tasks which can at any time be inserted to the end of the [MacroTask]
  539. * queue. This happens when the event fires.
  540. *
  541. */
  542. export declare interface Task {
  543. /**
  544. * Task type: `microTask`, `macroTask`, `eventTask`.
  545. */
  546. type: TaskType;
  547. /**
  548. * Task state: `notScheduled`, `scheduling`, `scheduled`, `running`, `canceling`, `unknown`.
  549. */
  550. state: TaskState;
  551. /**
  552. * Debug string representing the API which requested the scheduling of the task.
  553. */
  554. source: string;
  555. /**
  556. * The Function to be used by the VM upon entering the [Task]. This function will delegate to
  557. * [Zone.runTask] and delegate to `callback`.
  558. */
  559. invoke: Function;
  560. /**
  561. * Function which needs to be executed by the Task after the [Zone.currentTask] has been set to
  562. * the current task.
  563. */
  564. callback: Function;
  565. /**
  566. * Task specific options associated with the current task. This is passed to the `scheduleFn`.
  567. */
  568. data?: TaskData;
  569. /**
  570. * Represents the default work which needs to be done to schedule the Task by the VM.
  571. *
  572. * A zone may choose to intercept this function and perform its own scheduling.
  573. */
  574. scheduleFn?: (task: Task) => void;
  575. /**
  576. * Represents the default work which needs to be done to un-schedule the Task from the VM. Not
  577. * all Tasks are cancelable, and therefore this method is optional.
  578. *
  579. * A zone may chose to intercept this function and perform its own un-scheduling.
  580. */
  581. cancelFn?: (task: Task) => void;
  582. /**
  583. * @type {Zone} The zone which will be used to invoke the `callback`. The Zone is captured
  584. * at the time of Task creation.
  585. */
  586. readonly zone: Zone;
  587. /**
  588. * Number of times the task has been executed, or -1 if canceled.
  589. */
  590. runCount: number;
  591. /**
  592. * Cancel the scheduling request. This method can be called from `ZoneSpec.onScheduleTask` to
  593. * cancel the current scheduling interception. Once canceled the task can be discarded or
  594. * rescheduled using `Zone.scheduleTask` on a different zone.
  595. */
  596. cancelScheduleRequest(): void;
  597. }
  598. export declare interface MicroTask extends Task {
  599. type: 'microTask';
  600. }
  601. export declare interface MacroTask extends Task {
  602. type: 'macroTask';
  603. }
  604. export declare interface EventTask extends Task {
  605. type: 'eventTask';
  606. }
  607. export type AmbientZone = Zone;
  608. export declare function __symbol__(name: string): string;
  609. export declare function initZone(): ZoneType;