@elgato/streamdeck
Version:
The official Node.js SDK for creating Stream Deck plugins.
85 lines (84 loc) • 2.47 kB
JavaScript
import { actionStore } from "../actions/store.js";
import { connection } from "../connection.js";
/**
* Provides information about a device.
*/
export class Device {
/**
* Private backing field for {@link Device.isConnected}.
*/
#isConnected = false;
/**
* Private backing field for the device's information.
*/
#info;
/**
* Unique identifier of the device.
*/
id;
/**
* Initializes a new instance of the {@link Device} class.
* @param id Device identifier.
* @param info Information about the device.
* @param isConnected Determines whether the device is connected.
*/
constructor(id, info, isConnected) {
this.id = id;
this.#info = info;
this.#isConnected = isConnected;
// Set connected.
connection.prependListener("deviceDidConnect", (ev) => {
if (ev.device === this.id) {
this.#info = ev.deviceInfo;
this.#isConnected = true;
}
});
// Track changes.
connection.prependListener("deviceDidChange", (ev) => {
if (ev.device === this.id) {
this.#info = ev.deviceInfo;
}
});
// Set disconnected.
connection.prependListener("deviceDidDisconnect", (ev) => {
if (ev.device === this.id) {
this.#isConnected = false;
}
});
}
/**
* Actions currently visible on the device.
* @returns Collection of visible actions.
*/
get actions() {
return actionStore.filter((a) => a.device.id === this.id);
}
/**
* Determines whether the device is currently connected.
* @returns `true` when the device is connected; otherwise `false`.
*/
get isConnected() {
return this.#isConnected;
}
/**
* Name of the device, as specified by the user in the Stream Deck application.
* @returns Name of the device.
*/
get name() {
return this.#info.name;
}
/**
* Number of action slots, excluding dials / touchscreens, available to the device.
* @returns Size of the device.
*/
get size() {
return this.#info.size;
}
/**
* Type of the device that was connected, e.g. Stream Deck +, Stream Deck Pedal, etc. See {@link DeviceType}.
* @returns Type of the device.
*/
get type() {
return this.#info.type;
}
}