MCPcopy Create free account
hub / github.com/melonjs/melonJS / createPool

Function createPool

packages/melonjs/src/system/pool.ts:41–102  ·  view source on GitHub ↗
(
	options: (...args: A) => CreatePoolOptions<T, A>,
)

Source from the content-addressed store, hash-verified

39export const getRegisteredPools = () => pools;
40
41export const createPool = <T, A extends unknown[]>(
42 options: (...args: A) => CreatePoolOptions<T, A>,
43): Pool<T, A> => {
44 const available = new Set<T>();
45 const instanceResetMethods = new Map<T, Reset<A>>();
46 const instanceReleaseMethods = new Map<T, Release>();
47 let inUse: number = 0;
48
49 return {
50 /**
51 * release an object back to the pool
52 * @param instance The object to release.
53 */
54 release: (instance: T) => {
55 if (available.has(instance)) {
56 throw new Error("Instance is already in pool.");
57 }
58 const release = instanceReleaseMethods.get(instance);
59 release?.();
60 available.add(instance);
61 inUse--;
62 },
63 /**
64 * get an instance from the pool
65 * @param args The arguments for creating the instance.
66 */
67 get: (...args) => {
68 const object = takeFromSet(available);
69 if (object) {
70 const reset = instanceResetMethods.get(object);
71 reset?.(...args);
72 inUse++;
73 return object;
74 } else {
75 const { instance, reset, release } = options(...args);
76 instanceResetMethods.set(instance, reset);
77 instanceReleaseMethods.set(instance, release);
78 inUse++;
79 return instance;
80 }
81 },
82 /**
83 * purge the pool
84 */
85 purge: () => {
86 available.clear();
87 inUse = 0;
88 },
89 /**
90 * get the current size of the pool (how many objects are available)
91 */
92 size: () => {
93 return available.size;
94 },
95 /**
96 * get the number of objects currently in use
97 */
98 used: () => {

Callers 15

tween.tsFile · 0.90
bounds.tsFile · 0.90
particle.tsFile · 0.90
bitmaptextdata.tsFile · 0.90
sphere.tsFile · 0.90
rectangle.tsFile · 0.90
point.tsFile · 0.90
line.tsFile · 0.90
polygon.tsFile · 0.90
roundrect.tsFile · 0.90
ellipse.tsFile · 0.90
matrix3d.tsFile · 0.90

Calls 7

takeFromSetFunction · 0.85
getMethod · 0.65
clearMethod · 0.65
resetFunction · 0.50
hasMethod · 0.45
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected