Skip to content

Commit 527ae0c

Browse files
committed
Make changes based on PR comments;
Make internal ConfigUpdateListenerRegistration non-static to access private listener removal method from outer class. Add synchonization to listeners set.
1 parent 32e980a commit 527ae0c

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

firebase-config/src/main/java/com/google/firebase/remoteconfig/internal/ConfigRealtimeHttpClient.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616

1717
import com.google.firebase.remoteconfig.ConfigUpdateListener;
1818
import com.google.firebase.remoteconfig.ConfigUpdateListenerRegistration;
19-
import java.util.HashMap;
20-
import java.util.Map;
19+
import java.util.Collections;
20+
import java.util.LinkedHashSet;
21+
import java.util.Set;
2122

2223
public class ConfigRealtimeHttpClient {
2324

24-
private final Map<Integer, ConfigUpdateListener> listeners;
25-
private int listenerCount;
25+
private final Set<ConfigUpdateListener> listeners;
2626

2727
public ConfigRealtimeHttpClient() {
28-
listeners = new HashMap<>();
29-
listenerCount = 1;
28+
listeners = Collections.synchronizedSet(new LinkedHashSet<ConfigUpdateListener>());
3029
}
3130

3231
// Kicks off Http stream listening and autofetch
@@ -37,31 +36,28 @@ private void pauseRealtime() {}
3736

3837
public ConfigUpdateListenerRegistration addRealtimeConfigUpdateListener(
3938
ConfigUpdateListener configUpdateListener) {
40-
listeners.put(listenerCount, configUpdateListener);
39+
listeners.add(configUpdateListener);
4140
beginRealtime();
42-
return new ConfigUpdateListenerRegistrationInternal(this, listenerCount++);
41+
return new ConfigUpdateListenerRegistrationInternal(configUpdateListener);
4342
}
4443

45-
public void removeRealtimeConfigUpdateListener(int listenerKey) {
46-
listeners.remove(listenerKey);
44+
private void removeRealtimeConfigUpdateListener(ConfigUpdateListener listener) {
45+
listeners.remove(listener);
4746
if (listeners.isEmpty()) {
4847
pauseRealtime();
4948
}
5049
}
5150

52-
public static class ConfigUpdateListenerRegistrationInternal
51+
public class ConfigUpdateListenerRegistrationInternal
5352
implements ConfigUpdateListenerRegistration {
54-
private final ConfigRealtimeHttpClient client;
55-
private final int listenerKey;
53+
private final ConfigUpdateListener listener;
5654

57-
public ConfigUpdateListenerRegistrationInternal(
58-
ConfigRealtimeHttpClient client, int listenerKey) {
59-
this.client = client;
60-
this.listenerKey = listenerKey;
55+
public ConfigUpdateListenerRegistrationInternal(ConfigUpdateListener listener) {
56+
this.listener = listener;
6157
}
6258

6359
public void remove() {
64-
client.removeRealtimeConfigUpdateListener(listenerKey);
60+
removeRealtimeConfigUpdateListener(listener);
6561
}
6662
}
6763
}

0 commit comments

Comments
 (0)