[6a3a178] | 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 | /// <amd-module name="angular/packages/zone.js/lib/zone" />
|
---|
| 9 | /**
|
---|
| 10 | * Suppress closure compiler errors about unknown 'global' variable
|
---|
| 11 | * @fileoverview
|
---|
| 12 | * @suppress {undefinedVars}
|
---|
| 13 | */
|
---|
| 14 | /**
|
---|
| 15 | * Zone is a mechanism for intercepting and keeping track of asynchronous work.
|
---|
| 16 | *
|
---|
| 17 | * A Zone is a global object which is configured with rules about how to intercept and keep track
|
---|
| 18 | * of the asynchronous callbacks. Zone has these responsibilities:
|
---|
| 19 | *
|
---|
| 20 | * 1. Intercept asynchronous task scheduling
|
---|
| 21 | * 2. Wrap callbacks for error-handling and zone tracking across async operations.
|
---|
| 22 | * 3. Provide a way to attach data to zones
|
---|
| 23 | * 4. Provide a context specific last frame error handling
|
---|
| 24 | * 5. (Intercept blocking methods)
|
---|
| 25 | *
|
---|
| 26 | * A zone by itself does not do anything, instead it relies on some other code to route existing
|
---|
| 27 | * platform API through it. (The zone library ships with code which monkey patches all of the
|
---|
| 28 | * browsers's asynchronous API and redirects them through the zone for interception.)
|
---|
| 29 | *
|
---|
| 30 | * In its simplest form a zone allows one to intercept the scheduling and calling of asynchronous
|
---|
| 31 | * operations, and execute additional code before as well as after the asynchronous task. The rules
|
---|
| 32 | * of interception are configured using [ZoneConfig]. There can be many different zone instances in
|
---|
| 33 | * a system, but only one zone is active at any given time which can be retrieved using
|
---|
| 34 | * [Zone#current].
|
---|
| 35 | *
|
---|
| 36 | *
|
---|
| 37 | *
|
---|
| 38 | * ## Callback Wrapping
|
---|
| 39 | *
|
---|
| 40 | * An important aspect of the zones is that they should persist across asynchronous operations. To
|
---|
| 41 | * achieve this, when a future work is scheduled through async API, it is necessary to capture, and
|
---|
| 42 | * subsequently restore the current zone. For example if a code is running in zone `b` and it
|
---|
| 43 | * invokes `setTimeout` to scheduleTask work later, the `setTimeout` method needs to 1) capture the
|
---|
| 44 | * current zone and 2) wrap the `wrapCallback` in code which will restore the current zone `b` once
|
---|
| 45 | * the wrapCallback executes. In this way the rules which govern the current code are preserved in
|
---|
| 46 | * all future asynchronous tasks. There could be a different zone `c` which has different rules and
|
---|
| 47 | * is associated with different asynchronous tasks. As these tasks are processed, each asynchronous
|
---|
| 48 | * wrapCallback correctly restores the correct zone, as well as preserves the zone for future
|
---|
| 49 | * asynchronous callbacks.
|
---|
| 50 | *
|
---|
| 51 | * Example: Suppose a browser page consist of application code as well as third-party
|
---|
| 52 | * advertisement code. (These two code bases are independent, developed by different mutually
|
---|
| 53 | * unaware developers.) The application code may be interested in doing global error handling and
|
---|
| 54 | * so it configures the `app` zone to send all of the errors to the server for analysis, and then
|
---|
| 55 | * executes the application in the `app` zone. The advertising code is interested in the same
|
---|
| 56 | * error processing but it needs to send the errors to a different third-party. So it creates the
|
---|
| 57 | * `ads` zone with a different error handler. Now both advertising as well as application code
|
---|
| 58 | * create many asynchronous operations, but the [Zone] will ensure that all of the asynchronous
|
---|
| 59 | * operations created from the application code will execute in `app` zone with its error
|
---|
| 60 | * handler and all of the advertisement code will execute in the `ads` zone with its error handler.
|
---|
| 61 | * This will not only work for the async operations created directly, but also for all subsequent
|
---|
| 62 | * asynchronous operations.
|
---|
| 63 | *
|
---|
| 64 | * If you think of chain of asynchronous operations as a thread of execution (bit of a stretch)
|
---|
| 65 | * then [Zone#current] will act as a thread local variable.
|
---|
| 66 | *
|
---|
| 67 | *
|
---|
| 68 | *
|
---|
| 69 | * ## Asynchronous operation scheduling
|
---|
| 70 | *
|
---|
| 71 | * In addition to wrapping the callbacks to restore the zone, all operations which cause a
|
---|
| 72 | * scheduling of work for later are routed through the current zone which is allowed to intercept
|
---|
| 73 | * them by adding work before or after the wrapCallback as well as using different means of
|
---|
| 74 | * achieving the request. (Useful for unit testing, or tracking of requests). In some instances
|
---|
| 75 | * such as `setTimeout` the wrapping of the wrapCallback and scheduling is done in the same
|
---|
| 76 | * wrapCallback, but there are other examples such as `Promises` where the `then` wrapCallback is
|
---|
| 77 | * wrapped, but the execution of `then` is triggered by `Promise` scheduling `resolve` work.
|
---|
| 78 | *
|
---|
| 79 | * Fundamentally there are three kinds of tasks which can be scheduled:
|
---|
| 80 | *
|
---|
| 81 | * 1. [MicroTask] used for doing work right after the current task. This is non-cancelable which is
|
---|
| 82 | * guaranteed to run exactly once and immediately.
|
---|
| 83 | * 2. [MacroTask] used for doing work later. Such as `setTimeout`. This is typically cancelable
|
---|
| 84 | * which is guaranteed to execute at least once after some well understood delay.
|
---|
| 85 | * 3. [EventTask] used for listening on some future event. This may execute zero or more times, with
|
---|
| 86 | * an unknown delay.
|
---|
| 87 | *
|
---|
| 88 | * Each asynchronous API is modeled and routed through one of these APIs.
|
---|
| 89 | *
|
---|
| 90 | *
|
---|
| 91 | * ### [MicroTask]
|
---|
| 92 | *
|
---|
| 93 | * [MicroTask]s represent work which will be done in current VM turn as soon as possible, before VM
|
---|
| 94 | * yielding.
|
---|
| 95 | *
|
---|
| 96 | *
|
---|
| 97 | * ### [MacroTask]
|
---|
| 98 | *
|
---|
| 99 | * [MacroTask]s represent work which will be done after some delay. (Sometimes the delay is
|
---|
| 100 | * approximate such as on next available animation frame). Typically these methods include:
|
---|
| 101 | * `setTimeout`, `setImmediate`, `setInterval`, `requestAnimationFrame`, and all browser specific
|
---|
| 102 | * variants.
|
---|
| 103 | *
|
---|
| 104 | *
|
---|
| 105 | * ### [EventTask]
|
---|
| 106 | *
|
---|
| 107 | * [EventTask]s represent a request to create a listener on an event. Unlike the other task
|
---|
| 108 | * events they may never be executed, but typically execute more than once. There is no queue of
|
---|
| 109 | * events, rather their callbacks are unpredictable both in order and time.
|
---|
| 110 | *
|
---|
| 111 | *
|
---|
| 112 | * ## Global Error Handling
|
---|
| 113 | *
|
---|
| 114 | *
|
---|
| 115 | * ## Composability
|
---|
| 116 | *
|
---|
| 117 | * Zones can be composed together through [Zone.fork()]. A child zone may create its own set of
|
---|
| 118 | * rules. A child zone is expected to either:
|
---|
| 119 | *
|
---|
| 120 | * 1. Delegate the interception to a parent zone, and optionally add before and after wrapCallback
|
---|
| 121 | * hooks.
|
---|
| 122 | * 2. Process the request itself without delegation.
|
---|
| 123 | *
|
---|
| 124 | * Composability allows zones to keep their concerns clean. For example a top most zone may choose
|
---|
| 125 | * to handle error handling, while child zones may choose to do user action tracking.
|
---|
| 126 | *
|
---|
| 127 | *
|
---|
| 128 | * ## Root Zone
|
---|
| 129 | *
|
---|
| 130 | * At the start the browser will run in a special root zone, which is configured to behave exactly
|
---|
| 131 | * like the platform, making any existing code which is not zone-aware behave as expected. All
|
---|
| 132 | * zones are children of the root zone.
|
---|
| 133 | *
|
---|
| 134 | */
|
---|
| 135 | interface Zone {
|
---|
| 136 | /**
|
---|
| 137 | *
|
---|
| 138 | * @returns {Zone} The parent Zone.
|
---|
| 139 | */
|
---|
| 140 | parent: Zone | null;
|
---|
| 141 | /**
|
---|
| 142 | * @returns {string} The Zone name (useful for debugging)
|
---|
| 143 | */
|
---|
| 144 | name: string;
|
---|
| 145 | /**
|
---|
| 146 | * Returns a value associated with the `key`.
|
---|
| 147 | *
|
---|
| 148 | * If the current zone does not have a key, the request is delegated to the parent zone. Use
|
---|
| 149 | * [ZoneSpec.properties] to configure the set of properties associated with the current zone.
|
---|
| 150 | *
|
---|
| 151 | * @param key The key to retrieve.
|
---|
| 152 | * @returns {any} The value for the key, or `undefined` if not found.
|
---|
| 153 | */
|
---|
| 154 | get(key: string): any;
|
---|
| 155 | /**
|
---|
| 156 | * Returns a Zone which defines a `key`.
|
---|
| 157 | *
|
---|
| 158 | * Recursively search the parent Zone until a Zone which has a property `key` is found.
|
---|
| 159 | *
|
---|
| 160 | * @param key The key to use for identification of the returned zone.
|
---|
| 161 | * @returns {Zone} The Zone which defines the `key`, `null` if not found.
|
---|
| 162 | */
|
---|
| 163 | getZoneWith(key: string): Zone | null;
|
---|
| 164 | /**
|
---|
| 165 | * Used to create a child zone.
|
---|
| 166 | *
|
---|
| 167 | * @param zoneSpec A set of rules which the child zone should follow.
|
---|
| 168 | * @returns {Zone} A new child zone.
|
---|
| 169 | */
|
---|
| 170 | fork(zoneSpec: ZoneSpec): Zone;
|
---|
| 171 | /**
|
---|
| 172 | * Wraps a callback function in a new function which will properly restore the current zone upon
|
---|
| 173 | * invocation.
|
---|
| 174 | *
|
---|
| 175 | * The wrapped function will properly forward `this` as well as `arguments` to the `callback`.
|
---|
| 176 | *
|
---|
| 177 | * Before the function is wrapped the zone can intercept the `callback` by declaring
|
---|
| 178 | * [ZoneSpec.onIntercept].
|
---|
| 179 | *
|
---|
| 180 | * @param callback the function which will be wrapped in the zone.
|
---|
| 181 | * @param source A unique debug location of the API being wrapped.
|
---|
| 182 | * @returns {function(): *} A function which will invoke the `callback` through [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 | 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 | declare 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 | 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, 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 | 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 | declare type HasTaskState = {
|
---|
| 489 | microTask: boolean;
|
---|
| 490 | macroTask: boolean;
|
---|
| 491 | eventTask: boolean;
|
---|
| 492 | change: TaskType;
|
---|
| 493 | };
|
---|
| 494 | /**
|
---|
| 495 | * Task type: `microTask`, `macroTask`, `eventTask`.
|
---|
| 496 | */
|
---|
| 497 | declare type TaskType = 'microTask' | 'macroTask' | 'eventTask';
|
---|
| 498 | /**
|
---|
| 499 | * Task type: `notScheduled`, `scheduling`, `scheduled`, `running`, `canceling`, 'unknown'.
|
---|
| 500 | */
|
---|
| 501 | declare 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;
|
---|