chore: more unification of legacy APIs

1. unify waitForConnectionUpdate
2. unify printing QR in terminal
This commit is contained in:
Adhiraj Singh
2021-12-18 22:18:51 +05:30
parent 02dd8c93a6
commit f61f553e01
5 changed files with 54 additions and 80 deletions

View File

@@ -1,7 +1,10 @@
import { Boom } from '@hapi/boom'
import { randomBytes } from 'crypto'
import { platform, release } from 'os'
import { Logger } from 'pino'
import { ConnectionState } from '..'
import { proto } from '../../WAProto'
import { CommonBaileysEventEmitter, DisconnectReason } from '../Types'
import { Binary } from '../WABinary'
const PLATFORM_MAP = {
@@ -176,4 +179,40 @@ export async function promiseTimeout<T>(ms: number, promise: (resolve: (v?: T)=>
return p as Promise<T>
}
// generate a random ID to attach to a message
export const generateMessageID = () => 'BAE5' + randomBytes(6).toString('hex').toUpperCase()
export const generateMessageID = () => 'BAE5' + randomBytes(6).toString('hex').toUpperCase()
export const bindWaitForConnectionUpdate = (ev: CommonBaileysEventEmitter<any>) => (
async(check: (u: Partial<ConnectionState>) => boolean, timeoutMs?: number) => {
let listener: (item: Partial<ConnectionState>) => void
await (
promiseTimeout(
timeoutMs,
(resolve, reject) => {
listener = (update) => {
if(check(update)) {
resolve()
} else if(update.connection == 'close') {
reject(update.lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }))
}
}
ev.on('connection.update', listener)
}
)
.finally(() => (
ev.off('connection.update', listener)
))
)
}
)
export const printQRIfNecessaryListener = (ev: CommonBaileysEventEmitter<any>, logger: Logger) => {
ev.on('connection.update', async({ qr }) => {
if(qr) {
const QR = await import('qrcode-terminal')
.catch(err => {
logger.error('QR code terminal not added as dependency')
})
QR?.generate(qr, { small: true })
}
})
}