Skip to content

Commit

Permalink
Fixes after pr
Browse files Browse the repository at this point in the history
  • Loading branch information
gosha212 committed Dec 22, 2024
1 parent 93c73c2 commit 2820900
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ object ReactNativeExtension {
Log.i(LOG_TAG, "Reloading React Native")

(applicationContext as ReactApplication).let {
val networkSyncEnabled = rnIdlingResources?.networkSyncEnabled ?: true
clearIdlingResources()

val previousReactContext = getCurrentReactContextSafe(it)

reloadReactNativeInBackground(it)
val reactContext = awaitNewReactNativeContext(it, previousReactContext)

enableOrDisableSynchronization(reactContext, networkSyncEnabled)
enableOrDisableSynchronization(reactContext)
}
}

Expand Down Expand Up @@ -94,11 +93,6 @@ object ReactNativeExtension {
return null
}

@JvmStatic
fun setNetworkSynchronization(enable: Boolean) {
rnIdlingResources?.setNetworkSynchronization(enable)
}

@JvmStatic
fun toggleNetworkSynchronization(enable: Boolean) {
rnIdlingResources?.let {
Expand Down Expand Up @@ -130,11 +124,11 @@ object ReactNativeExtension {
return rnLoadingMonitor.getNewContext()!!
}

private fun enableOrDisableSynchronization(reactContext: ReactContext, networkSyncEnabled: Boolean = true) {
private fun enableOrDisableSynchronization(reactContext: ReactContext) {
if (shouldDisableSynchronization()) {
clearAllSynchronization()
} else {
setupIdlingResources(reactContext, networkSyncEnabled)
setupIdlingResources(reactContext)
}
}

Expand All @@ -143,10 +137,10 @@ object ReactNativeExtension {
return launchArgs.hasEnableSynchronization() && launchArgs.enableSynchronization.equals("0")
}

private fun setupIdlingResources(reactContext: ReactContext, networkSyncEnabled: Boolean = true) {
private fun setupIdlingResources(reactContext: ReactContext) {
val launchArgs = LaunchArgs()

rnIdlingResources = ReactNativeIdlingResources(reactContext, launchArgs, networkSyncEnabled).apply {
rnIdlingResources = ReactNativeIdlingResources(reactContext, launchArgs).apply {
registerAll()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.joor.Reflect
class ReactNativeIdlingResources(
private val reactContext: ReactContext,
private var launchArgs: LaunchArgs,
internal var networkSyncEnabled: Boolean = true,
private val idlingResourcesFactory: DetoxIdlingResourceFactory = DetoxIdlingResourceFactory(reactContext)
) {
companion object {
Expand Down Expand Up @@ -47,26 +46,9 @@ class ReactNativeIdlingResources(
unregisterIdlingResources()
}

fun setNetworkSynchronization(enable: Boolean) {
runBlocking {
if (networkSyncEnabled == enable) {
return@runBlocking
}

if (enable) {
setupIdlingResource(IdlingResourcesName.Network)
} else {
removeIdlingResource(IdlingResourcesName.Network)
}
networkSyncEnabled = enable
}
}

fun pauseNetworkSynchronization() = pauseIdlingResource(IdlingResourcesName.Network)
fun resumeNetworkSynchronization() {
if (networkSyncEnabled) {
resumeIdlingResource(IdlingResourcesName.Network)
}
resumeIdlingResource(IdlingResourcesName.Network)
}

fun pauseRNTimersIdlingResource() = pauseIdlingResource(IdlingResourcesName.Timers)
Expand Down Expand Up @@ -109,14 +91,10 @@ class ReactNativeIdlingResources(
}

private suspend fun setupIdlingResources() {
setupIdlingResource(IdlingResourcesName.RNBridge)
setupIdlingResource(IdlingResourcesName.Timers)
setupIdlingResource(IdlingResourcesName.UIModule)
setupIdlingResource(IdlingResourcesName.Animations)
if (networkSyncEnabled) {
setupIdlingResource(IdlingResourcesName.Network)
idlingResources.putAll(idlingResourcesFactory.create())
idlingResources.forEach { (_, idlingResource) ->
IdlingRegistry.getInstance().register(idlingResource)
}
setupIdlingResource(IdlingResourcesName.AsyncStorage)
}

private fun syncIdlingResources() {
Expand Down Expand Up @@ -150,15 +128,6 @@ class ReactNativeIdlingResources(
idlingResource?.resume()
}

private suspend fun setupIdlingResource(idlingResourcesName: IdlingResourcesName) {
val idlingResource = idlingResourcesFactory.create(idlingResourcesName)

idlingResource?.let {
IdlingRegistry.getInstance().register(it)
idlingResources[idlingResourcesName] = it
}
}

private fun removeIdlingResource(idlingResourcesName: IdlingResourcesName) {
val idlingResource = idlingResources[idlingResourcesName]
idlingResource?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class DetoxIdlingResourceFactory(private val reactContext: ReactContext) {
suspend fun create(name: IdlingResourcesName): DetoxIdlingResource? = withContext(Dispatchers.Main) {
return@withContext when (name) {
IdlingResourcesName.Timers -> TimersIdlingResource(reactContext)
IdlingResourcesName.AsyncStorage -> AsyncStorageIdlingResource.createIfNeeded(reactContext)
IdlingResourcesName.RNBridge -> BridgeIdlingResource(reactContext)
IdlingResourcesName.UIModule -> UIModuleIdlingResource(reactContext)
IdlingResourcesName.Animations -> AnimatedModuleIdlingResource(reactContext)
IdlingResourcesName.Network -> NetworkIdlingResource(reactContext)
suspend fun create(): Map<IdlingResourcesName, DetoxIdlingResource> = withContext(Dispatchers.Main) {
val result = mutableMapOf(
IdlingResourcesName.Timers to TimersIdlingResource(reactContext),
IdlingResourcesName.RNBridge to BridgeIdlingResource(reactContext),
IdlingResourcesName.UIModule to UIModuleIdlingResource(reactContext),
IdlingResourcesName.Animations to AnimatedModuleIdlingResource(reactContext),
IdlingResourcesName.Network to NetworkIdlingResource(reactContext)
)

val asyncStorageIdlingResource = AsyncStorageIdlingResource.createIfNeeded(reactContext)
if (asyncStorageIdlingResource != null) {
result[IdlingResourcesName.AsyncStorage] = asyncStorageIdlingResource
}

return@withContext result
}
}

0 comments on commit 2820900

Please sign in to comment.