2022-06-25 00:23:03 +08:00

17 lines
332 B
JavaScript

export default class CircularPool {
constructor(fn, size) {
this._cursor = 0;
this._data = new Array(size);
for (let i = 0; i < size; ++i) {
this._data[i] = fn();
}
}
request() {
let item = this._data[this._cursor];
this._cursor = (this._cursor + 1) % this._data.length;
return item;
}
}