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() { new Runnable() {
@Override @Override
public void run() { public void run() {
if (!mLifecycleEventListeners.contains(listener)) {
mLifecycleEventListeners.doIfContains(listener, new Runnable() { return;
@Override }
public void run() { try {
try { listener.onHostResume();
listener.onHostResume(); } catch (RuntimeException e) {
} catch (RuntimeException e) { handleException(e);
handleException(e); }
}
}
});
} }
}); });
break; break;

View File

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