shuttle-webhid
Version:
An npm module for interfacing with the Contour Shuttle devices in Node.js
50 lines • 1.86 kB
JavaScript
"use strict";
/* eslint-disable n/no-unsupported-features/node-builtins */
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebHIDDevice = void 0;
const eventemitter3_1 = require("eventemitter3");
/**
* The wrapped browser HIDDevice.
* This translates it into the common format (@see CoreHIDDevice) defined by @shuttle-lib/core
*/
class WebHIDDevice extends eventemitter3_1.EventEmitter {
constructor(device) {
super();
this._handleInputReport = this._handleInputReport.bind(this);
this._handleError = this._handleError.bind(this);
this._handleDisconnect = this._handleDisconnect.bind(this);
this.device = device;
this.device.addEventListener('inputreport', this._handleInputReport);
this.device.addEventListener('error', this._handleError);
navigator.hid.addEventListener('disconnect', this._handleDisconnect);
}
async close() {
await this.device.close();
this._cleanup();
}
write(data) {
this.device.sendReport(data[0], new Uint8Array(data.slice(1))).catch((err) => {
this.emit('error', err);
});
}
_cleanup() {
this.device.removeEventListener('inputreport', this._handleInputReport);
this.device.removeEventListener('error', this._handleError);
navigator.hid.removeEventListener('disconnect', this._handleDisconnect);
}
_handleInputReport(event) {
const buf = new Uint8Array(event.data.buffer);
this.emit('data', buf);
}
_handleError(error) {
this.emit('error', error);
}
_handleDisconnect(event) {
if (event.device === this.device) {
this.emit('error', 'WebHID disconnected');
}
this._cleanup();
}
}
exports.WebHIDDevice = WebHIDDevice;
//# sourceMappingURL=web-hid-wrapper.js.map