UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

95 lines (89 loc) 3.23 kB
/*! * NENT 2022 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-1829aebc.js'); const index$1 = require('./index-637e8c28.js'); const logging = require('./logging-37c154cf.js'); const state = require('./state-f97ff0e6.js'); const factory = require('./factory-0d7ddff9.js'); const interfaces = require('./interfaces-95d0415a.js'); const interfaces$1 = require('./interfaces-69054d94.js'); require('./index-96f3ab3f.js'); require('./values-b2399e33.js'); require('./promises-463f4e01.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(interfaces.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('*', () => { logging.debugIf(state.state.debug, `data-provider: ${name} changed`); this.eventBus.emit(interfaces.DATA_EVENTS.DataChanged, { provider: name, }); }); if (handle) this.disposeHandles.push(handle); factory.addDataProvider(name, provider); } async handleAction(actionEvent) { logging.debugIf(state.state.debug, `data-listener: action received {command:${actionEvent.command}}`); if (actionEvent.command === interfaces$1.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) { index.registerInstance(this, hostRef); /** * Turn on debugging to get helpful messages from the * data action systems. */ this.debug = false; } componentWillLoad() { logging.debugIf(this.debug, `n-data: registering data listener`); if (this.debug) factory.state.debug = true; this.listener = new DataListener(); state.state.dataEnabled = true; if (this.providerTimeout) factory.state.providerTimeout = this.providerTimeout; this.listener.initialize(window, index$1.actionBus, index$1.eventBus); } render() { return (index.h(index.Host, null, index.h("slot", null))); } disconnectedCallback() { this.listener.destroy(); factory.clearDataProviders(); } }; exports.n_data = Data;