16
16
17
17
import com .google .firebase .remoteconfig .ConfigUpdateListener ;
18
18
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 ;
21
22
22
23
public class ConfigRealtimeHttpClient {
23
24
24
- private final Map <Integer , ConfigUpdateListener > listeners ;
25
- private int listenerCount ;
25
+ private final Set <ConfigUpdateListener > listeners ;
26
26
27
27
public ConfigRealtimeHttpClient () {
28
- listeners = new HashMap <>();
29
- listenerCount = 1 ;
28
+ listeners = Collections .synchronizedSet (new LinkedHashSet <ConfigUpdateListener >());
30
29
}
31
30
32
31
// Kicks off Http stream listening and autofetch
@@ -37,31 +36,28 @@ private void pauseRealtime() {}
37
36
38
37
public ConfigUpdateListenerRegistration addRealtimeConfigUpdateListener (
39
38
ConfigUpdateListener configUpdateListener ) {
40
- listeners .put ( listenerCount , configUpdateListener );
39
+ listeners .add ( configUpdateListener );
41
40
beginRealtime ();
42
- return new ConfigUpdateListenerRegistrationInternal (this , listenerCount ++ );
41
+ return new ConfigUpdateListenerRegistrationInternal (configUpdateListener );
43
42
}
44
43
45
- public void removeRealtimeConfigUpdateListener (int listenerKey ) {
46
- listeners .remove (listenerKey );
44
+ private void removeRealtimeConfigUpdateListener (ConfigUpdateListener listener ) {
45
+ listeners .remove (listener );
47
46
if (listeners .isEmpty ()) {
48
47
pauseRealtime ();
49
48
}
50
49
}
51
50
52
- public static class ConfigUpdateListenerRegistrationInternal
51
+ public class ConfigUpdateListenerRegistrationInternal
53
52
implements ConfigUpdateListenerRegistration {
54
- private final ConfigRealtimeHttpClient client ;
55
- private final int listenerKey ;
53
+ private final ConfigUpdateListener listener ;
56
54
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 ;
61
57
}
62
58
63
59
public void remove () {
64
- client . removeRealtimeConfigUpdateListener (listenerKey );
60
+ removeRealtimeConfigUpdateListener (listener );
65
61
}
66
62
}
67
63
}
0 commit comments