1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var AnimationFrame = (function () {
|
4 | function AnimationFrame() {
|
5 | var _this = this;
|
6 | this.isUsingMockAnimationFrame = false;
|
7 | this.queued = {};
|
8 | this.currentAnimationFrame = 0;
|
9 | this.requestAnimationFrame = function (callback) {
|
10 | _this.currentAnimationFrame += 1;
|
11 | _this.queued[_this.currentAnimationFrame] = callback;
|
12 | return _this.currentAnimationFrame;
|
13 | };
|
14 | this.cancelAnimationFrame = function (frame) {
|
15 | delete _this.queued[frame];
|
16 | };
|
17 | }
|
18 | AnimationFrame.prototype.mock = function () {
|
19 | if (this.isUsingMockAnimationFrame) {
|
20 | throw new Error('The animation frame is already mocked, but you tried to mock it again.');
|
21 | }
|
22 | this.isUsingMockAnimationFrame = true;
|
23 | this.originalRequestAnimationFrame = window.requestAnimationFrame;
|
24 | window.requestAnimationFrame = this.requestAnimationFrame;
|
25 | this.originalCancelAnimationFrame = window.cancelAnimationFrame;
|
26 | window.cancelAnimationFrame = this.cancelAnimationFrame;
|
27 | };
|
28 | AnimationFrame.prototype.restore = function () {
|
29 | if (!this.isUsingMockAnimationFrame) {
|
30 | throw new Error('The animation frame is already real, but you tried to restore it again.');
|
31 | }
|
32 | this.isUsingMockAnimationFrame = false;
|
33 | window.requestAnimationFrame = this.originalRequestAnimationFrame;
|
34 | window.cancelAnimationFrame = this.originalCancelAnimationFrame;
|
35 | };
|
36 | AnimationFrame.prototype.isMocked = function () {
|
37 | return this.isUsingMockAnimationFrame;
|
38 | };
|
39 | AnimationFrame.prototype.runFrame = function () {
|
40 | var _this = this;
|
41 | this.ensureAnimationFrameIsMock();
|
42 |
|
43 |
|
44 | Object.keys(this.queued).forEach(function (frame) {
|
45 | var callback = _this.queued[frame];
|
46 | delete _this.queued[frame];
|
47 | callback(Date.now());
|
48 | });
|
49 | };
|
50 | AnimationFrame.prototype.ensureAnimationFrameIsMock = function () {
|
51 | if (!this.isUsingMockAnimationFrame) {
|
52 | throw new Error('You must call animationFrame.mock() before interacting with the mock request- or cancel- AnimationFrame methods.');
|
53 | }
|
54 | };
|
55 | return AnimationFrame;
|
56 | }());
|
57 | exports.default = AnimationFrame;
|