Add browser support with Socket.io transport, authentication, performance-optimized IPC, and CI/CD (#925)
This commit is contained in:
42
events/EventSystem/SocketIOClientEventBus.ts
Normal file
42
events/EventSystem/SocketIOClientEventBus.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Socket } from 'socket.io-client'
|
||||
import { CallbackStore } from './CallbackStore'
|
||||
import { EventBusInterface } from './EventBusInterface'
|
||||
import { Event } from '../Events'
|
||||
|
||||
export class SocketIOClientEventBus implements EventBusInterface {
|
||||
private socket: Socket
|
||||
private callbacks: Array<CallbackStore> = []
|
||||
|
||||
constructor(socket: Socket) {
|
||||
this.socket = socket
|
||||
}
|
||||
|
||||
public subscribe<MessageType>(event: Event<MessageType>, callback: (msg: MessageType) => void) {
|
||||
const wrappedCallback = (arg: any) => {
|
||||
callback(arg)
|
||||
}
|
||||
console.log('subscribing', event.topic)
|
||||
this.socket.on(event.topic, wrappedCallback)
|
||||
this.callbacks.push({
|
||||
callback,
|
||||
wrappedCallback,
|
||||
})
|
||||
}
|
||||
|
||||
public unsubscribeAll<MessageType>(event: Event<MessageType>) {
|
||||
this.socket.removeAllListeners(event.topic)
|
||||
}
|
||||
|
||||
public unsubscribe<MessageType>(event: Event<MessageType>, callback: any) {
|
||||
const item = this.callbacks.find(store => store.callback === callback)
|
||||
if (!item) {
|
||||
return
|
||||
}
|
||||
this.socket.off(event.topic, item.wrappedCallback)
|
||||
this.callbacks = this.callbacks.filter(a => a !== item)
|
||||
}
|
||||
|
||||
public emit<MessageType>(event: Event<MessageType>, msg: MessageType) {
|
||||
this.socket.emit(event.topic, msg)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user