mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
05ec70ad5e
* typing * remove old engine-3d stuffs * typing
21 lines
465 B
TypeScript
21 lines
465 B
TypeScript
export default class CircularPool<T = {}> {
|
|
private _cursor: number;
|
|
private _data: T[];
|
|
|
|
constructor (fn: () => T, size: number) {
|
|
this._cursor = 0;
|
|
this._data = new Array(size);
|
|
|
|
for (let i = 0; i < size; ++i) {
|
|
this._data[i] = fn();
|
|
}
|
|
}
|
|
|
|
public request () {
|
|
const item = this._data[this._cursor];
|
|
this._cursor = (this._cursor + 1) % this._data.length;
|
|
|
|
return item;
|
|
}
|
|
}
|