Skip to content

Commit

Permalink
Cache datastore values using StateFlows to improve performance and ad…
Browse files Browse the repository at this point in the history
…dress ANRs
  • Loading branch information
CrisBarreiro committed Oct 22, 2024
1 parent 1cec8c1 commit 89679f7
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.duckduckgo.privacy.config.api.TrackingParameters
import com.duckduckgo.subscriptions.api.Subscriptions
import java.net.URISyntaxException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import timber.log.Timber

Expand Down Expand Up @@ -92,9 +91,7 @@ class SpecialUrlDetectorImpl(

val uri = uriString.toUri()

val willNavigateToDuckPlayerDeferred = scope.async(dispatcherProvider.io()) { duckPlayer.willNavigateToDuckPlayer(uri) }

val willNavigateToDuckPlayer = runBlocking(dispatcherProvider.io()) { willNavigateToDuckPlayerDeferred.await() }
val willNavigateToDuckPlayer = runBlocking(dispatcherProvider.io()) { duckPlayer.willNavigateToDuckPlayer(uri) }

if (willNavigateToDuckPlayer) {
return UrlType.ShouldLaunchDuckPlayerLink(url = uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class DuckPlayerJSHelper @Inject constructor(
}
}

private suspend fun getInitialSetup(featureName: String, method: String, id: String): JsCallbackData {
private fun getInitialSetup(featureName: String, method: String, id: String): JsCallbackData {
val userValues = duckPlayer.getUserPreferences()
val privatePlayerMode = if (duckPlayer.getDuckPlayerState() == ENABLED) userValues.privatePlayerMode else PrivatePlayerMode.Disabled

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface DuckPlayer {
*
* @return The current state of the DuckPlayer as a DuckPlayerState enum.
*/
suspend fun getDuckPlayerState(): DuckPlayerState
fun getDuckPlayerState(): DuckPlayerState

/**
* Sends a pixel with the given name and data.
Expand All @@ -65,7 +65,7 @@ interface DuckPlayer {
*
* @return The user values.
*/
suspend fun getUserPreferences(): UserPreferences
fun getUserPreferences(): UserPreferences

/**
* Checks if the DuckPlayer overlay should be hidden after navigating back from Duck Player
Expand Down Expand Up @@ -105,7 +105,7 @@ interface DuckPlayer {
* @param uri The YouTube no-cookie URI.
* @return The DuckPlayer URI.
*/
suspend fun createDuckPlayerUriFromYoutubeNoCookie(uri: Uri): String?
fun createDuckPlayerUriFromYoutubeNoCookie(uri: Uri): String?

/**
* Checks if a string is a DuckPlayer URI.
Expand All @@ -120,7 +120,7 @@ interface DuckPlayer {
* @param uri The DuckPlayer URI.
* @return The YouTube URI.
*/
suspend fun createYoutubeWatchUrlFromDuckPlayer(uri: Uri): String?
fun createYoutubeWatchUrlFromDuckPlayer(uri: Uri): String?

/**
* Checks if a URI is a simulated YouTube no-cookie URI.
Expand All @@ -136,7 +136,7 @@ interface DuckPlayer {
* @param uri The URI to check.
* @return True if the URI is a YouTube no-cookie URI, false otherwise.
*/
suspend fun isYoutubeWatchUrl(uri: Uri): Boolean
fun isYoutubeWatchUrl(uri: Uri): Boolean

/**
* Checks if a URI is a YouTube URL.
Expand Down Expand Up @@ -184,7 +184,7 @@ interface DuckPlayer {
destinationUrl: Uri,
): Boolean

suspend fun shouldOpenDuckPlayerInNewTab(): OpenDuckPlayerInNewTab
fun shouldOpenDuckPlayerInNewTab(): OpenDuckPlayerInNewTab

fun observeShouldOpenInNewTab(): Flow<OpenDuckPlayerInNewTab>

Expand Down
Loading

0 comments on commit 89679f7

Please sign in to comment.