Name: zone.js
Owner: meteorhacks
Description: Implements Zones for JavaScript
Created: 2014-07-06 14:20:41.0
Updated: 2014-07-04 00:08:08.0
Pushed: 2014-06-12 19:03:35.0
Homepage: https://github.com/angular/zone.js
Size: 987
Language: null
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Implements Zones for JavaScript, inspired by Dart.
A Zone is an execution context that persists across async tasks. You can think of it as thread-local storage for JavaScript VMs.
See this video from ng-conf 2014 for a detailed explanation:
You can run code within a zone with zone.run
.
Tasks scheduled (with setTimeout
, setInterval
, or event listeners) stay within that zone.
.run(function () {
ne.inTheZone = true;
tTimeout(function () {
console.log('in the zone: ' + !!zone.inTheZone);
0);
ole.log('in the zone: ' + !!zone.inTheZone);
The above will log:
the zone: false'
the zone: true'
Note that the function delayed by setTimeout
stays inside the zone.
Zones have a set of hooks that allow you to change the behavior of code running within that zone. To change a zone, you fork it to get a new one.
.fork({
foreTask: function () {
console.log('hi');
un(function () {
do stuff
Hooks that you don't override when forking a zone are inherited from the existing one.
See the API docs below for more.
To start using Zones, you need to include the zone.js
script in this package onto
your page. This script should appear in the <head>
of your HTML file before any other
scripts, including shims/polyfills.
There are two kinds of examples:
For fully working examples:
python -m SimpleHTTPServer 3000
).http://localhost:3000/example
in your browserBelow are the aforementioned snippets.
Run some function at the end of each VM turn:
.fork({
terTask: function () {
// do some cleanup
un(function () {
do stuff
someZone = zone.fork({
terTask: function () {
console.log('goodbye');
Zone.fork({
terTask: function () {
console.log('cya l8r');
un(function () {
do stuff
ogs: cya l8r
When you fork a zone, you'll often want to control how the parent zone's hook gets called.
Prefixing a hook with $
means that the hook will be passed the
parent zone's hook, and the hook will be expected to return the function to
be invoked rather than be the function itself.
someZone = zone.fork({
terTask: function () {
console.log('goodbye');
Zone.fork({
fterTask: function (parentOnLeave) {
// return the hook
return function afterTask() {
parentOnLeave();
console.log('cya l8r');
};
un(function () {
do stuff
ogs: goodbye
cya l8r
+
and -
SugarMost of the time, you'll want to run a hook before or after the parent's implementation.
You can prefix a hook with -
for running before, and +
for running after.
The above can be written like this:
someZone = zone.fork({
terTask: function () {
console.log('goodbye');
Zone.fork({
afterTask': function (parentOnLeave) {
console.log('cya l8r');
un(function () {
do stuff
ogs: goodbye
cya l8r
This frees you from writing boilerplate to compose a new hook.
Zone.js exports a single object: window.zone
.
zone.run
Runs a given function within the zone. Explained above.
zone.bind
Transforms a function to run within the given zone.
zone.fork
.fork({
foreTask: function () {},
terTask: function () {},
Error: function () {},
tTimeout: function () {},
tInterval: function () {},
ert: function () {},
ompt: function () {},
dEventListener: function () {}
ne.run(function () {
woo!
Below describes the behavior of each of these hooks.
zone.onZoneCreated
Runs when a zone is forked.
zone.beforeTask
Before a function invoked with zone.run
, this hook runs.
If zone.beforeTask
throws, the function passed to run
will not be invoked.
zone.afterTask
After a function in a zone runs, the afterTask
hook runs.
This hook will run even if the function passed to run
throws.
zone.onError
This hook is called when the function passed to run
or the beforeTask
hook throws.
zone.enqueueTask
This hook is called when a function is registered with the VM.
For instance setTimeout
and addEventListener
.
zone.dequeueTask
This hook is called when a function is unregistered with the VM.
For instance clearTimeout
and removeEventListener
.
zone.setTimeout
, zone.setInterval
, zone.alert
, zone.prompt
These hooks allow you to change the behavior of window.setTimeout
, window.setInterval
, etc.
While in this zone, calls to window.setTimeout
will redirect to zone.setTimeout
.
zone.addEventListener
This hook allows you to intercept calls to EventTarget.addEventListener
.
setTimeout
, setInterval
, and addEventListener
work in FF23, IE10, and Chrome.elt.onevent
works in FF23, IE10, but not Chrome. There's a fix in the works though!Apache 2.0