From 86c5abac2a2402ba6eb4b8e32c262d5ee563177a Mon Sep 17 00:00:00 2001 From: Bill Alves Date: Mon, 27 Nov 2023 11:49:47 -0800 Subject: [PATCH] avoid scheduling frame callback if there are no events (#41658) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41658 changelog: [internal] FabricEventDispatcher does not need to run on every frame. Whenever a new event is added to Fabric's event queue, it will call `FabricUIManager.onRequestEventBeat` in Java. This in turn calls `FabricEventDispatcher.maybePostFrameCallbackFromNonUI` and adds a frame callback on the Choreographer. This makes code simpler, as we do not need to manage the frame callback subscription. Reviewed By: sammy-SC Differential Revision: D50604303 fbshipit-source-id: ce2c7b77678bfc14aa7ecac71e40f78263c7036a --- .../events/FabricEventDispatcher.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/FabricEventDispatcher.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/FabricEventDispatcher.java index 953649288c239a..a1515559cd1642 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/FabricEventDispatcher.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/FabricEventDispatcher.java @@ -11,6 +11,7 @@ import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.UiThreadUtil; +import com.facebook.react.config.ReactFeatureFlags; import com.facebook.react.modules.core.ReactChoreographer; import com.facebook.react.uimanager.common.UIManagerType; import com.facebook.systrace.Systrace; @@ -84,7 +85,9 @@ public void removeBatchEventDispatchedListener(BatchEventDispatchedListener list @Override public void onHostResume() { - maybePostFrameCallbackFromNonUI(); + if (!ReactFeatureFlags.enableOnDemandReactChoreographer) { + maybePostFrameCallbackFromNonUI(); + } } @Override @@ -94,17 +97,21 @@ public void onHostPause() { @Override public void onHostDestroy() { - stopFrameCallback(); + if (!ReactFeatureFlags.enableOnDemandReactChoreographer) { + stopFrameCallback(); + } } public void onCatalystInstanceDestroyed() { - UiThreadUtil.runOnUiThread( - new Runnable() { - @Override - public void run() { - stopFrameCallback(); - } - }); + if (!ReactFeatureFlags.enableOnDemandReactChoreographer) { + UiThreadUtil.runOnUiThread( + new Runnable() { + @Override + public void run() { + stopFrameCallback(); + } + }); + } } private void stopFrameCallback() { @@ -133,7 +140,7 @@ private class ScheduleDispatchFrameCallback implements Choreographer.FrameCallba public void doFrame(long frameTimeNanos) { UiThreadUtil.assertOnUiThread(); - if (mShouldStop) { + if (ReactFeatureFlags.enableOnDemandReactChoreographer || mShouldStop) { mIsPosted = false; } else { post();