mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-23 18:30:20 +01:00
Remove job adds from database transactions.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package org.thoughtcrime.securesms.util.concurrent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* Allows you to specify a filter upon which a job will be executed on the provided executor. If
|
||||
* it doesn't match the filter, it will be run on the calling thread.
|
||||
*/
|
||||
public final class FilteredExecutor implements Executor {
|
||||
|
||||
private final Executor backgroundExecutor;
|
||||
private final Filter filter;
|
||||
|
||||
public FilteredExecutor(@NonNull Executor backgroundExecutor, @NonNull Filter filter) {
|
||||
this.backgroundExecutor = backgroundExecutor;
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(@NonNull Runnable runnable) {
|
||||
if (filter.shouldRunOnExecutor()) {
|
||||
backgroundExecutor.execute(runnable);
|
||||
} else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
public interface Filter {
|
||||
boolean shouldRunOnExecutor();
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package org.thoughtcrime.securesms.util.concurrent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* If submitted on the main thread, the task will be executed on the provided background executor.
|
||||
* Otherwise, the task will be run on the calling thread.
|
||||
*/
|
||||
public final class NonMainThreadExecutor implements Executor {
|
||||
|
||||
private final Executor backgroundExecutor;
|
||||
|
||||
public NonMainThreadExecutor(@NonNull Executor backgroundExecutor) {
|
||||
this.backgroundExecutor = backgroundExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(@NonNull Runnable runnable) {
|
||||
if (Util.isMainThread()) {
|
||||
backgroundExecutor.execute(runnable);
|
||||
} else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user