Back out "[react-native][PR] There is a small gap in the SynchronizedWeakHashSet implementation. T?"

Summary: Original commit changeset: 2998bffb06e2

Reviewed By: JoshuaGross

Differential Revision: D14689422

fbshipit-source-id: 2638bed8005859cc95972ba5f78a9e9cace878ef
This commit is contained in:
Christoph Nakazawa
2019-03-29 14:48:58 -07:00
committed by Facebook Github Bot
parent 5fa8258138
commit d2981af68f
2 changed files with 13 additions and 60 deletions

View File

@@ -176,17 +176,14 @@ public class ReactContext extends ContextWrapper {
new Runnable() {
@Override
public void run() {
mLifecycleEventListeners.doIfContains(listener, new Runnable() {
@Override
public void run() {
try {
listener.onHostResume();
} catch (RuntimeException e) {
handleException(e);
}
}
});
if (!mLifecycleEventListeners.contains(listener)) {
return;
}
try {
listener.onHostResume();
} catch (RuntimeException e) {
handleException(e);
}
}
});
break;

View File

@@ -24,15 +24,9 @@ public class SynchronizedWeakHashSet<T> {
private Queue<Pair<T, Command>> mPendingOperations = new ArrayDeque<>();
private boolean mIterating;
public void doIfContains(T item, Runnable runnable) {
public boolean contains(T item) {
synchronized (mMap) {
if (mIterating) {
mPendingOperations.add(new Pair<>(item, Command.newDoIfContains(runnable)));
} else {
if (mMap.containsKey(item)) {
runnable.run();
}
}
return mMap.containsKey(item);
}
}
@@ -67,19 +61,13 @@ public class SynchronizedWeakHashSet<T> {
while (!mPendingOperations.isEmpty()) {
Pair<T, Command> pair = mPendingOperations.poll();
Command command = pair.second;
switch (command.getType()) {
switch (pair.second) {
case ADD:
mMap.put(pair.first, null);
break;
case REMOVE:
mMap.remove(pair.first);
break;
case DO_IF_CONTAINS:
if (mMap.containsKey(pair.first)) {
command.execute();
}
break;
default:
throw new AssertionException("Unsupported command" + pair.second);
}
@@ -91,40 +79,8 @@ public class SynchronizedWeakHashSet<T> {
void iterate(T item);
}
private enum CommandType {
private enum Command {
ADD,
REMOVE,
DO_IF_CONTAINS
}
private static class Command {
public static final Command ADD = new Command(CommandType.ADD);
public static final Command REMOVE = new Command(CommandType.REMOVE);
private CommandType mType;
private Runnable mRunnable;
public static Command newDoIfContains(Runnable runnable) {
return new Command(CommandType.DO_IF_CONTAINS, runnable);
}
private Command(CommandType type) {
this(type, null);
}
private Command(CommandType type, Runnable runnable) {
mType = type;
mRunnable = runnable;
}
public CommandType getType() {
return mType;
}
public void execute() {
if (mRunnable != null) {
mRunnable.run();
}
}
REMOVE
}
}