79 lines
1.7 KiB
JavaScript
79 lines
1.7 KiB
JavaScript
|
const m = require('mithril')
|
||
|
var io = require('./socket')
|
||
|
var socket = io()
|
||
|
|
||
|
class Client {
|
||
|
constructor(socket) {
|
||
|
this.socket = socket
|
||
|
this.isConnected = false
|
||
|
|
||
|
this.socket.on('disconnect', this.disconnected.bind(this))
|
||
|
this.socket.on('connect', this.connected.bind(this))
|
||
|
this.components = new Map()
|
||
|
}
|
||
|
|
||
|
registerComponent(component) {
|
||
|
this.components.set(component, [])
|
||
|
if (component.ioInit) {
|
||
|
component.ioInit()
|
||
|
}
|
||
|
if (this.isConnected && component.ioConnected) {
|
||
|
component.ioConnected()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
safeGetComponent(component) {
|
||
|
if (typeof(component) === 'string') {
|
||
|
throw new Error('io.on was called without specifying active component')
|
||
|
}
|
||
|
let arr = this.components.get(component)
|
||
|
if (!arr) {
|
||
|
throw new Error('Registered component was missing but was attempting to add socket listener')
|
||
|
}
|
||
|
return arr
|
||
|
}
|
||
|
|
||
|
on(component, name, fn) {
|
||
|
let arr = this.safeGetComponent(component)
|
||
|
arr.push([name, fn])
|
||
|
|
||
|
this.socket.on(name, (data) => {
|
||
|
Promise.resolve().then(() => {
|
||
|
return fn(data)
|
||
|
})
|
||
|
.catch(err => {
|
||
|
console.error('Error running io handler for ' + name, err)
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
emit(name, data) {
|
||
|
this.socket.emit(name, data)
|
||
|
}
|
||
|
|
||
|
unregisterComponent(component) {
|
||
|
let arr = this.safeGetComponent(component)
|
||
|
for (let item of arr) {
|
||
|
this.socket.off(item[0], item[1])
|
||
|
}
|
||
|
this.components.delete(component)
|
||
|
}
|
||
|
|
||
|
disconnected() {
|
||
|
this.isConnected = false
|
||
|
m.redraw()
|
||
|
}
|
||
|
|
||
|
connected() {
|
||
|
this.isConnected = true
|
||
|
for (let component of this.components) {
|
||
|
if (component[0].ioConnected) {
|
||
|
component[0].ioConnected()
|
||
|
}
|
||
|
}
|
||
|
m.redraw()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = new Client(socket)
|