Do not pass ReactContext to getViewManagersMap (#44379)

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

A common pattern to implement `ViewManagerOnDemandReactPackage` is to use a `getViewManagersMap` helper. If we capture `ReactApplicationContext` there, we will indefinitely retain the the very first ReactApplicationContext, and break/leak across reloads. Instead we should pass the `ReactApplicationContext` whenever we construct the ViewManager.

Changelog: [Internal]

Reviewed By: RSNara

Differential Revision: D56838427

fbshipit-source-id: 76583dd7f5564ed29f0dbfcef33d8d288cbb90e0
This commit is contained in:
Pieter De Baets
2024-05-02 13:25:18 -07:00
committed by Facebook GitHub Bot
parent bbb52c575d
commit 88de74b2dc
@@ -18,35 +18,29 @@ import com.facebook.react.uimanager.ViewManager
@ReactModuleList(nativeModules = arrayOf())
public class PopupMenuPackage() : BaseReactPackage(), ViewManagerOnDemandReactPackage {
private var viewManagersMap: Map<String, ModuleSpec>? = null
private val viewManagersMap: Map<String, ModuleSpec> =
mapOf(
ReactPopupMenuManager.REACT_CLASS to
ModuleSpec.viewManagerSpec({ ReactPopupMenuManager() }),
)
override fun getModule(name: String, context: ReactApplicationContext): NativeModule? {
return null
}
private fun getViewManagersMap(): Map<String, ModuleSpec> {
val viewManagers =
viewManagersMap
?: mapOf(
ReactPopupMenuManager.REACT_CLASS to
ModuleSpec.viewManagerSpec({ ReactPopupMenuManager() }))
viewManagersMap = viewManagers
return viewManagers
}
protected override fun getViewManagers(context: ReactApplicationContext): List<ModuleSpec> {
return ArrayList(getViewManagersMap().values)
return viewManagersMap.values.toList()
}
override fun getViewManagerNames(context: ReactApplicationContext): Collection<String> {
return getViewManagersMap().keys
return viewManagersMap.keys
}
override fun createViewManager(
reactContext: ReactApplicationContext,
viewManagerName: String
): ViewManager<*, *>? {
val spec: ModuleSpec? = getViewManagersMap().get(viewManagerName)
val spec: ModuleSpec? = viewManagersMap.get(viewManagerName)
return if (spec != null) (spec.getProvider().get() as ViewManager<*, *>) else null
}