@nent/core
Version:
54 lines (53 loc) • 2.08 kB
JavaScript
/*!
* NENT 2022
*/
import { commonState, debugIf, } from '../../../services/common';
import { addDataProvider } from '../../../services/data/factory';
import { DATA_EVENTS, DATA_TOPIC, } from '../../../services/data/interfaces';
import { DATA_COMMANDS, } from './interfaces';
/* It listens for actions on the `DATA_TOPIC` topic, and when it receives a
`DATA_COMMANDS.RegisterDataProvider` command, it registers the provider with the `addDataProvider`
function */
export class DataListener {
constructor() {
this.disposeHandles = [];
}
/**
* > This function is called when the plugin is initialized. It sets up an event listener for the
* `DATA_TOPIC` event on the `actionBus` and calls `handleAction` when the event is emitted
* @param {Window} _window - Window - The window object
* @param {IEventEmitter} actionBus - This is the event emitter that is used to listen for actions.
* @param {IEventEmitter} eventBus - This is the event bus that the plugin will use to emit events.
*/
initialize(_window, actionBus, eventBus) {
this.eventBus = eventBus;
const handle = actionBus.on(DATA_TOPIC, e => {
this.handleAction(e);
});
this.disposeHandles.push(handle);
}
registerProvider(name, provider) {
var _a;
const handle = (_a = provider.changed) === null || _a === void 0 ? void 0 : _a.on('*', () => {
debugIf(commonState.debug, `data-provider: ${name} changed`);
this.eventBus.emit(DATA_EVENTS.DataChanged, {
provider: name,
});
});
if (handle)
this.disposeHandles.push(handle);
addDataProvider(name, provider);
}
async handleAction(actionEvent) {
debugIf(commonState.debug, `data-listener: action received {command:${actionEvent.command}}`);
if (actionEvent.command === DATA_COMMANDS.RegisterDataProvider) {
const { name, provider } = actionEvent.data;
if (name && provider) {
this.registerProvider(name, provider);
}
}
}
destroy() {
this.disposeHandles.forEach(h => h === null || h === void 0 ? void 0 : h.call(this));
}
}