Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
d4vidi committed Dec 18, 2023
1 parent 2f93b9e commit 426af8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Activity
import android.util.Log
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.ReactApplicationContext
import com.reactnativenavigation.utils.sleepSafe
import com.reactnativenavigation.utils.busyRetry
import java.lang.ref.WeakReference

private const val ACTIVITY_WAIT_INTERVAL = 100L
Expand Down Expand Up @@ -43,13 +43,13 @@ class NullRNActivityWorkaround(reactAppContext: ReactApplicationContext) {
}

private fun waitForActivity() {
val activityReady = { (reactAppContext.get()?.hasCurrentActivity() ?: false) }
val isActivityReady = { (reactAppContext.get()?.hasCurrentActivity() ?: false) }

var tries = 0
while (tries < ACTIVITY_WAIT_TRIES && hasHost && !activityReady()) {
Log.d(LOG_TAG, "Busy-waiting for activity! Try: #$tries...")
sleepSafe(ACTIVITY_WAIT_INTERVAL)
tries++
busyRetry(ACTIVITY_WAIT_TRIES, ACTIVITY_WAIT_INTERVAL) { tries ->
if (!isActivityReady() && hasHost) {
Log.d(LOG_TAG, "Busy-waiting for activity! Try: #$tries...")
true
} else false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.reactnativenavigation.utils

fun busyRetry(times: Int, interval: Long, callback: (tries: Int) -> Boolean) {
var tries = 0
while (tries < times && !callback(tries)) {
sleepSafe(interval)
tries++
}
}

0 comments on commit 426af8a

Please sign in to comment.