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.

46 lines (45 loc) 1.95 kB
/*! * NENT 2022 */ import { debounce } from '../../../services/common'; import { EventEmitter } from '../../../services/common/emitter'; import { DATA_EVENTS, } from '../../../services/data/interfaces'; /* It listens to changes in the DOM and emits a `changed` event when it detects a change */ export class ElementsDataProvider { /** * A constructor function that takes in two parameters, a document and an elementListener. It then * creates a new EventEmitter and assigns it to the changed property. It then creates a new variable * called change and assigns it to a debounce function. The debounce function takes in three * parameters, a number, a function, and a boolean. The function that is passed in as a parameter * emits a DataChanged event. The listenerSubscription property is assigned to the * elementListener.changed property and is passed in a function that calls the change function. * @param {Document} doc - Document - this is the document object that is used to create the * elements. * @param {ElementsActionListener} elementListener - ElementsActionListener - this is the service * that listens to the changes in the elements. */ constructor(doc, elementListener) { this.doc = doc; this.elementListener = elementListener; this.changed = new EventEmitter(); const change = debounce(1000, () => { this.changed.emit(DATA_EVENTS.DataChanged, { provider: 'element', }); }, true); this.listenerSubscription = this.elementListener.changed.on('changed', () => { change(); }); } async get(key) { const element = this.doc.querySelector('#' + key); return (element === null || element === void 0 ? void 0 : element.value) || element.innerText || null; } /** * The function unsubscribes from the listenerSubscription, which is a subscription to the listener * function */ destroy() { this.listenerSubscription(); } }