{"version":3,"file":"Pool.mjs","sources":["../../../src/utils/pool/Pool.ts"],"sourcesContent":["/**\n * A generic class for managing a pool of items.\n * @template T The type of items in the pool. Must implement {@link PoolItem}.\n * @template I The type of argument passed to item's `init` method if it exists.\n * @category utils\n * @advanced\n */\nexport class Pool<T extends PoolItem, I = Parameters<NonNullable<T['init']>>[0]>\n{\n    /** @internal */\n    public readonly _classType: PoolItemConstructor<T>;\n    private readonly _pool: T[] = [];\n    private _count = 0;\n    private _index = 0;\n\n    /**\n     * Constructs a new Pool.\n     * @param ClassType - The constructor of the items in the pool.\n     * @param {number} [initialSize] - The initial size of the pool.\n     */\n    constructor(ClassType: PoolItemConstructor<T>, initialSize?: number)\n    {\n        this._classType = ClassType;\n\n        if (initialSize)\n        {\n            this.prepopulate(initialSize);\n        }\n    }\n\n    /**\n     * Prepopulates the pool with a given number of items.\n     * @param total - The number of items to add to the pool.\n     */\n    public prepopulate(total: number): void\n    {\n        for (let i = 0; i < total; i++)\n        {\n            this._pool[this._index++] = new this._classType();\n        }\n\n        this._count += total;\n    }\n\n    /**\n     * Gets an item from the pool. Calls the item's `init` method if it exists.\n     * If there are no items left in the pool, a new one will be created.\n     * @param {I} [data] - Optional data to pass to the item's constructor.\n     * @returns {T} The item from the pool.\n     */\n    public get(data?: I): T\n    {\n        let item;\n\n        if (this._index > 0)\n        {\n            item = this._pool[--this._index];\n        }\n        else\n        {\n            item = new this._classType();\n            this._count++;\n        }\n\n        item.init?.(data);\n\n        return item;\n    }\n\n    /**\n     * Returns an item to the pool. Calls the item's `reset` method if it exists.\n     * @param {T} item - The item to return to the pool.\n     */\n    public return(item: T): void\n    {\n        item.reset?.();\n\n        this._pool[this._index++] = item;\n    }\n\n    /**\n     * Gets the number of items in the pool.\n     * @readonly\n     */\n    get totalSize(): number\n    {\n        return this._count;\n    }\n\n    /**\n     * Gets the number of items in the pool that are free to use without needing to create more.\n     * @readonly\n     */\n    get totalFree(): number\n    {\n        return this._index;\n    }\n\n    /**\n     * Gets the number of items in the pool that are currently in use.\n     * @readonly\n     */\n    get totalUsed(): number\n    {\n        return this._count - this._index;\n    }\n\n    /** clears the pool */\n    public clear()\n    {\n        if (this._pool.length > 0 && this._pool[0].destroy)\n        {\n            for (let i = 0; i < this._index; i++)\n            {\n                this._pool[i].destroy();\n            }\n        }\n        this._pool.length = 0;\n        this._count = 0;\n        this._index = 0;\n    }\n}\n\n/**\n * An object that can be stored in a {@link Pool}.\n * @category utils\n * @advanced\n */\nexport type PoolItem = {\n    init?: (data?: any) => void;\n    reset?: () => void;\n    destroy?: () => void;\n    [key: string]: any;\n};\n\n/**\n * The constructor of an object that can be stored in a {@link Pool}.\n * @typeParam K - The type of the object that can be stored in a {@link Pool}.\n * @category utils\n * @advanced\n */\nexport type PoolItemConstructor<K extends PoolItem> = new () => K;\n"],"names":[],"mappings":";AAOO,MAAM,IAAA,CACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYI,WAAA,CAAY,WAAmC,WAAA,EAC/C;AAVA,IAAA,IAAA,CAAiB,QAAa,EAAC;AAC/B,IAAA,IAAA,CAAQ,MAAA,GAAS,CAAA;AACjB,IAAA,IAAA,CAAQ,MAAA,GAAS,CAAA;AASb,IAAA,IAAA,CAAK,UAAA,GAAa,SAAA;AAElB,IAAA,IAAI,WAAA,EACJ;AACI,MAAA,IAAA,CAAK,YAAY,WAAW,CAAA;AAAA,IAChC;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,YAAY,KAAA,EACnB;AACI,IAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,EAAO,CAAA,EAAA,EAC3B;AACI,MAAA,IAAA,CAAK,MAAM,IAAA,CAAK,MAAA,EAAQ,CAAA,GAAI,IAAI,KAAK,UAAA,EAAW;AAAA,IACpD;AAEA,IAAA,IAAA,CAAK,MAAA,IAAU,KAAA;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,IAAI,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