UNPKG

xkeys-webhid

Version:

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

53 lines 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XKeysWatcher = void 0; const core_1 = require("@xkeys-lib/core"); const methods_1 = require("./methods"); const globalConnectListener_1 = require("./globalConnectListener"); /** * Set up a watcher for newly connected X-keys panels. * Note: It is highly recommended to set up a listener for the disconnected event on the X-keys panel, to clean up after a disconnected device. */ class XKeysWatcher extends core_1.GenericXKeysWatcher { constructor(options) { super(options); this.eventListeners = []; this.pollingInterval = undefined; this.handleConnectEvent = () => { // Called whenever a device is connected or disconnected if (!this.isActive) return; this.triggerUpdateConnectedDevices(true); }; if (!this.options.usePolling) { this.eventListeners.push(globalConnectListener_1.GlobalConnectListener.listenForAnyDisconnect(this.handleConnectEvent)); this.eventListeners.push(globalConnectListener_1.GlobalConnectListener.listenForAnyConnect(this.handleConnectEvent)); } else { this.pollingInterval = setInterval(() => { this.triggerUpdateConnectedDevices(false); }, this.options.pollingInterval); } } /** * Stop the watcher * @param closeAllDevices Set to false in order to NOT close all devices. Use this if you only want to stop the watching. Defaults to true */ async stop(closeAllDevices = true) { this.eventListeners.forEach((listener) => listener.stop()); if (this.pollingInterval) { clearInterval(this.pollingInterval); this.pollingInterval = undefined; } await super.stop(closeAllDevices); } async getConnectedDevices() { // Returns a Set of devicePaths of the connected devices return new Set(await (0, methods_1.getOpenedXKeysPanels)()); } async setupXkeysPanel(device) { return (0, methods_1.setupXkeysPanel)(device); } } exports.XKeysWatcher = XKeysWatcher; //# sourceMappingURL=watcher.js.map