Skip to content

Commit

Permalink
Add new dangerouslyForceOverride method to feature flags (facebook#46963
Browse files Browse the repository at this point in the history
)

Summary:

Changelog: [internal]

This introduces a new method in `ReactNativeFeatureFlags` to force setting overrides without triggering any errors. This is partially equivalent to calling `dangerouslyReset` and `override` but completely removing the errors that could be caused by race conditions.

Reviewed By: mdvacca

Differential Revision: D64186262
  • Loading branch information
rubennorte authored and facebook-github-bot committed Oct 10, 2024
1 parent e39c917 commit d5aa3e9
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 78 deletions.
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<<405d53cd6aba78616b8690c26a0accad>>
* @generated SignedSource<<7d6e3103a357196572e1d2cd428079e8>>
*/

/**
Expand Down Expand Up @@ -358,6 +358,25 @@ public object ReactNativeFeatureFlags {
accessor = accessorProvider()
}

/**
* This is a combination of `dangerouslyReset` and `override` that reduces
* the likeliness of a race condition between the two calls.
*
* This is **dangerous** because it can introduce consistency issues that will
* be much harder to debug. For example, it could hide the fact that feature
* flags are read before you set the values you want to use everywhere. It
* could also cause a workflow to suddently have different feature flags for
* behaviors that were configured with different values before.
*
* Please see the documentation of `dangerouslyReset` for additional details.
*/
@JvmStatic
public fun dangerouslyForceOverride(provider: ReactNativeFeatureFlagsProvider) {
val newAccessor = accessorProvider()
newAccessor.dangerouslyForceOverride(provider)
accessor = newAccessor
}

/**
* This is just used to replace the default ReactNativeFeatureFlagsCxxAccessor
* that uses JNI with a version that doesn't, to simplify testing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ReactNativeFeatureFlagsAccessor : ReactNativeFeatureFlagsProvid
public fun override(provider: ReactNativeFeatureFlagsProvider)

public fun dangerouslyReset()

public fun dangerouslyForceOverride(provider: ReactNativeFeatureFlagsProvider)
}
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<<84ee754f916b48a7c55ea94e166510c7>>
* @generated SignedSource<<e1cd412fb134f16bce35538931c42dbb>>
*/

/**
Expand Down Expand Up @@ -515,4 +515,7 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
ReactNativeFeatureFlagsCxxInterop.override(provider as Any)

override fun dangerouslyReset(): Unit = ReactNativeFeatureFlagsCxxInterop.dangerouslyReset()

override fun dangerouslyForceOverride(provider: ReactNativeFeatureFlagsProvider): Unit =
ReactNativeFeatureFlagsCxxInterop.dangerouslyForceOverride(provider as Any)
}
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<<eebeaa749dafac5d49d9d6b356f88817>>
* @generated SignedSource<<1dbf1813a7c1295544eb720ce51006e7>>
*/

/**
Expand Down Expand Up @@ -129,4 +129,6 @@ public object ReactNativeFeatureFlagsCxxInterop {
@DoNotStrip @JvmStatic public external fun override(provider: Any)

@DoNotStrip @JvmStatic public external fun dangerouslyReset()

@DoNotStrip @JvmStatic public external fun dangerouslyForceOverride(provider: Any)
}
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<<4dc2364f5bcd765d7b61dbcab0d9533d>>
* @generated SignedSource<<30d2773236646a70210f58ac99dea454>>
*/

/**
Expand Down Expand Up @@ -577,4 +577,8 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
// We don't need to do anything here because `ReactNativeFeatureFlags` will
// just create a new instance of this class.
}

override fun dangerouslyForceOverride(provider: ReactNativeFeatureFlagsProvider) {
currentProvider = provider
}
}
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<<0bb021affcef5162b578ad3f1e45508f>>
* @generated SignedSource<<5a019346f4d58f2afd1685f8e39f32a3>>
*/

/**
Expand Down Expand Up @@ -594,11 +594,21 @@ void JReactNativeFeatureFlagsCxxInterop::dangerouslyReset(
ReactNativeFeatureFlags::dangerouslyReset();
}

void JReactNativeFeatureFlagsCxxInterop::dangerouslyForceOverride(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/,
jni::alias_ref<jobject> provider) {
ReactNativeFeatureFlags::dangerouslyForceOverride(
std::make_unique<ReactNativeFeatureFlagsProviderHolder>(provider));
}

void JReactNativeFeatureFlagsCxxInterop::registerNatives() {
javaClassLocal()->registerNatives({
makeNativeMethod(
"override", JReactNativeFeatureFlagsCxxInterop::override),
makeNativeMethod("dangerouslyReset", JReactNativeFeatureFlagsCxxInterop::dangerouslyReset),
makeNativeMethod(
"dangerouslyForceOverride",
JReactNativeFeatureFlagsCxxInterop::dangerouslyForceOverride),
makeNativeMethod(
"commonTestFlag",
JReactNativeFeatureFlagsCxxInterop::commonTestFlag),
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<<fe97369e30d5359ffcd9c8e304dbc121>>
* @generated SignedSource<<6db47a6cb809ac7a2e6a6cda190abed4>>
*/

/**
Expand Down Expand Up @@ -184,6 +184,10 @@ class JReactNativeFeatureFlagsCxxInterop
static void dangerouslyReset(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);

static void dangerouslyForceOverride(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>,
jni::alias_ref<jobject> provider);

static void registerNatives();
};

Expand Down
Loading

0 comments on commit d5aa3e9

Please sign in to comment.