/
githubmirror
/
pixijs
Обзор
Документация
Войти
/
githubmirror
/
pixijs
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
src/utils/pool/PoolGroup.ts
115 строк
4 KB
Zyie
fix: memory leaks in renderer systems (#11581)
02 сен 2025, 15:27
Не верифицирован
02 сен 2025, 15:27
6aeb34a
Код
Авторство
О чём код?
import { GlobalResourceRegistry } from './GlobalResourceRegistry'; import { Pool } from './Pool'; import type { PoolItem, PoolItemConstructor } from './Pool'; /** * A type alias for a constructor of a Pool. * @template T The type of items in the pool. Must extend PoolItem. * @category utils * @advanced */ export type PoolConstructor<T extends PoolItem> = new () => Pool<T>; /** * A group of pools that can be used to store objects of different types. * @category utils * @advanced */ export class PoolGroupClass { /** * A map to store the pools by their class type. * @private */ private readonly _poolsByClass: Map<PoolItemConstructor<PoolItem>, Pool<PoolItem>> = new Map(); /** * Prepopulates a specific pool with a given number of items. * @template T The type of items in the pool. Must extend PoolItem. * @param {PoolItemConstructor<T>} Class - The constructor of the items in the pool. * @param {number} total - The number of items to add to the pool. */ public prepopulate<T extends PoolItem>(Class: PoolItemConstructor<T>, total: number): void { const classPool = this.getPool(Class); classPool.prepopulate(total); } /** * Gets an item from a specific pool. * @template T The type of items in the pool. Must extend PoolItem. * @param {PoolItemConstructor<T>} Class - The constructor of the items in the pool. * @param {unknown} [data] - Optional data to pass to the item's constructor. * @returns {T} The item from the pool. */ public get<T extends PoolItem>(Class: PoolItemConstructor<T>, data?: unknown): T { const pool = this.getPool(Class); return pool.get(data) as T; } /** * Returns an item to its respective pool. * @param {PoolItem} item - The item to return to the pool. */ public return(item: PoolItem): void { const pool = this.getPool(item.constructor as PoolItemConstructor<PoolItem>); pool.return(item); } /** * Gets a specific pool based on the class type. * @template T The type of items in the pool. Must extend PoolItem. * @param {PoolItemConstructor<T>} ClassType - The constructor of the items in the pool. * @returns {Pool<T>} The pool of the given class type. */ public getPool<T extends PoolItem>(ClassType: PoolItemConstructor<T>): Pool<T> { if (!this._poolsByClass.has(ClassType)) { this._poolsByClass.set(ClassType, new Pool(ClassType)); } return this._poolsByClass.get(ClassType) as Pool<T>; } /** gets the usage stats of each pool in the system */ public stats(): Record<string, {free: number; used: number; size: number}> { const stats = {} as Record<string, {free: number; used: number; size: number}>; this._poolsByClass.forEach((pool) => { // TODO: maybe we should allow the name to be set when `createEntity` is called const name = stats[pool._classType.name] ? pool._classType.name + (pool._classType as any).ID : pool._classType.name; stats[name] = { free: pool.totalFree, used: pool.totalUsed, size: pool.totalSize, }; }); return stats; } /** Clears all pools in the group. This will reset all pools and free their resources. */ public clear(): void { this._poolsByClass.forEach((pool) => pool.clear()); this._poolsByClass.clear(); } } /** * A singleton instance of the PoolGroupClass that can be used throughout the application. * @internal */ export const BigPool = new PoolGroupClass(); GlobalResourceRegistry.register(BigPool);