import {cache} from "../globals"; import {SelectOption} from "./fields/MultiSelectSearch"; export class FieldCache { private cache = cache; /** * I think this needs to be refactored to use individual caches for different data types * because we might have clashes between IDs of different types of data. * For example, a product and a category might have the same ID. * Its rare, but possible. */ get(id: string): T | undefined { return this.cache.find(option => option.id === id) as T } set(value: SelectOption): void { if (!value || value.id === undefined || value.id === null) { return; } if (!this.has(value.id)) { this.cache.push(value); } } setValues(values: SelectOption[] = []): void { values.forEach(value => this.set(value)); } has(key: string | number | null | undefined): boolean { if (key === undefined || key === null) { return false; } return this.cache.some(option => option?.id?.toString() === key.toString()); } clear(): void { this.cache = []; } } export const fieldsCacheContainer = new class { private containers: Record = {}; get(containerId: string): FieldCache { if (!this.containers[containerId]) { this.containers[containerId] = new FieldCache(); } return this.containers[containerId]; } } export const fieldsCache = new FieldCache()