-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathBaseChannel.ts
More file actions
109 lines (93 loc) · 2.53 KB
/
BaseChannel.ts
File metadata and controls
109 lines (93 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import type { Socket } from "socket.io-client"
/**
* Abstract base class for communication channels in the bridge system.
* Provides common functionality for bidirectional communication between
* the VSCode extension and web application.
*
* @template TCommand - Type of commands this channel can receive.
* @template TEvent - Type of events this channel can publish.
*/
export abstract class BaseChannel<TCommand = unknown, TEventName extends string = string, TEventData = unknown> {
protected socket: Socket | null = null
protected readonly instanceId: string
constructor(instanceId: string) {
this.instanceId = instanceId
}
/**
* Called when socket connects.
*/
public async onConnect(socket: Socket): Promise<void> {
this.socket = socket
await this.handleConnect(socket)
}
/**
* Called when socket disconnects.
*/
public onDisconnect(): void {
this.socket = null
this.handleDisconnect()
}
/**
* Called when socket reconnects.
*/
public async onReconnect(socket: Socket): Promise<void> {
this.socket = socket
await this.handleReconnect(socket)
}
/**
* Cleanup resources.
*/
public async cleanup(socket: Socket | null): Promise<void> {
if (socket) {
await this.handleCleanup(socket)
}
this.socket = null
}
/**
* Emit a socket event with error handling.
*/
protected publish<Params extends object>(
eventName: TEventName,
data: TEventData,
callback?: (params: Params) => void,
): boolean {
if (!this.socket) {
console.error(`[${this.constructor.name}#emit] socket not available for ${eventName}`)
return false
}
try {
// console.log(`[${this.constructor.name}#emit] emit() -> ${eventName}`, data)
this.socket.emit(eventName, data, callback)
return true
} catch (error) {
console.error(
`[${this.constructor.name}#emit] emit() failed -> ${eventName}: ${
error instanceof Error ? error.message : String(error)
}`,
)
return false
}
}
/**
* Handle incoming commands - must be implemented by subclasses.
*/
public abstract handleCommand(command: TCommand): Promise<void>
/**
* Handle connection-specific logic.
*/
protected abstract handleConnect(socket: Socket): Promise<void>
/**
* Handle disconnection-specific logic.
*/
protected handleDisconnect(): void {
// Default implementation - can be overridden.
}
/**
* Handle reconnection-specific logic.
*/
protected abstract handleReconnect(socket: Socket): Promise<void>
/**
* Handle cleanup-specific logic.
*/
protected abstract handleCleanup(socket: Socket): Promise<void>
}