generated from vivid-lapin/ts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDrpcMain.ts
63 lines (61 loc) · 1.72 KB
/
DrpcMain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { DRPC_ACTIVITY_EVENT_ID, DRPC_META } from "./constants"
import { RPC } from "./rpcLoader"
import { InitPlugin } from "../@types/plugin"
import { wait } from "../shared/utils"
export const DrpcMain: InitPlugin["main"] = async ({ packages }) => {
let rpc: RPC.Client | undefined
let isAlive = true
const clientId = "828277784396824596"
const loop = async () => {
while (isAlive) {
try {
const _rpc = new RPC.Client({ transport: "ipc" })
RPC.register(clientId)
await _rpc.login({ clientId })
packages.Electron.ipcMain.removeHandler(DRPC_ACTIVITY_EVENT_ID)
packages.Electron.ipcMain.handle(
DRPC_ACTIVITY_EVENT_ID,
(_ev, args) => {
if (args !== null) {
_rpc.setActivity(args)
} else {
_rpc.clearActivity()
}
}
)
rpc = _rpc
await Promise.all([
wait(1000 * 60),
new Promise<void>((res) => {
_rpc.addListener("disconnected", () => {
console.info(`[${DRPC_META.name}] disconnected`)
_rpc.removeAllListeners()
res()
})
}),
])
} catch (error) {
if (error instanceof Error) {
console.error(`[${DRPC_META.name}] ${error.message}`)
} else {
console.error(error)
}
packages.Electron.ipcMain.removeHandler(DRPC_ACTIVITY_EVENT_ID)
packages.Electron.ipcMain.handle(DRPC_ACTIVITY_EVENT_ID, () => {
return
})
await wait(1000 * 60)
}
}
}
return {
...DRPC_META,
setup: () => {
loop()
},
destroy: () => {
isAlive = false
rpc?.destroy()
},
}
}