|
| 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 |
| 9 | + |
| 10 | +import com.facebook.react.bridge.ModuleHolder |
| 11 | +import com.facebook.react.bridge.NativeModuleRegistry |
| 12 | +import com.facebook.react.bridge.ReactApplicationContext |
| 13 | + |
| 14 | +/** Helper class to build NativeModuleRegistry. */ |
| 15 | +public class NativeModuleRegistryBuilder( |
| 16 | + private val reactApplicationContext: ReactApplicationContext, |
| 17 | +) { |
| 18 | + |
| 19 | + private val modules = HashMap<String, ModuleHolder>() |
| 20 | + |
| 21 | + @Deprecated( |
| 22 | + "ReactInstanceManager is not used", |
| 23 | + ReplaceWith("NativeModuleRegistryBuilder(reactApplicationContext)")) |
| 24 | + public constructor( |
| 25 | + reactApplicationContext: ReactApplicationContext, |
| 26 | + @Suppress("UNUSED_PARAMETER") reactInstanceManager: ReactInstanceManager |
| 27 | + ) : this(reactApplicationContext) {} |
| 28 | + |
| 29 | + public fun processPackage(reactPackage: ReactPackage) { |
| 30 | + // We use an iterable instead of an iterator here to ensure thread safety, and that this list |
| 31 | + // cannot be modified |
| 32 | + val moduleHolders = |
| 33 | + @Suppress("DEPRECATION") |
| 34 | + if (reactPackage is LazyReactPackage) { |
| 35 | + reactPackage.getNativeModuleIterator(reactApplicationContext) |
| 36 | + } else if (reactPackage is BaseReactPackage) { |
| 37 | + reactPackage.getNativeModuleIterator(reactApplicationContext) |
| 38 | + } else { |
| 39 | + ReactPackageHelper.getNativeModuleIterator(reactPackage, reactApplicationContext) |
| 40 | + } |
| 41 | + for (moduleHolder in moduleHolders) { |
| 42 | + val name = moduleHolder.name |
| 43 | + val existingNativeModule = modules[name] |
| 44 | + if (existingNativeModule != null) { |
| 45 | + check(moduleHolder.canOverrideExistingModule) { |
| 46 | + """ |
| 47 | +Native module $name tried to override ${existingNativeModule.className}. |
| 48 | +
|
| 49 | +Check the getPackages() method in MainApplication.java, it might be that module is being created twice. |
| 50 | +If this was your intention, set canOverrideExistingModule=true. This error may also be present if the |
| 51 | +package is present only once in getPackages() but is also automatically added later during build time |
| 52 | +by autolinking. Try removing the existing entry and rebuild. |
| 53 | +""" |
| 54 | + } |
| 55 | + } |
| 56 | + modules[name] = moduleHolder |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public fun build(): NativeModuleRegistry = NativeModuleRegistry(reactApplicationContext, modules) |
| 61 | +} |
0 commit comments