1 line
4.9 KiB
Plaintext
1 line
4.9 KiB
Plaintext
{"version":3,"file":"PoolGroup.mjs","sources":["../../../src/utils/pool/PoolGroup.ts"],"sourcesContent":["import { Pool } from './Pool';\n\nimport type { PoolItem, PoolItemConstructor } from './Pool';\n\n/**\n * A type alias for a constructor of a Pool.\n * @template T The type of items in the pool. Must extend PoolItem.\n * @memberof utils\n */\nexport type PoolConstructor<T extends PoolItem> = new () => Pool<T>;\n\n/**\n * A group of pools that can be used to store objects of different types.\n * @memberof utils\n */\nexport class PoolGroupClass\n{\n /**\n * A map to store the pools by their class type.\n * @private\n */\n private readonly _poolsByClass: Map<PoolItemConstructor<PoolItem>, Pool<PoolItem>> = new Map();\n\n /**\n * Prepopulates a specific pool with a given number of items.\n * @template T The type of items in the pool. Must extend PoolItem.\n * @param {PoolItemConstructor<T>} Class - The constructor of the items in the pool.\n * @param {number} total - The number of items to add to the pool.\n */\n public prepopulate<T extends PoolItem>(Class: PoolItemConstructor<T>, total: number): void\n {\n const classPool = this.getPool(Class);\n\n classPool.prepopulate(total);\n }\n\n /**\n * Gets an item from a specific pool.\n * @template T The type of items in the pool. Must extend PoolItem.\n * @param {PoolItemConstructor<T>} Class - The constructor of the items in the pool.\n * @param {unknown} [data] - Optional data to pass to the item's constructor.\n * @returns {T} The item from the pool.\n */\n public get<T extends PoolItem>(Class: PoolItemConstructor<T>, data?: unknown): T\n {\n const pool = this.getPool(Class);\n\n return pool.get(data) as T;\n }\n\n /**\n * Returns an item to its respective pool.\n * @param {PoolItem} item - The item to return to the pool.\n */\n public return(item: PoolItem): void\n {\n const pool = this.getPool(item.constructor as PoolItemConstructor<PoolItem>);\n\n pool.return(item);\n }\n\n /**\n * Gets a specific pool based on the class type.\n * @template T The type of items in the pool. Must extend PoolItem.\n * @param {PoolItemConstructor<T>} ClassType - The constructor of the items in the pool.\n * @returns {Pool<T>} The pool of the given class type.\n */\n public getPool<T extends PoolItem>(ClassType: PoolItemConstructor<T>): Pool<T>\n {\n if (!this._poolsByClass.has(ClassType))\n {\n this._poolsByClass.set(ClassType, new Pool(ClassType));\n }\n\n return this._poolsByClass.get(ClassType) as Pool<T>;\n }\n\n /** gets the usage stats of each pool in the system */\n public stats(): Record<string, {free: number; used: number; size: number}>\n {\n const stats = {} as Record<string, {free: number; used: number; size: number}>;\n\n this._poolsByClass.forEach((pool) =>\n {\n // TODO: maybe we should allow the name to be set when `createEntity` is called\n const name = stats[pool._classType.name]\n ? pool._classType.name + (pool._classType as any).ID : pool._classType.name;\n\n stats[name] = {\n free: pool.totalFree,\n used: pool.totalUsed,\n size: pool.totalSize,\n };\n });\n\n return stats;\n }\n}\n\nexport const BigPool = new PoolGroupClass();\n"],"names":[],"mappings":";;;AAeO,MAAM,cACb,CAAA;AAAA,EADO,WAAA,GAAA;AAMH;AAAA;AAAA;AAAA;AAAA,IAAiB,IAAA,CAAA,aAAA,uBAAwE,GAAI,EAAA,CAAA;AAAA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQtF,WAAA,CAAgC,OAA+B,KACtE,EAAA;AACI,IAAM,MAAA,SAAA,GAAY,IAAK,CAAA,OAAA,CAAQ,KAAK,CAAA,CAAA;AAEpC,IAAA,SAAA,CAAU,YAAY,KAAK,CAAA,CAAA;AAAA,GAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,GAAA,CAAwB,OAA+B,IAC9D,EAAA;AACI,IAAM,MAAA,IAAA,GAAO,IAAK,CAAA,OAAA,CAAQ,KAAK,CAAA,CAAA;AAE/B,IAAO,OAAA,IAAA,CAAK,IAAI,IAAI,CAAA,CAAA;AAAA,GACxB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,OAAO,IACd,EAAA;AACI,IAAA,MAAM,IAAO,GAAA,IAAA,CAAK,OAAQ,CAAA,IAAA,CAAK,WAA4C,CAAA,CAAA;AAE3E,IAAA,IAAA,CAAK,OAAO,IAAI,CAAA,CAAA;AAAA,GACpB;AAAA;AA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