-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce kotlin and reimplement RN-timers idling resource
- Loading branch information
Showing
8 changed files
with
392 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 0 additions & 198 deletions
198
...x/android/detox/src/main/java/com/wix/detox/espresso/ReactNativeTimersIdlingResource.java
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
detox/android/detox/src/main/java/com/wix/detox/espresso/ReactNativeTimersIdlingResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.wix.detox.espresso | ||
|
||
import android.support.test.espresso.IdlingResource | ||
import android.view.Choreographer | ||
import com.facebook.react.bridge.ReactContext | ||
import com.facebook.react.modules.core.Timing | ||
import org.joor.Reflect | ||
import java.util.* | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
const val BUSY_WINDOW_THRESHOLD = 1500 | ||
|
||
class TimerReflected(timer: Any) { | ||
private var reflected = Reflect.on(timer) | ||
|
||
val isRepeating: Boolean | ||
get() = reflected.field("mRepeat").get() | ||
|
||
val interval: Int | ||
get() = reflected.field("mInterval").get() | ||
|
||
val targetTime: Long | ||
get() = reflected.field("mTargetTime").get() | ||
} | ||
|
||
class TimingModuleReflected(reactContext: ReactContext) { | ||
private var nativeModule = reactContext.getNativeModule(Timing::class.java) | ||
|
||
val timersQueue: PriorityQueue<Any> | ||
get() = Reflect.on(nativeModule).field("mTimers").get() | ||
|
||
val timersLock: Object | ||
get() = Reflect.on(nativeModule).field("mTimerGuard").get() | ||
|
||
operator fun component1() = timersQueue | ||
operator fun component2() = timersLock | ||
} | ||
|
||
class ReactNativeTimersIdlingResource @JvmOverloads constructor( | ||
private val reactContext: ReactContext, | ||
private val getChoreographer: () -> Choreographer = { Choreographer.getInstance() } | ||
) : IdlingResource, Choreographer.FrameCallback { | ||
|
||
private var callback: IdlingResource.ResourceCallback? = null | ||
private var paused = AtomicBoolean(false) | ||
|
||
override fun getName(): String = this.javaClass.name | ||
|
||
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) { | ||
this.callback = callback | ||
getChoreographer().postFrameCallback(this) | ||
} | ||
|
||
override fun isIdleNow(): Boolean { | ||
if (paused.get()) { | ||
return true | ||
} | ||
|
||
return checkIdle().apply { | ||
val result = this | ||
if (result) { | ||
callback?.onTransitionToIdle() | ||
} else { | ||
getChoreographer().postFrameCallback(this@ReactNativeTimersIdlingResource) | ||
} | ||
} | ||
} | ||
|
||
override fun doFrame(frameTimeNanos: Long) { | ||
callback?.let { | ||
isIdleNow | ||
} | ||
} | ||
|
||
public fun pause() { | ||
paused.set(true) | ||
callback?.onTransitionToIdle() | ||
} | ||
|
||
public fun resume() { | ||
paused.set(false) | ||
} | ||
|
||
private fun checkIdle(): Boolean { | ||
val now = System.nanoTime() / 1000000L | ||
val (timersQueue, timersLock) = TimingModuleReflected(reactContext) | ||
|
||
synchronized(timersLock) { | ||
val nextTimer = timersQueue.peek() | ||
nextTimer?.let { | ||
return !isTimerInBusyWindow(it, now) && !hasBusyTimers(timersQueue, now) | ||
} | ||
return true | ||
} | ||
} | ||
|
||
private fun isTimerInBusyWindow(timer: Any, now: Long): Boolean { | ||
val timerReflected = TimerReflected(timer) | ||
return when { | ||
timerReflected.isRepeating -> false | ||
timerReflected.targetTime < now -> false | ||
timerReflected.interval > BUSY_WINDOW_THRESHOLD -> false | ||
else -> true | ||
} | ||
} | ||
|
||
private fun hasBusyTimers(timersQueue: PriorityQueue<Any>, now: Long): Boolean { | ||
timersQueue.forEach { | ||
if (isTimerInBusyWindow(it, now)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} |
Oops, something went wrong.