@observertc/samples-decoder
Version:
ObserveRTC Library for Decoding Samples
84 lines (83 loc) • 3.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataChannelDecoder = void 0;
const utils_1 = require("./utils");
const utils_2 = require("./utils");
const Logger_1 = require("./Logger");
class DataChannelDecoder {
constructor(id, _attachmentsDecoder) {
this.id = id;
this._attachmentsDecoder = _attachmentsDecoder;
this._visited = false;
// Initialize decoders for each field based on their type
this._timestampDecoder = new utils_2.NumberToNumberDecoder();
this._labelDecoder = new utils_2.StringToStringDecoder();
this._protocolDecoder = new utils_2.StringToStringDecoder();
this._dataChannelIdentifierDecoder = new utils_2.NumberToNumberDecoder();
this._stateDecoder = new utils_2.StringToStringDecoder();
this._messagesSentDecoder = new utils_2.NumberToNumberDecoder();
this._bytesSentDecoder = new utils_1.BigIntToNumberDecoder();
this._messagesReceivedDecoder = new utils_2.NumberToNumberDecoder();
this._bytesReceivedDecoder = new utils_1.BigIntToNumberDecoder();
}
get visited() {
const result = this._visited;
this._visited = false;
return result;
}
reset() {
// Reset all decoders
this._timestampDecoder.reset();
this._labelDecoder.reset();
this._protocolDecoder.reset();
this._dataChannelIdentifierDecoder.reset();
this._stateDecoder.reset();
this._messagesSentDecoder.reset();
this._bytesSentDecoder.reset();
this._messagesReceivedDecoder.reset();
this._bytesReceivedDecoder.reset();
this._attachmentsDecoder.reset();
}
decode(input) {
this._visited = true;
const timestamp = this._timestampDecoder.decode(input.timestamp);
if (!timestamp) {
Logger_1.logger.warn("Invalid data channel sample: missing timestamp or id");
return undefined;
}
this._actualValue = {
timestamp,
id: this.id,
label: this._labelDecoder.decode(input.label),
protocol: this._protocolDecoder.decode(input.protocol),
dataChannelIdentifier: this._dataChannelIdentifierDecoder.decode(input.dataChannelIdentifier),
state: this._stateDecoder.decode(input.state),
messagesSent: this._messagesSentDecoder.decode(input.messagesSent),
bytesSent: this._bytesSentDecoder.decode(input.bytesSent),
messagesReceived: this._messagesReceivedDecoder.decode(input.messagesReceived),
bytesReceived: this._bytesReceivedDecoder.decode(input.bytesReceived),
attachments: this._attachmentsDecoder.decode(input.attachments),
};
return this._actualValue;
}
get actualValue() {
return this._actualValue;
}
set actualValue(sample) {
if (!sample)
return;
this._visited = true;
this._actualValue = sample;
this._timestampDecoder.actualValue = sample.timestamp;
this._labelDecoder.actualValue = sample.label;
this._protocolDecoder.actualValue = sample.protocol;
this._dataChannelIdentifierDecoder.actualValue = sample.dataChannelIdentifier;
this._stateDecoder.actualValue = sample.state;
this._messagesSentDecoder.actualValue = sample.messagesSent;
this._bytesSentDecoder.actualValue = sample.bytesSent;
this._messagesReceivedDecoder.actualValue = sample.messagesReceived;
this._bytesReceivedDecoder.actualValue = sample.bytesReceived;
this._attachmentsDecoder.actualValue = sample.attachments;
}
}
exports.DataChannelDecoder = DataChannelDecoder;