Remove getChannelStates and _channelStates abstraction, use an object instead
This commit is contained in:
parent
b6cca28838
commit
86a4911502
|
|
@ -22,10 +22,25 @@ import * as Log from './util/logging.js';
|
||||||
const ENABLE_COPYWITHIN = false;
|
const ENABLE_COPYWITHIN = false;
|
||||||
const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024; // 40 MiB
|
const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024; // 40 MiB
|
||||||
|
|
||||||
|
// Constants pulled from RTCDataChannelState enum
|
||||||
|
// https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/readyState#RTCDataChannelState_enum
|
||||||
|
const DataChannel = {
|
||||||
|
CONNECTING: "connecting",
|
||||||
|
OPEN: "open",
|
||||||
|
CLOSING: "closing",
|
||||||
|
CLOSED: "closed"
|
||||||
|
};
|
||||||
|
|
||||||
|
const ReadyStates = {
|
||||||
|
CONNECTING: [WebSocket.CONNECTING, DataChannel.CONNECTING],
|
||||||
|
OPEN: [WebSocket.OPEN, DataChannel.OPEN],
|
||||||
|
CLOSING: [WebSocket.CLOSING, DataChannel.CLOSING],
|
||||||
|
CLOSED: [WebSocket.CLOSED, DataChannel.CLOSED],
|
||||||
|
};
|
||||||
|
|
||||||
export default class WebChannel {
|
export default class WebChannel {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._rawChannel = null; // WebSocket or RTCDataChannel object
|
this._rawChannel = null; // WebSocket or RTCDataChannel object
|
||||||
this._channelStates = null; // Cross compatible states enum for WebSocket / RTCDataChannel
|
|
||||||
|
|
||||||
this._rQi = 0; // Receive queue index
|
this._rQi = 0; // Receive queue index
|
||||||
this._rQlen = 0; // Next write position in the receive queue
|
this._rQlen = 0; // Next write position in the receive queue
|
||||||
|
|
@ -144,7 +159,7 @@ export default class WebChannel {
|
||||||
// Send Queue
|
// Send Queue
|
||||||
|
|
||||||
flush() {
|
flush() {
|
||||||
if (this._sQlen > 0 && this._rawChannel.readyState === this._channelStates.OPEN) {
|
if (this._sQlen > 0 && ReadyStates.OPEN.includes(this._rawChannel.readyState)) {
|
||||||
this._rawChannel.send(this._encode_message());
|
this._rawChannel.send(this._encode_message());
|
||||||
this._sQlen = 0;
|
this._sQlen = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -174,28 +189,6 @@ export default class WebChannel {
|
||||||
this._sQ = new Uint8Array(this._sQbufferSize);
|
this._sQ = new Uint8Array(this._sQbufferSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
_getChannelStates(channelType) {
|
|
||||||
if (channelType === "WebSocket") {
|
|
||||||
return {
|
|
||||||
CONNECTING: WebSocket.CONNECTING,
|
|
||||||
OPEN: WebSocket.OPEN,
|
|
||||||
CLOSING: WebSocket.CLOSING,
|
|
||||||
CLOSED: WebSocket.CLOSED
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (channelType === "RTCDataChannel") {
|
|
||||||
// Constants pulled from RTCDataChannelState enum
|
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/readyState#RTCDataChannelState_enum
|
|
||||||
return {
|
|
||||||
CONNECTING: "connecting",
|
|
||||||
OPEN: "open",
|
|
||||||
CLOSING: "closing",
|
|
||||||
CLOSED: "closed"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
throw new Error(`channelType: ${channelType} not recognized`);
|
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
this._allocate_buffers();
|
this._allocate_buffers();
|
||||||
this._rQi = 0;
|
this._rQi = 0;
|
||||||
|
|
@ -214,8 +207,6 @@ export default class WebChannel {
|
||||||
this._rawChannel.binaryType = "arraybuffer";
|
this._rawChannel.binaryType = "arraybuffer";
|
||||||
this._rawChannel.onmessage = this._recv_message.bind(this);
|
this._rawChannel.onmessage = this._recv_message.bind(this);
|
||||||
|
|
||||||
this._channelStates = this._getChannelStates("WebSocket");
|
|
||||||
|
|
||||||
const onOpen = () => {
|
const onOpen = () => {
|
||||||
Log.Debug(`>> WebChannel.onopen`);
|
Log.Debug(`>> WebChannel.onopen`);
|
||||||
if (this._rawChannel.protocol) {
|
if (this._rawChannel.protocol) {
|
||||||
|
|
@ -247,8 +238,8 @@ export default class WebChannel {
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
if (this._rawChannel) {
|
if (this._rawChannel) {
|
||||||
if ((this._rawChannel.readyState === this._channelStates.OPEN) ||
|
if ((ReadyStates.CONNECTING.includes(this._rawChannel.readyState)) ||
|
||||||
(this._rawChannel.readyState === this._channelStates.CONNECTING)) {
|
(ReadyStates.OPEN.includes(this._rawChannel.readyState))) {
|
||||||
Log.Info(`Closing WebChannel connection`);
|
Log.Info(`Closing WebChannel connection`);
|
||||||
this._rawChannel.close();
|
this._rawChannel.close();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue