Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext) :
// emit the event to JS to resync the trees
val onAnimationEndedData = buildReadableMap { putArray("tags") { tags.forEach { add(it) } } }

val reactApplicationContext = reactApplicationContextIfActiveOrWarn
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
reactApplicationContext?.emitDeviceEvent("onUserDrivenAnimationEnded", onAnimationEndedData)
}

Expand Down Expand Up @@ -352,7 +352,7 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext) :
*/
get() {
if (nodesManagerRef.get() == null) {
val reactApplicationContext = reactApplicationContextIfActiveOrWarn
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()

if (reactApplicationContext != null) {
nodesManagerRef.compareAndSet(null, NativeAnimatedNodesManager(reactApplicationContext))
Expand Down Expand Up @@ -435,7 +435,7 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext) :
return
}

val reactApplicationContext = reactApplicationContextIfActiveOrWarn
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
if (reactApplicationContext != null) {
val uiManager = UIManagerHelper.getUIManager(reactApplicationContext, uiManagerType)
if (uiManager != null) {
Expand Down Expand Up @@ -543,7 +543,7 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext) :
put("offset", offset)
}

val reactApplicationContext = reactApplicationContextIfActiveOrWarn
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
reactApplicationContext?.emitDeviceEvent("onAnimatedValueUpdate", onAnimatedValueData)
}

Expand Down Expand Up @@ -981,7 +981,7 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext) :
addUnbatchedOperation(
object : UIThreadOperation() {
override fun execute(animatedNodesManager: NativeAnimatedNodesManager) {
val reactApplicationContext = reactApplicationContextIfActiveOrWarn
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()

var viewTag = -1
var i = 0
Expand Down Expand Up @@ -1013,7 +1013,7 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext) :
put("offset", offset)
}

val reactApplicationContext = reactApplicationContextIfActiveOrWarn
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
reactApplicationContext?.emitDeviceEvent(
"onAnimatedValueUpdate",
onAnimatedValueData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.bridge;

import static com.facebook.infer.annotation.ThreadConfined.ANY;

import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.annotations.StableReactNativeAPI;
import com.facebook.react.common.build.ReactBuildConfig;
import java.util.Map;
package com.facebook.react.bridge

import com.facebook.common.logging.FLog
import com.facebook.infer.annotation.Assertions
import com.facebook.infer.annotation.Nullsafe
import com.facebook.infer.annotation.ThreadConfined
import com.facebook.infer.annotation.ThreadConfined.ANY
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.react.common.ReactConstants
import com.facebook.react.common.annotations.StableReactNativeAPI
import com.facebook.react.common.build.ReactBuildConfig

/**
* Base class for Catalyst native modules whose implementations are written in Java. Default
Expand Down Expand Up @@ -50,58 +47,48 @@
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
@StableReactNativeAPI
public abstract class BaseJavaModule implements NativeModule {
// taken from Libraries/Utilities/MessageQueue.js
public static final String METHOD_TYPE_ASYNC = "async";
public static final String METHOD_TYPE_PROMISE = "promise";
public static final String METHOD_TYPE_SYNC = "sync";
public abstract class BaseJavaModule : NativeModule {
@JvmField
protected var mEventEmitterCallback: CxxCallbackImpl? = null

protected @Nullable CxxCallbackImpl mEventEmitterCallback;
private val mReactApplicationContext: ReactApplicationContext?

private final @Nullable ReactApplicationContext mReactApplicationContext;
public constructor() : this(null)

public BaseJavaModule() {
this(null);
}

public BaseJavaModule(@Nullable ReactApplicationContext reactContext) {
mReactApplicationContext = reactContext;
public constructor(reactContext: ReactApplicationContext?) {
mReactApplicationContext = reactContext
}

/**
* @return a map of constants this module exports to JS. Supports JSON types.
*/
public @Nullable Map<String, Object> getConstants() {
return null;
}
public open fun getConstants(): Map<String, Any>? = null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, please revert this back to the function form.

I applied your review @javache


@Override
public void initialize() {
public override fun initialize() {
// do nothing
}

@Override
public boolean canOverrideExistingModule() {
return false;
@Deprecated("canOverrideExistingModule is not used in the New Architecture")
public override fun canOverrideExistingModule(): Boolean {
return false
}

/**
* The CatalystInstance is going away with Venice. Therefore, the TurboModule infra introduces the
* invalidate() method to allow NativeModules to clean up after themselves.
*/
@Override
public void invalidate() {}
public override fun invalidate() {}

/**
* Subclasses can use this method to access {@link ReactApplicationContext} passed as a
* constructor.
* Property accessor for Kotlin code using property syntax. Subclasses can use this to access
* {@link ReactApplicationContext} passed as a constructor.
*/
protected final ReactApplicationContext getReactApplicationContext() {
return Assertions.assertNotNull(
mReactApplicationContext,
"Tried to get ReactApplicationContext even though NativeModule wasn't instantiated with"
+ " one");
}
protected val reactApplicationContext: ReactApplicationContext
@JvmName("getReactApplicationContext")
get() = Assertions.assertNotNull(
mReactApplicationContext,
"Tried to get ReactApplicationContext even though NativeModule wasn't instantiated with one"
)

/**
* Subclasses can use this method to access {@link ReactApplicationContext} passed as a
Expand All @@ -117,24 +104,31 @@ protected final ReactApplicationContext getReactApplicationContext() {
* thread-safe.
*/
@ThreadConfined(ANY)
protected @Nullable final ReactApplicationContext getReactApplicationContextIfActiveOrWarn() {
protected fun getReactApplicationContextIfActiveOrWarn(): ReactApplicationContext? {
if (mReactApplicationContext != null && mReactApplicationContext.hasActiveReactInstance()) {
return mReactApplicationContext;
return mReactApplicationContext
}

// We want to collect data about how often this happens, but SoftExceptions will cause a crash
// in debug mode, which isn't usually desirable.
String msg = "React Native Instance has already disappeared: requested by " + getName();
val msg = "React Native Instance has already disappeared: requested by $name"
if (ReactBuildConfig.DEBUG) {
FLog.w(ReactConstants.TAG, msg);
FLog.w(ReactConstants.TAG, msg)
} else {
ReactSoftExceptionLogger.logSoftException(ReactConstants.TAG, new RuntimeException(msg));
ReactSoftExceptionLogger.logSoftException(ReactConstants.TAG, RuntimeException(msg))
}
return null;
return null
}

@DoNotStrip
protected void setEventEmitterCallback(CxxCallbackImpl eventEmitterCallback) {
mEventEmitterCallback = eventEmitterCallback;
protected fun setEventEmitterCallback(eventEmitterCallback: CxxCallbackImpl) {
mEventEmitterCallback = eventEmitterCallback
}

public companion object {
// taken from Libraries/Utilities/MessageQueue.js
public const val METHOD_TYPE_ASYNC: String = "async"
public const val METHOD_TYPE_PROMISE: String = "promise"
public const val METHOD_TYPE_SYNC: String = "sync"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ internal class JavaModuleWrapper(
val baseJavaModule = module

Systrace.beginSection(TRACE_TAG_REACT, "module.getConstants")
val map = baseJavaModule.constants
val map = baseJavaModule.getConstants()
Systrace.endSection(TRACE_TAG_REACT)

Systrace.beginSection(TRACE_TAG_REACT, "create WritableNativeMap")
Expand Down
Loading
Loading