UNPKG

@haelp/teto

Version:

A typescript-based controllable TETR.IO client.

87 lines (86 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Queue", { enumerable: true, get: function() { return Queue; } }); const _rng = _export_star(require("./rng"), exports); _export_star(require("./types"), exports); function _export_star(from, to) { Object.keys(from).forEach(function(k) { if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) { Object.defineProperty(to, k, { enumerable: true, get: function() { return from[k]; } }); } }); return from; } 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 = _rng.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); } } } //# sourceMappingURL=index.js.map