@embrace-io/web-sdk
Version:
77 lines (76 loc) • 2.91 kB
JavaScript
import { generateUUID } from "../../utils/generateUUID.js";
import { OTelPerformanceManager, updateZeroTimeMillis } from "../../utils/PerformanceManager/OTelPerformanceManager.js";
import { diag } from "@opentelemetry/api";
//#region src/managers/EmbracePageManager/EmbracePageManager.ts
var EmbracePageManager = class {
_currentRoute = null;
_currentPageId = null;
_pageLabel = null;
_titleDocument;
_useDocumentTitleAsPageLabel;
_routeChangedListeners = [];
_diag;
_navigationHost;
_userSessionManager;
_perf;
constructor({ useDocumentTitleAsPageLabel = true, titleDocument = window.document, diag: diagParam, navigationHost = window, userSessionManager, perf = new OTelPerformanceManager() } = {}) {
this._useDocumentTitleAsPageLabel = useDocumentTitleAsPageLabel;
this._titleDocument = titleDocument;
this._diag = diagParam ?? diag.createComponentLogger({ namespace: "EmbracePageManager" });
this._navigationHost = navigationHost;
this._userSessionManager = userSessionManager;
this._perf = perf;
this._navigationHost.navigation?.addEventListener("currententrychange", this._onSoftNavigation);
this._setInitialRouteFromCurrentLocation();
}
getCurrentPageId = () => this._currentPageId;
getCurrentRoute = () => this._currentRoute;
setPageLabel = (label) => {
this._pageLabel = label;
};
getPageLabel = () => {
return this._pageLabel || (!this._useDocumentTitleAsPageLabel || !this._titleDocument ? null : this._titleDocument.title);
};
setCurrentRoute = (route) => {
if (!this._currentRoute || this._currentRoute.url !== route.url) this._currentPageId = generateUUID();
this._currentRoute = route;
if (route.label) this._pageLabel = route.label;
else this._pageLabel = null;
for (const listener of this._routeChangedListeners) try {
listener(route);
} catch (error) {
this._diag.warn("Error while executing route changed listener", error);
}
};
clearCurrentRoute = () => {
this._currentRoute = null;
this._currentPageId = null;
this._pageLabel = null;
};
addRouteChangedListener = (listener) => {
this._routeChangedListeners.push(listener);
return () => {
const index = this._routeChangedListeners.indexOf(listener);
if (index !== -1) this._routeChangedListeners.splice(index, 1);
};
};
_onSoftNavigation = (event) => {
if (event.from.url === this._navigationHost.location.href) return;
updateZeroTimeMillis(this._perf.epochMillisFromOrigin(event.timeStamp));
this._userSessionManager?.rolloverSessionPartInternal({
endReason: "web_soft_navigation",
startReason: "web_soft_navigation"
});
this._setInitialRouteFromCurrentLocation();
};
_setInitialRouteFromCurrentLocation = () => {
const pathname = this._navigationHost.location.pathname;
this.setCurrentRoute({
path: pathname,
url: pathname
});
};
};
//#endregion
export { EmbracePageManager };
//# sourceMappingURL=EmbracePageManager.js.map