diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index f8ce6806c9c..c96350d7fce 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3262,6 +3262,8 @@ public final class com/facebook/react/modules/core/ReactChoreographer$CallbackTy } public final class com/facebook/react/modules/core/TimingModule : com/facebook/fbreact/specs/NativeTimingSpec, com/facebook/react/modules/core/JavaScriptTimerExecutor { + public static final field Companion Lcom/facebook/react/modules/core/TimingModule$Companion; + public static final field NAME Ljava/lang/String; public fun (Lcom/facebook/react/bridge/ReactApplicationContext;Lcom/facebook/react/devsupport/interfaces/DevSupportManager;)V public fun callIdleCallbacks (D)V public fun callTimers (Lcom/facebook/react/bridge/WritableArray;)V @@ -3273,6 +3275,9 @@ public final class com/facebook/react/modules/core/TimingModule : com/facebook/f public fun setSendIdleEvents (Z)V } +public final class com/facebook/react/modules/core/TimingModule$Companion { +} + public final class com/facebook/react/modules/debug/DevSettingsModule : com/facebook/fbreact/specs/NativeDevSettingsSpec { public fun (Lcom/facebook/react/bridge/ReactApplicationContext;Lcom/facebook/react/devsupport/interfaces/DevSupportManager;)V public fun addListener (Ljava/lang/String;)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.java deleted file mode 100644 index af70e66e3eb..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.modules.core; - -import com.facebook.fbreact.specs.NativeTimingSpec; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.WritableArray; -import com.facebook.react.common.annotations.VisibleForTesting; -import com.facebook.react.devsupport.interfaces.DevSupportManager; -import com.facebook.react.jstasks.HeadlessJsTaskContext; -import com.facebook.react.module.annotations.ReactModule; - -/** Native module for JS timer execution. Timers fire on frame boundaries. */ -@ReactModule(name = NativeTimingSpec.NAME) -public final class TimingModule extends NativeTimingSpec implements JavaScriptTimerExecutor { - private final JavaTimerManager mJavaTimerManager; - - public TimingModule(ReactApplicationContext reactContext, DevSupportManager devSupportManager) { - super(reactContext); - - mJavaTimerManager = - new JavaTimerManager( - reactContext, this, ReactChoreographer.getInstance(), devSupportManager); - } - - @Override - public void initialize() { - HeadlessJsTaskContext headlessJsTaskContext = - HeadlessJsTaskContext.getInstance(getReactApplicationContext()); - headlessJsTaskContext.addTaskEventListener(mJavaTimerManager); - } - - @Override - public void createTimer( - final double callbackIDDouble, - final double durationDouble, - final double jsSchedulingTime, - final boolean repeat) { - final int callbackID = (int) callbackIDDouble; - final int duration = (int) durationDouble; - - mJavaTimerManager.createAndMaybeCallTimer(callbackID, duration, jsSchedulingTime, repeat); - } - - @Override - public void deleteTimer(double timerIdDouble) { - int timerId = (int) timerIdDouble; - - mJavaTimerManager.deleteTimer(timerId); - } - - @Override - public void setSendIdleEvents(final boolean sendIdleEvents) { - mJavaTimerManager.setSendIdleEvents(sendIdleEvents); - } - - @Override - public void callTimers(WritableArray timerIDs) { - ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn(); - - if (reactApplicationContext != null) { - reactApplicationContext.getJSModule(JSTimers.class).callTimers(timerIDs); - } - } - - @Override - public void callIdleCallbacks(double frameTime) { - ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn(); - - if (reactApplicationContext != null) { - reactApplicationContext.getJSModule(JSTimers.class).callIdleCallbacks(frameTime); - } - } - - @Override - public void emitTimeDriftWarning(String warningMessage) { - ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn(); - - if (reactApplicationContext != null) { - reactApplicationContext.getJSModule(JSTimers.class).emitTimeDriftWarning(warningMessage); - } - } - - @Override - public void invalidate() { - ReactApplicationContext reactApplicationContext = getReactApplicationContext(); - - HeadlessJsTaskContext headlessJsTaskContext = - HeadlessJsTaskContext.getInstance(reactApplicationContext); - headlessJsTaskContext.removeTaskEventListener(mJavaTimerManager); - - mJavaTimerManager.onInstanceDestroy(); - } - - @VisibleForTesting - public boolean hasActiveTimersInRange(long rangeMs) { - return mJavaTimerManager.hasActiveTimersInRange(rangeMs); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.kt new file mode 100644 index 00000000000..5f797fdf7fc --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.kt @@ -0,0 +1,83 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.modules.core + +import com.facebook.fbreact.specs.NativeTimingSpec +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.WritableArray +import com.facebook.react.common.annotations.VisibleForTesting +import com.facebook.react.devsupport.interfaces.DevSupportManager +import com.facebook.react.jstasks.HeadlessJsTaskContext +import com.facebook.react.module.annotations.ReactModule + +/** Native module for JS timer execution. Timers fire on frame boundaries. */ +@ReactModule(name = NativeTimingSpec.NAME) +public class TimingModule( + reactContext: ReactApplicationContext?, + devSupportManager: DevSupportManager? +) : com.facebook.fbreact.specs.NativeTimingSpec(reactContext), JavaScriptTimerExecutor { + private val javaTimerManager: JavaTimerManager = + JavaTimerManager(reactContext, this, ReactChoreographer.getInstance(), devSupportManager) + + override fun initialize() { + HeadlessJsTaskContext.getInstance(getReactApplicationContext()) + .addTaskEventListener(javaTimerManager) + } + + override fun createTimer( + callbackIDDouble: Double, + durationDouble: Double, + jsSchedulingTime: Double, + repeat: Boolean + ) { + val callbackID = callbackIDDouble.toInt() + val duration = durationDouble.toInt() + javaTimerManager.createAndMaybeCallTimer(callbackID, duration, jsSchedulingTime, repeat) + } + + override fun deleteTimer(timerIdDouble: Double) { + val timerId = timerIdDouble.toInt() + javaTimerManager.deleteTimer(timerId) + } + + override fun setSendIdleEvents(sendIdleEvents: Boolean) { + javaTimerManager.setSendIdleEvents(sendIdleEvents) + } + + override fun callTimers(timerIDs: WritableArray) { + getReactApplicationContextIfActiveOrWarn() + ?.getJSModule(JSTimers::class.java) + ?.callTimers(timerIDs) + } + + override fun callIdleCallbacks(frameTime: Double) { + getReactApplicationContextIfActiveOrWarn() + ?.getJSModule(JSTimers::class.java) + ?.callIdleCallbacks(frameTime) + } + + override fun emitTimeDriftWarning(warningMessage: String) { + getReactApplicationContextIfActiveOrWarn() + ?.getJSModule(JSTimers::class.java) + ?.emitTimeDriftWarning(warningMessage) + } + + override fun invalidate() { + val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(getReactApplicationContext()) + headlessJsTaskContext.removeTaskEventListener(javaTimerManager) + javaTimerManager.onInstanceDestroy() + } + + @VisibleForTesting + public fun hasActiveTimersInRange(rangeMs: Long): Boolean = + javaTimerManager.hasActiveTimersInRange(rangeMs) + + public companion object { + public const val NAME: String = NativeTimingSpec.NAME + } +}