From f89f21a47fd2764ab48d908dd93dd78f8ff8b471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Wed, 14 Sep 2022 01:57:02 +0200 Subject: [PATCH] xP: pass all events through one handler This is a mild clean up. --- xP/public/xP.js | 76 ++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/xP/public/xP.js b/xP/public/xP.js index ec2b476..ca5fcf2 100644 --- a/xP/public/xP.js +++ b/xP/public/xP.js @@ -84,10 +84,7 @@ class RelayRpc extends EventTarget { throw "Invalid event tag" e.eventSeq = message.eventSeq - this.dispatchEvent(new CustomEvent(e.event, {detail: e})) - - // Minor abstraction layering violation. - m.redraw() + this.dispatchEvent(new CustomEvent('event', {detail: e})) return } @@ -184,6 +181,7 @@ function updateIcon(highlighted) { // ---- Event processing ------------------------------------------------------- let rpc = new RelayRpc(proxy) +let rpcEventHandlers = {} let buffers = new Map() let bufferLast = undefined @@ -248,14 +246,23 @@ rpc.addEventListener('close', event => { m.redraw() }) -rpc.addEventListener('Ping', event => { - rpc.send({command: 'PingResponse', eventSeq: event.detail.eventSeq}) +rpc.addEventListener('event', event => { + 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -rpc.addEventListener('BufferUpdate', event => { - let e = event.detail, b = buffers.get(e.bufferName) +rpcEventHandlers['BufferUpdate'] = e => { + let b = buffers.get(e.bufferName) if (b === undefined) { buffers.set(e.bufferName, (b = { lines: [], @@ -268,38 +275,36 @@ rpc.addEventListener('BufferUpdate', event => { b.hideUnimportant = e.hideUnimportant b.kind = e.context.kind b.server = servers.get(e.context.serverName) -}) +} -rpc.addEventListener('BufferStats', event => { - let e = event.detail, b = buffers.get(e.bufferName) +rpcEventHandlers['BufferStats'] = e => { + let b = buffers.get(e.bufferName) if (b === undefined) return b.newMessages = e.newMessages, b.newUnimportantMessages = e.newUnimportantMessages b.highlighted = e.highlighted -}) +} -rpc.addEventListener('BufferRename', event => { - let e = event.detail +rpcEventHandlers['BufferRename'] = e => { buffers.set(e.new, buffers.get(e.bufferName)) buffers.delete(e.bufferName) -}) +} -rpc.addEventListener('BufferRemove', event => { - let e = event.detail +rpcEventHandlers['BufferRemove'] = e => { buffers.delete(e.bufferName) if (e.bufferName === bufferLast) bufferLast = undefined -}) +} -rpc.addEventListener('BufferActivate', event => { +rpcEventHandlers['BufferActivate'] = e => { let old = buffers.get(bufferCurrent) if (old !== undefined) bufferResetStats(old) bufferLast = bufferCurrent - let e = event.detail, b = buffers.get(e.bufferName) + let b = buffers.get(e.bufferName) bufferCurrent = e.bufferName bufferLog = undefined bufferAutoscroll = true @@ -326,11 +331,12 @@ rpc.addEventListener('BufferActivate', event => { textarea.value = b.input textarea.setSelectionRange(b.inputStart, b.inputEnd, b.inputDirection) } -}) +} -rpc.addEventListener('BufferLine', event => { - let e = event.detail, b = buffers.get(e.bufferName), line = {...e} +rpcEventHandlers['BufferLine'] = e => { + let b = buffers.get(e.bufferName), line = {...e} delete line.event + delete line.eventSeq delete line.leakToActive if (b === undefined) return @@ -370,33 +376,31 @@ rpc.addEventListener('BufferLine', event => { if (!visible) b.highlighted = true } -}) +} -rpc.addEventListener('BufferClear', event => { - let e = event.detail, b = buffers.get(e.bufferName) +rpcEventHandlers['BufferClear'] = e => { + let b = buffers.get(e.bufferName) if (b !== undefined) b.lines.length = 0 -}) +} // ~~~ Server events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -rpc.addEventListener('ServerUpdate', event => { - let e = event.detail, s = servers.get(e.serverName) +rpcEventHandlers['ServerUpdate'] = e => { + let s = servers.get(e.serverName) if (s === undefined) servers.set(e.serverName, (s = {})) s.state = e.state -}) +} -rpc.addEventListener('ServerRename', event => { - let e = event.detail +rpcEventHandlers['ServerRename'] = e => { servers.set(e.new, servers.get(e.serverName)) servers.delete(e.serverName) -}) +} -rpc.addEventListener('ServerRemove', event => { - let e = event.detail +rpcEventHandlers['ServerRemove'] = e => { servers.delete(e.serverName) -}) +} // --- Colours -----------------------------------------------------------------