| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | export type ExtensionBridgeConnectionType = |
| 11 | | 'react-devtools-extension-bridge-connection-ready' |
| 12 | | 'react-devtools-extension-bridge-connection-disconnected'; |
| 13 | |
| 14 | export const EXTENSION_BRIDGE_CONNECTION_READY: ExtensionBridgeConnectionType = |
| 15 | 'react-devtools-extension-bridge-connection-ready'; |
| 16 | export const EXTENSION_BRIDGE_CONNECTION_DISCONNECTED: ExtensionBridgeConnectionType = |
| 17 | 'react-devtools-extension-bridge-connection-disconnected'; |
| 18 | |
| 19 | export function getExtensionBridgeConnectionType( |
| 20 | message: mixed, |
| 21 | ): ExtensionBridgeConnectionType | null { |
| 22 | if ( |
| 23 | message === null || |
| 24 | typeof message !== 'object' || |
| 25 | !('source' in message) || |
| 26 | message.source !== 'react-devtools-background' || |
| 27 | !('payload' in message) |
| 28 | ) { |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | const payload = message.payload; |
| 33 | if (payload === null || typeof payload !== 'object' || !('type' in payload)) { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | const type = payload.type; |
| 38 | if (type === EXTENSION_BRIDGE_CONNECTION_READY) { |
| 39 | return EXTENSION_BRIDGE_CONNECTION_READY; |
| 40 | } |
| 41 | if (type === EXTENSION_BRIDGE_CONNECTION_DISCONNECTED) { |
| 42 | return EXTENSION_BRIDGE_CONNECTION_DISCONNECTED; |
| 43 | } |
| 44 | return null; |
| 45 | } |