UNPKG

@nova-ui/dashboards

Version:

Nova Dashboards is a framework designed to provide feature developers with a common solution for presenting data coming from various sources within a single view, as well as a set of predefined widget visualizations that are 100% configuration-driven and

148 lines 6.86 kB
"use strict"; // © 2022 SolarWinds Worldwide, LLC. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. Object.defineProperty(exports, "__esModule", { value: true }); exports.WidgetToDashboardEventProxyService = void 0; const tslib_1 = require("tslib"); const core_1 = require("@angular/core"); const keyBy_1 = tslib_1.__importDefault(require("lodash/keyBy")); const uniq_1 = tslib_1.__importDefault(require("lodash/uniq")); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const bits_1 = require("@nova-ui/bits"); const pizzagna_service_1 = require("../pizzagna/services/pizzagna.service"); const types_1 = require("../types"); const event_registry_service_1 = require("./event-registry.service"); const widget_configuration_service_1 = require("./widget-configuration.service"); /** * This provider transmits events between the dashboard event bus and the widget event bus. * It needs to be configured with the events that need transmission each way. */ let WidgetToDashboardEventProxyService = class WidgetToDashboardEventProxyService { constructor(pizzagnaBus, dashboardBus, widgetConfigurationService, eventRegistry, pizzagnaService) { this.pizzagnaBus = pizzagnaBus; this.dashboardBus = dashboardBus; this.widgetConfigurationService = widgetConfigurationService; this.eventRegistry = eventRegistry; this.pizzagnaService = pizzagnaService; this.upstreamSubscriptions = {}; this.downstreamSubscriptions = {}; this.destroy$ = new rxjs_1.Subject(); } ngOnDestroy() { this.destroy$.next(); this.destroy$.complete(); } setComponent(component) { this.component = component; } updateConfiguration(properties) { this.providerKey = properties.providerKey; this.upstreams = properties.upstreams; this.downstreams = properties.downstreams; if (this.upstreams) { // upstream = widget -> dashboard this.registerUpstreamSubscriptions(...this.upstreams); } if (this.downstreams) { // downstream = dashboard -> widget this.registerDownstreamSubscriptions(...this.downstreams); } } addUpstream(stream) { this.addStream(stream, "upstreams"); } addDownstream(stream) { this.addStream(stream, "downstreams"); } addStream(stream, streamKey) { const streams = (0, uniq_1.default)((this[streamKey] || []).concat(stream.id)); this[streamKey] = streams; if (!this.component) { return; } this.pizzagnaService.setProperty({ componentId: this.component.componentId, pizzagnaKey: types_1.PizzagnaLayer.Data, providerKey: this.providerKey, propertyPath: [streamKey], }, streams); } registerUpstreamSubscriptions(...upstreams) { if (!this.dashboardBus) { return; } this.registerSubscriptions(upstreams, this.upstreamSubscriptions, this.pizzagnaBus, (stream, event) => { // add widgetId to all events passed to the dashboard const widgetId = this.widgetConfigurationService.getWidget().id; this.dashboardBus .getStream(stream) .next(Object.assign({}, event, { widgetId })); }); } registerDownstreamSubscriptions(...downstreams) { if (!this.dashboardBus) { return; } this.registerSubscriptions(downstreams, this.downstreamSubscriptions, this.dashboardBus, (stream, event) => { // pass only events that have matching widgetId or a widgetId that is not defined const widgetId = this.widgetConfigurationService.getWidget().id; if (typeof event.widgetId === "undefined" || widgetId === event.widgetId) { this.pizzagnaBus.getStream(stream).next(event); } }); } registerSubscriptions(streams = [], subscriptions, sourceBus, handleEvent) { const streamsIndex = streams ? (0, keyBy_1.default)(streams, (x) => x) : null; // remove subscriptions that are not valid anymore for (const streamId of Object.keys(subscriptions)) { if (!streamsIndex || !streamsIndex[streamId]) { subscriptions[streamId].unsubscribe(); delete subscriptions[streamId]; } } // add subscriptions that were not registered before for (const streamId of Object.keys(streamsIndex ?? {}).filter((s) => !subscriptions[s])) { const eventDefinition = this.eventRegistry.getEvent(streamId); subscriptions[streamId] = sourceBus.getStream(eventDefinition) .pipe((0, operators_1.takeUntil)(this.destroy$)) .subscribe((event) => { handleEvent(eventDefinition, event); }); } } }; exports.WidgetToDashboardEventProxyService = WidgetToDashboardEventProxyService; exports.WidgetToDashboardEventProxyService = WidgetToDashboardEventProxyService = tslib_1.__decorate([ (0, core_1.Injectable)(), tslib_1.__param(0, (0, core_1.Inject)(types_1.PIZZAGNA_EVENT_BUS)), tslib_1.__param(1, (0, core_1.Optional)()), tslib_1.__param(1, (0, core_1.Inject)(types_1.DASHBOARD_EVENT_BUS)), tslib_1.__param(2, (0, core_1.Optional)()), tslib_1.__metadata("design:paramtypes", [bits_1.EventBus, bits_1.EventBus, widget_configuration_service_1.WidgetConfigurationService, event_registry_service_1.EventRegistryService, pizzagna_service_1.PizzagnaService]) ], WidgetToDashboardEventProxyService); //# sourceMappingURL=widget-to-dashboard-event-proxy.service.js.map