-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamin Strauß
committed
Jan 30, 2018
1 parent
8b82ffd
commit b26a769
Showing
3 changed files
with
152 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
goog.module('clulib.animation.rendering'); | ||
|
||
const EventTarget = goog.require('goog.events.EventTarget'); | ||
const Event = goog.require('goog.events.Event'); | ||
|
||
/** | ||
* A render loop which runs on requestAnimationFrame. | ||
* | ||
* Dispatches RenderLoopEvents | ||
*/ | ||
class RenderLoop extends EventTarget { | ||
constructor () { | ||
super(); | ||
|
||
/** | ||
* @type {boolean} | ||
* @private | ||
*/ | ||
this.isRunning_ = false; | ||
|
||
/** | ||
* @type {?number} | ||
* @private | ||
*/ | ||
this.id_ = null; | ||
} | ||
|
||
/** | ||
* @returns {boolean} | ||
*/ | ||
get isRunning () { | ||
return this.isRunning_; | ||
} | ||
|
||
start () { | ||
if (this.isRunning_) | ||
return; | ||
this.id_ = window.requestAnimationFrame(this.tick_.bind(this)); | ||
this.isRunning_ = true; | ||
this.dispatchEvent(new RenderLoopEvent(RenderLoopEventType.START, this)); | ||
} | ||
|
||
/** | ||
* @param {number} highResolutionTimestamp | ||
* @private | ||
*/ | ||
tick_ (highResolutionTimestamp) { | ||
this.dispatchEvent(new RenderLoopEvent(RenderLoopEventType.TICK, this, highResolutionTimestamp)); | ||
} | ||
|
||
stop () { | ||
if (!this.isRunning_) | ||
return; | ||
window.cancelAnimationFrame(/** @type {number} */ (this.id_)); | ||
this.isRunning_ = false; | ||
this.dispatchEvent(new RenderLoopEvent(RenderLoopEventType.END, this)); | ||
} | ||
} | ||
|
||
/** | ||
* The event for RenderLoop. Holds the elapsed time. | ||
*/ | ||
class RenderLoopEvent extends Event { | ||
/** | ||
* @param {RenderLoopEventType} type | ||
* @param {RenderLoop} target | ||
* @param {number=} elapsedTime | ||
*/ | ||
constructor (type, target, elapsedTime) { | ||
super(type, target); | ||
|
||
/** | ||
* @type {?number} | ||
* @private | ||
*/ | ||
this.elapsedTime_ = /** @type {?number} */ (elapsedTime); | ||
} | ||
|
||
/** | ||
* The elapsed time since the last tick in milliseconds. | ||
* | ||
* @returns {?number} | ||
*/ | ||
get elapsedTime () { | ||
return this.elapsedTime_; | ||
} | ||
} | ||
|
||
/** | ||
* @enum {string} | ||
*/ | ||
const RenderLoopEventType = { | ||
START: 'start', | ||
TICK: 'tick', | ||
END: 'end' | ||
}; | ||
|
||
exports = {RenderLoop, RenderLoopEvent, RenderLoopEventType}; |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
goog.module('test.clulib.animation.rendering'); | ||
|
||
const {RenderLoop, RenderLoopEventType} = goog.require('clulib.animation.rendering'); | ||
|
||
/** | ||
* @param {number} ms | ||
* @returns {Promise<void>} | ||
*/ | ||
const waitFor = function (ms) { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, ms); | ||
}); | ||
}; | ||
|
||
exports = function () { | ||
describe('clulib.animation.rendering', () => { | ||
describe('RenderLoop', () => { | ||
it('should dispatch tick events', async () => { | ||
const loop = new RenderLoop(); | ||
let started = false; | ||
let ticked = false; | ||
let ended = false; | ||
let elapsedTime = 0; | ||
|
||
loop.addEventListener(RenderLoopEventType.START, () => { | ||
started = true; | ||
}); | ||
|
||
loop.addEventListener(RenderLoopEventType.TICK, event => { | ||
ticked = true; | ||
elapsedTime = event.elapsedTime; | ||
loop.stop(); | ||
}); | ||
|
||
loop.addEventListener(RenderLoopEventType.END, () => { | ||
ended = true; | ||
}); | ||
|
||
loop.start(); | ||
|
||
await waitFor(1000); | ||
|
||
expect(started).toBe(true); | ||
expect(ticked).toBe(true); | ||
expect(ended).toBe(true); | ||
expect(elapsedTime > 0).toBe(true); | ||
}); | ||
}); | ||
}); | ||
}; |
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