@@ -49,6 +49,7 @@ public class ComponentRuntime implements ComponentContainer, ComponentLoader {
49
49
private final Map <Qualified <?>, Provider <?>> lazyInstanceMap = new HashMap <>();
50
50
private final Map <Qualified <?>, LazySet <?>> lazySetMap = new HashMap <>();
51
51
private final List <Provider <ComponentRegistrar >> unprocessedRegistrarProviders ;
52
+ private final List <Component <?>> processedComponents = new ArrayList <>();
52
53
private final EventBus eventBus ;
53
54
private final AtomicReference <Boolean > eagerComponentsInitializedWith = new AtomicReference <>();
54
55
private final ComponentRegistrarProcessor componentRegistrarProcessor ;
@@ -107,6 +108,8 @@ private void discoverComponents(List<Component<?>> componentsToAdd) {
107
108
// instead of executing such code in the synchronized block below, we store it in a list and
108
109
// execute right after the synchronized section.
109
110
List <Runnable > runAfterDiscovery = new ArrayList <>();
111
+ List <Component <?>> componentsAdding = new ArrayList <>();
112
+ Set <String > existingInterfaces = new HashSet <>();
110
113
synchronized (this ) {
111
114
Iterator <Provider <ComponentRegistrar >> iterator = unprocessedRegistrarProviders .iterator ();
112
115
while (iterator .hasNext ()) {
@@ -122,16 +125,41 @@ private void discoverComponents(List<Component<?>> componentsToAdd) {
122
125
Log .w (ComponentDiscovery .TAG , "Invalid component registrar." , ex );
123
126
}
124
127
}
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
+ }
125
153
126
154
if (components .isEmpty ()) {
127
- CycleDetector .detect (componentsToAdd );
155
+ CycleDetector .detect (componentsAdding );
128
156
} else {
129
157
ArrayList <Component <?>> allComponents = new ArrayList <>(this .components .keySet ());
130
- allComponents .addAll (componentsToAdd );
158
+ allComponents .addAll (componentsAdding );
131
159
CycleDetector .detect (allComponents );
132
160
}
133
161
134
- for (Component <?> component : componentsToAdd ) {
162
+ for (Component <?> component : componentsAdding ) {
135
163
Lazy <?> lazy =
136
164
new Lazy <>(
137
165
() ->
@@ -142,7 +170,8 @@ private void discoverComponents(List<Component<?>> componentsToAdd) {
142
170
components .put (component , lazy );
143
171
}
144
172
145
- runAfterDiscovery .addAll (processInstanceComponents (componentsToAdd ));
173
+ runAfterDiscovery .addAll (processInstanceComponents (componentsAdding ));
174
+ processedComponents .addAll (componentsAdding );
146
175
runAfterDiscovery .addAll (processSetComponents ());
147
176
processDependencies ();
148
177
}
0 commit comments