From 92eecccfcbe9f4a2c7db8a156349d7670c973250 Mon Sep 17 00:00:00 2001 From: Matthew Horan Date: Tue, 20 Feb 2024 19:02:35 -0500 Subject: [PATCH] Refactor WeechatConnection, fix type annotations Move initialization of WeechatConnection fields into the constructor. This ensures the required fields are always set. Replace remaining any type annotations with proper types. --- src/lib/weechat/connection.ts | 33 ++++++++++++++++++--------------- src/store/index.ts | 2 +- src/usecase/Root.tsx | 14 +++++++++----- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/lib/weechat/connection.ts b/src/lib/weechat/connection.ts index 9732134..ac866cf 100644 --- a/src/lib/weechat/connection.ts +++ b/src/lib/weechat/connection.ts @@ -1,39 +1,41 @@ import { WeeChatProtocol } from './parser'; import { transformToReduxAction } from './action_transformer'; +import { StoreState } from '../../store'; +import { ThunkDispatch } from 'redux-thunk'; +import { AnyAction } from 'redux'; const protocol = new WeeChatProtocol(); export default class WeechatConnection { - dispatch: any; + dispatch: ThunkDispatch; hostname: string; password: string; - ssl: boolean; - compressed: boolean; - websocket: WebSocket; + ssl: boolean = true; + compressed: boolean = false; + websocket?: WebSocket; onSuccess: (conn: WeechatConnection) => void; onError: (reconnect: boolean) => void; connected: boolean; reconnect: boolean; - constructor(dispatch) { - this.dispatch = dispatch; - this.websocket = null; - this.reconnect = this.connected = false; - } - - connect( + constructor( + dispatch: ThunkDispatch, host: string, - password = '', + password: string, ssl: boolean, onSuccess: (conn: WeechatConnection) => void, onError: (reconnect: boolean) => void - ): void { + ) { + this.dispatch = dispatch; this.hostname = host; this.password = password; this.ssl = ssl; this.onSuccess = onSuccess; this.onError = onError; + this.reconnect = this.connected = false; + } + connect(): void { this.openSocket(); } @@ -68,7 +70,7 @@ export default class WeechatConnection { close(): void { this.connected = false; this.send('quit'); - this.websocket.close(); + this.websocket?.close(); this.dispatch({ type: 'DISCONNECT' }); @@ -80,7 +82,7 @@ export default class WeechatConnection { } onmessage(event: WebSocketMessageEvent): void { - const parsed = protocol.parse(event.data) as WeechatResponse; + const parsed = protocol.parse(event.data) as WeechatResponse; console.log('Parsed data:', parsed); try { @@ -94,6 +96,7 @@ export default class WeechatConnection { } send(data: string): void { + if (!this.websocket) return; console.log('Sending data:', data); this.websocket.send(data + '\n'); } diff --git a/src/store/index.ts b/src/store/index.ts index 6c99229..5d7c3b6 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -31,7 +31,7 @@ const initialState: AppState = { const app = ( state: AppState = initialState, - action: { type: string; bufferId: string } + action: { type: string; bufferId?: string } ) => { switch (action.type) { case 'DISCONNECT': diff --git a/src/usecase/Root.tsx b/src/usecase/Root.tsx index b9452b3..609b123 100644 --- a/src/usecase/Root.tsx +++ b/src/usecase/Root.tsx @@ -20,11 +20,10 @@ export default class WeechatNative extends React.Component { connecting: false }; - connection: WeechatConnection; + connection?: WeechatConnection; constructor(props: null) { super(props); - this.connection = new WeechatConnection(store.dispatch); } setNotificationToken = async (): Promise => { @@ -48,21 +47,26 @@ export default class WeechatNative extends React.Component { }; disconnect = (): void => { - this.connection.close(); + this.connection && this.connection.close(); }; onConnect = (hostname: string, password: string, ssl: boolean): void => { this.setState({ connecting: true }); - this.connection.connect( + this.connection = new WeechatConnection( + store.dispatch, hostname, password, ssl, this.onConnectionSuccess, this.onConnectionError ); + this.connection.connect(); }; - fetchBufferInfo = (bufferId: string, numLines = Buffer.DEFAULT_LINE_INCREMENT): void => { + fetchBufferInfo = ( + bufferId: string, + numLines = Buffer.DEFAULT_LINE_INCREMENT + ): void => { if (this.connection) { this.connection.send( `(lines) hdata buffer:0x${bufferId}/own_lines/last_line(-${numLines})/data`