1 line
6.5 KiB
Plaintext
1 line
6.5 KiB
Plaintext
|
|
{"version":3,"file":"Cache.mjs","sources":["../../src/cache/Cache.ts"],"sourcesContent":["import { BaseTexture, Texture } from '@pixi/core';\nimport { convertToList } from '../utils';\n\nimport type { CacheParser } from './CacheParser';\n\n/**\n * A single Cache for all assets.\n *\n * When assets are added to the cache via set they normally are added to the cache as key-value pairs.\n *\n * With this cache, you can add parsers that will take the object and convert it to a list of assets that can be cached.\n * for example a cacheSprite Sheet parser will add all of the textures found within its sprite sheet directly to the cache.\n *\n * This gives devs the flexibility to cache any type of object however we want.\n *\n * It is not intended that this class is created by developers - it is part of the Asset package.\n * This is the first major system of PixiJS' main Assets class.\n * @memberof PIXI\n * @class Cache\n */\nclass CacheClass\n{\n private _parsers: CacheParser[] = [];\n\n private readonly _cache: Map<string, any> = new Map();\n private readonly _cacheMap: Map<string, {\n keys: string[],\n cacheKeys: string[],\n }> = new Map();\n\n /** Clear all entries. */\n public reset(): void\n {\n this._cacheMap.clear();\n this._cache.clear();\n }\n\n /**\n * Check if the key exists\n * @param key - The key to check\n */\n public has(key: string): boolean\n {\n return this._cache.has(key);\n }\n\n /**\n * Fetch entry by key\n * @param key - The key of the entry to get\n */\n public get<T = any>(key: string): T\n {\n const result = this._cache.get(key);\n\n if (!result)\n {\n if (process.env.DEBUG)\n {\n console.warn(`[Assets] Asset id ${key} was not found in the Cache`);\n }\n }\n\n return result as T;\n }\n\n /**\n * Set a value by key or keys name\n * @param key - The key or keys to set\n * @param value - The value to store in the cache or from which cacheable assets will be derived.\n */\n public set(key: string | string[], value: unknown): void\n {\n const keys = convertToList<string>(key);\n\n let cacheableAssets: Record<string, any>;\n\n for (let i = 0; i < this.parsers.length; i++)\n {\n const parser = this.parsers[i];\n\n if (parser.test(value))\n {\n cacheableAssets = parser.getCacheableAssets(keys, value);\n\n break;\n }\n }\n\n if (!cacheableAssets)\n {\n cacheableAssets = {};\n\n keys.forEach((key) =>\n {\n cacheableAssets[key] = value;\n });\n }\n\n const cacheKeys = Object.keys(cacheableAssets);\n\n const cachedAssets = {\n cacheKeys,\n keys\n };\n\n // this is so we can remove them later..\n keys.forEach((key) =>\n {\n this._cacheMap.set(key, cachedAssets);\n });\n\n cacheKeys.forEach((key) =>\n {\n if (this._cache.has(key) && this._cache.get(key) !== value)\n {\n if (process.env.DEBUG)\n {\n console.warn('[Cache] already has key:', key);\n }\n }\n\n this._cache.set(key, cacheableAssets[key]);\n });\n\n // temporary to keep compatible with existing texture caching.. until we remove them!\n if (value instanceof Texture)\n {\n const texture: Texture = value;\n\n keys.forEach((key) =>\n {\n if (texture.baseTexture !== Texture.EMPTY.baseTexture)\n {\n BaseTexture.addToCache(texture.baseTexture, key);\n }\n\n Texture.addToCache(texture, key);\n });\n }\n }\n\n /**\n * Remove entry by key\n *\n * This function will also remove any associated ali
|