-
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move arrpc server into a worker thread
- Loading branch information
Showing
4 changed files
with
122 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-3.0 | ||
* Vesktop, a desktop app aiming to give you a snappier Discord Experience | ||
* Copyright (c) 2023 Vendicated and Vencord contributors | ||
*/ | ||
|
||
import Server from "arrpc"; | ||
import { IpcEvents } from "shared/IpcEvents"; | ||
import { MessagePort, workerData } from "worker_threads"; | ||
|
||
import { ArrpcEvent, ArrpcHostEvent } from "./utils/arrpcWorkerTypes"; | ||
|
||
let server: any; | ||
|
||
type InviteCallback = (valid: boolean) => void; | ||
|
||
let inviteCallbacks: Array<InviteCallback> = []; | ||
|
||
(async function () { | ||
const { workerPort }: { workerPort: MessagePort } = workerData; | ||
server = await new Server(); | ||
server.on("activity", (data: any) => { | ||
const event: ArrpcEvent = { | ||
eventType: IpcEvents.ARRPC_ACTIVITY, | ||
data: JSON.stringify(data) | ||
}; | ||
workerPort.postMessage(event); | ||
}); | ||
server.on("invite", (invite: string, callback: InviteCallback) => { | ||
const event: ArrpcEvent = { | ||
eventType: "invite", | ||
data: invite, | ||
inviteId: inviteCallbacks.push(callback) - 1 | ||
}; | ||
workerPort.postMessage(event); | ||
}); | ||
|
||
workerPort.on("message", (e: ArrpcHostEvent) => { | ||
inviteCallbacks[e.inviteId](e.data); | ||
inviteCallbacks = [...inviteCallbacks.slice(0, e.inviteId), ...inviteCallbacks.slice(e.inviteId + 1)]; | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-3.0 | ||
* Vesktop, a desktop app aiming to give you a snappier Discord Experience | ||
* Copyright (c) 2023 Vendicated and Vencord contributors | ||
*/ | ||
|
||
import { IpcEvents } from "shared/IpcEvents"; | ||
|
||
export type ArrpcEvent = ArrpcActivityEvent | ArrpcInviteEvent; | ||
|
||
export interface ArrpcActivityEvent { | ||
eventType: IpcEvents.ARRPC_ACTIVITY; | ||
data: string; | ||
} | ||
|
||
export interface ArrpcInviteEvent { | ||
eventType: "invite"; | ||
data: string; | ||
inviteId: number; | ||
} | ||
|
||
export interface ArrpcHostEvent { | ||
eventType: "ack-invite"; | ||
inviteId: number; | ||
data: boolean; | ||
} |