-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathStateSerialRunner.java
More file actions
65 lines (59 loc) · 2.25 KB
/
Copy pathStateSerialRunner.java
File metadata and controls
65 lines (59 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cn.reactnative.modules.update;
import android.util.Log;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.Promise;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
* Runs state-persistence operations (switchVersion / markSuccess / setUuid /
* setLocalHashInfo) on a dedicated single background thread.
*
* These operations only read/modify SharedPreferences via a synchronous
* commit(); they were previously dispatched to the UI thread purely to
* serialize them. markSuccess in particular runs on every cold start, so doing
* its blocking disk write on the main thread caused jank/ANR on low-end
* devices. A single-thread executor preserves the same serialization guarantee
* while keeping the disk I/O off the UI thread.
*
* Note: reload/restart operations must still run on the UI thread and therefore
* keep using {@link UiThreadRunner}.
*/
final class StateSerialRunner {
interface Operation {
void run() throws Throwable;
}
// Single worker thread -> operations stay serialized in submission order,
// matching the previous UI-thread behavior. The thread is named so it is
// identifiable in thread dumps / ANR traces when diagnosing persistence.
private static final Executor EXECUTOR = Executors.newSingleThreadExecutor(
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "pushy-state-serial");
}
});
private StateSerialRunner() {
}
static void run(
@Nullable final Promise promise,
final String errorCode,
final String operationName,
final Operation operation
) {
EXECUTOR.execute(new Runnable() {
@Override
public void run() {
try {
operation.run();
} catch (Throwable error) {
if (promise != null) {
promise.reject(errorCode, operationName + " failed", error);
} else {
Log.e(UpdateContext.TAG, operationName + " failed", error);
}
}
}
});
}
}