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

Commit

Permalink
feat: refactor and test counting zone
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Mar 29, 2014
1 parent c6202a1 commit 648a95d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
15 changes: 8 additions & 7 deletions counting-zone.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
/*
* See example/counting.html
*/
zone.countingZone = {
Zone.countingZone = {
'-onZoneCreated': function () {
zone.countingZone.counter += 1;
Zone.countingZone.counter += 1;
},
'+onZoneLeave': function () {
zone.countingZone.counter -= 1;
if (zone.countingZone.counter === 0) {
Zone.countingZone.counter -= 1;
if (Zone.countingZone.counter <= 0) {
Zone.countingZone.counter = 0;
this.onFlush();
}
},
reset: function () {
zone.countingZone.counter = 0;
'-run': function () {
Zone.countingZone.counter = 0;
},
counter: function () {
return zone.countingZone.counter;
return Zone.countingZone.counter;
},
onFlush: function () {}
};
8 changes: 4 additions & 4 deletions example/counting.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ <h1>Counting Pending Tasks</h1>
/*
* Zone that counts pending async tasks
*/
var myCountingZone = zone.fork(zone.countingZone).fork({
var myCountingZone = zone.fork(Zone.countingZone).fork({
'+onZoneCreated': function () {
zone.countingZone.start || (zone.countingZone.start = Date.now());
Zone.countingZone.start || (Zone.countingZone.start = Date.now());
this.print();
},
'-onZoneLeave': function (delegate) {
this.print();
},
'+reset': function (delegate) {
zone.countingZone.start = 0;
Zone.countingZone.start = 0;
},
print: function () {
counter = this.counter();
output.innerHTML = counter ?
'pending task count: ' + counter :
' DONE! ' + (Date.now() - zone.countingZone.start)/1000 + 's';
' DONE! ' + (Date.now() - Zone.countingZone.start)/1000 + 's';
}
});

Expand Down
34 changes: 34 additions & 0 deletions test/counting-zone.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';


describe('Zone.countingZone', function () {
var flushSpy = jasmine.createSpy('flush'),
countingZone = zone.fork(Zone.countingZone).fork({
onFlush: flushSpy
});

beforeEach(function () {
jasmine.Clock.useMock();
flushSpy.reset();
});

it('should flush at the end of a run', function () {
countingZone.run(function () {});
expect(flushSpy).toHaveBeenCalled();
expect(countingZone.counter()).toBe(0);
});

it('should work', function () {
countingZone.run(function () {

setTimeout(function () {}, 0);
expect(countingZone.counter()).toBe(1);

//jasmine.Clock.tick(0);

//expect(countingZone.counter()).toBe(0);
});

//jasmine.Clock.tick(0);
});
});

0 comments on commit 648a95d

Please sign in to comment.