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

Commit 648a95d

Browse files
committed
feat: refactor and test counting zone
1 parent c6202a1 commit 648a95d

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

counting-zone.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
/*
22
* See example/counting.html
33
*/
4-
zone.countingZone = {
4+
Zone.countingZone = {
55
'-onZoneCreated': function () {
6-
zone.countingZone.counter += 1;
6+
Zone.countingZone.counter += 1;
77
},
88
'+onZoneLeave': function () {
9-
zone.countingZone.counter -= 1;
10-
if (zone.countingZone.counter === 0) {
9+
Zone.countingZone.counter -= 1;
10+
if (Zone.countingZone.counter <= 0) {
11+
Zone.countingZone.counter = 0;
1112
this.onFlush();
1213
}
1314
},
14-
reset: function () {
15-
zone.countingZone.counter = 0;
15+
'-run': function () {
16+
Zone.countingZone.counter = 0;
1617
},
1718
counter: function () {
18-
return zone.countingZone.counter;
19+
return Zone.countingZone.counter;
1920
},
2021
onFlush: function () {}
2122
};

example/counting.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ <h1>Counting Pending Tasks</h1>
2626
/*
2727
* Zone that counts pending async tasks
2828
*/
29-
var myCountingZone = zone.fork(zone.countingZone).fork({
29+
var myCountingZone = zone.fork(Zone.countingZone).fork({
3030
'+onZoneCreated': function () {
31-
zone.countingZone.start || (zone.countingZone.start = Date.now());
31+
Zone.countingZone.start || (Zone.countingZone.start = Date.now());
3232
this.print();
3333
},
3434
'-onZoneLeave': function (delegate) {
3535
this.print();
3636
},
3737
'+reset': function (delegate) {
38-
zone.countingZone.start = 0;
38+
Zone.countingZone.start = 0;
3939
},
4040
print: function () {
4141
counter = this.counter();
4242
output.innerHTML = counter ?
4343
'pending task count: ' + counter :
44-
' DONE! ' + (Date.now() - zone.countingZone.start)/1000 + 's';
44+
' DONE! ' + (Date.now() - Zone.countingZone.start)/1000 + 's';
4545
}
4646
});
4747

test/counting-zone.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
4+
describe('Zone.countingZone', function () {
5+
var flushSpy = jasmine.createSpy('flush'),
6+
countingZone = zone.fork(Zone.countingZone).fork({
7+
onFlush: flushSpy
8+
});
9+
10+
beforeEach(function () {
11+
jasmine.Clock.useMock();
12+
flushSpy.reset();
13+
});
14+
15+
it('should flush at the end of a run', function () {
16+
countingZone.run(function () {});
17+
expect(flushSpy).toHaveBeenCalled();
18+
expect(countingZone.counter()).toBe(0);
19+
});
20+
21+
it('should work', function () {
22+
countingZone.run(function () {
23+
24+
setTimeout(function () {}, 0);
25+
expect(countingZone.counter()).toBe(1);
26+
27+
//jasmine.Clock.tick(0);
28+
29+
//expect(countingZone.counter()).toBe(0);
30+
});
31+
32+
//jasmine.Clock.tick(0);
33+
});
34+
});

0 commit comments

Comments
 (0)