Skip to content

Commit

Permalink
Pass default values for feature flags to overrides providers (#46533)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46533

Changelog: [internal]

When defining overrides for feature flags, it's common to have this pattern:

```javascript
myFeatureFlag: () => someCondition ? value : defaultValueForFlag
```

But it's not always obvious how to get the default value defined in the feature flag system. This modifies the API so we receive the default value as a parameter to simplify the logic:

```javascript
myFeatureFlag: (defaultValueForFlag) => someCondition ? value : defaultValueForFlag
```

Reviewed By: mdvacca, rshest

Differential Revision: D62853299

fbshipit-source-id: 9421e31d00662e2a23f1e3fb43bdc9f5cf03fdf3
  • Loading branch information
rubennorte authored and facebook-github-bot committed Sep 17, 2024
1 parent 958d35f commit 89f824b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ${DO_NOT_MODIFY_COMMENT}
import {
type Getter,
type OverridesFor,
createJavaScriptFlagGetter,
createNativeFlagGetter,
setOverrides,
Expand All @@ -42,7 +43,7 @@ ${Object.entries(definitions.jsOnly)
.join('\n')}
};
export type ReactNativeFeatureFlagsJsOnlyOverrides = Partial<ReactNativeFeatureFlagsJsOnly>;
export type ReactNativeFeatureFlagsJsOnlyOverrides = OverridesFor<ReactNativeFeatureFlagsJsOnly>;
export type ReactNativeFeatureFlags = {
...ReactNativeFeatureFlagsJsOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<08cb1ef37f80dbd53e1aa9ff5ba97286>>
* @generated SignedSource<<a1d731250e59d99ac390c9d01c4dbd2f>>
* @flow strict
*/

Expand All @@ -20,6 +20,7 @@

import {
type Getter,
type OverridesFor,
createJavaScriptFlagGetter,
createNativeFlagGetter,
setOverrides,
Expand All @@ -43,7 +44,7 @@ export type ReactNativeFeatureFlagsJsOnly = {
useRefsForTextInputState: Getter<boolean>,
};

export type ReactNativeFeatureFlagsJsOnlyOverrides = Partial<ReactNativeFeatureFlagsJsOnly>;
export type ReactNativeFeatureFlagsJsOnlyOverrides = OverridesFor<ReactNativeFeatureFlagsJsOnly>;

export type ReactNativeFeatureFlags = {
...ReactNativeFeatureFlagsJsOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ let overrides: ?ReactNativeFeatureFlagsJsOnlyOverrides;

export type Getter<T> = () => T;

// This defines the types for the overrides object, whose methods also receive
// the default value as a parameter.
export type OverridesFor<T> = Partial<{
[key in keyof T]: (ReturnType<T[key]>) => ReturnType<T[key]>,
}>;

function createGetter<T: boolean | number | string>(
configName: string,
customValueGetter: Getter<?T>,
Expand All @@ -45,7 +51,7 @@ export function createJavaScriptFlagGetter<
configName,
() => {
accessedFeatureFlags.add(configName);
return overrides?.[configName]?.();
return overrides?.[configName]?.(defaultValue);
},
defaultValue,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('ReactNativeFeatureFlags', () => {
it('should access and cache overridden JS-only flags', () => {
const ReactNativeFeatureFlags = require('../ReactNativeFeatureFlags');

const jsOnlyTestFlagFn = jest.fn(() => true);
const jsOnlyTestFlagFn = jest.fn((defaultValue: boolean) => true);
ReactNativeFeatureFlags.override({
jsOnlyTestFlag: jsOnlyTestFlagFn,
});
Expand Down Expand Up @@ -125,4 +125,17 @@ describe('ReactNativeFeatureFlags', () => {
}),
).toThrow('Feature flags cannot be overridden more than once');
});

it('should pass the default value of the feature flag to the function that provides its override', () => {
const ReactNativeFeatureFlags = require('../ReactNativeFeatureFlags');

ReactNativeFeatureFlags.override({
jsOnlyTestFlag: (defaultValue: boolean) => {
expect(defaultValue).toBe(false);
return true;
},
});

expect(ReactNativeFeatureFlags.jsOnlyTestFlag()).toBe(true);
});
});

0 comments on commit 89f824b

Please sign in to comment.