Skip to content

Commit e6481e2

Browse files
committed
update
1 parent fe84885 commit e6481e2

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

firebase-components/src/main/java/com/google/firebase/components/ComponentRuntime.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class ComponentRuntime implements ComponentContainer, ComponentLoader {
4949
private final Map<Qualified<?>, Provider<?>> lazyInstanceMap = new HashMap<>();
5050
private final Map<Qualified<?>, LazySet<?>> lazySetMap = new HashMap<>();
5151
private final List<Provider<ComponentRegistrar>> unprocessedRegistrarProviders;
52+
private final List<Component<?>> processedComponents = new ArrayList<>();
5253
private final EventBus eventBus;
5354
private final AtomicReference<Boolean> eagerComponentsInitializedWith = new AtomicReference<>();
5455
private final ComponentRegistrarProcessor componentRegistrarProcessor;
@@ -107,6 +108,8 @@ private void discoverComponents(List<Component<?>> componentsToAdd) {
107108
// instead of executing such code in the synchronized block below, we store it in a list and
108109
// execute right after the synchronized section.
109110
List<Runnable> runAfterDiscovery = new ArrayList<>();
111+
List<Component<?>> componentsAdding = new ArrayList<>();
112+
Set<String> existingInterfaces = new HashSet<>();
110113
synchronized (this) {
111114
Iterator<Provider<ComponentRegistrar>> iterator = unprocessedRegistrarProviders.iterator();
112115
while (iterator.hasNext()) {
@@ -122,16 +125,41 @@ private void discoverComponents(List<Component<?>> componentsToAdd) {
122125
Log.w(ComponentDiscovery.TAG, "Invalid component registrar.", ex);
123126
}
124127
}
128+
for (int i = 0; i < processedComponents.size(); i++) {
129+
Object[] interfaces = processedComponents.get(i).getProvidedInterfaces().toArray();
130+
for (Object anInterface : interfaces) {
131+
if (anInterface.toString().contains("kotlinx.coroutines.CoroutineDispatcher")) {
132+
existingInterfaces.add(anInterface.toString());
133+
}
134+
}
135+
}
136+
for (int i = 0; i < componentsToAdd.size(); i++) {
137+
Object[] interfaces = componentsToAdd.get(i).getProvidedInterfaces().toArray();
138+
boolean addComponent = true;
139+
for (Object anInterface : interfaces) {
140+
String interfaceString = anInterface.toString();
141+
if (interfaceString.contains("kotlinx.coroutines.CoroutineDispatcher")) {
142+
if (existingInterfaces.contains(interfaceString)) {
143+
addComponent = false;
144+
} else {
145+
existingInterfaces.add(interfaceString);
146+
}
147+
}
148+
}
149+
if (addComponent) {
150+
componentsAdding.add(componentsToAdd.get(i));
151+
}
152+
}
125153

126154
if (components.isEmpty()) {
127-
CycleDetector.detect(componentsToAdd);
155+
CycleDetector.detect(componentsAdding);
128156
} else {
129157
ArrayList<Component<?>> allComponents = new ArrayList<>(this.components.keySet());
130-
allComponents.addAll(componentsToAdd);
158+
allComponents.addAll(componentsAdding);
131159
CycleDetector.detect(allComponents);
132160
}
133161

134-
for (Component<?> component : componentsToAdd) {
162+
for (Component<?> component : componentsAdding) {
135163
Lazy<?> lazy =
136164
new Lazy<>(
137165
() ->
@@ -142,7 +170,8 @@ private void discoverComponents(List<Component<?>> componentsToAdd) {
142170
components.put(component, lazy);
143171
}
144172

145-
runAfterDiscovery.addAll(processInstanceComponents(componentsToAdd));
173+
runAfterDiscovery.addAll(processInstanceComponents(componentsAdding));
174+
processedComponents.addAll(componentsAdding);
146175
runAfterDiscovery.addAll(processSetComponents());
147176
processDependencies();
148177
}

0 commit comments

Comments
 (0)