UNPKG

dbus-sdk

Version:

A Node.js SDK for interacting with DBus, enabling seamless service calling and exposure with TypeScript support

270 lines (269 loc) 10.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DBusSignalEmitter = void 0; const node_events_1 = __importDefault(require("node:events")); /** * A class that extends Node.js EventEmitter to handle DBus signal emissions. * Manages signal listeners and emitters for a specific DBus service, object path, and interface. * Supports wildcard '*' for unique name, object path, and interface to handle multiple sources. */ class DBusSignalEmitter extends node_events_1.default { /** * A set of all DBusSignalEmitter instances. * Used to track active signal emitters and manage their lifecycle. */ #signalEmitters; /** * The unique name of the DBus connection or service (e.g., ':1.123') or '*' for any. * Represents the specific connection or a wildcard to match any connection. */ #uniqueName; /** * The object path on the DBus (e.g., '/org/freedesktop/DBus') or '*' for any. * Represents the specific object path or a wildcard to match any path. */ #objectPath; /** * The DBus interface name (e.g., 'org.freedesktop.DBus.Properties') or '*' for any. * Represents the specific interface or a wildcard to match any interface. */ #interface; /** * Getter for the unique name of the DBus connection or wildcard '*'. * * @returns The unique name or '*' if wildcard is used. */ get uniqueName() { return this.#uniqueName; } /** * Getter for the object path or wildcard '*'. * * @returns The object path or '*' if wildcard is used. */ get objectPath() { return this.#objectPath; } /** * Getter for the interface name or wildcard '*'. * * @returns The interface name or '*' if wildcard is used. */ get interface() { return this.#interface; } /** * Constructor for DBusSignalEmitter. * Initializes the emitter with options and a handler for signal events. * * @param opts - Options for creating the signal emitter, including uniqueName, objectPath, and interface. * @param signalEmitters - A set of all DBusSignalEmitter instances to manage active emitters. * @param onSignalHandler - A callback function invoked when signals are registered or updated. */ constructor(opts, signalEmitters, onSignalHandler) { super(); this.#signalEmitters = signalEmitters; this.onSignalHandler = onSignalHandler; this.#uniqueName = opts.uniqueName; this.#objectPath = opts.objectPath; this.#interface = opts.interface; } /** * Updates the unique name of the emitter if it is not set to wildcard '*'. * Notifies the signal handler for each registered event with the updated unique name. * * @param newUniqueName - The new unique name to set for this emitter. */ updateUniqueName(newUniqueName) { if (this.uniqueName === '*') return; this.#uniqueName = newUniqueName; this.eventNames().forEach((eventName) => this.onSignalHandler(this.uniqueName, this.objectPath, this.interface, eventName)); } /** * Updates the set of signal emitters based on whether this emitter has active events. * Removes this emitter from the set if no events are registered, or adds it if events exist. */ updateSignalEmitters() { if (!this.eventNames().length) { if (this.#signalEmitters.has(this)) this.#signalEmitters.delete(this); } else { if (!this.#signalEmitters.has(this)) this.#signalEmitters.add(this); } } /** * Adds a listener for the specified event (signal name) and notifies the signal handler. * Overrides EventEmitter's addListener to include DBus-specific logic. * * @param eventName - The name of the event (signal) to listen for. * @param listener - The callback function to execute when the event is emitted. * @returns This instance for method chaining. */ addListener(eventName, listener) { this.onSignalHandler(this.uniqueName, this.objectPath, this.interface, eventName); super.addListener(eventName, listener); this.updateSignalEmitters(); return this; } /** * Emits an event (signal) with the provided arguments and updates the signal emitters set. * Overrides EventEmitter's emit to include DBus-specific logic. * * @param eventName - The name of the event (signal) to emit. * @param args - Arguments to pass to the event listeners. * @returns True if the event had listeners, false otherwise. */ emit(eventName, ...args) { const emitResult = super.emit(eventName, ...args); this.updateSignalEmitters(); return emitResult; } /** * Returns an array of event names (signal names) for which this emitter has listeners. * * @returns An array of event names as strings. */ eventNames() { return super.eventNames(); } /** * Returns the maximum number of listeners allowed for any event. * * @returns The maximum number of listeners. */ getMaxListeners() { return super.getMaxListeners(); } /** * Returns the number of listeners for a specific event. * * @param eventName - The name of the event to check. * @param listener - Optional specific listener function to count. * @returns The number of listeners for the event. */ listenerCount(eventName, listener) { return super.listenerCount(eventName, listener); } /** * Returns an array of listener functions for a specific event. * * @param eventName - The name of the event to retrieve listeners for. * @returns An array of listener functions. */ listeners(eventName) { return super.listeners(eventName); } /** * Removes a specific listener for an event and updates the signal emitters set. * * @param eventName - The name of the event. * @param listener - The listener function to remove. * @returns This instance for method chaining. */ off(eventName, listener) { super.off(eventName, listener); this.updateSignalEmitters(); return this; } /** * Adds a listener for an event and notifies the signal handler. * * @param eventName - The name of the event (signal) to listen for. * @param listener - The callback function to execute when the event is emitted. * @returns This instance for method chaining. */ on(eventName, listener) { this.onSignalHandler(this.uniqueName, this.objectPath, this.interface, eventName); super.on(eventName, listener); this.updateSignalEmitters(); return this; } /** * Adds a one-time listener for an event and notifies the signal handler. * * @param eventName - The name of the event (signal) to listen for. * @param listener - The callback function to execute once when the event is emitted. * @returns This instance for method chaining. */ once(eventName, listener) { this.onSignalHandler(this.uniqueName, this.objectPath, this.interface, eventName); super.once(eventName, listener); this.updateSignalEmitters(); return this; } /** * Adds a listener to the beginning of the listeners array for an event and notifies the signal handler. * * @param eventName - The name of the event (signal) to listen for. * @param listener - The callback function to execute when the event is emitted. * @returns This instance for method chaining. */ prependListener(eventName, listener) { this.onSignalHandler(this.uniqueName, this.objectPath, this.interface, eventName); super.prependListener(eventName, listener); this.updateSignalEmitters(); return this; } /** * Adds a one-time listener to the beginning of the listeners array for an event and notifies the signal handler. * * @param eventName - The name of the event (signal) to listen for. * @param listener - The callback function to execute once when the event is emitted. * @returns This instance for method chaining. */ prependOnceListener(eventName, listener) { this.onSignalHandler(this.uniqueName, this.objectPath, this.interface, eventName); super.prependOnceListener(eventName, listener); this.updateSignalEmitters(); return this; } /** * Returns an array of raw listener functions for a specific event (including wrappers like 'once'). * * @param eventName - The name of the event to retrieve listeners for. * @returns An array of raw listener functions. */ rawListeners(eventName) { return super.rawListeners(eventName); } /** * Removes all listeners for an event (or all events if no event name is provided) and updates the signal emitters set. * * @param eventName - Optional name of the event to remove listeners for. * @returns This instance for method chaining. */ removeAllListeners(eventName) { super.removeAllListeners(eventName); this.updateSignalEmitters(); return this; } /** * Removes a specific listener for an event and updates the signal emitters set. * * @param eventName - The name of the event. * @param listener - The listener function to remove. * @returns This instance for method chaining. */ removeListener(eventName, listener) { super.removeListener(eventName, listener); this.updateSignalEmitters(); return this; } /** * Sets the maximum number of listeners allowed for any event. * * @param n - The maximum number of listeners to set. * @returns This instance for method chaining. */ setMaxListeners(n) { super.setMaxListeners(n); return this; } } exports.DBusSignalEmitter = DBusSignalEmitter;