Search in sources :

Example 1 with SerialMonoLifoExecutor

use of org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor in project Signal-Android by WhisperSystems.

the class LiveDataUtil method mapAsync.

/**
 * Runs the {@param backgroundFunction} on the supplied {@param executor}.
 * <p>
 * Regardless of the executor supplied, the background function is run serially.
 * <p>
 * The background function may not run for all {@param source} updates. Later updates taking priority.
 */
public static <A, B> LiveData<B> mapAsync(@NonNull Executor executor, @NonNull LiveData<A> source, @NonNull Function<A, B> backgroundFunction) {
    MediatorLiveData<B> outputLiveData = new MediatorLiveData<>();
    Executor liveDataExecutor = new SerialMonoLifoExecutor(executor);
    outputLiveData.addSource(source, currentValue -> {
        liveDataExecutor.execute(() -> {
            outputLiveData.postValue(backgroundFunction.apply(currentValue));
        });
    });
    return outputLiveData;
}
Also used : SerialMonoLifoExecutor(org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor) Executor(java.util.concurrent.Executor) SerialMonoLifoExecutor(org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor) MediatorLiveData(androidx.lifecycle.MediatorLiveData)

Example 2 with SerialMonoLifoExecutor

use of org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor in project Signal-Android by signalapp.

the class LiveDataUtil method mapAsync.

/**
 * Runs the {@param backgroundFunction} on the supplied {@param executor}.
 * <p>
 * Regardless of the executor supplied, the background function is run serially.
 * <p>
 * The background function may not run for all {@param source} updates. Later updates taking priority.
 */
public static <A, B> LiveData<B> mapAsync(@NonNull Executor executor, @NonNull LiveData<A> source, @NonNull Function<A, B> backgroundFunction) {
    MediatorLiveData<B> outputLiveData = new MediatorLiveData<>();
    Executor liveDataExecutor = new SerialMonoLifoExecutor(executor);
    outputLiveData.addSource(source, currentValue -> {
        liveDataExecutor.execute(() -> {
            outputLiveData.postValue(backgroundFunction.apply(currentValue));
        });
    });
    return outputLiveData;
}
Also used : SerialMonoLifoExecutor(org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor) Executor(java.util.concurrent.Executor) SerialMonoLifoExecutor(org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor) MediatorLiveData(androidx.lifecycle.MediatorLiveData)

Aggregations

MediatorLiveData (androidx.lifecycle.MediatorLiveData)2 Executor (java.util.concurrent.Executor)2 SerialMonoLifoExecutor (org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor)2