"use strict";
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FixedLengthBuffer = void 0;
var FixedLengthBuffer = /** @class */ (function () {
    function FixedLengthBuffer(maxLength) {
        this.maxLength = maxLength;
        this.nextKey = 0;
        this.firstKey = 0;
        this.currentLength = 0;
        this.buffer = Object.create(null);
        this.removeListeners = new Set();
    }
    Object.defineProperty(FixedLengthBuffer.prototype, "length", {
        get: function () {
            return this.currentLength;
        },
        enumerable: false,
        configurable: true
    });
    FixedLengthBuffer.prototype.push = function () {
        var e_1, _a;
        var items = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            items[_i] = arguments[_i];
        }
        try {
            for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
                var item = items_1_1.value;
                this.pushOne(item);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    };
    FixedLengthBuffer.prototype.onRemove = function (listener) {
        this.removeListeners.add(listener);
    };
    FixedLengthBuffer.prototype.dispose = function () {
        this.buffer = {};
        this.removeListeners = new Set();
    };
    FixedLengthBuffer.prototype.pushOne = function (item) {
        if (this.currentLength >= this.maxLength) {
            var keyToDrop = this.firstKey++;
            var itemToDrop_1 = this.buffer[keyToDrop];
            delete this.buffer[keyToDrop];
            this.removeListeners.forEach(function (listener) { return listener(itemToDrop_1); });
        }
        else {
            this.currentLength++;
        }
        var key = this.nextKey++;
        this.buffer[key] = item;
    };
    return FixedLengthBuffer;
}());
exports.FixedLengthBuffer = FixedLengthBuffer;
//# sourceMappingURL=fixed-length-buffer.js.map