fix: Re-install Frame Processor JSI Bindings after Reload (#691)

This commit is contained in:
Janic Duplessis 2022-01-02 10:02:17 -05:00 committed by GitHub
parent 4584d33921
commit dcbbae5fc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 33 deletions

View File

@ -6,44 +6,13 @@ import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
import com.mrousavy.camera.frameprocessor.FrameProcessorRuntimeManager
import java.util.concurrent.Executors
@Suppress("unused")
class CameraViewManager(reactContext: ReactApplicationContext) : SimpleViewManager<CameraView>() {
private val frameProcessorThread = Executors.newSingleThreadExecutor()
private var frameProcessorManager: FrameProcessorRuntimeManager? = null
init {
if (frameProcessorManager == null) {
frameProcessorThread.execute {
frameProcessorManager = FrameProcessorRuntimeManager(reactContext, frameProcessorThread)
reactContext.runOnJSQueueThread {
frameProcessorManager!!.installJSIBindings()
}
}
}
}
private fun destroy() {
frameProcessorManager = null
frameProcessorThread.shutdown()
}
override fun onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy()
destroy()
}
override fun invalidate() {
super.invalidate()
destroy()
}
public override fun createViewInstance(context: ThemedReactContext): CameraView {
return CameraView(context, frameProcessorThread)
val cameraViewModule = context.getNativeModule(CameraViewModule::class.java)!!
return CameraView(context, cameraViewModule.frameProcessorThread)
}
override fun onAfterUpdateTransaction(view: CameraView) {

View File

@ -13,13 +13,18 @@ import androidx.camera.extensions.ExtensionsManager
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import com.facebook.react.bridge.*
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.modules.core.PermissionAwareActivity
import com.facebook.react.modules.core.PermissionListener
import com.mrousavy.camera.frameprocessor.FrameProcessorRuntimeManager
import com.mrousavy.camera.parsers.*
import com.mrousavy.camera.utils.*
import kotlinx.coroutines.*
import kotlinx.coroutines.guava.await
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
@ReactModule(name = CameraViewModule.TAG)
@Suppress("unused")
class CameraViewModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
companion object {
@ -35,12 +40,29 @@ class CameraViewModule(reactContext: ReactApplicationContext) : ReactContextBase
}
}
var frameProcessorThread: ExecutorService = Executors.newSingleThreadExecutor()
private val coroutineScope = CoroutineScope(Dispatchers.Default) // TODO: or Dispatchers.Main?
private var frameProcessorManager: FrameProcessorRuntimeManager? = null
private fun cleanup() {
if (coroutineScope.isActive) {
coroutineScope.cancel("CameraViewModule has been destroyed.")
}
frameProcessorManager = null
}
override fun initialize() {
super.initialize()
if (frameProcessorManager == null) {
frameProcessorThread.execute {
frameProcessorManager = FrameProcessorRuntimeManager(reactApplicationContext, frameProcessorThread)
reactApplicationContext.runOnJSQueueThread {
frameProcessorManager!!.installJSIBindings()
}
}
}
}
override fun onCatalystInstanceDestroy() {