From 61cd7b4d99d4a4b90adb73530610d578a7b46334 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Fri, 13 Oct 2023 03:49:58 +0200 Subject: arrpc: refactor for use in vesktop --- src/plugins/arRPC.web/index.tsx | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/plugins/arRPC.web/index.tsx b/src/plugins/arRPC.web/index.tsx index f0d4841..1db0c45 100644 --- a/src/plugins/arRPC.web/index.tsx +++ b/src/plugins/arRPC.web/index.tsx @@ -58,6 +58,26 @@ export default definePlugin({ ), + async handleEvent(e: MessageEvent) { + const data = JSON.parse(e.data); + + const { activity } = data; + const assets = activity?.assets; + + if (assets?.large_image) assets.large_image = await lookupAsset(activity.application_id, assets.large_image); + if (assets?.small_image) assets.small_image = await lookupAsset(activity.application_id, assets.small_image); + + if (activity) { + const appId = activity.application_id; + apps[appId] ||= await lookupApp(appId); + + const app = apps[appId]; + activity.name ||= app.name; + } + + FluxDispatcher.dispatch({ type: "LOCAL_ACTIVITY_UPDATE", ...data }); + }, + async start() { // ArmCord comes with its own arRPC implementation, so this plugin just confuses users if ("armcord" in window) return; @@ -65,22 +85,7 @@ export default definePlugin({ if (ws) ws.close(); ws = new WebSocket("ws://127.0.0.1:1337"); // try to open WebSocket - ws.onmessage = async e => { // on message, set status to data - const data = JSON.parse(e.data); - - if (data.activity?.assets?.large_image) data.activity.assets.large_image = await lookupAsset(data.activity.application_id, data.activity.assets.large_image); - if (data.activity?.assets?.small_image) data.activity.assets.small_image = await lookupAsset(data.activity.application_id, data.activity.assets.small_image); - - if (data.activity) { - const appId = data.activity.application_id; - apps[appId] ||= await lookupApp(appId); - - const app = apps[appId]; - data.activity.name ||= app.name; - } - - FluxDispatcher.dispatch({ type: "LOCAL_ACTIVITY_UPDATE", ...data }); - }; + ws.onmessage = this.handleEvent; const connectionSuccessful = await new Promise(res => setTimeout(() => res(ws.readyState === WebSocket.OPEN), 1000)); // check if open after 1s if (!connectionSuccessful) { -- cgit