@devexperts/dxcharts-lite
Version:
52 lines (51 loc) • 1.66 kB
JavaScript
/*
* Copyright (C) 2019 - 2026 Devexperts Solutions IE Limited
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (C) 2019 - 2024 Devexperts Solutions IE Limited
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
let fired = false;
export let animationFrameId = 0;
// map is faster than record
const actions = new Map();
const priorActions = new Map();
const animFrame = () => {
if (!fired) {
fired = true;
animationFrameId = requestAnimationFrame(() => {
priorActions.forEach((action, key) => {
action();
priorActions.delete(key);
});
actions.forEach((action, key) => {
action();
actions.delete(key);
});
fired = false;
});
}
};
export const animationFrameThrottled = (name, action) => {
actions.set(name, action);
animFrame();
};
export const cancelThrottledAnimationFrame = (name) => {
actions.delete(name);
};
export const cancelThrottledAnimationFramePrior = (name) => {
priorActions.delete(name);
};
/**
* Prior actions will be called before regular actions
* An example of regular action - draw event
* @param name
* @param action
*/
export const animationFrameThrottledPrior = (name, action) => {
priorActions.set(name, action);
animFrame();
};