Overview of zone.js (Monkey Patching, Forking, Interception). How its used in Angular for triggering Change Detection and a demo in NodeJS for authentication handling.
All my slides and demos can be found here: https://github.com/IliaIdakiev/slides
4. WHAT IS ZONE.JS
• Library written by the team that is inspired by DART
• It provides a mechanism for encapsulating and intercepting
asynchronous activities in the browser
5. WHY ZONE.JS
• Useful for accessing data which persist across async operations.
• Useful for frameworks/applications to be able to schedule and
intercept async tasks
• Helps us detect theVM turns
• Useful for debugging, testing and profiling
6. ZONE
• A zone is an execution context that persists across asynchronous tasks.
• It allows us to control execution of the code within the zone.
7. ZONES.JS
• The library monkey patches all the async browser APIs.
• Redirects all the async operations trough a zone of interception.
• Creates a <root> zone.
• Provides a global Zone object used for zone management.
8. MONKEY PATCH
A monkey patch is a way for a program to extend or modify
supporting system software locally (affecting only the running instance
of the program).
( wikipedia )
13. CONTEXT PROPAGATION
• A zone keeps a reference to its parent zone.
• A given stack frame can be only associated with one zone.
• Data attached to the zone is shallow-immutable.
• Child zone inherit parent zone properties.
20. ZONE DELEGATE
• A delegate which handles the interception of the current zone
methods.
• This way a child zone can invoke a method on a parent zone.
• The wrap method delegates to intercept and the run method to
invoke.
21. TASKTYPES
• MicroTask - work which will execute as ASAP on empty stack frame.A microtask is
guaranteed to run before host environment performs rendering or I/O operations.
• MacroTask - 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. Macro tasks have an implied execution order.
• EventTask - Similar to macro tasks, but unlike macro tasks they may never run.
When an EventTask is run, it pre-empts whatever the next task is the macro task
queue. Event tasks do not create a queue.
33. PERFORMANCE OPTIMIZATION
• Asynchronous tasks that don't require UI updates or error handling
to be handled by Angular can be kicked off via "runOutsideAngular".
• If needed, these tasks can reenter the Angular zone via "run".
35. FAKEASYNC
• Instead of calling the browser, it saves all the calls into an array.
• It can then emulate the passage of time by going through that array
and invoking the stored functions.