-
Notifications
You must be signed in to change notification settings - Fork 12
/
web-hid-wrapper.ts
46 lines (40 loc) · 1.34 KB
/
web-hid-wrapper.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
/* eslint-disable @typescript-eslint/unbound-method */
import { HIDDevice as CoreHIDDevice } from '@xkeys-lib/core'
import { EventEmitter } from 'events'
import Queue from 'p-queue'
import { Buffer as WebBuffer } from 'buffer'
/**
* The wrapped browser HIDDevice.
* This translates it into the common format (@see CoreHIDDevice) defined by @xkeys-lib/core
*/
export class WebHIDDevice extends EventEmitter implements CoreHIDDevice {
private readonly device: HIDDevice
private readonly reportQueue = new Queue({ concurrency: 1 })
constructor(device: HIDDevice) {
super()
this.device = device
this.device.addEventListener('inputreport', this._handleInputreport)
this.device.addEventListener('error', this._handleError)
}
public write(data: number[]): void {
this.reportQueue
.add(async () => {
await this.device.sendReport(data[0], new Uint8Array(data.slice(1)))
})
.catch((err) => {
this.emit('error', err)
})
}
public async close(): Promise<void> {
await this.device.close()
this.device.removeEventListener('inputreport', this._handleInputreport)
this.device.removeEventListener('error', this._handleError)
}
private _handleInputreport = (event: HIDInputReportEvent) => {
const buf = WebBuffer.from(event.data.buffer)
this.emit('data', buf)
}
private _handleError = (error: any) => {
this.emit('error', error)
}
}