use readonly constructor props.

This commit is contained in:
Miroslav Šedivý 2021-10-04 00:22:11 +02:00
parent fcc57bf2fc
commit 79856e29a1
10 changed files with 45 additions and 50 deletions

View File

@ -1,7 +1,7 @@
import * as Api from '../api' import * as Api from '../api'
export class NekoApi { export class NekoApi {
private _config = new Api.Configuration({ private readonly _config = new Api.Configuration({
basePath: location.href.replace(/\/+$/, ''), basePath: location.href.replace(/\/+$/, ''),
baseOptions: { withCredentials: true }, baseOptions: { withCredentials: true },
}) })

View File

@ -22,7 +22,6 @@ export interface NekoConnectionEvents {
} }
export class NekoConnection extends EventEmitter<NekoConnectionEvents> { export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
private _state: Connection
private _open = false private _open = false
public websocket = new NekoWebSocket() public websocket = new NekoWebSocket()
@ -40,13 +39,15 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
private _webrtcCongestionControlHandle: (stats: WebRTCStats) => void private _webrtcCongestionControlHandle: (stats: WebRTCStats) => void
constructor(state: Connection) { // eslint-disable-next-line
constructor(
private readonly _state: Connection,
) {
super() super()
this._state = state
this._reconnector = { this._reconnector = {
websocket: new Reconnector(new WebsocketReconnector(state, this.websocket), state.websocket.config), websocket: new Reconnector(new WebsocketReconnector(_state, this.websocket), _state.websocket.config),
webrtc: new Reconnector(new WebrtcReconnector(state, this.websocket, this.webrtc), state.webrtc.config), webrtc: new Reconnector(new WebrtcReconnector(_state, this.websocket, this.webrtc), _state.webrtc.config),
} }
this._onConnectHandle = () => { this._onConnectHandle = () => {

View File

@ -8,14 +8,14 @@ const FLUSH_TIMEOUT_MS = 250
const RETRY_INTERVAL_MS = 2500 const RETRY_INTERVAL_MS = 2500
export class NekoLoggerFactory { export class NekoLoggerFactory {
private _ws: NekoWebSocket
private _logs: message.SystemLog[] = [] private _logs: message.SystemLog[] = []
private _timeout: number | null = null private _timeout: number | null = null
private _interval: number | null = null private _interval: number | null = null
constructor(websocket: NekoWebSocket) { // eslint-disable-next-line
this._ws = websocket constructor(
} private readonly _ws: NekoWebSocket,
) {}
private _flush() { private _flush() {
if (this._logs.length > 0) { if (this._logs.length > 0) {

View File

@ -42,18 +42,18 @@ export interface NekoEvents {
} }
export class NekoMessages extends EventEmitter<NekoEvents> { export class NekoMessages extends EventEmitter<NekoEvents> {
private _connection: NekoConnection
private _state: NekoState
private _localLog: Logger private _localLog: Logger
private _remoteLog: Logger private _remoteLog: Logger
constructor(connection: NekoConnection, state: NekoState) { // eslint-disable-next-line
constructor(
private readonly _connection: NekoConnection,
private readonly _state: NekoState,
) {
super() super()
this._connection = connection
this._state = state
this._localLog = new Logger('messages') this._localLog = new Logger('messages')
this._remoteLog = connection.getLogger('messages') this._remoteLog = _connection.getLogger('messages')
this._connection.websocket.on('message', async (event: string, payload: any) => { this._connection.websocket.on('message', async (event: string, payload: any) => {
// @ts-ignore // @ts-ignore

View File

@ -31,7 +31,6 @@ export interface ReconnectorEvents {
} }
export class Reconnector extends EventEmitter<ReconnectorEvents> { export class Reconnector extends EventEmitter<ReconnectorEvents> {
private _conn: ReconnectorAbstract
private _config: ReconnectorConfig private _config: ReconnectorConfig
private _timeout?: number private _timeout?: number
@ -42,10 +41,13 @@ export class Reconnector extends EventEmitter<ReconnectorEvents> {
private _onConnectHandle: () => void private _onConnectHandle: () => void
private _onDisconnectHandle: (error?: Error) => void private _onDisconnectHandle: (error?: Error) => void
constructor(conn: ReconnectorAbstract, config?: ReconnectorConfig) { // eslint-disable-next-line
constructor(
private readonly _conn: ReconnectorAbstract,
config?: ReconnectorConfig,
) {
super() super()
this._conn = conn
this._config = { this._config = {
max_reconnects: 10, max_reconnects: 10,
timeout_ms: 1500, timeout_ms: 1500,

View File

@ -7,20 +7,17 @@ import { NekoWebRTC } from '../webrtc'
import { ReconnectorAbstract } from '.' import { ReconnectorAbstract } from '.'
export class WebrtcReconnector extends ReconnectorAbstract { export class WebrtcReconnector extends ReconnectorAbstract {
private _state: Connection
private _websocket: NekoWebSocket
private _webrtc: NekoWebRTC
private _onConnectHandle: () => void private _onConnectHandle: () => void
private _onDisconnectHandle: (error?: Error) => void private _onDisconnectHandle: (error?: Error) => void
constructor(state: Connection, websocket: NekoWebSocket, webrtc: NekoWebRTC) { // eslint-disable-next-line
constructor(
private readonly _state: Connection,
private readonly _websocket: NekoWebSocket,
private readonly _webrtc: NekoWebRTC,
) {
super() super()
this._state = state
this._websocket = websocket
this._webrtc = webrtc
this._onConnectHandle = () => this.emit('connect') this._onConnectHandle = () => this.emit('connect')
this._webrtc.on('connected', this._onConnectHandle) this._webrtc.on('connected', this._onConnectHandle)

View File

@ -5,18 +5,16 @@ import { NekoWebSocket } from '../websocket'
import { ReconnectorAbstract } from '.' import { ReconnectorAbstract } from '.'
export class WebsocketReconnector extends ReconnectorAbstract { export class WebsocketReconnector extends ReconnectorAbstract {
private _state: Connection
private _websocket: NekoWebSocket
private _onConnectHandle: () => void private _onConnectHandle: () => void
private _onDisconnectHandle: (error?: Error) => void private _onDisconnectHandle: (error?: Error) => void
constructor(state: Connection, websocket: NekoWebSocket) { // eslint-disable-next-line
constructor(
private readonly _state: Connection,
private readonly _websocket: NekoWebSocket,
) {
super() super()
this._state = state
this._websocket = websocket
this._onConnectHandle = () => this.emit('connect') this._onConnectHandle = () => this.emit('connect')
this._websocket.on('connected', this._onConnectHandle) this._websocket.on('connected', this._onConnectHandle)

View File

@ -33,13 +33,13 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
private _track?: MediaStreamTrack private _track?: MediaStreamTrack
private _state: RTCIceConnectionState = 'disconnected' private _state: RTCIceConnectionState = 'disconnected'
private _candidates: RTCIceCandidateInit[] = [] private _candidates: RTCIceCandidateInit[] = []
private _log: Logger
private _statsStop?: () => void private _statsStop?: () => void
constructor(logger?: Logger) { // eslint-disable-next-line
constructor(
private readonly _log: Logger = new Logger('webrtc'),
) {
super() super()
this._log = logger || new Logger('webrtc')
} }
get supported() { get supported() {

View File

@ -10,12 +10,12 @@ export interface NekoWebSocketEvents {
export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> { export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
private _ws?: WebSocket private _ws?: WebSocket
private _log: Logger
constructor(logger?: Logger) { // eslint-disable-next-line
constructor(
private readonly _log: Logger = new Logger('websocket'),
) {
super() super()
this._log = logger || new Logger('websocket')
} }
get supported() { get supported() {

View File

@ -1,11 +1,8 @@
export class Logger { export class Logger {
protected _scope: string = 'main' // eslint-disable-next-line
constructor(
constructor(scope?: string) { protected readonly _scope: string = 'main',
if (scope) { ) {}
this._scope = scope
}
}
protected _console(level: string, m: string, fields?: Record<string, any>) { protected _console(level: string, m: string, fields?: Record<string, any>) {
let t = '' let t = ''