Skip to content

Commit

Permalink
Refactor WeechatConnection, fix type annotations
Browse files Browse the repository at this point in the history
Move initialization of WeechatConnection fields into the constructor.
This ensures the required fields are always set. Replace remaining any
type annotations with proper types.
  • Loading branch information
mhoran committed Feb 21, 2024
1 parent db89089 commit 92eeccc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
33 changes: 18 additions & 15 deletions src/lib/weechat/connection.ts
Original file line number Diff line number Diff line change
@@ -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<StoreState, undefined, AnyAction>;
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<StoreState, undefined, AnyAction>,
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();
}

Expand Down Expand Up @@ -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'
});
Expand All @@ -80,7 +82,7 @@ export default class WeechatConnection {
}

onmessage(event: WebSocketMessageEvent): void {
const parsed = protocol.parse(event.data) as WeechatResponse<any>;
const parsed = protocol.parse(event.data) as WeechatResponse<unknown>;

Check failure on line 85 in src/lib/weechat/connection.ts

View workflow job for this annotation

GitHub Actions / Run tsc

Expected 2 arguments, but got 1.

console.log('Parsed data:', parsed);
try {
Expand All @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
14 changes: 9 additions & 5 deletions src/usecase/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ export default class WeechatNative extends React.Component<null, State> {
connecting: false
};

connection: WeechatConnection;
connection?: WeechatConnection;

constructor(props: null) {
super(props);
this.connection = new WeechatConnection(store.dispatch);
}

setNotificationToken = async (): Promise<void> => {
Expand All @@ -48,21 +47,26 @@ export default class WeechatNative extends React.Component<null, State> {
};

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`
Expand Down

0 comments on commit 92eeccc

Please sign in to comment.