modules/core/TimingModule to Kotlin (#45815)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45815

# Changelog:
[Internal] -

As in the title.

Reviewed By: alanleedev

Differential Revision: D60446859

fbshipit-source-id: 0ca52c59e2bbaa18c751f4459afac811e9c89703
This commit is contained in:
Ruslan Shestopalyuk
2024-07-31 08:38:18 -07:00
committed by Facebook GitHub Bot
parent 6742af4a9d
commit 3735cb3d4f
3 changed files with 88 additions and 104 deletions
@@ -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 <init> (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 <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Lcom/facebook/react/devsupport/interfaces/DevSupportManager;)V
public fun addListener (Ljava/lang/String;)V
@@ -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);
}
}
@@ -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
}
}