1
0
Fork 0

Use AC RunWhenReadyQueue (#12800)

master
Tiger Oakes 2020-07-22 09:48:36 -07:00 committed by GitHub
parent d9357f1e32
commit c2d940cf06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 4 additions and 61 deletions

View File

@ -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.toNormalizedUrl
import mozilla.components.support.locale.LocaleAwareAppCompatActivity
import mozilla.components.support.utils.RunWhenReadyQueue
import mozilla.components.support.utils.SafeIntent
import mozilla.components.support.utils.toSafeIntent
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.trackingprotectionexceptions.TrackingProtectionExceptionsFragmentDirections
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)

View File

@ -30,6 +30,7 @@ import mozilla.components.service.fxa.manager.SCOPE_SYNC
import mozilla.components.service.fxa.manager.SyncEnginesStorage
import mozilla.components.service.fxa.sync.GlobalSyncableStoreProvider
import mozilla.components.service.sync.logins.SyncableLoginsStorage
import mozilla.components.support.utils.RunWhenReadyQueue
import org.mozilla.fenix.Config
import org.mozilla.fenix.FeatureFlags
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.sync.SyncedTabsIntegration
import org.mozilla.fenix.utils.Mockable
import org.mozilla.fenix.utils.RunWhenReadyQueue
import org.mozilla.fenix.utils.Settings
/**

View File

@ -4,7 +4,7 @@
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.

View File

@ -7,11 +7,11 @@ package org.mozilla.fenix.session
import android.app.Activity
import android.app.Application
import android.os.Bundle
import mozilla.components.support.utils.RunWhenReadyQueue
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.IntentReceiverActivity
import org.mozilla.fenix.browser.BrowserPerformanceTestActivity
import org.mozilla.fenix.settings.account.AuthIntentReceiverActivity
import org.mozilla.fenix.utils.RunWhenReadyQueue
import org.mozilla.fenix.widget.VoiceSearchActivity
/**

View File

@ -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() }
}
}
}