File tree Expand file tree Collapse file tree 3 files changed +43
-31
lines changed
android/src/main/java/com/reactnativecommunity/asyncstorage Expand file tree Collapse file tree 3 files changed +43
-31
lines changed Original file line number Diff line number Diff line change @@ -46,37 +46,6 @@ public final class AsyncStorageModule
46
46
private ReactDatabaseSupplier mReactDatabaseSupplier ;
47
47
private boolean mShuttingDown = false ;
48
48
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
-
80
49
private final SerialExecutor executor ;
81
50
82
51
public AsyncStorageModule (ReactApplicationContext reactContext ) {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
package com.reactnativecommunity.asyncstorage.next
2
2
3
3
import android.content.Context
4
+ import androidx.annotation.VisibleForTesting
4
5
import com.facebook.react.bridge.Arguments
5
6
import com.facebook.react.bridge.Callback
6
7
import com.facebook.react.bridge.ReactContext
7
8
import com.facebook.react.bridge.ReactContextBaseJavaModule
8
9
import com.facebook.react.bridge.ReactMethod
9
10
import com.facebook.react.bridge.ReadableArray
11
+ import com.reactnativecommunity.asyncstorage.SerialExecutor
10
12
import kotlinx.coroutines.CoroutineName
11
13
import kotlinx.coroutines.CoroutineScope
12
14
import kotlinx.coroutines.Dispatchers
13
15
import kotlinx.coroutines.SupervisorJob
16
+ import kotlinx.coroutines.asExecutor
14
17
import kotlinx.coroutines.launch
15
18
16
19
class StorageModule (reactContext : ReactContext ) : ReactContextBaseJavaModule(), CoroutineScope {
17
20
override fun getName () = " RNC_AsyncSQLiteDBStorage"
18
21
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
+
19
27
override val coroutineContext =
20
28
Dispatchers .IO + CoroutineName (" AsyncStorageScope" ) + SupervisorJob ()
21
29
You can’t perform that action at this time.
0 commit comments