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.

57 lines (56 loc) 2.3 kB
/*! * NENT 2022 */ import { debugIf } from '../../../services/common'; import { ROUTE_EVENTS, } from '../../n-views/services/interfaces'; import { ANALYTICS_COMMANDS, ANALYTICS_EVENTS, ANALYTICS_TOPIC, } from './interfaces'; /* It listens for events on the `ANALYTICS_TOPIC` topic and calls the appropriate handler method */ export 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(ANALYTICS_TOPIC, e => { this.handleEventAction(e); })); this.removeSubscription.push(this.events.on(ROUTE_EVENTS.RouteChanged, (location) => { var _a; (_a = this.handlePageView) === null || _a === void 0 ? void 0 : _a.call(this, location); })); this.events.emit(ANALYTICS_EVENTS.ListenerRegistered, this); } handleEventAction(eventAction) { var _a, _b, _c; debugIf(this.debug, `analytics-listener: action received ${JSON.stringify(eventAction)}`); switch (eventAction.command) { case ANALYTICS_COMMANDS.SendEvent: { (_a = this.handleEvent) === null || _a === void 0 ? void 0 : _a.call(this, eventAction.data); break; } case ANALYTICS_COMMANDS.SendViewTime: { (_b = this.handleViewTime) === null || _b === void 0 ? void 0 : _b.call(this, eventAction.data); break; } case 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()); } }