From 5634eee7cc220f550a653628614287bbf97353cd Mon Sep 17 00:00:00 2001 From: Olivier Biot Date: Sun, 18 Aug 2024 10:03:22 +0800 Subject: [PATCH] pooling: rename `destroy` to `release` to avoid confusion with generic object `destroy` method triggered when an object is removed from the scene --- packages/melonjs/src/system/pool.ts | 14 +++++++------- packages/melonjs/tests/pool.test.ts | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/melonjs/src/system/pool.ts b/packages/melonjs/src/system/pool.ts index f55f1bc6a..66d88d6db 100644 --- a/packages/melonjs/src/system/pool.ts +++ b/packages/melonjs/src/system/pool.ts @@ -14,12 +14,12 @@ export interface Pool { } type Reset = ((...args: A) => void) | undefined; -type Destroy = (() => void) | undefined; +type Release = (() => void) | undefined; export interface CreatePoolOptions { instance: T; reset?: Reset; - destroy?: Destroy; + release?: Release; } export const createPool = ( @@ -27,7 +27,7 @@ export const createPool = ( ): Pool => { const available = new Set(); const instanceResetMethods = new Map>(); - const instanceDestroyMethods = new Map(); + const instanceReleaseMethods = new Map(); let inUse: number = 0; return { @@ -39,8 +39,8 @@ export const createPool = ( if (available.has(instance)) { throw new Error("Instance is already in pool."); } - const destroy = instanceDestroyMethods.get(instance); - destroy?.(); + const release = instanceReleaseMethods.get(instance); + release?.(); available.add(instance); inUse--; }, @@ -56,9 +56,9 @@ export const createPool = ( inUse++; return object; } else { - const { instance, reset, destroy } = options(...args); + const { instance, reset, release } = options(...args); instanceResetMethods.set(instance, reset); - instanceDestroyMethods.set(instance, destroy); + instanceReleaseMethods.set(instance, release); inUse++; return instance; } diff --git a/packages/melonjs/tests/pool.test.ts b/packages/melonjs/tests/pool.test.ts index a140755f6..ec6e964cc 100644 --- a/packages/melonjs/tests/pool.test.ts +++ b/packages/melonjs/tests/pool.test.ts @@ -7,7 +7,7 @@ class GameObject { reset() { this.resetCalled = true; } - destroy() { + release() { this.destroyCalled = true; } } @@ -47,8 +47,8 @@ test("object reset and destroy are called when getting & releasing object", () = reset: () => { gameObject.reset(); }, - destroy: () => { - gameObject.destroy(); + release: () => { + gameObject.release(); }, }; });