Add event bus to pool for socket open/close
This commit is contained in:
+11
-2
@@ -1,16 +1,25 @@
|
||||
import {Socket} from "./util/Socket"
|
||||
import {EventBus} from "./util/EventBus"
|
||||
|
||||
export class Pool {
|
||||
data: Map<string, Socket>
|
||||
constructor() {
|
||||
this.data = new Map()
|
||||
this.bus = new EventBus()
|
||||
}
|
||||
has(url) {
|
||||
return this.data.has(url)
|
||||
}
|
||||
get(url) {
|
||||
if (!this.data.has(url)) {
|
||||
this.data.set(url, new Socket(url))
|
||||
const socket = new Socket(url)
|
||||
|
||||
this.data.set(url, socket)
|
||||
|
||||
socket.bus.addListeners({
|
||||
open: () => this.bus.emit('open', {url}),
|
||||
close: () => this.bus.emit('close', {url}),
|
||||
})
|
||||
}
|
||||
|
||||
return this.data.get(url)
|
||||
@@ -19,7 +28,7 @@ export class Pool {
|
||||
const socket = this.data.get(url)
|
||||
|
||||
if (socket) {
|
||||
socket.disconnect()
|
||||
socket.cleanup()
|
||||
this.data.delete(url)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user