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ourcesContent":["import { rpcErrors } from '@metamask/rpc-errors';\n\nimport { rootRealmGlobal } from './globalObject';\n\n/**\n * Adds an event listener platform agnostically, trying both `globalThis.addEventListener` and `globalThis.process.on`\n *\n * @param event - The event to listen for.\n * @param listener - The listener to be triggered when the event occurs.\n * @returns The result of the platform agnostic operation if any.\n * @throws If none of the platform options are present.\n */\nexport function addEventListener(\n  event: string,\n  listener: (...args: any[]) => void,\n) {\n  if (\n    'addEventListener' in rootRealmGlobal &&\n    typeof rootRealmGlobal.addEventListener === 'function'\n  ) {\n    return rootRealmGlobal.addEventListener(event.toLowerCase(), listener);\n  }\n\n  if (\n    rootRealmGlobal.process &&\n    'on' in rootRealmGlobal.process &&\n    typeof rootRealmGlobal.process.on === 'function'\n  ) {\n    return rootRealmGlobal.process.on(event, listener);\n  }\n\n  throw rpcErrors.internal('Platform agnostic addEventListener failed.');\n}\n\n/**\n * Removes an event listener platform agnostically, trying both `globalThis.removeEventListener` and `globalThis.process.removeListener`\n *\n * @param event - The event to remove the listener for.\n * @param listener - The currently attached listener.\n * @returns The result of the platform agnostic operation if any.\n * @throws If none of the platform options are present.\n */\nexport function removeEventListener(\n  event: string,\n  listener: (...args: any[]) => void,\n) {\n  if (\n    'removeEventListener' in rootRealmGlobal &&\n    typeof rootRealmGlobal.removeEventListener === 'function'\n  ) {\n    return rootRealmGlobal.removeEventListener(event.toLowerCase(), listener);\n  }\n\n  if (\n    rootRealmGlobal.process &&\n    'removeListener' in rootRealmGlobal.process &&\n    typeof rootRealmGlobal.process.removeListener === 'function'\n  ) {\n    return rootRealmGlobal.process.removeListener(event, listener);\n  }\n\n  throw new Error('Platform agnostic removeEventListener failed');\n}\n"]}