2024-02-13 23:54:45 +00:00
|
|
|
const m = require('mithril')
|
|
|
|
var io = require('./socket')
|
|
|
|
var socket = io()
|
|
|
|
|
2024-02-20 04:57:49 +00:00
|
|
|
let onevent = socket.onevent
|
|
|
|
|
|
|
|
socket.onevent = function(packet) {
|
|
|
|
onevent.call(this, packet) // original call
|
|
|
|
m.redraw()
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:54:45 +00:00
|
|
|
class Client {
|
|
|
|
constructor(socket) {
|
|
|
|
this.socket = socket
|
|
|
|
this.isConnected = false
|
2024-02-20 04:57:49 +00:00
|
|
|
this.status = {}
|
2024-02-13 23:54:45 +00:00
|
|
|
|
2024-02-20 04:57:49 +00:00
|
|
|
this.socket.on('status', status => {
|
2024-02-25 22:01:37 +00:00
|
|
|
if (this.status.version && status?.version && this.status.version !== status.version) {
|
|
|
|
window.location.reload(true)
|
|
|
|
}
|
2024-02-20 04:57:49 +00:00
|
|
|
this.status = status
|
|
|
|
})
|
2024-02-13 23:54:45 +00:00
|
|
|
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
|
2024-02-20 04:57:49 +00:00
|
|
|
this.status = { }
|
|
|
|
this.notifyConnectionChanged()
|
2024-02-13 23:54:45 +00:00
|
|
|
m.redraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
connected() {
|
|
|
|
this.isConnected = true
|
2024-02-20 04:57:49 +00:00
|
|
|
this.socket.emit('status', {})
|
|
|
|
this.notifyConnectionChanged()
|
|
|
|
m.redraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyConnectionChanged() {
|
2024-02-13 23:54:45 +00:00
|
|
|
for (let component of this.components) {
|
2024-02-20 04:57:49 +00:00
|
|
|
if (component[0].ioConnectionChanged) {
|
|
|
|
component[0].ioConnectionChanged(this.isConnected)
|
2024-02-13 23:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new Client(socket)
|