xkeys
Version:
An npm module for interfacing with the X-keys panels in Node.js
46 lines • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeHIDDevice = void 0;
const events_1 = require("events");
const p_queue_1 = require("p-queue");
/**
* This class wraps the node-hid.HID Device.
* This translates it into the common format (@see HIDDevice) defined by @xkeys-lib/core
*/
class NodeHIDDevice extends events_1.EventEmitter {
constructor(device) {
super();
this.device = device;
this.writeQueue = new p_queue_1.default({ concurrency: 1 });
this._handleData = (data) => {
this.emit('data', data);
};
this._handleError = (error) => {
this.emit('error', error);
};
this.device.on('error', this._handleError);
this.device.on('data', this._handleData);
}
write(data) {
this.writeQueue
.add(async () => 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, NodeHIDDevice.CLOSE_WAIT_TIME));
this.device.removeListener('error', this._handleError);
this.device.removeListener('data', this._handleData);
}
async flush() {
await this.writeQueue.onIdle();
}
}
exports.NodeHIDDevice = NodeHIDDevice;
NodeHIDDevice.CLOSE_WAIT_TIME = 300;
//# sourceMappingURL=node-hid-wrapper.js.map