# base/objectpool (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/objectpool/

오브젝트 풀.

## ObjectPool extends Object

오브젝트 풀.
- 매 프레임 생성 / 해제되는 객체(탄환, 이펙트 등)를 재사용해 GC 압박을 줄인다.
- acquire() 로 빌리고 release() 로 되돌린다. 되돌릴 때 resetHandler 가 상태를 비운다.

예제: 탄환 빌리고 되돌리기

```js
import { ObjectPool } from "./build/vanilla.js";

const bulletPool = new ObjectPool(() => {
	return { x: 0, y: 0, isAlive: false };
}, (bullet) => {
	bullet.isAlive = false;
}, 4);
const bullet = bulletPool.acquire();
bullet.isAlive = true;
console.log(bulletPool.getFreeCount(), bulletPool.getTotalCount());
bulletPool.release(bullet);
console.log(bulletPool.getFreeCount(), bullet.isAlive);
```

- `constructor(createHandler, resetHandler = null, preloadCount = 0)` (constructor, since 0.3.0): 생성.
  - `createHandler` Function: 새 객체를 만드는 함수.
  - `resetHandler` Function | null: 되돌릴 때 상태를 비우는 함수. (객체를 인자로 받는다)
  - `preloadCount` number: 미리 만들어 둘 개수.
- `acquire()` (method, since 0.3.0): 빌리기. (없으면 새로 만든다)
  - returns object: 놀고 있던 객체. (마지막에 되돌린 것부터 꺼내고, 없으면 createHandler 로 새로 만든다)
- `release(instance)` (method, since 0.3.0): 되돌리기.
  - `instance` object: 되돌릴 객체. (null 이나 undefined 면 무시한다, 이미 되돌려 놀고 있는 객체도 무시한다)
- `getFreeCount()` (method, since 0.3.0): 놀고 있는 객체 수 반환.
  - returns number: 되돌려 쌓여 있는 객체 수.
- `getTotalCount()` (method, since 0.3.0): 지금까지 만든 전체 객체 수 반환.
  - returns number: 만든 객체 수. (미리 만든 것도 들고 clear() 해도 줄지 않는다)
- `clear()` (method, since 0.3.0): 풀 비우기.
