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AAC,MAAM,GAAG,CAAC,CAAC;KACxB;AACH,CAAC;AAED,SAAS,aAAa;IACpB,0CAA0C;AAC5C,CAAC;AAED,SAAS,gBAAgB;IACvB,IAAI,0BAA0B,KAAK,IAAI,EAAE;QACvC,OAAO;KACR;IAED,uEAAuE;IACvE,uFAAuF;IACvF,aAAa,EAAE,CAAC;IAEhB,MAAM,OAAO,GAAG,0BAA0B,CAAC;IAC3C,0BAA0B,GAAG,IAAI,CAAC;IAElC,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IACjC,gDAAgD;IAChD,0CAA0C;IAC1C,mBAAmB;IACnB,KAAK,CAAC,oBAAoB,GAAG,IAAI,CAAC;IAClC,MAAM,KAAK,CAAC;AACd,CAAC;AAED,kBAAe,SAAS,CAAC","sourcesContent":["/**\n * Copyright (c) 650 Industries.\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n * Based on this but with web support:\n * https://github.com/facebook/react-native/blob/086714b02b0fb838dee5a66c5bcefe73b53cf3df/Libraries/Utilities/HMRClient.js\n */\nimport prettyFormat, { plugins } from 'pretty-format';\n\nimport LoadingView from './LoadingView';\nimport LogBox from './error-overlay/LogBox';\nimport getDevServer from './getDevServer';\n\nconst MetroHMRClient = require('metro-runtime/src/modules/HMRClient');\nconst pendingEntryPoints: string[] = [];\n\ntype HMRClientType = {\n send: (msg: string) => void;\n isEnabled: () => boolean;\n disable: () => void;\n enable: () => void;\n hasPendingUpdates: () => boolean;\n};\n\nlet hmrClient: HMRClientType | null = null;\nlet hmrUnavailableReason: string | null = null;\nlet currentCompileErrorMessage: string | null = null;\nlet didConnect: boolean = false;\nconst pendingLogs: [LogLevel, any[]][] = [];\n\ntype LogLevel =\n | 'trace'\n | 'info'\n | 'warn'\n | 'error'\n | 'log'\n | 'group'\n | 'groupCollapsed'\n | 'groupEnd'\n | 'debug';\n\nexport type HMRClientNativeInterface = {\n enable(): void;\n disable(): void;\n registerBundle(requestUrl: string): void;\n log(level: LogLevel, data: any[]): void;\n setup(props: { isEnabled: boolean }): void;\n};\n\nfunction assert(foo: any, msg: string): asserts foo {\n if (!foo) throw new Error(msg);\n}\n\n/**\n * HMR Client that receives from the server HMR updates and propagates them\n * runtime to reflects those changes.\n */\nconst HMRClient: HMRClientNativeInterface = {\n enable() {\n if (hmrUnavailableReason !== null) {\n // If HMR became unavailable while you weren't using it,\n // explain why when you try to turn it on.\n // This is an error (and not a warning) because it is shown\n // in response to a direct user action.\n throw new Error(hmrUnavailableReason);\n }\n\n assert(hmrClient, 'Expected HMRClient.setup() call at startup.');\n\n // We use this for internal logging only.\n // It doesn't affect the logic.\n hmrClient.send(JSON.stringify({ type: 'log-opt-in' }));\n\n // When toggling Fast Refresh on, we might already have some stashed updates.\n // Since they'll get applied now, we'll show a banner.\n const hasUpdates = hmrClient!.hasPendingUpdates();\n\n if (hasUpdates) {\n LoadingView.showMessage('Refreshing...', 'refresh');\n }\n try {\n hmrClient.enable();\n } finally {\n if (hasUpdates) {\n LoadingView.hide();\n }\n }\n\n // There could be a compile error while Fast Refresh was off,\n // but we ignored it at the time. Show it now.\n showCompileError();\n },\n\n disable() {\n assert(hmrClient, 'Expected HMRClient.setup() call at startup.');\n hmrClient.disable();\n },\n\n registerBundle(requestUrl: string) {\n assert(hmrClient, 'Expected HMRClient.setup() call at startup.');\n pendingEntryPoints.push(requestUrl);\n registerBundleEntryPoints(hmrClient);\n },\n\n log(level: LogLevel, data: any[]) {\n if (!hmrClient) {\n // Catch a reasonable number of early logs\n // in case hmrClient gets initialized later.\n pendingLogs.push([level, data]);\n if (pendingLogs.length > 100) {\n pendingLogs.shift();\n }\n return;\n }\n try {\n hmrClient.send(\n JSON.stringify({\n type: 'log',\n level,\n mode: 'BRIDGE',\n data: data.map((item) =>\n typeof item === 'string'\n ? item\n : prettyFormat(item, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n plugins: [plugins.ReactElement],\n })\n ),\n })\n );\n } catch {\n // If sending logs causes any failures we want to silently ignore them\n // to ensure we do not cause infinite-logging loops.\n }\n },\n\n // Called once by the bridge on startup, even if Fast Refresh is off.\n // It creates the HMR client but doesn't actually set up the socket yet.\n setup({ isEnabled }: { isEnabled: boolean }) {\n assert(!hmrClient, 'Cannot initialize hmrClient twice');\n\n const serverScheme = window.location.protocol === 'https:' ? 'wss' : 'ws';\n const client = new MetroHMRClient(`${serverScheme}://${window.location.host}/hot`);\n hmrClient = client;\n\n const { fullBundleUrl } = getDevServer();\n pendingEntryPoints.push(\n // HMRServer understands regular bundle URLs, so prefer that in case\n // there are any important URL parameters we can't reconstruct from\n // `setup()`'s arguments.\n fullBundleUrl\n );\n\n client.on('connection-error', (e: Error) => {\n let error = `Cannot connect to Metro.\n \n Try the following to fix the issue:\n - Ensure the Metro dev server is running and available on the same network as this device`;\n error += `\n \n URL: ${window.location.host}\n \n Error: ${e.message}`;\n\n setHMRUnavailableReason(error);\n });\n\n client.on('update-start', ({ isInitialUpdate }: { isInitialUpdate?: boolean }) => {\n currentCompileErrorMessage = null;\n didConnect = true;\n\n if (client.isEnabled() && !isInitialUpdate) {\n LoadingView.showMessage('Refreshing...', 'refresh');\n }\n });\n\n client.on('update', ({ isInitialUpdate }: { isInitialUpdate?: boolean }) => {\n if (client.isEnabled() && !isInitialUpdate) {\n dismissRedbox();\n LogBox.clearAllLogs();\n }\n });\n\n client.on('update-done', () => {\n LoadingView.hide();\n });\n\n client.on('error', (data: { type: string; message: string }) => {\n LoadingView.hide();\n\n if (data.type === 'GraphNotFoundError') {\n client.close();\n setHMRUnavailableReason('Metro has restarted since the last edit. Reload to reconnect.');\n } else if (data.type === 'RevisionNotFoundError') {\n client.close();\n setHMRUnavailableReason('Metro and the client are out of sync. Reload to reconnect.');\n } else {\n currentCompileErrorMessage = `${data.type} ${data.message}`;\n if (client.isEnabled()) {\n showCompileError();\n }\n }\n });\n\n client.on('close', (closeEvent: { code: number; reason: string }) => {\n LoadingView.hide();\n\n // https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1\n // https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5\n const isNormalOrUnsetCloseReason =\n closeEvent == null ||\n closeEvent.code === 1000 ||\n closeEvent.code === 1005 ||\n closeEvent.code == null;\n\n setHMRUnavailableReason(\n `${\n isNormalOrUnsetCloseReason\n ? 'Disconnected from Metro.'\n : `Disconnected from Metro (${closeEvent.code}: \"${closeEvent.reason}\").`\n }\n\nTo reconnect:\n- Ensure that Metro is running and available on the same network\n- Reload this app (will trigger further help if Metro cannot be connected to)\n `\n );\n });\n\n if (isEnabled) {\n HMRClient.enable();\n } else {\n HMRClient.disable();\n }\n\n registerBundleEntryPoints(hmrClient);\n flushEarlyLogs();\n },\n};\n\nfunction setHMRUnavailableReason(reason: string) {\n assert(hmrClient, 'Expected HMRClient.setup() call at startup.');\n if (hmrUnavailableReason !== null) {\n // Don't show more than one warning.\n return;\n }\n hmrUnavailableReason = reason;\n\n // We only want to show a warning if Fast Refresh is on *and* if we ever\n // previously managed to connect successfully. We don't want to show\n // the warning to native engineers who use cached bundles without Metro.\n if (hmrClient.isEnabled() && didConnect) {\n console.warn(reason);\n // (Not using the `warning` module to prevent a Buck cycle.)\n }\n}\n\nfunction registerBundleEntryPoints(client: HMRClientType | null) {\n if (hmrUnavailableReason != null) {\n // \"Bundle Splitting – Metro disconnected\"\n window.location.reload();\n return;\n }\n\n if (pendingEntryPoints.length > 0) {\n client?.send(\n JSON.stringify({\n type: 'register-entrypoints',\n entryPoints: pendingEntryPoints,\n })\n );\n pendingEntryPoints.length = 0;\n }\n}\n\nfunction flushEarlyLogs() {\n try {\n pendingLogs.forEach(([level, data]) => {\n HMRClient.log(level, data);\n });\n } finally {\n pendingLogs.length = 0;\n }\n}\n\nfunction dismissRedbox() {\n // TODO(EvanBacon): Error overlay for web.\n}\n\nfunction showCompileError() {\n if (currentCompileErrorMessage === null) {\n return;\n }\n\n // Even if there is already a redbox, syntax errors are more important.\n // Otherwise you risk seeing a stale runtime error while a syntax error is more recent.\n dismissRedbox();\n\n const message = currentCompileErrorMessage;\n currentCompileErrorMessage = null;\n\n const error = new Error(message);\n // Symbolicating compile errors is wasted effort\n // because the stack trace is meaningless:\n // @ts-expect-error\n error.preventSymbolication = true;\n throw error;\n}\n\nexport default HMRClient;\n"]}