Dealing with a huge amount of objects can be really hard. To make it easier for you, we create a Base Pool Class. The class provides the feature, that you can create a "Pool" for any kind of objects. Internaly we use a simple ES6 Map. The usage is really simple and if you only need the basic stuff, you only need a few lines of code to create your own pool.
The BasePool Class
The BasePool is a simple class with some methods. This class is created to use as extend not as instance self.
The BasePool has two generic types. T stands for the type you want to store, K stands for the key identifier.
exportabstractclassBasePool<T,K=string|number> {/** * Contains the pool * * @type{Map<string|number, T>} * @protected */protected pool:Map<K,T> =newMap<K,T>();/** * Return the pool size * * @return{number} */publicgetsize():number {returnthis.pool.size; }/** * Create new entity inside pool if not exists * * @param{K} identifier * @param{T} entity * @return{Map<string|number, T> | void} */publicadd(identifier:K, entity:T):Map<K,T> |void {if (!this.has(identifier)) {this.pool.set(identifier, entity); } }/** * Get entity from pool if exists * * @param{K} identifier * @return{T | void} */publicget(identifier:K):T|void {if (this.has(identifier)) {returnthis.pool.get(identifier); } }/** * Check if the pool has identifier * * @param{K} identifier * @return{boolean} */publichas(identifier:K):boolean {returnthis.pool.has(identifier); }/** * Return all entries from pool * * @return{T[]} */publicentries():T[] {returnArray.from(this.pool.values()); }/** * Return all keys from pool * * @return{(K)[]} */publickeys(): (K)[] {returnArray.from(this.pool.keys()); }/** * Remove entity from pool if exists * * @param{K} identifier * @return{boolean} */publicremove(identifier:K):boolean {returnthis.pool.delete(identifier); }/** * Remove all entities from pool */publicremoveAll():void {this.pool.clear(); }}
import { Player } from'alt-client';import { PlayerPool } from'./player.pool';@Component()exportclassPlayerComponent {constructor(privatereadonly playerPool:PlayerPool, ) {}/** * Add player by it's id to pool * * @param{Player} player */publicaddPlayerToPool(player:Player):void {this.playerPool.add(player.id, player); }/** * Remove player from pool by id * * @param{Player} player */publicremovePlayerFromPool(player:Player):void {this.playerPool.remove(player.id); }}