{"version":3,"file":"EventHandler.js","sources":["../../src/event/EventHandler.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { ICrypto, Logger, AccountEntity, CacheManager } from \"@azure/msal-common\";\nimport { InteractionType } from \"../utils/BrowserConstants\";\nimport { EventCallbackFunction, EventError, EventMessage, EventPayload } from \"./EventMessage\";\nimport { EventType } from \"./EventType\";\n\nexport class EventHandler {\n // Callback for subscribing to events\n private eventCallbacks: Map;\n private logger: Logger;\n private browserCrypto: ICrypto;\n private listeningToStorageEvents: boolean;\n\n constructor(logger: Logger, browserCrypto: ICrypto) {\n this.eventCallbacks = new Map();\n this.logger = logger;\n this.browserCrypto = browserCrypto;\n this.listeningToStorageEvents = false;\n this.handleAccountCacheChange = this.handleAccountCacheChange.bind(this);\n }\n\n /**\n * Adds event callbacks to array\n * @param callback\n */\n addEventCallback(callback: EventCallbackFunction): string | null {\n if (typeof window !== \"undefined\") {\n const callbackId = this.browserCrypto.createNewGuid();\n this.eventCallbacks.set(callbackId, callback);\n this.logger.verbose(`Event callback registered with id: ${callbackId}`);\n \n return callbackId;\n }\n \n return null;\n }\n\n /**\n * Removes callback with provided id from callback array\n * @param callbackId\n */\n removeEventCallback(callbackId: string): void {\n this.eventCallbacks.delete(callbackId);\n this.logger.verbose(`Event callback ${callbackId} removed.`);\n }\n\n /**\n * Adds event listener that emits an event when a user account is added or removed from localstorage in a different browser tab or window\n */\n enableAccountStorageEvents(): void {\n if (typeof window === \"undefined\") {\n return;\n }\n\n if (!this.listeningToStorageEvents) {\n this.logger.verbose(\"Adding account storage listener.\");\n this.listeningToStorageEvents = true;\n window.addEventListener(\"storage\", this.handleAccountCacheChange);\n } else {\n this.logger.verbose(\"Account storage listener already registered.\");\n }\n }\n\n /**\n * Removes event listener that emits an event when a user account is added or removed from localstorage in a different browser tab or window\n */\n disableAccountStorageEvents(): void {\n if (typeof window === \"undefined\") {\n return;\n }\n\n if (this.listeningToStorageEvents) {\n this.logger.verbose(\"Removing account storage listener.\");\n window.removeEventListener(\"storage\", this.handleAccountCacheChange);\n this.listeningToStorageEvents = false;\n } else {\n this.logger.verbose(\"No account storage listener registered.\");\n }\n }\n\n /**\n * Emits events by calling callback with event message\n * @param eventType\n * @param interactionType\n * @param payload\n * @param error\n */\n emitEvent(eventType: EventType, interactionType?: InteractionType, payload?: EventPayload, error?: EventError): void {\n if (typeof window !== \"undefined\") {\n const message: EventMessage = {\n eventType: eventType,\n interactionType: interactionType || null,\n payload: payload || null,\n error: error || null,\n timestamp: Date.now()\n };\n\n this.logger.info(`Emitting event: ${eventType}`);\n\n this.eventCallbacks.forEach((callback: EventCallbackFunction, callbackId: string) => {\n this.logger.verbose(`Emitting event to callback ${callbackId}: ${eventType}`);\n callback.apply(null, [message]);\n });\n }\n }\n\n /**\n * Emit account added/removed events when cached accounts are changed in a different tab or frame\n */\n private handleAccountCacheChange(e: StorageEvent): void {\n try {\n const cacheValue = e.newValue || e.oldValue;\n if (!cacheValue) {\n return;\n }\n const parsedValue = JSON.parse(cacheValue);\n if (typeof parsedValue !== \"object\" || !AccountEntity.isAccountEntity(parsedValue)) {\n return;\n }\n const accountEntity = CacheManager.toObject(new AccountEntity(), parsedValue);\n const accountInfo = accountEntity.getAccountInfo();\n if (!e.oldValue && e.newValue) {\n this.logger.info(\"Account was added to cache in a different window\");\n this.emitEvent(EventType.ACCOUNT_ADDED, undefined, accountInfo);\n } else if (!e.newValue && e.oldValue) {\n this.logger.info(\"Account was removed from cache in a different window\");\n this.emitEvent(EventType.ACCOUNT_REMOVED, undefined, accountInfo);\n }\n } catch (e) {\n return;\n }\n }\n}\n"],"names":[],"mappings":";;;;;AAAA;;;;;IAiBI,sBAAY,MAAc,EAAE,aAAsB;QAC9C,IAAI,CAAC,cAAc,GAAG,IAAI,GAAG,EAAE,CAAC;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;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