This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose hooks for enqueuing and dequing tasks
- Loading branch information
Showing
3 changed files
with
185 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
/* | ||
* See example/counting.html | ||
*/ | ||
|
||
Zone.countingZone = { | ||
'-onZoneCreated': function () { | ||
Zone.countingZone.counter += 1; | ||
|
||
// setTimeout | ||
enqueueTask: function () { | ||
this.data.count += 1; | ||
}, | ||
'+afterTask': function () { | ||
Zone.countingZone.counter -= 1; | ||
if (Zone.countingZone.counter <= 0) { | ||
Zone.countingZone.counter = 0; | ||
this.onFlush(); | ||
} | ||
|
||
// fires when... | ||
// - clearTimeout | ||
// - setTimeout finishes | ||
dequeueTask: function () { | ||
this.data.count -= 1; | ||
}, | ||
'-run': function () { | ||
Zone.countingZone.counter = 0; | ||
|
||
afterTask: function () { | ||
if (this.data.count === 0 && !this.data.flushed) { | ||
this.data.flushed = true; | ||
this.run(this.onFlush); | ||
} | ||
}, | ||
|
||
counter: function () { | ||
return Zone.countingZone.counter; | ||
return this.data.count; | ||
}, | ||
|
||
data: { | ||
count: 0, | ||
flushed: false | ||
}, | ||
|
||
onFlush: function () {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,90 @@ | ||
'use strict'; | ||
|
||
describe('Zone.countingZone', function () { | ||
var flushSpy = jasmine.createSpy('flush'), | ||
countingZone = zone.fork(Zone.countingZone).fork({ | ||
onFlush: flushSpy | ||
}); | ||
var flushSpy, countingZone; | ||
|
||
beforeEach(function () { | ||
jasmine.Clock.useMock(); | ||
flushSpy.reset(); | ||
flushSpy = jasmine.createSpy('flush'); | ||
countingZone = zone.fork(Zone.longStackTraceZone). | ||
fork(Zone.countingZone). | ||
fork({ | ||
onFlush: flushSpy | ||
}); | ||
}); | ||
|
||
it('should flush at the end of a run', function () { | ||
countingZone.run(function () {}); | ||
expect(flushSpy).toHaveBeenCalled(); | ||
countingZone.run(function () { | ||
expect(countingZone.counter()).toBe(0); | ||
}); | ||
expect(countingZone.counter()).toBe(0); | ||
expect(flushSpy.calls.length).toBe(1); | ||
}); | ||
|
||
it('should work with setTimeout', function () { | ||
var latch; | ||
|
||
runs(function () { | ||
countingZone.run(function () { | ||
setTimeout(function () { | ||
latch = true; | ||
}, 0); | ||
expect(countingZone.counter()).toBe(1); | ||
}); | ||
}); | ||
|
||
waitsFor(function () { | ||
return latch; | ||
}); | ||
|
||
runs(function () { | ||
expect(countingZone.counter()).toBe(0); | ||
}) | ||
}); | ||
|
||
it('should work', function () { | ||
it('should work with clearTimeout', function () { | ||
var latch = false; | ||
countingZone.run(function () { | ||
var id = setTimeout(function () { | ||
latch = true; | ||
}, 0); | ||
expect(countingZone.counter()).toBe(1); | ||
clearTimeout(id); | ||
expect(countingZone.counter()).toBe(0); | ||
}); | ||
}); | ||
|
||
|
||
setTimeout(function () {}, 0); | ||
it('should work with addEventListener', function () { | ||
var elt = document.createElement('button'); | ||
var clicked = false; | ||
|
||
runs(function () { | ||
countingZone.run(main); | ||
}); | ||
|
||
function main () { | ||
expect(countingZone.counter()).toBe(0); | ||
elt.addEventListener('click', onClick); | ||
expect(countingZone.counter()).toBe(1); | ||
|
||
//jasmine.Clock.tick(0); | ||
elt.click(); | ||
function onClick () { | ||
expect(countingZone.counter()).toBe(1); | ||
elt.removeEventListener('click', onClick); | ||
expect(countingZone.counter()).toBe(0); | ||
clicked = true; | ||
} | ||
|
||
expect(countingZone.counter()).toBe(0); | ||
} | ||
|
||
waitsFor(function () { | ||
return clicked; | ||
}, 10, 'the thing'); | ||
|
||
//expect(countingZone.counter()).toBe(0); | ||
runs(function () { | ||
expect(flushSpy.calls.length).toBe(1); | ||
}); | ||
|
||
//jasmine.Clock.tick(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters