xP: pass all events through one handler

This is a mild clean up.
This commit is contained in:
Přemysl Eric Janouch 2022-09-14 01:57:02 +02:00
parent fa85ea8208
commit f89f21a47f
Signed by: p
GPG Key ID: A0420B94F92B9493

View File

@ -84,10 +84,7 @@ class RelayRpc extends EventTarget {
throw "Invalid event tag" throw "Invalid event tag"
e.eventSeq = message.eventSeq e.eventSeq = message.eventSeq
this.dispatchEvent(new CustomEvent(e.event, {detail: e})) this.dispatchEvent(new CustomEvent('event', {detail: e}))
// Minor abstraction layering violation.
m.redraw()
return return
} }
@ -184,6 +181,7 @@ function updateIcon(highlighted) {
// ---- Event processing ------------------------------------------------------- // ---- Event processing -------------------------------------------------------
let rpc = new RelayRpc(proxy) let rpc = new RelayRpc(proxy)
let rpcEventHandlers = {}
let buffers = new Map() let buffers = new Map()
let bufferLast = undefined let bufferLast = undefined
@ -248,14 +246,23 @@ rpc.addEventListener('close', event => {
m.redraw() m.redraw()
}) })
rpc.addEventListener('Ping', event => { rpc.addEventListener('event', event => {
rpc.send({command: 'PingResponse', eventSeq: event.detail.eventSeq}) const handler = rpcEventHandlers[event.detail.event]
if (handler !== undefined) {
handler(event.detail)
if (bufferCurrent !== undefined || event.detail.event !== 'BufferLine')
m.redraw()
}
}) })
rpcEventHandlers['Ping'] = e => {
rpc.send({command: 'PingResponse', eventSeq: e.eventSeq})
}
// ~~~ Buffer events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~ Buffer events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rpc.addEventListener('BufferUpdate', event => { rpcEventHandlers['BufferUpdate'] = e => {
let e = event.detail, b = buffers.get(e.bufferName) let b = buffers.get(e.bufferName)
if (b === undefined) { if (b === undefined) {
buffers.set(e.bufferName, (b = { buffers.set(e.bufferName, (b = {
lines: [], lines: [],
@ -268,38 +275,36 @@ rpc.addEventListener('BufferUpdate', event => {
b.hideUnimportant = e.hideUnimportant b.hideUnimportant = e.hideUnimportant
b.kind = e.context.kind b.kind = e.context.kind
b.server = servers.get(e.context.serverName) b.server = servers.get(e.context.serverName)
}) }
rpc.addEventListener('BufferStats', event => { rpcEventHandlers['BufferStats'] = e => {
let e = event.detail, b = buffers.get(e.bufferName) let b = buffers.get(e.bufferName)
if (b === undefined) if (b === undefined)
return return
b.newMessages = e.newMessages, b.newMessages = e.newMessages,
b.newUnimportantMessages = e.newUnimportantMessages b.newUnimportantMessages = e.newUnimportantMessages
b.highlighted = e.highlighted b.highlighted = e.highlighted
}) }
rpc.addEventListener('BufferRename', event => { rpcEventHandlers['BufferRename'] = e => {
let e = event.detail
buffers.set(e.new, buffers.get(e.bufferName)) buffers.set(e.new, buffers.get(e.bufferName))
buffers.delete(e.bufferName) buffers.delete(e.bufferName)
}) }
rpc.addEventListener('BufferRemove', event => { rpcEventHandlers['BufferRemove'] = e => {
let e = event.detail
buffers.delete(e.bufferName) buffers.delete(e.bufferName)
if (e.bufferName === bufferLast) if (e.bufferName === bufferLast)
bufferLast = undefined bufferLast = undefined
}) }
rpc.addEventListener('BufferActivate', event => { rpcEventHandlers['BufferActivate'] = e => {
let old = buffers.get(bufferCurrent) let old = buffers.get(bufferCurrent)
if (old !== undefined) if (old !== undefined)
bufferResetStats(old) bufferResetStats(old)
bufferLast = bufferCurrent bufferLast = bufferCurrent
let e = event.detail, b = buffers.get(e.bufferName) let b = buffers.get(e.bufferName)
bufferCurrent = e.bufferName bufferCurrent = e.bufferName
bufferLog = undefined bufferLog = undefined
bufferAutoscroll = true bufferAutoscroll = true
@ -326,11 +331,12 @@ rpc.addEventListener('BufferActivate', event => {
textarea.value = b.input textarea.value = b.input
textarea.setSelectionRange(b.inputStart, b.inputEnd, b.inputDirection) textarea.setSelectionRange(b.inputStart, b.inputEnd, b.inputDirection)
} }
}) }
rpc.addEventListener('BufferLine', event => { rpcEventHandlers['BufferLine'] = e => {
let e = event.detail, b = buffers.get(e.bufferName), line = {...e} let b = buffers.get(e.bufferName), line = {...e}
delete line.event delete line.event
delete line.eventSeq
delete line.leakToActive delete line.leakToActive
if (b === undefined) if (b === undefined)
return return
@ -370,33 +376,31 @@ rpc.addEventListener('BufferLine', event => {
if (!visible) if (!visible)
b.highlighted = true b.highlighted = true
} }
}) }
rpc.addEventListener('BufferClear', event => { rpcEventHandlers['BufferClear'] = e => {
let e = event.detail, b = buffers.get(e.bufferName) let b = buffers.get(e.bufferName)
if (b !== undefined) if (b !== undefined)
b.lines.length = 0 b.lines.length = 0
}) }
// ~~~ Server events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~ Server events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rpc.addEventListener('ServerUpdate', event => { rpcEventHandlers['ServerUpdate'] = e => {
let e = event.detail, s = servers.get(e.serverName) let s = servers.get(e.serverName)
if (s === undefined) if (s === undefined)
servers.set(e.serverName, (s = {})) servers.set(e.serverName, (s = {}))
s.state = e.state s.state = e.state
}) }
rpc.addEventListener('ServerRename', event => { rpcEventHandlers['ServerRename'] = e => {
let e = event.detail
servers.set(e.new, servers.get(e.serverName)) servers.set(e.new, servers.get(e.serverName))
servers.delete(e.serverName) servers.delete(e.serverName)
}) }
rpc.addEventListener('ServerRemove', event => { rpcEventHandlers['ServerRemove'] = e => {
let e = event.detail
servers.delete(e.serverName) servers.delete(e.serverName)
}) }
// --- Colours ----------------------------------------------------------------- // --- Colours -----------------------------------------------------------------