Source: observer/EventBusObserver.js

(function() {
    /**
     * observes event bus events
     * @augments Help4.observer.Observer
     */
    Help4.observer.EventBusObserver = class extends Help4.observer.Observer {
        /**
         * observes events from a certain event bus
         * @override
         * @param {Help4.EventBus} target - an event bus
         * @param {Object} options - configuration
         * @returns {Help4.observer.EventBusObserver}
         */
        observe(target, options) {
            if (!Help4.isArray(options.type)) options.type = [options.type];

            super.observe(target, options);

            options._ebs = new Help4.observer.EventBusObserver.EventBusSubscriber(options.type, this._callback);
            target.subscribe(options._ebs);
            return this;
        }

        /**
         * @override
         * @returns {Help4.observer.EventBusObserver}
         */
        disconnect() {
            for (const {target, options} of this._connections) {
                target.unsubscribe(options._ebs);
                options._ebs.destroy();
                options._ebs = null;
            }

            super.disconnect();
            return this;
        }
    }
})();

(function() {
    /**
     * only EventBusSubscribers can be subscribed to listen to EventBus events
     */
    Help4.observer.EventBusObserver.EventBusSubscriber = class {
        /**
         * @param {string[]} type
         * @param {Help4.observer.Callback} callback
         */
        constructor(type, callback) {
            this._callback = callback;

            const accept = this._accept = {};
            type.forEach(t => accept[t] = true);
        }

        /**
         * destroys the EventBusSubscriber
         */
        destroy() {
            delete this._accept;
            delete this._callback;
        }

        /**
         * the event bus has fired an event
         * dispatch to our callback if accepted
         * @param {Object} event
         */
        onEvent(event) {
            if (this._accept?.[event.type]) this._callback(event);
        }
    }
})();