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.

104 lines (98 loc) 3.71 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 interfaces$1 = require('./interfaces-1da33056.js'); const interfaces = require('./interfaces-564b9114.js'); const logging = require('./logging-37c154cf.js'); const state = require('./state-f97ff0e6.js'); require('./index-96f3ab3f.js'); /* It listens for events on the `ANALYTICS_TOPIC` topic and calls the appropriate handler method */ class AnalyticsActionListener { /** * The constructor function is called when the class is instantiated. It takes in the `actions` and * `events` objects, and subscribes to the `ANALYTICS_TOPIC` and `ROUTE_EVENTS.RouteChanged` events * @param {IEventEmitter} actions - IEventEmitter - This is the event emitter that is used to listen * for the ANALYTICS_TOPIC. * @param {IEventEmitter} events - IEventEmitter - This is the event emitter that is used to listen * to events. * @param {boolean} [debug=false] - boolean - If true, will log all events to the console. */ constructor(actions, events, debug = false) { this.actions = actions; this.events = events; this.debug = debug; this.removeSubscription = []; this.removeSubscription.push(this.actions.on(interfaces.ANALYTICS_TOPIC, e => { this.handleEventAction(e); })); this.removeSubscription.push(this.events.on(interfaces$1.ROUTE_EVENTS.RouteChanged, (location) => { var _a; (_a = this.handlePageView) === null || _a === void 0 ? void 0 : _a.call(this, location); })); this.events.emit(interfaces.ANALYTICS_EVENTS.ListenerRegistered, this); } handleEventAction(eventAction) { var _a, _b, _c; logging.debugIf(this.debug, `analytics-listener: action received ${JSON.stringify(eventAction)}`); switch (eventAction.command) { case interfaces.ANALYTICS_COMMANDS.SendEvent: { (_a = this.handleEvent) === null || _a === void 0 ? void 0 : _a.call(this, eventAction.data); break; } case interfaces.ANALYTICS_COMMANDS.SendViewTime: { (_b = this.handleViewTime) === null || _b === void 0 ? void 0 : _b.call(this, eventAction.data); break; } case interfaces.ANALYTICS_COMMANDS.SendPageView: { (_c = this.handlePageView) === null || _c === void 0 ? void 0 : _c.call(this, eventAction.data); break; } } } /** * It removes all subscriptions. */ destroy() { this.removeSubscription.forEach(d => d()); } } const Analytics = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.event = index.createEvent(this, "custom-event", 4); this.pageView = index.createEvent(this, "page-view", 4); this.viewTime = index.createEvent(this, "view-time", 4); /** * Turn on debugging to get helpful messages from the * app, routing, data and action systems. */ this.debug = false; } componentWillLoad() { logging.debugIf(this.debug, `n-app-analytics: loading`); state.state.analyticsEnabled = true; this.listener = new AnalyticsActionListener(index$1.actionBus, index$1.eventBus, this.debug); this.listener.handleEvent = e => { this.event.emit(e); }; this.listener.handlePageView = (e) => { this.pageView.emit(e); }; this.listener.handleViewTime = (e) => { this.viewTime.emit(e); }; logging.debugIf(this.debug, `n-app-analytics: loaded`); } render() { return null; } disconnectedCallback() { state.state.analyticsEnabled = false; this.listener.destroy(); } }; exports.n_app_analytics = Analytics;