|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.bridge |
| 9 | + |
| 10 | +import com.facebook.common.logging.FLog |
| 11 | +import com.facebook.react.module.annotations.ReactModule |
| 12 | +import javax.inject.Provider |
| 13 | + |
| 14 | +/** |
| 15 | + * A specification for a native module. This exists so that we don't have to pay the cost for |
| 16 | + * creation until/if the module is used. |
| 17 | + */ |
| 18 | +public class ModuleSpec |
| 19 | +private constructor( |
| 20 | + @get:JvmName("provider") public val provider: Provider<out NativeModule>, |
| 21 | + @get:JvmName("moduleName") public val name: String? = null |
| 22 | +) { |
| 23 | + public fun getProvider(): Provider<out NativeModule> = provider |
| 24 | + |
| 25 | + public fun getName(): String? = name |
| 26 | + |
| 27 | + public companion object { |
| 28 | + private const val TAG: String = "ModuleSpec" |
| 29 | + |
| 30 | + @JvmStatic |
| 31 | + public fun viewManagerSpec(provider: Provider<out NativeModule>): ModuleSpec = |
| 32 | + ModuleSpec(provider) |
| 33 | + |
| 34 | + @JvmStatic |
| 35 | + public fun nativeModuleSpec( |
| 36 | + type: Class<out NativeModule>, |
| 37 | + provider: Provider<out NativeModule> |
| 38 | + ): ModuleSpec { |
| 39 | + val annotation: ReactModule? = type.getAnnotation(ReactModule::class.java) |
| 40 | + |
| 41 | + return if (annotation == null) { |
| 42 | + FLog.w( |
| 43 | + TAG, |
| 44 | + "Could not find @ReactModule annotation on ${type.name}. " + |
| 45 | + "Creating the module eagerly to get the name. Consider adding the annotation.") |
| 46 | + val nativeModule: NativeModule = provider.get() |
| 47 | + ModuleSpec(provider, nativeModule.name) |
| 48 | + } else { |
| 49 | + ModuleSpec(provider, annotation.name) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @JvmStatic |
| 54 | + public fun nativeModuleSpec( |
| 55 | + className: String, |
| 56 | + provider: Provider<out NativeModule> |
| 57 | + ): ModuleSpec = ModuleSpec(provider, className) |
| 58 | + } |
| 59 | +} |
0 commit comments