Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make AsyncStorage serially execute requests #18522

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

package com.facebook.react.modules.storage;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.concurrent.Executor;

import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.os.AsyncTask;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
Expand Down Expand Up @@ -43,6 +46,33 @@ public final class AsyncStorageModule
private ReactDatabaseSupplier mReactDatabaseSupplier;
private boolean mShuttingDown = false;

// Borrowed from https://android.googlesource.com/platform/frameworks/base.git/+/1488a3a19d4681a41fb45570c15e14d99db1cb66/core/java/android/os/AsyncTask.java#237
private static class SerialExecutor implements Executor {
final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
Runnable mActive;
public synchronized void execute(final Runnable r) {
mTasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (mActive == null) {
scheduleNext();
}
}
protected synchronized void scheduleNext() {
if ((mActive = mTasks.poll()) != null) {
AsyncTask.THREAD_POOL_EXECUTOR.execute(mActive);
}
}
}

private static final Executor ASYNC_STORAGE_EXECUTOR = new SerialExecutor();

public AsyncStorageModule(ReactApplicationContext reactContext) {
super(reactContext);
mReactDatabaseSupplier = ReactDatabaseSupplier.getInstance(reactContext);
Expand Down Expand Up @@ -141,7 +171,7 @@ protected void doInBackgroundGuarded(Void... params) {

callback.invoke(null, data);
}
}.execute();
}.executeOnExecutor(ASYNC_STORAGE_EXECUTOR);
}

/**
Expand Down Expand Up @@ -208,7 +238,7 @@ protected void doInBackgroundGuarded(Void... params) {
callback.invoke();
}
}
}.execute();
}.executeOnExecutor(ASYNC_STORAGE_EXECUTOR);
}

/**
Expand Down Expand Up @@ -259,7 +289,7 @@ protected void doInBackgroundGuarded(Void... params) {
callback.invoke();
}
}
}.execute();
}.executeOnExecutor(ASYNC_STORAGE_EXECUTOR);
}

/**
Expand Down Expand Up @@ -322,7 +352,7 @@ protected void doInBackgroundGuarded(Void... params) {
callback.invoke();
}
}
}.execute();
}.executeOnExecutor(ASYNC_STORAGE_EXECUTOR);
}

/**
Expand All @@ -345,7 +375,7 @@ protected void doInBackgroundGuarded(Void... params) {
callback.invoke(AsyncStorageErrorUtil.getError(null, e.getMessage()));
}
}
}.execute();
}.executeOnExecutor(ASYNC_STORAGE_EXECUTOR);
}

/**
Expand Down Expand Up @@ -379,7 +409,7 @@ protected void doInBackgroundGuarded(Void... params) {
}
callback.invoke(null, data);
}
}.execute();
}.executeOnExecutor(ASYNC_STORAGE_EXECUTOR);
}

/**
Expand Down