Skip to content

Commit d99f5c2

Browse files
committed
lints
1 parent b2c6be9 commit d99f5c2

File tree

4 files changed

+173
-118
lines changed

4 files changed

+173
-118
lines changed

src/client/client.ts

+77-55
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ const TICK_INTERVAL = 50;
2424
const REQUEST_INTERVAL = 50;
2525

2626
export class Client extends Emitter<ClientEvents> {
27-
public socket!: Socket;
28-
public framer!: Framer;
27+
public socket!: Socket | null;
28+
public framer!: Framer | null;
2929
public options: ClientOptions;
3030
private tickTimer?: NodeJS.Timeout;
3131
private connectionTimeout?: NodeJS.Timeout;
3232
private requestInterval?: NodeJS.Timeout;
33-
public serverAddress!: Address;
33+
public serverAddress!: Address | null;
3434

3535
public status = Status.Disconnected;
3636

@@ -45,12 +45,12 @@ export class Client extends Emitter<ClientEvents> {
4545
this.socket = createSocket("udp4");
4646
this.framer = new Framer(this);
4747

48-
this.remove("tick", () => this.framer.tick());
49-
this.on("tick", () => this.framer.tick());
48+
this.remove("tick", () => this.framer?.tick());
49+
this.on("tick", () => this.framer?.tick());
5050

51-
this.socket.removeAllListeners("message");
51+
this.socket.removeAllListeners();
5252
this.socket.on("message", (payload, rinfo) => {
53-
this.framer.incommingMessage(payload, rinfo);
53+
this.framer?.incommingMessage(payload, rinfo);
5454
});
5555

5656
this.socket.on("error", (err) => {
@@ -171,6 +171,10 @@ export class Client extends Emitter<ClientEvents> {
171171

172172
public sendFrame(frame: Frame, priority: Priority): void {
173173
try {
174+
if (!this.framer) {
175+
Logger.error("[Client] Cannot send frame: framer is null");
176+
return;
177+
}
174178
this.framer.sendFrame(frame, priority);
175179
} catch (error) {
176180
Logger.error("[Client] Failed to send frame", error);
@@ -190,6 +194,11 @@ export class Client extends Emitter<ClientEvents> {
190194
return;
191195
}
192196

197+
if (!this.socket) {
198+
Logger.error("[Client] Cannot send packet: socket is null");
199+
return;
200+
}
201+
193202
Logger.debug(
194203
`[Client] Sending packet ${buffer[0]}, ${buffer.length} bytes to ${this.options.address}:${this.options.port}`,
195204
);
@@ -212,6 +221,62 @@ export class Client extends Emitter<ClientEvents> {
212221
}
213222
}
214223

224+
private cleanupSocket() {
225+
if (!this.socket) return;
226+
try {
227+
this.socket.removeAllListeners();
228+
Logger.cleanup();
229+
if (this.status === Status.Connected) {
230+
const disconnect = new DisconnectionNotification();
231+
this.socket.send(
232+
disconnect.serialize(),
233+
0,
234+
disconnect.serialize().length,
235+
this.options.port,
236+
this.options.address,
237+
);
238+
}
239+
240+
const stateSymbol = Symbol.for("state symbol");
241+
const socketWithState = this.socket as unknown as {
242+
[key: symbol]: { handle: { close: () => void } | null };
243+
};
244+
const state = socketWithState[stateSymbol];
245+
if (state?.handle) {
246+
state.handle.close();
247+
state.handle = null;
248+
}
249+
250+
this.socket.close(() => {
251+
console.log("socket closed");
252+
this.socket?.removeAllListeners();
253+
});
254+
255+
// (this.socket as { _handle?: unknown })._handle = undefined;
256+
} catch (err) {
257+
Logger.error("[Client] Error during socket cleanup", err as Error);
258+
try {
259+
const stateSymbol = Symbol.for("state symbol");
260+
const socketWithState = this.socket as unknown as {
261+
[key: symbol]: { handle: { close: () => void } | null };
262+
};
263+
const state = socketWithState[stateSymbol];
264+
if (state?.handle) {
265+
state.handle.close();
266+
state.handle = null;
267+
}
268+
} catch (_) {}
269+
this.socket = null;
270+
}
271+
}
272+
273+
private cleanupFramer() {
274+
if (!this.framer) return;
275+
(this.framer as { _events?: unknown })._events = undefined;
276+
(this.framer as { _eventsCount?: unknown })._eventsCount = undefined;
277+
this.framer = null;
278+
}
279+
215280
public cleanup(): void {
216281
if (this.status === Status.Disconnected) return;
217282

@@ -237,56 +302,13 @@ export class Client extends Emitter<ClientEvents> {
237302
this.removeAll();
238303
this.removeAllAfter();
239304
this.removeAllBefore();
240-
241-
if (this.socket) {
242-
try {
243-
this.socket.removeAllListeners();
244-
Logger.cleanup();
245-
if (wasConnected) {
246-
const disconnect = new DisconnectionNotification();
247-
this.socket.send(
248-
disconnect.serialize(),
249-
0,
250-
disconnect.serialize().length,
251-
this.options.port,
252-
this.options.address
253-
);
254-
}
255-
256-
const state = (this.socket as any)[Symbol.for('state symbol')];
257-
if (state?.handle) {
258-
state.handle.close();
259-
state.handle = null;
260-
}
261-
262-
this.socket.close(() => {
263-
console.log("socket closed");
264-
this.socket.removeAllListeners();
265-
});
266-
267-
delete (this.socket as any)._handle;
268-
} catch (err) {
269-
Logger.error("[Client] Error during socket cleanup", err as Error);
270-
try {
271-
const state = (this.socket as any)[Symbol.for('state symbol')];
272-
if (state?.handle) {
273-
state.handle.close();
274-
state.handle = null;
275-
}
276-
} catch (_) {}
277-
this.socket = undefined as any;
278-
}
279-
}
280305

281-
if (this.framer) {
282-
delete (this.framer as any)._events;
283-
delete (this.framer as any)._eventsCount;
284-
this.framer = undefined as any;
285-
}
306+
this.cleanupSocket();
307+
this.cleanupFramer();
286308

287-
this.serverAddress = undefined as any;
288-
delete (this as any)._events;
289-
delete (this as any)._eventsCount;
309+
this.serverAddress = null;
310+
(this as { _events?: unknown })._events = undefined;
311+
(this as { _eventsCount?: unknown })._eventsCount = undefined;
290312
this.status = Status.Disconnected;
291313

292314
Logger.debug("[Client] Cleanup complete");

0 commit comments

Comments
 (0)