UNPKG

shuttle-node

Version:

An npm module for interfacing with the Contour Shuttle devices in Node.js

40 lines 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeHIDDevice = void 0; const eventemitter3_1 = require("eventemitter3"); /** * This class wraps the node-hid.HID Device. * This translates it into the common format (@see HIDDevice) defined by @shuttle-lib/core */ class NodeHIDDevice extends eventemitter3_1.EventEmitter { constructor(device) { super(); this.device = device; this._handleData = this._handleData.bind(this); this._handleError = this._handleError.bind(this); this.device.on('error', this._handleError); this.device.on('data', this._handleData); } write(data) { this.device.write(data).catch((err) => { this.emit('error', err); }); } async close() { await this.device.close(); // For some unknown reason, we need to wait a bit before returning because it // appears that the HID-device isn't actually closed properly until after a short while. // (This issue has been observed in Electron, where a app.quit() causes the application to crash with "Exit status 3221226505".) await new Promise((resolve) => setTimeout(resolve, 300)); this.device.removeListener('error', this._handleError); this.device.removeListener('data', this._handleData); } _handleData(data) { this.emit('data', data); } _handleError(error) { this.emit('error', error); } } exports.NodeHIDDevice = NodeHIDDevice; //# sourceMappingURL=node-hid-wrapper.js.map