{"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 utils.PoolItem}.\n * @memberof utils\n */\nexport class Pool<T extends PoolItem>\n{\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 {unknown} [data] - Optional data to pass to the item's constructor.\n     * @returns {T} The item from the pool.\n     */\n    public get(data?: unknown): 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        }\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     * @member {number}\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     * @member {number}\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     * @member {number}\n     */\n    get totalUsed(): number\n    {\n        return this._count - this._index;\n    }\n\n    /** clears the pool - mainly used for debugging! */\n    public clear()\n    {\n        this._pool.length = 0;\n        this._index = 0;\n    }\n}\n\n/**\n * An object that can be stored in a {@link utils.Pool}.\n * @memberof utils\n */\nexport type PoolItem = {\n    init?: (data?: any) => void;\n    reset?: () => void;\n    [key: string]: any;\n};\n\n/**\n * The constructor of an object that can be stored in a {@link utils.Pool}.\n * @typeParam K - The type of the object that can be stored in a {@link utils.Pool}.\n * @memberof utils\n */\nexport type PoolItemConstructor<K extends PoolItem> = new () => K;\n"],"names":[],"mappings":";AAKO,MAAM,IACb,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWI,WAAA,CAAY,WAAmC,WAC/C,EAAA;AAVA,IAAA,IAAA,CAAiB,QAAa,EAAC,CAAA;AAC/B,IAAA,IAAA,CAAQ,MAAS,GAAA,CAAA,CAAA;AACjB,IAAA,IAAA,CAAQ,MAAS,GAAA,CAAA,CAAA;AASb,IAAA,IAAA,CAAK,UAAa,GAAA,SAAA,CAAA;AAElB,IAAA,IAAI,WACJ,EAAA;AACI,MAAA,IAAA,CAAK,YAAY,WAAW,CAAA,CAAA;AAAA,KAChC;AAAA,GACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,YAAY,KACnB,EAAA;AACI,IAAA,KAAA,IAAS,CAAI,GAAA,CAAA,EAAG,CAAI,GAAA,KAAA,EAAO,CAC3B,EAAA,EAAA;AACI,MAAA,IAAA,CAAK,MAAM,IAAK,CAAA,MAAA,EAAQ,CAAI,GAAA,IAAI,KAAK,UAAW,EAAA,CAAA;AAAA,KACpD;AAEA,IAAA,IAAA,CAAK,MAAU,IAAA,KAAA,CAAA;AAAA,GACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,IAAI,IACX,EAAA;AACI,IAAI,IAAA,IAAA,CAAA;AAEJ,IAAI,IAAA,IAAA,CAAK,SAAS,CAClB,EAAA;AACI,MAAA,IAAA,GAA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