UNPKG

xkeys-webhid

Version:

An npm module for interfacing with the X-keys panels in a browser

45 lines 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebHIDDevice = void 0; const events_1 = require("events"); const p_queue_1 = require("p-queue"); const buffer_1 = require("buffer"); /** * The wrapped browser HIDDevice. * This translates it into the common format (@see CoreHIDDevice) defined by @xkeys-lib/core */ class WebHIDDevice extends events_1.EventEmitter { constructor(device) { super(); this.reportQueue = new p_queue_1.default({ concurrency: 1 }); this._handleInputreport = (event) => { const buf = buffer_1.Buffer.from(event.data.buffer); this.emit('data', buf); }; this._handleError = (error) => { this.emit('error', error); }; this.device = device; this.device.addEventListener('inputreport', this._handleInputreport); this.device.addEventListener('error', this._handleError); } write(data) { this.reportQueue .add(async () => { await this.device.sendReport(data[0], new Uint8Array(data.slice(1))); }) .catch((err) => { this.emit('error', err); }); } async flush() { await this.reportQueue.onIdle(); } async close() { await this.device.close(); this.device.removeEventListener('inputreport', this._handleInputreport); this.device.removeEventListener('error', this._handleError); } } exports.WebHIDDevice = WebHIDDevice; //# sourceMappingURL=web-hid-wrapper.js.map