Skip to content

Commit 38c7c15

Browse files
author
Krzysztof Borowy
committed
please detox
1 parent 993a262 commit 38c7c15

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,6 @@ public final class AsyncStorageModule
4646
private ReactDatabaseSupplier mReactDatabaseSupplier;
4747
private boolean mShuttingDown = false;
4848

49-
// Adapted from https://android.googlesource.com/platform/frameworks/base.git/+/1488a3a19d4681a41fb45570c15e14d99db1cb66/core/java/android/os/AsyncTask.java#237
50-
private class SerialExecutor implements Executor {
51-
private final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
52-
private Runnable mActive;
53-
private final Executor executor;
54-
55-
SerialExecutor(Executor executor) {
56-
this.executor = executor;
57-
}
58-
59-
public synchronized void execute(final Runnable r) {
60-
mTasks.offer(new Runnable() {
61-
public void run() {
62-
try {
63-
r.run();
64-
} finally {
65-
scheduleNext();
66-
}
67-
}
68-
});
69-
if (mActive == null) {
70-
scheduleNext();
71-
}
72-
}
73-
synchronized void scheduleNext() {
74-
if ((mActive = mTasks.poll()) != null) {
75-
executor.execute(mActive);
76-
}
77-
}
78-
}
79-
8049
private final SerialExecutor executor;
8150

8251
public AsyncStorageModule(ReactApplicationContext reactContext) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.reactnativecommunity.asyncstorage;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.concurrent.Executor;
5+
6+
// Adapted from https://android.googlesource.com/platform/frameworks/base.git/+/1488a3a19d4681a41fb45570c15e14d99db1cb66/core/java/android/os/AsyncTask.java#237
7+
public class SerialExecutor implements Executor {
8+
private final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
9+
private Runnable mActive;
10+
private final Executor executor;
11+
12+
public SerialExecutor(Executor executor) {
13+
this.executor = executor;
14+
}
15+
16+
public synchronized void execute(final Runnable r) {
17+
mTasks.offer(new Runnable() {
18+
public void run() {
19+
try {
20+
r.run();
21+
} finally {
22+
scheduleNext();
23+
}
24+
}
25+
});
26+
if (mActive == null) {
27+
scheduleNext();
28+
}
29+
}
30+
synchronized void scheduleNext() {
31+
if ((mActive = mTasks.poll()) != null) {
32+
executor.execute(mActive);
33+
}
34+
}
35+
}

android/src/main/java/com/reactnativecommunity/asyncstorage/next/StorageModule.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package com.reactnativecommunity.asyncstorage.next
22

33
import android.content.Context
4+
import androidx.annotation.VisibleForTesting
45
import com.facebook.react.bridge.Arguments
56
import com.facebook.react.bridge.Callback
67
import com.facebook.react.bridge.ReactContext
78
import com.facebook.react.bridge.ReactContextBaseJavaModule
89
import com.facebook.react.bridge.ReactMethod
910
import com.facebook.react.bridge.ReadableArray
11+
import com.reactnativecommunity.asyncstorage.SerialExecutor
1012
import kotlinx.coroutines.CoroutineName
1113
import kotlinx.coroutines.CoroutineScope
1214
import kotlinx.coroutines.Dispatchers
1315
import kotlinx.coroutines.SupervisorJob
16+
import kotlinx.coroutines.asExecutor
1417
import kotlinx.coroutines.launch
1518

1619
class StorageModule(reactContext: ReactContext) : ReactContextBaseJavaModule(), CoroutineScope {
1720
override fun getName() = "RNC_AsyncSQLiteDBStorage"
1821

22+
// this executor is here only to please detox, which relies on internal implementation
23+
// of current Async Storage
24+
@VisibleForTesting
25+
private val executor = SerialExecutor(Dispatchers.Main.asExecutor())
26+
1927
override val coroutineContext =
2028
Dispatchers.IO + CoroutineName("AsyncStorageScope") + SupervisorJob()
2129

0 commit comments

Comments
 (0)