mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-11 13:11:01 +00:00
Use AC RunWhenReadyQueue (#12800)
This commit is contained in:
parent
d9357f1e32
commit
c2d940cf06
@ -52,6 +52,7 @@ import mozilla.components.support.ktx.android.content.share
|
|||||||
import mozilla.components.support.ktx.kotlin.isUrl
|
import mozilla.components.support.ktx.kotlin.isUrl
|
||||||
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
|
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
|
||||||
import mozilla.components.support.locale.LocaleAwareAppCompatActivity
|
import mozilla.components.support.locale.LocaleAwareAppCompatActivity
|
||||||
|
import mozilla.components.support.utils.RunWhenReadyQueue
|
||||||
import mozilla.components.support.utils.SafeIntent
|
import mozilla.components.support.utils.SafeIntent
|
||||||
import mozilla.components.support.utils.toSafeIntent
|
import mozilla.components.support.utils.toSafeIntent
|
||||||
import mozilla.components.support.webextensions.WebExtensionPopupFeature
|
import mozilla.components.support.webextensions.WebExtensionPopupFeature
|
||||||
@ -97,7 +98,6 @@ import org.mozilla.fenix.theme.DefaultThemeManager
|
|||||||
import org.mozilla.fenix.theme.ThemeManager
|
import org.mozilla.fenix.theme.ThemeManager
|
||||||
import org.mozilla.fenix.trackingprotectionexceptions.TrackingProtectionExceptionsFragmentDirections
|
import org.mozilla.fenix.trackingprotectionexceptions.TrackingProtectionExceptionsFragmentDirections
|
||||||
import org.mozilla.fenix.utils.BrowsersCache
|
import org.mozilla.fenix.utils.BrowsersCache
|
||||||
import org.mozilla.fenix.utils.RunWhenReadyQueue
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main activity of the application. The application is primarily a single Activity (this one)
|
* The main activity of the application. The application is primarily a single Activity (this one)
|
||||||
|
@ -30,6 +30,7 @@ import mozilla.components.service.fxa.manager.SCOPE_SYNC
|
|||||||
import mozilla.components.service.fxa.manager.SyncEnginesStorage
|
import mozilla.components.service.fxa.manager.SyncEnginesStorage
|
||||||
import mozilla.components.service.fxa.sync.GlobalSyncableStoreProvider
|
import mozilla.components.service.fxa.sync.GlobalSyncableStoreProvider
|
||||||
import mozilla.components.service.sync.logins.SyncableLoginsStorage
|
import mozilla.components.service.sync.logins.SyncableLoginsStorage
|
||||||
|
import mozilla.components.support.utils.RunWhenReadyQueue
|
||||||
import org.mozilla.fenix.Config
|
import org.mozilla.fenix.Config
|
||||||
import org.mozilla.fenix.FeatureFlags
|
import org.mozilla.fenix.FeatureFlags
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
@ -39,7 +40,6 @@ import org.mozilla.fenix.ext.components
|
|||||||
import org.mozilla.fenix.ext.settings
|
import org.mozilla.fenix.ext.settings
|
||||||
import org.mozilla.fenix.sync.SyncedTabsIntegration
|
import org.mozilla.fenix.sync.SyncedTabsIntegration
|
||||||
import org.mozilla.fenix.utils.Mockable
|
import org.mozilla.fenix.utils.Mockable
|
||||||
import org.mozilla.fenix.utils.RunWhenReadyQueue
|
|
||||||
import org.mozilla.fenix.utils.Settings
|
import org.mozilla.fenix.utils.Settings
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
package org.mozilla.fenix.components
|
package org.mozilla.fenix.components
|
||||||
|
|
||||||
import org.mozilla.fenix.utils.RunWhenReadyQueue
|
import mozilla.components.support.utils.RunWhenReadyQueue
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Component group for all functionality related to performance.
|
* Component group for all functionality related to performance.
|
||||||
|
@ -7,11 +7,11 @@ package org.mozilla.fenix.session
|
|||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import mozilla.components.support.utils.RunWhenReadyQueue
|
||||||
import org.mozilla.fenix.HomeActivity
|
import org.mozilla.fenix.HomeActivity
|
||||||
import org.mozilla.fenix.IntentReceiverActivity
|
import org.mozilla.fenix.IntentReceiverActivity
|
||||||
import org.mozilla.fenix.browser.BrowserPerformanceTestActivity
|
import org.mozilla.fenix.browser.BrowserPerformanceTestActivity
|
||||||
import org.mozilla.fenix.settings.account.AuthIntentReceiverActivity
|
import org.mozilla.fenix.settings.account.AuthIntentReceiverActivity
|
||||||
import org.mozilla.fenix.utils.RunWhenReadyQueue
|
|
||||||
import org.mozilla.fenix.widget.VoiceSearchActivity
|
import org.mozilla.fenix.widget.VoiceSearchActivity
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,57 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
package org.mozilla.fenix.utils
|
|
||||||
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A queue that acts as a gate, either executing tasks right away if the queue is marked as "ready",
|
|
||||||
* i.e. gate is open, or queues them to be executed whenever the queue is marked as ready in the
|
|
||||||
* future, i.e. gate becomes open.
|
|
||||||
*/
|
|
||||||
class RunWhenReadyQueue {
|
|
||||||
private val tasks = CopyOnWriteArrayList<() -> Unit>()
|
|
||||||
private val isReady = AtomicBoolean(false)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Was this queue ever marked as 'ready' via a call to [ready]?
|
|
||||||
*
|
|
||||||
* @return Boolean value indicating if this queue is 'ready'.
|
|
||||||
*/
|
|
||||||
fun isReady(): Boolean = isReady.get()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs the [task] if this queue is marked as ready, or queues it for later execution.
|
|
||||||
* Task will be executed on the main thread.
|
|
||||||
*
|
|
||||||
* @param task: The task to run now if queue is ready or queue for later execution.
|
|
||||||
*/
|
|
||||||
fun runIfReadyOrQueue(task: () -> Unit) {
|
|
||||||
if (isReady.get()) {
|
|
||||||
CoroutineScope(Dispatchers.Main).launch { task.invoke() }
|
|
||||||
} else {
|
|
||||||
tasks.add(task)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mark queue as ready. Pending tasks will execute, and all tasks passed to [runIfReadyOrQueue]
|
|
||||||
* after this point will be executed immediately.
|
|
||||||
*/
|
|
||||||
fun ready() {
|
|
||||||
// Make sure that calls to `ready` are idempotent.
|
|
||||||
if (!isReady.compareAndSet(false, true)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
|
||||||
tasks.forEach { it.invoke() }.also { tasks.clear() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user