Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Android): Prefer dev server if it's running #116

Merged
merged 1 commit into from
Jun 15, 2020
Merged
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 @@ -2,14 +2,21 @@ package com.sample.react

import android.app.Activity
import android.app.Application
import android.content.Context
import com.facebook.react.PackageList
import com.facebook.react.ReactInstanceManager
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.ReactMarker
import com.facebook.react.bridge.ReactMarkerConstants
import com.facebook.react.common.LifecycleState
import com.facebook.react.devsupport.DevInternalSettings
import com.facebook.react.devsupport.DevServerHelper
import com.facebook.react.devsupport.InspectorPackagerConnection.BundleStatus
import com.facebook.react.devsupport.InspectorPackagerConnection.BundleStatusProvider
import com.facebook.soloader.SoLoader
import com.sample.BuildConfig
import java.util.concurrent.CountDownLatch
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -39,12 +46,16 @@ class TestAppReactNativeHost @Inject constructor(
application: Application,
private val reactBundleNameProvider: ReactBundleNameProvider
) : ReactNativeHost(application) {
var source: BundleSource = reactBundleNameProvider.bundleName
?.let { BundleSource.Disk } ?: BundleSource.Server
var source: BundleSource =
if (reactBundleNameProvider.bundleName == null || isPackagerRunning(application)) {
BundleSource.Server
} else {
BundleSource.Disk
}

fun reload(activity: Activity, newSource: BundleSource) {
assert(hasInstance()) {
"init() must be called the first time ReactInstanceManager is created"
if (BuildConfig.DEBUG && !hasInstance()) {
error("init() must be called the first time ReactInstanceManager is created")
}

val action = source.moveTo(newSource)
Expand All @@ -66,7 +77,9 @@ class TestAppReactNativeHost @Inject constructor(
}

fun init() {
assert(!hasInstance()) { "init() can only be called once on startup" }
if (BuildConfig.DEBUG && hasInstance()) {
error("init() can only be called once on startup")
}

SoLoader.init(application, false)
reactInstanceManager.createReactContextInBackground()
Expand Down Expand Up @@ -98,4 +111,19 @@ class TestAppReactNativeHost @Inject constructor(
override fun getUseDeveloperSupport() = source == BundleSource.Server

override fun getPackages(): List<ReactPackage> = PackageList(application).packages

private fun isPackagerRunning(context: Context): Boolean {
val latch = CountDownLatch(1)
var packagerIsRunning = false
DevServerHelper(
DevInternalSettings.withoutNativeDeltaClient(context) {},
context.packageName,
BundleStatusProvider { BundleStatus() }
).isPackagerRunning {
packagerIsRunning = it
latch.countDown()
}
latch.await()
return packagerIsRunning
}
}