1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var RequestIdleCallback = (function () {
|
5 | function RequestIdleCallback() {
|
6 | var _this = this;
|
7 | this.isUsingMockIdleCallback = false;
|
8 | this.isMockingUnsupported = false;
|
9 | this.queued = {};
|
10 | this.currentIdleCallback = 0;
|
11 | this.requestIdleCallback = function (callback) {
|
12 | _this.currentIdleCallback += 1;
|
13 | _this.queued[_this.currentIdleCallback] = callback;
|
14 | return _this.currentIdleCallback;
|
15 | };
|
16 | this.cancelIdleCallback = function (callback) {
|
17 | delete _this.queued[callback];
|
18 | };
|
19 | }
|
20 | RequestIdleCallback.prototype.mock = function () {
|
21 | if (this.isUsingMockIdleCallback) {
|
22 | throw new Error('requestIdleCallback is already mocked, but you tried to mock it again.');
|
23 | }
|
24 | this.isUsingMockIdleCallback = true;
|
25 | var windowWithIdle = window;
|
26 | this.originalRequestIdleCallback = windowWithIdle.requestIdleCallback;
|
27 | windowWithIdle.requestIdleCallback = this.requestIdleCallback;
|
28 | this.originalCancelIdleCallback = windowWithIdle.cancelIdleCallback;
|
29 | windowWithIdle.cancelIdleCallback = this.cancelIdleCallback;
|
30 | };
|
31 | RequestIdleCallback.prototype.mockAsUnsupported = function () {
|
32 | if (this.isUsingMockIdleCallback) {
|
33 | throw new Error('requestIdleCallback is already mocked, but you tried to mock it again.');
|
34 | }
|
35 | this.isUsingMockIdleCallback = true;
|
36 | this.isMockingUnsupported = true;
|
37 | var windowWithIdle = window;
|
38 | this.originalRequestIdleCallback = windowWithIdle.requestIdleCallback;
|
39 | delete windowWithIdle.requestIdleCallback;
|
40 | this.originalCancelIdleCallback = windowWithIdle.cancelIdleCallback;
|
41 | delete windowWithIdle.cancelIdleCallback;
|
42 | };
|
43 | RequestIdleCallback.prototype.restore = function () {
|
44 | if (!this.isUsingMockIdleCallback) {
|
45 | throw new Error('requestIdleCallback is already real, but you tried to restore it again.');
|
46 | }
|
47 | if (Object.keys(this.queued).length > 0) {
|
48 |
|
49 | console.warn('You are restoring requestIdleCallback, but some idle callbacks have not been run. You can run requestIdleCallback.cancelIdleCallback() to clear them all and avoid this warning.');
|
50 | this.cancelIdleCallbacks();
|
51 | }
|
52 | this.isUsingMockIdleCallback = false;
|
53 | this.isMockingUnsupported = false;
|
54 | if (this.originalRequestIdleCallback) {
|
55 | window.requestIdleCallback = this.originalRequestIdleCallback;
|
56 | }
|
57 | else {
|
58 | delete window.requestIdleCallback;
|
59 | }
|
60 | if (this.originalCancelIdleCallback) {
|
61 | window.cancelIdleCallback = this.originalCancelIdleCallback;
|
62 | }
|
63 | else {
|
64 | delete window.cancelIdleCallback;
|
65 | }
|
66 | };
|
67 | RequestIdleCallback.prototype.isMocked = function () {
|
68 | return this.isUsingMockIdleCallback;
|
69 | };
|
70 | RequestIdleCallback.prototype.runIdleCallbacks = function (timeRemaining, didTimeout) {
|
71 | var _this = this;
|
72 | if (timeRemaining === void 0) { timeRemaining = Infinity; }
|
73 | if (didTimeout === void 0) { didTimeout = false; }
|
74 | this.ensureIdleCallbackIsMock();
|
75 |
|
76 |
|
77 | Object.keys(this.queued).forEach(function (frame) {
|
78 | var callback = _this.queued[frame];
|
79 | delete _this.queued[frame];
|
80 | callback({ timeRemaining: function () { return timeRemaining; }, didTimeout: didTimeout });
|
81 | });
|
82 | };
|
83 | RequestIdleCallback.prototype.cancelIdleCallbacks = function () {
|
84 | var e_1, _a;
|
85 | this.ensureIdleCallbackIsMock();
|
86 | try {
|
87 | for (var _b = tslib_1.__values(Object.keys(this.queued)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
88 | var id = _c.value;
|
89 | this.cancelIdleCallback(id);
|
90 | }
|
91 | }
|
92 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
93 | finally {
|
94 | try {
|
95 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
96 | }
|
97 | finally { if (e_1) throw e_1.error; }
|
98 | }
|
99 | };
|
100 | RequestIdleCallback.prototype.ensureIdleCallbackIsMock = function () {
|
101 | if (!this.isUsingMockIdleCallback) {
|
102 | throw new Error('You must call requestIdleCallback.mock() before interacting with the mock request- or cancel- IdleCallback methods.');
|
103 | }
|
104 | if (this.isMockingUnsupported) {
|
105 | throw new Error('You have mocked requestIdleCallback as unsupported. Call requestIdleCallback.restore(), then requestIdleCallback.mock() if you want to simulate idle callbacks.');
|
106 | }
|
107 | };
|
108 | return RequestIdleCallback;
|
109 | }());
|
110 | exports.default = RequestIdleCallback;
|