mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-18 16:53:57 +00:00
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
27 lines
795 B
TypeScript
27 lines
795 B
TypeScript
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)
|
|
})
|
|
}
|