@haelp/teto
Version:
A typescript-based controllable TETR.IO client.
65 lines (64 loc) • 1.57 kB
JavaScript
import { rngMap } from "./rng/index.mjs";
export class Queue {
seed;
type;
genFunction;
value;
_minLength;
index;
repopulateListener = null;
constructor(options){
this.seed = options.seed;
this.type = options.type;
this.reset();
this.value = [];
this.minLength = options.minLength;
this.index = 0;
}
reset(index = 0) {
this.genFunction = rngMap[this.type](this.seed);
this.value = [];
this.index = 0;
this.repopulate();
for(let i = 0; i < index; i++){
this.shift();
this.repopulate();
}
}
onRepopulate(listener) {
this.repopulateListener = listener;
}
get minLength() {
return this._minLength;
}
set minLength(val) {
this._minLength = val;
this.repopulate();
}
get next() {
return this.value[0];
}
at(index) {
return this.value.at(index);
}
shift() {
const val = this.value.shift();
this.index++;
this.repopulate();
return val;
}
repopulate() {
const added = [];
while(this.value.length < this.minLength){
const newValues = this.genFunction();
this.value.push(...newValues);
added.push(...newValues);
}
if (this.repopulateListener && added.length) {
this.repopulateListener(added);
}
}
}
export * from "./rng/index.mjs";
export * from "./types.mjs";
//# sourceMappingURL=index.js.map