Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions plugins/shelteRPC/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@

let maybeUnregisterGameSettings = [() => {}]

let ws: WebSocket
let ws: WebSocket | null
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
let unloading = false

function scheduleReconnect() {
if (unloading || reconnectTimer) return

reconnectTimer = setTimeout(() => {
reconnectTimer = null
onLoad()
}, store.retryWait ?? 3000)
}

const apps: Record<string, { name: string } | string> = {}

// when we load, set current game as nothing
Expand Down Expand Up @@ -157,14 +169,27 @@
}

export const onLoad = async () => {
if (ws && ws?.close) ws.close()
unloading = false

if (reconnectTimer) {
clearTimeout(reconnectTimer)
reconnectTimer = null
}

if (ws) {
ws.onclose = null
ws.onerror = null
ws.close()
ws = null
}

const connected = await retry(
async (curTry) => {
ws = new WebSocket('ws://' + (store.connAddr || '127.0.0.1:1337'))
ws.onmessage = handleMessage
ws.onerror = (e) => { throw e }

ws.onerror = () => {
try { ws?.close?.() } catch {}

Check failure on line 191 in plugins/shelteRPC/index.tsx

View workflow job for this annotation

GitHub Actions / tsc-eslint-checks

Empty block statement
}
await new Promise((r) => setTimeout(r, 1000))

if (ws.readyState !== WebSocket.OPEN) {
Expand All @@ -186,6 +211,8 @@
store.retryWait ?? 3000,
)

maybeUnregisterGameSettings.forEach((section) => section())

maybeUnregisterGameSettings = [
registerSection('divider'),
registerSection('header', 'shelteRPC'),
Expand All @@ -198,13 +225,15 @@
]

if (!connected) return

ws.onclose = () => {
showToast({
title: 'ShelteRPC',
content: 'ShelteRPC server disconnected',
content: 'ShelteRPC server disconnected; reconnecting...',
duration: 3000,
})

scheduleReconnect()
}

showToast({
Expand All @@ -215,6 +244,13 @@
}

export const onUnload = async () => {
unloading = true

if (reconnectTimer) {
clearTimeout(reconnectTimer)
reconnectTimer = null
}

if (ws?.close) ws.close?.()

if (maybeUnregisterGameSettings) {
Expand Down Expand Up @@ -257,4 +293,4 @@
/>
</div>
</>
)
)
Loading