@nent/core
Version:
91 lines (87 loc) • 3.14 kB
JavaScript
/*!
* NENT 2022
*/
import { r as registerInstance, h, H as Host } from './index-916ca544.js';
import { a as actionBus, e as eventBus } from './index-f7016b94.js';
import { f as debugIf } from './logging-5a93c8af.js';
import { a as state } from './state-27a8a5bc.js';
import { a as addDataProvider, s as state$1, c as clearDataProviders } from './factory-acbf0d3d.js';
import { a as DATA_TOPIC, D as DATA_EVENTS } from './interfaces-8c5cd1b8.js';
import { D as DATA_COMMANDS } from './interfaces-4b724211.js';
import './index-4bfabbbd.js';
import './values-ddfac998.js';
import './promises-584c4ece.js';
/* 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 */
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(state.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(state.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));
}
}
const Data = class {
constructor(hostRef) {
registerInstance(this, hostRef);
/**
* Turn on debugging to get helpful messages from the
* data action systems.
*/
this.debug = false;
}
componentWillLoad() {
debugIf(this.debug, `n-data: registering data listener`);
if (this.debug)
state$1.debug = true;
this.listener = new DataListener();
state.dataEnabled = true;
if (this.providerTimeout)
state$1.providerTimeout = this.providerTimeout;
this.listener.initialize(window, actionBus, eventBus);
}
render() {
return (h(Host, null, h("slot", null)));
}
disconnectedCallback() {
this.listener.destroy();
clearDataProviders();
}
};
export { Data as n_data };