UNPKG

bacnet-device

Version:

A TypeScript library for implementing BACnet IP devices in Node.js.

59 lines 1.97 kB
/** * Event handling module for BACnet devices * * This module provides an asynchronous event system for BACnet components. * The implementation is inspired by Node.js's native EventEmitter but supports * asynchronous event handlers. * * @module */ /** * Implements an event emitter, conceptually similar to Node.js' native * EventEmitter, that supports asynchronous event handlers/listeners. * * This class provides a foundation for event-based communication between * BACnet components. It allows objects to register listeners for specific events * and trigger those events asynchronously. * * @typeParam T - An interface mapping event names to their argument arrays */ export class BDEvented { /** * Internal mapping of event names to their registered listeners * @private */ #callbacks; /** * Creates a new Evented instance with no registered listeners */ constructor() { this.#callbacks = Object.create(null); } /** * Adds a new listener for the specified event. * * @param event - The event name to subscribe to * @param cb - The callback function to execute when the event is triggered * @returns The callback function for chaining */ subscribe(event, cb) { if (!this.#callbacks[event]) { this.#callbacks[event] = []; } this.#callbacks[event].push(cb); } /** * Fires an event. All subscribed listeners will be called in parallel. * * @param event - The event name to trigger * @param args - The arguments to pass to each listener * @returns A promise that resolves when all listeners have completed */ async trigger(event, ...args) { if (this.#callbacks[event]) { // TODO: consider whether to call listeners in series. await Promise.all(this.#callbacks[event].map(cb => cb.apply(this, args))); } } } //# sourceMappingURL=evented.js.map