|
| 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 | +import type {ReactBuildType} from 'react-devtools-shared/src/backend/types'; |
| 10 | + |
| 11 | +function reduceReactBuild( |
| 12 | + currentReactBuildType: null | ReactBuildType, |
| 13 | + nextReactBuildType: ReactBuildType, |
| 14 | +): ReactBuildType { |
| 15 | + if ( |
| 16 | + currentReactBuildType === null || |
| 17 | + currentReactBuildType === 'production' |
| 18 | + ) { |
| 19 | + return nextReactBuildType; |
| 20 | + } |
| 21 | + |
| 22 | + // We only display the "worst" build type, so if we've already detected a non-production build, |
| 23 | + // we ignore any future production builds. This way if a page has multiple renderers, |
| 24 | + // and at least one of them is a non-production build, we'll display that instead of "production". |
| 25 | + return nextReactBuildType === 'production' |
| 26 | + ? currentReactBuildType |
| 27 | + : nextReactBuildType; |
| 28 | +} |
| 29 | + |
| 30 | +export function createReactRendererListener(target: { |
| 31 | + postMessage: Function, |
| 32 | + ... |
| 33 | +}): ({reactBuildType: ReactBuildType}) => void { |
| 34 | + let displayedReactBuild: null | ReactBuildType = null; |
| 35 | + |
| 36 | + return function ({reactBuildType}) { |
| 37 | + displayedReactBuild = reduceReactBuild(displayedReactBuild, reactBuildType); |
| 38 | + |
| 39 | + target.postMessage( |
| 40 | + { |
| 41 | + source: 'react-devtools-hook', |
| 42 | + payload: { |
| 43 | + type: 'react-renderer-attached', |
| 44 | + reactBuildType: displayedReactBuild, |
| 45 | + }, |
| 46 | + }, |
| 47 | + '*', |
| 48 | + ); |
| 49 | + }; |
| 50 | +} |
0 commit comments