Skip to content

Commit

Permalink
pooling: rename destroy to release to avoid confusion with generi…
Browse files Browse the repository at this point in the history
…c object `destroy` method triggered when an object is removed from the scene
  • Loading branch information
obiot committed Aug 18, 2024
1 parent f1eaff7 commit 5634eee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions packages/melonjs/src/system/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ export interface Pool<T, A extends unknown[]> {
}

type Reset<A extends unknown[]> = ((...args: A) => void) | undefined;
type Destroy = (() => void) | undefined;
type Release = (() => void) | undefined;

export interface CreatePoolOptions<T, A extends unknown[]> {
instance: T;
reset?: Reset<A>;
destroy?: Destroy;
release?: Release;
}

export const createPool = <T, A extends unknown[]>(
options: (...args: A) => CreatePoolOptions<T, A>,
): Pool<T, A> => {
const available = new Set<T>();
const instanceResetMethods = new Map<T, Reset<A>>();
const instanceDestroyMethods = new Map<T, Destroy>();
const instanceReleaseMethods = new Map<T, Release>();
let inUse: number = 0;

return {
Expand All @@ -39,8 +39,8 @@ export const createPool = <T, A extends unknown[]>(
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--;
},
Expand All @@ -56,9 +56,9 @@ export const createPool = <T, A extends unknown[]>(
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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/melonjs/tests/pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class GameObject {
reset() {
this.resetCalled = true;
}
destroy() {
release() {
this.destroyCalled = true;
}
}
Expand Down Expand Up @@ -47,8 +47,8 @@ test("object reset and destroy are called when getting & releasing object", () =
reset: () => {
gameObject.reset();
},
destroy: () => {
gameObject.destroy();
release: () => {
gameObject.release();
},
};
});
Expand Down

0 comments on commit 5634eee

Please sign in to comment.