UNPKG

3.38 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23/**
24 * AnimationFrame provides a user-friendly abstraction around requesting
25 * and canceling animation frames.
26 */
27var AnimationFrame = /** @class */ (function () {
28 function AnimationFrame() {
29 this.rafIDs = new Map();
30 }
31 /**
32 * Requests an animation frame. Cancels any existing frame with the same key.
33 * @param {string} key The key for this callback.
34 * @param {FrameRequestCallback} callback The callback to be executed.
35 */
36 AnimationFrame.prototype.request = function (key, callback) {
37 var _this = this;
38 this.cancel(key);
39 var frameID = requestAnimationFrame(function (frame) {
40 _this.rafIDs.delete(key);
41 // Callback must come *after* the key is deleted so that nested calls to
42 // request with the same key are not deleted.
43 callback(frame);
44 });
45 this.rafIDs.set(key, frameID);
46 };
47 /**
48 * Cancels a queued callback with the given key.
49 * @param {string} key The key for this callback.
50 */
51 AnimationFrame.prototype.cancel = function (key) {
52 var rafID = this.rafIDs.get(key);
53 if (rafID) {
54 cancelAnimationFrame(rafID);
55 this.rafIDs.delete(key);
56 }
57 };
58 /**
59 * Cancels all queued callback.
60 */
61 AnimationFrame.prototype.cancelAll = function () {
62 var _this = this;
63 // Need to use forEach because it's the only iteration method supported
64 // by IE11. Suppress the underscore because we don't need it.
65 // tslint:disable-next-line:enforce-name-casing
66 this.rafIDs.forEach(function (_, key) {
67 _this.cancel(key);
68 });
69 };
70 /**
71 * Returns the queue of unexecuted callback keys.
72 */
73 AnimationFrame.prototype.getQueue = function () {
74 var queue = [];
75 // Need to use forEach because it's the only iteration method supported
76 // by IE11. Suppress the underscore because we don't need it.
77 // tslint:disable-next-line:enforce-name-casing
78 this.rafIDs.forEach(function (_, key) {
79 queue.push(key);
80 });
81 return queue;
82 };
83 return AnimationFrame;
84}());
85export { AnimationFrame };
86//# sourceMappingURL=animationframe.js.map
\No newline at end of file