This document provides an in-depth overview of Zone.js and how it works. It discusses:
- The event loop and how Zone.js intercepts asynchronous tasks like setTimeout and promises.
- How zones provide execution contexts and how Zone.js uses zones to observe and control code execution.
- How zones can be forked to create child zones and intercepted using hooks to monitor asynchronous tasks.
- How Zone.js monkey patches browser APIs and schedules tasks through the current zone rather than calling APIs directly.
39. ! Micro Tasks - A microtask is work which will execute as soon as possible on empty stack frame. A
microtask is guaranteed to run before host environment performs rendering or I/O operations. A
microtask queue must be empty before another MacroTask or EventTask runs.
(i.e. Promise.then() executes in microtask)
! Macro Tasks - Macro tasks are interleaved with rendering and I/O operations of the host environment.
(ie setTimeout, setInterval, etc..). Macro tasks are guaranteed to run at least once or canceled
(some can run repeatedly such as setInterval). Macro tasks have an implied execution order.
! Event Tasks - Event tasks are similar to macro tasks, but unlike macro tasks they may never
run. Event tasks are scheduled using addEventListener('click', eventCallback), or similar mechanisms.
Tasks
41. A zone is an execution context that persists across async tasks, and allows
the creator of the zone to observe and control the execution of the code
within the zone.
Zone
43. ! Knowing when a task has executed and when the micro task queue is empty allows frameworks to know
when it’s time to re-render the UIs.
! Enforcing that no tasks are scheduled allows test frameworks to ensure that the tests are synchronous and
fast (controlling time).
! Tracking when all scheduled tasks are executed allows a test framework to know when an async test has
completed.
! Long stack traces between async operations.
! Measuring latency for user operations (Profiling).
! And many more…
Use Cases
45. The zone library ships with code which monkey patches all of the browsers's
asynchronous API and redirects them through the zone for interception.
Zone.js
61. ! Each stack frame is associated with one zone (execution context).
! Data attached to the zone is shallow immutable.
! Once the zone is forked its properties cannot be changed.
! Child zones inherit parent zones properties (we can use them for communication).
Zones Basics Summary
74. ! We can intercept zone.run using the onInvoke hook and delegate.invoke inside.
! We can intercept zone.wrap using the onIntercept hook and delegate.intercept inside.
! We can intercept zone.form using the onFork hook and delegate.fork inside.
! We can’t use inheritance because the Zone.current is dynamic and we can’t know it on define time so we
need to use hooks and delegates.
! We can’t simply call a parent zone method because doing so would create a callback which is bound to
the parent zone and we are interested in intercepting the callback before it is bound to any zone so we
need to use hooks and delegates.
Zones Interception Summary
75. ! Zone composition
! Observability of task execution
! Centralised error handling
Zones Interception Benefits
97. ! We can scheduleMacroTask, scheduleMicroTask, scheduleEventTask and
scheduleTask (mostly useful for existing task when they were cancelled and we want to
reschedule them).
! We can intercept task scheduling with onScheduleTask, onInvokeTask, onCancelTask,
onHasTask, onHandleError.
! Task Lifecycle
Zones Tasks Summary
99. ! We can’t use inheritance because the Zone.current is dynamic and we can’t know it on
define time so we need to use hooks and delegates.
! We can’t simply call a parent zone method because doing so would create a callback
which is bound to the parent zone and we are interested in intercepting the callback before it is
bound to any zone so we need to use hooks and delegates.
Why do we need hooks and delegates?