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,CAAC","sourcesContent":["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"]}