Skip to content

Commit

Permalink
fix(Android): 修复ConcurrentModificationException问题
Browse files Browse the repository at this point in the history
  • Loading branch information
bbssyyuui committed Mar 22, 2023
1 parent 9105cdb commit b932527
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,15 @@ private static synchronized void removeEventListener(long contextId, String key,
cbList.clear();
} else {
// clear one
ICallback cbToRemove = null;
for (ICallback cb : cbList) {
if (cb != null && cb.equals(callback)) {
cbToRemove = cb;
break;
}
int index = cbList.indexOf(callback);
if (callback instanceof JSCallback) {
((JSCallback) callback).release();
}
if (cbToRemove instanceof JSCallback) {
((JSCallback) cbToRemove).release();
cbList.remove(cbToRemove);
if (index >= 0) {
ICallback cb = cbList.get(index);
if (cb instanceof JSCallback) {
((JSCallback) cb).release();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.didi.hummer.render.event;

import com.didi.hummer.core.engine.JSCallback;
import com.didi.hummer.render.event.base.Event;
import com.didi.hummer.lifecycle.ILifeCycle;
import com.didi.hummer.render.event.base.Event;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class EventManager implements ILifeCycle, IEventListener {
public HashMap<String, List<JSCallback>> mEventListeners;
public Map<String, List<JSCallback>> mEventListeners;

@Override
public void onCreate() {
mEventListeners = new HashMap<>();
mEventListeners = new ConcurrentHashMap<>();
}

@Override
Expand All @@ -37,17 +38,15 @@ public void onDestroy() {
public void addEventListener(String eventName, JSCallback callback) {
if (mEventListeners.containsKey(eventName)) {
List<JSCallback> callbacks = mEventListeners.get(eventName);

if (callbacks.contains(callback)) {
return;
if (callbacks == null) {
callbacks = new CopyOnWriteArrayList<>();
callbacks.add(callback);
} else if (!callbacks.contains(callback)) {
callbacks.add(callback);
}

callbacks.add(callback);
} else {
List<JSCallback> callbacks = new ArrayList<>();

List<JSCallback> callbacks = new CopyOnWriteArrayList<>();
callbacks.add(callback);

mEventListeners.put(eventName, callbacks);
}
}
Expand All @@ -59,20 +58,18 @@ public void removeEventListener(String eventName, JSCallback callback) {
}

List<JSCallback> callbacks = mEventListeners.get(eventName);
if (callbacks == null) {
return;
}

JSCallback cbToRemove = null;
for (JSCallback cb : callbacks) {
if (cb != null && cb.equals(callback)) {
int index = callbacks.indexOf(callback);
callback.release();
if (index >= 0) {
JSCallback cb = callbacks.get(index);
if (cb != null) {
cb.release();
callback.release();
cbToRemove = callback;
break;
}
}

if (cbToRemove != null) {
callbacks.remove(cbToRemove);
}
}

@Override
Expand All @@ -82,6 +79,10 @@ public void clearEventListeners(String eventName) {
}

List<JSCallback> callbacks = mEventListeners.get(eventName);
if (callbacks == null) {
return;
}

for (JSCallback cb : callbacks) {
if (cb != null) {
cb.release();
Expand All @@ -96,6 +97,9 @@ public void dispatchEvent(String eventName, Event event) {
}

List<JSCallback> callbacks = mEventListeners.get(eventName);
if (callbacks == null) {
return;
}

for (JSCallback callback : callbacks) {
callback.call(event);
Expand Down

0 comments on commit b932527

Please sign in to comment.