UNPKG

@azure/msal-browser

Version:
111 lines (108 loc) 4 kB
/*! @azure/msal-browser v5.6.3 2026-04-01 */ 'use strict'; import { Logger } from '@azure/msal-common/browser'; import { EventType } from './EventType.mjs'; import { createGuid } from '../utils/BrowserUtils.mjs'; /* * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ const BROADCAST_CHANNEL_NAME = "msal.broadcast.event"; class EventHandler { constructor(logger) { this.eventCallbacks = new Map(); this.logger = logger || new Logger({}); if (typeof BroadcastChannel !== "undefined") { this.broadcastChannel = new BroadcastChannel(BROADCAST_CHANNEL_NAME); } this.invokeCrossTabCallbacks = this.invokeCrossTabCallbacks.bind(this); } /** * Adds event callbacks to array * @param callback - callback to be invoked when an event is raised * @param eventTypes - list of events that this callback will be invoked for, if not provided callback will be invoked for all events * @param callbackId - Identifier for the callback, used to locate and remove the callback when no longer required */ addEventCallback(callback, eventTypes, callbackId) { if (typeof window !== "undefined") { const id = callbackId || createGuid(); if (this.eventCallbacks.has(id)) { this.logger.error("1578i0", ""); return null; } this.eventCallbacks.set(id, [callback, eventTypes || []]); this.logger.verbose("1cnec4", ""); return id; } return null; } /** * Removes callback with provided id from callback array * @param callbackId */ removeEventCallback(callbackId) { this.eventCallbacks.delete(callbackId); this.logger.verbose("12zotd", ""); } /** * Emits events by calling callback with event message * @param eventType * @param interactionType * @param payload * @param error */ emitEvent(eventType, correlationId, interactionType, payload, error) { const message = { eventType: eventType, interactionType: interactionType || null, payload: payload || null, error: error || null, correlationId: correlationId, timestamp: Date.now(), }; switch (eventType) { case EventType.LOGIN_SUCCESS: case EventType.LOGOUT_SUCCESS: case EventType.ACTIVE_ACCOUNT_CHANGED: // Send event to other open tabs / MSAL instances on same domain this.broadcastChannel?.postMessage(message); } // Emit event to callbacks registered in this instance this.invokeCallbacks(message); } /** * Invoke registered callbacks * @param message */ invokeCallbacks(message) { this.eventCallbacks.forEach(([callback, eventTypes], callbackId) => { if (eventTypes.length === 0 || eventTypes.includes(message.eventType)) { this.logger.verbose("15jpwk", ""); callback.apply(null, [message]); } }); } /** * Wrapper around invokeCallbacks to handle broadcast events received from other tabs/instances * @param event */ invokeCrossTabCallbacks(event) { const message = event.data; this.invokeCallbacks(message); } /** * Listen for events broadcasted from other tabs/instances */ subscribeCrossTab() { this.broadcastChannel?.addEventListener("message", this.invokeCrossTabCallbacks); } /** * Unsubscribe from broadcast events */ unsubscribeCrossTab() { this.broadcastChannel?.removeEventListener("message", this.invokeCrossTabCallbacks); } } export { EventHandler }; //# sourceMappingURL=EventHandler.mjs.map