Remove job adds from database transactions.

This commit is contained in:
Greyson Parrelli
2020-12-03 17:33:04 -05:00
parent 31960b53a0
commit 97047bccde
6 changed files with 63 additions and 43 deletions

View File

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

View File

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