UNPKG

@devexperts/dxcharts-lite

Version:
78 lines (77 loc) 2.98 kB
/* * Copyright (C) 2019 - 2025 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/. */ import { animationFrameThrottled } from './request-animation-frame-throttle.utils'; import { uuid } from '../uuid.utils'; /** * Use this as a wrapper to cache calculation results during single animation frame cycle. * It is useful if * - you have multiple invocations of same code in same animation frame * - you're sure that result will always be the same */ export class AnimationFrameCache { constructor(dataProvider, dataUpdatedPredicate = () => true) { this.dataProvider = dataProvider; this.dataUpdatedPredicate = dataUpdatedPredicate; this.calculatedInThisFrame = false; this.animFrameId = `anim_cache_${uuid()}`; } /** * Calculates or retrieves a cached value. * If the value has not been calculated in the current frame, it calculates it by calling the dataProvider function. * If the data has been updated, it sets the calculatedInThisFrame flag to true. * It also sets a throttled animation frame to reset the calculatedInThisFrame flag to false. * If the cache is not null, it returns the cached value, otherwise it calls the dataProvider function and returns its result. * @returns {T} The cached or calculated value. */ calculateOrGet() { var _a; if (!this.calculatedInThisFrame) { this.cache = this.dataProvider(); if (this.dataUpdatedPredicate()) { this.calculatedInThisFrame = true; } animationFrameThrottled(this.animFrameId, () => { this.calculatedInThisFrame = false; }); } return (_a = this.cache) !== null && _a !== void 0 ? _a : this.dataProvider(); } /** * Invalidates the current state and returns the result of the calculateOrGet() method. * @returns {T} The result of the calculateOrGet() method. */ forceCalculateOrGet() { this.invalidate(); return this.calculateOrGet(); } /** * Returns the last cached value or undefined if there is no cached value. * @returns {T | undefined} The last cached value or undefined. */ getLastCachedValue() { return this.cache; } /** * Updates the cache value with the provided value. * @param {T} value - The new value to be set in the cache. * @returns {void} */ updateCacheValue(value) { this.cache = value; } /** * Invalidates the cache and sets the calculatedInThisFrame flag to false. * @function * @name invalidate * @memberof ClassName * @instance * @returns {void} */ invalidate() { this.cache = undefined; this.calculatedInThisFrame = false; } }