Skip to content

Commit 9f65442

Browse files
j-piaseckiblakef
authored andcommitted
Fix timers in headless tasks on bridgeless mode (#47496)
Summary: Fixes #47495 `JavaTimerManager` is being registered to receive headless tasks events in the [`TimingModule`](https://github.com/facebook/react-native/blob/0ee963ea65bcc88122044d51027511e611bde584/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.kt#L28-L29). This module is not used on bridgeless: [1](https://github.com/facebook/react-native/blob/0ee963ea65bcc88122044d51027511e611bde584/packages/react-native/Libraries/Core/setUpTimers.js#L44-L61), [2](https://github.com/facebook/react-native/blob/0ee963ea65bcc88122044d51027511e611bde584/packages/react-native/Libraries/Core/setUpTimers.js#L123-L132) and since it's loaded lazily, the event listener is never registered. This PR moves registration to the constructor of `JavaTimerManager` and deregistration to the `onInstanceDestroy` method. This way the event listener is always registered when an instance of the timer manager exists. ## Changelog: [ANDROID] [FIXED] - Fix timers in headless tasks on bridgeless mode Pull Request resolved: #47496 Test Plan: See the reproducer from the issue Reviewed By: javache Differential Revision: D65615601 Pulled By: alanleedev fbshipit-source-id: 6e1d36f8783e813065f79730a928b99c3e385718
1 parent c4fd80f commit 9f65442

File tree

3 files changed

+2
-9
lines changed

3 files changed

+2
-9
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3306,7 +3306,6 @@ public final class com/facebook/react/modules/core/TimingModule : com/facebook/f
33063306
public fun createTimer (DDDZ)V
33073307
public fun deleteTimer (D)V
33083308
public fun emitTimeDriftWarning (Ljava/lang/String;)V
3309-
public fun initialize ()V
33103309
public fun invalidate ()V
33113310
public fun setSendIdleEvents (Z)V
33123311
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaTimerManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public open class JavaTimerManager(
6565

6666
init {
6767
reactApplicationContext.addLifecycleEventListener(this)
68+
HeadlessJsTaskContext.getInstance(reactApplicationContext).addTaskEventListener(this)
6869
}
6970

7071
override fun onHostPause() {
@@ -103,6 +104,7 @@ public open class JavaTimerManager(
103104
}
104105

105106
public open fun onInstanceDestroy() {
107+
HeadlessJsTaskContext.getInstance(reactApplicationContext).removeTaskEventListener(this)
106108
reactApplicationContext.removeLifecycleEventListener(this)
107109
clearFrameCallback()
108110
clearChoreographerIdleCallback()

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.facebook.react.bridge.ReactApplicationContext
1212
import com.facebook.react.bridge.WritableArray
1313
import com.facebook.react.common.annotations.VisibleForTesting
1414
import com.facebook.react.devsupport.interfaces.DevSupportManager
15-
import com.facebook.react.jstasks.HeadlessJsTaskContext
1615
import com.facebook.react.module.annotations.ReactModule
1716

1817
/** Native module for JS timer execution. Timers fire on frame boundaries. */
@@ -24,11 +23,6 @@ public class TimingModule(
2423
private val javaTimerManager: JavaTimerManager =
2524
JavaTimerManager(reactContext, this, ReactChoreographer.getInstance(), devSupportManager)
2625

27-
override fun initialize() {
28-
HeadlessJsTaskContext.getInstance(getReactApplicationContext())
29-
.addTaskEventListener(javaTimerManager)
30-
}
31-
3226
override fun createTimer(
3327
callbackIDDouble: Double,
3428
durationDouble: Double,
@@ -68,8 +62,6 @@ public class TimingModule(
6862
}
6963

7064
override fun invalidate() {
71-
val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(getReactApplicationContext())
72-
headlessJsTaskContext.removeTaskEventListener(javaTimerManager)
7365
javaTimerManager.onInstanceDestroy()
7466
}
7567

0 commit comments

Comments
 (0)