"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractWsConnection = exports.LOW_BACK_PRESSURE = exports.HIGH_BACK_PRESSURE = void 0;
const utils_1 = require("@streamr/utils");
exports.HIGH_BACK_PRESSURE = 1024 * 1024 * 2;
exports.LOW_BACK_PRESSURE = 1024 * 1024;
class AbstractWsConnection {
    constructor(peerInfo) {
        this.respondedPong = true;
        this.highBackPressure = false;
        this.peerInfo = peerInfo;
        this.logger = new utils_1.Logger(module, peerInfo.peerId);
    }
    setBackPressureHandlers(onLowBackPressure, onHighBackPressure) {
        if (this.onLowBackPressure === undefined && this.onHighBackPressure === undefined) {
            this.onLowBackPressure = onLowBackPressure;
            this.onHighBackPressure = onHighBackPressure;
        }
        else {
            throw new Error('invariant: cannot re-set backpressure handlers');
        }
    }
    ping() {
        this.respondedPong = false;
        this.rttStart = Date.now();
        this.sendPing();
    }
    onPong() {
        this.respondedPong = true;
        this.rtt = Date.now() - this.rttStart;
    }
    evaluateBackPressure() {
        const bufferedAmount = this.getBufferedAmount();
        if (!this.highBackPressure && bufferedAmount > exports.HIGH_BACK_PRESSURE) {
            this.logger.trace('Back pressure HIGH for %s at %d', this.getPeerInfo(), bufferedAmount);
            this.highBackPressure = true;
            if (this.onHighBackPressure === undefined) {
                throw new Error('onHighBackPressure listener not set');
            }
            this.onHighBackPressure();
        }
        else if (this.highBackPressure && bufferedAmount < exports.LOW_BACK_PRESSURE) {
            this.logger.trace('Back pressure LOW for %s at %d', this.getPeerInfo(), bufferedAmount);
            this.highBackPressure = false;
            if (this.onLowBackPressure === undefined) {
                throw new Error('onLowBackPressure listener not set');
            }
            this.onLowBackPressure();
        }
    }
    getPeerInfo() {
        return this.peerInfo;
    }
    getRespondedPong() {
        return this.respondedPong;
    }
    getRtt() {
        return this.rtt;
    }
    getPeerId() {
        return this.getPeerInfo().peerId;
    }
}
exports.AbstractWsConnection = AbstractWsConnection;
//# sourceMappingURL=AbstractWsConnection.js.map