Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit b134391

Browse files
committed
feat: add exceptZone
1 parent 1515176 commit b134391

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

Diff for: example/except.html

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Except Zone</title>
6+
<link rel="stylesheet" href="style.css">
7+
<script src="../zone.js"></script>
8+
<script src="../except-zone.js"></script>
9+
</head>
10+
<body>
11+
12+
<h1>Except Zone</h1>
13+
14+
<p>We want to know about just the events outside of a given function invocation</p>
15+
16+
<button id="b1">Start Profiling</button>
17+
18+
<p id="output"></p>
19+
20+
<script>
21+
22+
23+
24+
/*
25+
* Let's say we want to know the CPU cost from some action
26+
* that includes async tasks. We can do this with zones!
27+
*/
28+
29+
/*
30+
* For this demo, we're going to sort an array using an async
31+
* algorithm when a button is pressed.
32+
*/
33+
function sortAndPrintArray (unsortedArray) {
34+
//zone.reset();
35+
asyncBogosort(unsortedArray, function (sortedArray) {
36+
console.log(sortedArray);
37+
});
38+
}
39+
40+
41+
/*
42+
* This is a really efficient algorithm.
43+
*
44+
* First, check if the array is sorted.
45+
* - If it is, call the callback
46+
* - If it isn't, randomize the array and recur
47+
*
48+
* This implementation is async because JavaScript
49+
*/
50+
function asyncBogosort (arr, cb) {
51+
setTimeout(function () {
52+
if (isSorted(arr)) {
53+
cb(arr);
54+
} else {
55+
var newArr = arr.slice(0);
56+
newArr.sort(function () {
57+
return Math.random() - 0.5;
58+
});
59+
asyncBogosort(newArr, cb);
60+
}
61+
}, 0);
62+
}
63+
64+
function isSorted (things) {
65+
for (var i = 1; i < things.length; i += 1) {
66+
if (things[i] < things[i - 1]) {
67+
return false;
68+
}
69+
}
70+
return true;
71+
}
72+
73+
74+
75+
76+
/*
77+
* This zone starts a timer at the start of each task,
78+
* and stops it at the end. It accumulates the total run
79+
* time internally, exposing it via `zone.time()`
80+
*
81+
* Note that this is the time the CPU is spending doing
82+
* bogosort, as opposed to the time from the start
83+
* of the algorithm until it's completion.
84+
*/
85+
var profilingZone = (function () {
86+
var time = 0,
87+
// use the high-res timer if available
88+
timer = performance ?
89+
performance.now.bind(performance) :
90+
Date.now.bind(Date);
91+
return {
92+
onZoneEnter: function () {
93+
this.start = timer();
94+
},
95+
onZoneLeave: function () {
96+
time += timer() - this.start;
97+
console.log('sorting took ' + zone.time() + ' of CPU time');
98+
},
99+
time: function () {
100+
return Math.floor(time*100) / 100 + 'ms';
101+
},
102+
reset: function () {
103+
time = 0;
104+
}
105+
};
106+
}());
107+
108+
/*
109+
* Zone that profiles async tasks
110+
*/
111+
var myZone = zone.fork(Zone.exceptZone).fork(profilingZone);
112+
113+
114+
/*
115+
* Bind button
116+
*/
117+
b1.addEventListener('click', function () {
118+
myZone.run(function () {
119+
var unsortedArray = [3,4,1,2,7];
120+
sortAndPrintArray(unsortedArray);
121+
});
122+
});
123+
124+
125+
/*
126+
* There may be other async actions going on in the background.
127+
* Because this is not in the zone, our profiling ignores it.
128+
* Nice.
129+
*/
130+
function noop () {
131+
setTimeout(noop, 10*Math.random());
132+
}
133+
noop();
134+
</script>
135+
136+
</body>
137+
</html>

Diff for: except-zone.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* See example/except.html
3+
*/
4+
Zone.exceptZone = {
5+
boringZone: window.zone,
6+
interestingZone: window.zone,
7+
onZoneEnter: function () {
8+
this._oldZone = window.zone;
9+
window.zone = Zone.exceptZone.boringZone;
10+
},
11+
onZoneLeave: function () {
12+
window.zone = this._oldZone;
13+
},
14+
fork: function (ops) {
15+
return window.zone = window.zone.fork(ops);
16+
}
17+
};

0 commit comments

Comments
 (0)