forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 22
Refactor and centralise RN experiment registration #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
06ad02a
Refactor and centralise RN experiment registration
motiz88 ee64372
Remove globalThis.reactNativeDocLink
motiz88 8cfd8f8
Remove docLink from REACT_NATIVE_SPECIFIC_UI experiment
motiz88 bf919e3
Update list of Meta-specific paths in check_license_header
motiz88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| // Copyright 2024 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| // Chrome DevTools has an experiment system integrated deeply with its panel | ||
| // framework and settings UI. We add some React Native-specific experiments, | ||
| // some of which control new RN-specific UI and some of which add gating to | ||
| // *existing* features. | ||
| // | ||
| // The goals are: | ||
| // 1. To allow the core, non-RN entry points (like `inspector.ts`) to continue | ||
| // to work, largely unmodified, for ease of testing. | ||
| // 2. To allow users of each entry point to enable or disable experiments as | ||
| // needed through the UI. | ||
| // 3. To only show experiments in Settings if they are relevant to the current | ||
| // entry point. | ||
| // 4. To minimise RN-specific changes to core code, for ease of rebasing onto | ||
| // new versions of Chrome DevTools. | ||
| // 5. To allow RN entry points to enable/configure *core* experiments before | ||
| // they are registered (in MainImpl). | ||
| // | ||
| // To add a new React Native-specific experiment: | ||
| // - define it in the RNExperiments enum and Experiments enum (in Runtime.ts) | ||
| // - register it in this file (rn_experiments.ts) | ||
| // - set `enabledByDefault` and `configurable` as appropriate | ||
| // - optionally, configure it further in each RN-specific entry point | ||
| // (rn_inspector.ts, rn_fusebox.ts) | ||
| // | ||
| // React Native-specific experiments are merged into the main ExperimentsSupport | ||
| // object in MainImpl and can't be configured further afterwards (except | ||
| // through the UI). | ||
|
|
||
| import * as Root from '../../core/root/root.js'; | ||
|
|
||
| export const RNExperimentName = Root.Runtime.RNExperimentName; | ||
| export type RNExperimentName = Root.Runtime.RNExperimentName; | ||
|
|
||
| const state = { | ||
| didInitializeExperiments: false, | ||
| isReactNativeEntryPoint: false, | ||
| }; | ||
|
|
||
| /** | ||
| * Set whether the current entry point is a React Native entry point. | ||
| * This must be called before constructing MainImpl. | ||
| */ | ||
| export function setIsReactNativeEntryPoint(value: boolean) { | ||
| if (state.didInitializeExperiments) { | ||
| throw new Error( | ||
| 'setIsReactNativeEntryPoint must be called before constructing MainImpl' | ||
| ); | ||
| } | ||
| state.isReactNativeEntryPoint = value; | ||
| } | ||
|
|
||
| type RNExperimentPredicate = ({ | ||
| isReactNativeEntryPoint, | ||
| }: { | ||
| isReactNativeEntryPoint: boolean; | ||
| }) => boolean; | ||
| type RNExperimentSpec = { | ||
| name: RNExperimentName; | ||
| title: string; | ||
| unstable: boolean; | ||
| docLink?: string; | ||
| feedbackLink?: string; | ||
| enabledByDefault?: boolean | RNExperimentPredicate; | ||
| configurable?: boolean | RNExperimentPredicate; | ||
| }; | ||
|
|
||
| class RNExperiment { | ||
| readonly name: RNExperimentName; | ||
| readonly title: string; | ||
| readonly unstable: boolean; | ||
| readonly docLink?: string; | ||
| readonly feedbackLink?: string; | ||
| enabledByDefault: RNExperimentPredicate; | ||
| configurable: RNExperimentPredicate; | ||
|
|
||
| constructor(spec: RNExperimentSpec) { | ||
| this.name = spec.name; | ||
| this.title = spec.title; | ||
| this.unstable = spec.unstable; | ||
| this.docLink = spec.docLink; | ||
| this.feedbackLink = spec.feedbackLink; | ||
| this.enabledByDefault = normalizePredicate(spec.enabledByDefault, false); | ||
| this.configurable = normalizePredicate(spec.configurable, true); | ||
| } | ||
| } | ||
|
|
||
| function normalizePredicate( | ||
| pred: boolean | null | undefined | RNExperimentPredicate, | ||
| defaultValue: boolean | ||
| ): RNExperimentPredicate { | ||
| if (pred == null) { | ||
| return () => defaultValue; | ||
| } | ||
| if (typeof pred === 'boolean') { | ||
| return () => pred; | ||
| } | ||
| return pred; | ||
| } | ||
|
|
||
| class RNExperimentsSupport { | ||
| #experiments: Map<Root.Runtime.RNExperimentName, RNExperiment> = new Map(); | ||
| #defaultEnabledCoreExperiments = new Set<Root.Runtime.ExperimentName>(); | ||
| #nonConfigurableCoreExperiments = new Set<Root.Runtime.ExperimentName>(); | ||
|
|
||
| register(spec: RNExperimentSpec): void { | ||
| if (state.didInitializeExperiments) { | ||
| throw new Error( | ||
| 'Experiments must be registered before constructing MainImpl' | ||
| ); | ||
| } | ||
| const { name } = spec; | ||
| if (this.#experiments.has(name)) { | ||
| throw new Error(`React Native Experiment ${name} is already registered`); | ||
| } | ||
| this.#experiments.set(name, new RNExperiment(spec)); | ||
| } | ||
|
|
||
| /** | ||
| * Enable the given (RN-specific or core) experiments by default. | ||
| */ | ||
| enableExperimentsByDefault(names: Root.Runtime.ExperimentName[]) { | ||
| if (state.didInitializeExperiments) { | ||
| throw new Error( | ||
| 'Experiments must be configured before constructing MainImpl' | ||
| ); | ||
| } | ||
| for (const name of names) { | ||
| if (Object.prototype.hasOwnProperty.call(RNExperimentName, name)) { | ||
| const experiment = this.#experiments.get( | ||
| name as unknown as RNExperimentName | ||
| ); | ||
| if (!experiment) { | ||
| throw new Error(`React Native Experiment ${name} is not registered`); | ||
| } | ||
| experiment.enabledByDefault = () => true; | ||
| } else { | ||
| this.#defaultEnabledCoreExperiments.add( | ||
| name as Root.Runtime.ExperimentName | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Set the given (RN-specific or core) experiments to be non-configurable. | ||
| */ | ||
| setNonConfigurableExperiments(names: Root.Runtime.ExperimentName[]) { | ||
| if (state.didInitializeExperiments) { | ||
| throw new Error( | ||
| 'Experiments must be configured before constructing MainImpl' | ||
| ); | ||
| } | ||
| for (const name of names) { | ||
| if (Object.prototype.hasOwnProperty.call(RNExperimentName, name)) { | ||
| const experiment = this.#experiments.get( | ||
| name as unknown as RNExperimentName | ||
| ); | ||
| if (!experiment) { | ||
| throw new Error(`React Native Experiment ${name} is not registered`); | ||
| } | ||
| experiment.configurable = () => false; | ||
| } else { | ||
| this.#nonConfigurableCoreExperiments.add( | ||
| name as Root.Runtime.ExperimentName | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| copyInto(other: Root.Runtime.ExperimentsSupport, titlePrefix: string = ''): void { | ||
| for (const [name, spec] of this.#experiments) { | ||
| other.register( | ||
| name, | ||
| titlePrefix + spec.title, | ||
| spec.unstable, | ||
| spec.docLink, | ||
| spec.feedbackLink | ||
| ); | ||
| if ( | ||
| spec.enabledByDefault({ | ||
| isReactNativeEntryPoint: state.isReactNativeEntryPoint, | ||
| }) | ||
| ) { | ||
| other.enableExperimentsByDefault([name]); | ||
| } | ||
| if ( | ||
| !spec.configurable({ | ||
| isReactNativeEntryPoint: state.isReactNativeEntryPoint, | ||
| }) | ||
| ) { | ||
| other.setNonConfigurableExperiments([name]); | ||
| } | ||
| } | ||
| for (const name of this.#defaultEnabledCoreExperiments) { | ||
| other.enableExperimentsByDefault([name]); | ||
| } | ||
| for (const name of this.#nonConfigurableCoreExperiments) { | ||
| other.setNonConfigurableExperiments([name]); | ||
| } | ||
| state.didInitializeExperiments = true; | ||
| } | ||
| } | ||
|
|
||
| // Early registration for React Native-specific experiments. Only use this | ||
| // *before* constructing MainImpl; afterwards read from Root.Runtime.experiments | ||
| // as normal. | ||
| export const RNExperiments = new RNExperimentsSupport(); | ||
|
|
||
| RNExperiments.register({ | ||
| name: RNExperimentName.JS_HEAP_PROFILER_ENABLE, | ||
| title: 'Enable Heap Profiler', | ||
| unstable: false, | ||
| enabledByDefault: ({ isReactNativeEntryPoint }) => !isReactNativeEntryPoint, | ||
| configurable: ({ isReactNativeEntryPoint }) => isReactNativeEntryPoint, | ||
| }); | ||
|
|
||
| RNExperiments.register({ | ||
| name: RNExperimentName.ENABLE_REACT_DEVTOOLS_PANEL, | ||
| title: 'Enable React DevTools panel', | ||
| unstable: true, | ||
| enabledByDefault: false, | ||
| configurable: ({ isReactNativeEntryPoint }) => isReactNativeEntryPoint, | ||
| }); | ||
|
|
||
| RNExperiments.register({ | ||
| name: RNExperimentName.REACT_NATIVE_SPECIFIC_UI, | ||
| title: 'Show React Native-specific UI', | ||
| unstable: false, | ||
| enabledByDefault: ({ isReactNativeEntryPoint }) => isReactNativeEntryPoint, | ||
| configurable: false, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reverts a change made in 4f0b002 - the experiment is now registered here, but (pre)enabled in
rn_inspectorusingRNExperiments.enableExperimentsByDefault.