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.

109 lines (104 loc) 3.58 kB
/*! * NENT 2022 */ import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client'; import { a as actionBus, e as eventBus } from './index2.js'; import { f as debugIf } from './logging.js'; import { a as state } from './state.js'; import { a as addDataProvider, s as state$1, c as clearDataProviders } from './factory.js'; import { a as DATA_TOPIC, D as DATA_EVENTS } from './interfaces3.js'; import { D as DATA_COMMANDS } from './interfaces5.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 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement { constructor() { super(); this.__registerHost(); this.__attachShadow(); /** * 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(); } }, [1, "n-data", { "debug": [4], "providerTimeout": [2, "provider-timeout"] }]); function defineCustomElement$1() { if (typeof customElements === "undefined") { return; } const components = ["n-data"]; components.forEach(tagName => { switch (tagName) { case "n-data": if (!customElements.get(tagName)) { customElements.define(tagName, Data); } break; } }); } const NData = Data; const defineCustomElement = defineCustomElement$1; export { NData, defineCustomElement };