import { DevToolInternals, MessageBroker } from '../../types'; import { createDevToolsMessageBroker } from './devtools-message-broker'; import { createElectronMessageBroker } from './electron-message-broker'; import { createPostMessageBroker } from './post-message-broker'; import { createStubMessageBroker } from './stub-message-broker'; const SESSION_STORAGE_KEY = '__raydiantWindowId'; const getWindowIdFromSession = (): string | null => { if (typeof window !== 'undefined' && window?.sessionStorage?.getItem) { // To handle environments that don't support sessionStorage, wrap in a try/catch. // This prevents an error when loading the SDK from within an iframe, which doesn't // support sessionStorage (Dashboard previews). try { return window.sessionStorage.getItem(SESSION_STORAGE_KEY); } catch (error) { console.warn('Raydiant SDK: Unable to load windowId from session', error); return null; } } return null; }; const saveWindowIdToSession = (windowId: string): void => { if (typeof window !== 'undefined' && window?.sessionStorage?.setItem) { try { return window.sessionStorage.setItem(SESSION_STORAGE_KEY, windowId); } catch (error) { console.warn('Raydiant SDK: Unable to save windowId to session', error); } } }; export function createMessageBroker( devToolInternals: DevToolInternals ): MessageBroker { // Create the message broker based on the environment we are executing in. // The message broker is responsible for subscribing to messages sent from // the playlist runtime and passing them along to the SDK to handle. It is // also responsible for sending messages to the playlist runtime. if (typeof window === 'undefined') { // Running in a server environment, use stub message broker. The stub message broker // will log a warning if trying to use any APIs in an unsupported environment. console.info('Raydiant SDK: Using stub message broker'); return createStubMessageBroker(); } // The playlist runtime can be an Electron app with BrowserWindows (where // communication is done via IPC) or entirely in the browser with