@nent/core
Version:
118 lines (113 loc) • 4.08 kB
JavaScript
/*!
* NENT 2022
*/
import { proxyCustomElement, HTMLElement, createEvent } from '@stencil/core/internal/client';
import { a as actionBus, e as eventBus } from './index2.js';
import { R as ROUTE_EVENTS } from './interfaces4.js';
import { A as ANALYTICS_TOPIC, a as ANALYTICS_EVENTS, b as ANALYTICS_COMMANDS } from './interfaces7.js';
import { f as debugIf } from './logging.js';
import { a as state } from './state.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 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
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();
}
}, [0, "n-app-analytics", {
"debug": [4]
}]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["n-app-analytics"];
components.forEach(tagName => { switch (tagName) {
case "n-app-analytics":
if (!customElements.get(tagName)) {
customElements.define(tagName, Analytics);
}
break;
} });
}
const NAppAnalytics = Analytics;
const defineCustomElement = defineCustomElement$1;
export { NAppAnalytics, defineCustomElement };