@nent/core
Version:
100 lines (96 loc) • 3.61 kB
JavaScript
/*!
* NENT 2022
*/
import { r as registerInstance, d as createEvent } from './index-916ca544.js';
import { a as actionBus, e as eventBus } from './index-f7016b94.js';
import { R as ROUTE_EVENTS } from './interfaces-3b78db83.js';
import { A as ANALYTICS_TOPIC, a as ANALYTICS_EVENTS, b as ANALYTICS_COMMANDS } from './interfaces-89f61157.js';
import { f as debugIf } from './logging-5a93c8af.js';
import { a as state } from './state-27a8a5bc.js';
import './index-4bfabbbd.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(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());
}
}
const Analytics = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.event = createEvent(this, "custom-event", 4);
this.pageView = createEvent(this, "page-view", 4);
this.viewTime = createEvent(this, "view-time", 4);
/**
* Turn on debugging to get helpful messages from the
* app, routing, data and action systems.
*/
this.debug = false;
}
componentWillLoad() {
debugIf(this.debug, `n-app-analytics: loading`);
state.analyticsEnabled = true;
this.listener = new AnalyticsActionListener(actionBus, 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);
};
debugIf(this.debug, `n-app-analytics: loaded`);
}
render() {
return null;
}
disconnectedCallback() {
state.analyticsEnabled = false;
this.listener.destroy();
}
};
export { Analytics as n_app_analytics };