Skip to content

Instantly share code, notes, and snippets.

@saurabharora90
Last active June 21, 2020 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurabharora90/2795b0ff9000e333f1db7fdcd165a12b to your computer and use it in GitHub Desktop.
Save saurabharora90/2795b0ff9000e333f1db7fdcd165a12b to your computer and use it in GitHub Desktop.
private val initializedModules = HashSet<Class<out ModuleInitializer>>()
fun getInitializerCompletable(): Completable {
val completableList = mutableListOf<Completable>()
map.forEach { (clazz, moduleInitializer) ->
completableList.addAll(getCompletableForModule(clazz, moduleInitializer))
}
return Completable.concat(completableList)
}
private fun getCompletableForModule(
clazz: Class<out ModuleInitializer>,
moduleToInitialize: ModuleInitializer
): List<Completable> {
if (moduleToInitialize.dependencies().isEmpty() && initializedModules.contains(clazz)) {
return emptyList()
} else {
val completableList = mutableListOf<Completable>()
moduleToInitialize.dependencies().forEach {
completableList.addAll(getCompletableForModule(it, map.getValue(it)))
}
if (!initializedModules.contains(clazz)) {
initializedModules.add(clazz)
completableList.add(moduleToInitialize.initialize(context))
}
return completableList
}
}
@stewemetal
Copy link

stewemetal commented Jun 21, 2020

map[it]!! could be replaced with map.getValue(it) if you want to avoid using !!. :) Also, you get a more detailed exception instead of an NPE this way.

@saurabharora90
Copy link
Author

updated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment