-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b325de
commit b6888ec
Showing
6 changed files
with
174 additions
and
77 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
59 changes: 59 additions & 0 deletions
59
packages/core/android/src/main/kotlin/com/datadog/reactnative/FrameRateProvider.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,59 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.reactnative | ||
|
||
import com.facebook.react.modules.core.ChoreographerCompat | ||
|
||
internal class FrameRateProvider( | ||
reactFrameRateCallback: ((Double) -> Unit)? | ||
) { | ||
private val frameCallback: FpsFrameCallback = FpsFrameCallback(reactFrameRateCallback) | ||
|
||
fun start() { | ||
frameCallback.reset() | ||
frameCallback.start() | ||
} | ||
|
||
fun stop() { | ||
frameCallback.stop() | ||
} | ||
} | ||
|
||
internal class FpsFrameCallback( | ||
private val reactFrameRateCallback: ((Double) -> Unit)? | ||
) : | ||
ChoreographerCompat.FrameCallback() { | ||
private var mChoreographer: ChoreographerCompat? = null | ||
|
||
private var mLastFrameTime = -1L | ||
|
||
override fun doFrame(time: Long) { | ||
if (mLastFrameTime != -1L) { | ||
reactFrameRateCallback?.let { it((time - mLastFrameTime).toDouble()) } | ||
} | ||
mLastFrameTime = time | ||
mChoreographer?.postFrameCallback(this) | ||
} | ||
|
||
fun start() { | ||
UiThreadExecutor.Provider.uiThreadExecutor.runOnUiThread { | ||
mChoreographer = ChoreographerCompat.getInstance() | ||
mChoreographer?.postFrameCallback(this@FpsFrameCallback) | ||
} | ||
} | ||
|
||
fun stop() { | ||
UiThreadExecutor.Provider.uiThreadExecutor.runOnUiThread { | ||
mChoreographer = ChoreographerCompat.getInstance() | ||
mChoreographer?.removeFrameCallback(this@FpsFrameCallback) | ||
} | ||
} | ||
|
||
fun reset() { | ||
mLastFrameTime = -1L | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/core/android/src/main/kotlin/com/datadog/reactnative/UiThreadExecutor.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,40 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.reactnative | ||
|
||
import com.facebook.react.bridge.UiThreadUtil | ||
|
||
internal interface UiThreadExecutor { | ||
fun runOnUiThread(runnable: Runnable) | ||
|
||
object Provider { | ||
val uiThreadExecutor: UiThreadExecutor by lazy { | ||
if (isRunningInTest()) { | ||
TestUiThreadExecutor() | ||
} else { | ||
RealUiThreadExecutor() | ||
} | ||
} | ||
|
||
private fun isRunningInTest(): Boolean { | ||
return System.getProperty("IS_UNIT_TEST") == "true" | ||
} | ||
} | ||
} | ||
|
||
internal class RealUiThreadExecutor : UiThreadExecutor { | ||
override fun runOnUiThread(runnable: Runnable) { | ||
UiThreadUtil.runOnUiThread(runnable) | ||
} | ||
} | ||
|
||
internal class TestUiThreadExecutor : UiThreadExecutor { | ||
override fun runOnUiThread(runnable: Runnable) { | ||
// Run immediately in the same thread for tests | ||
runnable.run() | ||
} | ||
} |
41 changes: 0 additions & 41 deletions
41
packages/core/android/src/main/kotlin/com/datadog/reactnative/VitalFrameCallback.kt
This file was deleted.
Oops, something went wrong.
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