mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-19 09:13:58 +00:00
feat!: big feature changes
BREAKING CHANGES: - Heartbeating removed - `config.consoleLogLevel` -> `config.logLevel` NEW FEATURES: - Training messages - Sequence number system - WebSocket close codes used instead of disconnect packets FIXES: - Improved error handling - Some performance improvements - Made code more clean - Updated dependencies
This commit is contained in:
26
packages/api/src/utils/packets.ts
Normal file
26
packages/api/src/utils/packets.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import type { Packet, ServerOperation } from '@revanced/bot-shared'
|
||||
import type { ClientWebSocketManager } from 'src/classes'
|
||||
|
||||
export function awaitPacket<TOp extends ServerOperation>(
|
||||
ws: ClientWebSocketManager,
|
||||
op: TOp,
|
||||
expectedSeq: number,
|
||||
timeout = 10000,
|
||||
): Promise<Packet<TOp>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
ws.off('packet', handler)
|
||||
reject('Awaiting packet timed out')
|
||||
}, timeout)
|
||||
|
||||
function handler(packet: Packet) {
|
||||
if (packet.op === op && packet.s === expectedSeq) {
|
||||
clearTimeout(timer)
|
||||
ws.off('packet', handler)
|
||||
resolve(packet as Packet<TOp>)
|
||||
}
|
||||
}
|
||||
|
||||
ws.on('packet', handler)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user