-
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 ccfc9d8
Showing
7 changed files
with
170 additions
and
79 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
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
/* | ||
* 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), | ||
uiThreadExecutor: UiThreadExecutor | ||
) { | ||
private val frameCallback: FpsFrameCallback = FpsFrameCallback( | ||
reactFrameRateCallback, | ||
uiThreadExecutor | ||
) | ||
|
||
fun start() { | ||
frameCallback.reset() | ||
frameCallback.start() | ||
} | ||
|
||
fun stop() { | ||
frameCallback.stop() | ||
} | ||
} | ||
|
||
internal class FpsFrameCallback( | ||
private val reactFrameRateCallback: ((Double) -> Unit), | ||
private val uiThreadExecutor: UiThreadExecutor | ||
) : ChoreographerCompat.FrameCallback() { | ||
|
||
private var choreographer: ChoreographerCompat? = null | ||
private var lastFrameTime = -1L | ||
|
||
override fun doFrame(time: Long) { | ||
if (lastFrameTime != -1L) { | ||
reactFrameRateCallback((time - lastFrameTime).toDouble()) | ||
} | ||
lastFrameTime = time | ||
choreographer?.postFrameCallback(this) | ||
} | ||
|
||
fun start() { | ||
uiThreadExecutor.runOnUiThread { | ||
choreographer = ChoreographerCompat.getInstance() | ||
choreographer?.postFrameCallback(this@FpsFrameCallback) | ||
} | ||
} | ||
|
||
fun stop() { | ||
uiThreadExecutor.runOnUiThread { | ||
choreographer = ChoreographerCompat.getInstance() | ||
choreographer?.removeFrameCallback(this@FpsFrameCallback) | ||
} | ||
} | ||
|
||
fun reset() { | ||
lastFrameTime = -1L | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
/* | ||
* 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 | ||
|
||
/** | ||
* Simple UI Thread Executor. By default it is based on [UiThreadUtil.runOnUiThread]. | ||
*/ | ||
interface UiThreadExecutor { | ||
/** | ||
* Runs the given runnable on the UI Thread. | ||
*/ | ||
fun runOnUiThread(runnable: Runnable) | ||
} | ||
|
||
internal class ReactUiThreadExecutor : UiThreadExecutor { | ||
override fun runOnUiThread(runnable: Runnable) { | ||
UiThreadUtil.runOnUiThread(runnable) | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
packages/core/android/src/test/kotlin/com/datadog/tools/unit/TestUiThreadExecutor.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,16 @@ | ||
/* | ||
* 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.tools.unit | ||
|
||
import com.datadog.reactnative.UiThreadExecutor | ||
|
||
internal class TestUiThreadExecutor : UiThreadExecutor { | ||
override fun runOnUiThread(runnable: Runnable) { | ||
// Run immediately in the same thread for tests | ||
runnable.run() | ||
} | ||
} |