Skip to content

Instantly share code, notes, and snippets.

@itsandreramon
Last active January 11, 2024 14:31
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 itsandreramon/dcda3ba9b7816dbc2287a98c550b8aa3 to your computer and use it in GitHub Desktop.
Save itsandreramon/dcda3ba9b7816dbc2287a98c550b8aa3 to your computer and use it in GitHub Desktop.
@SingleIn(AppScope::class)
class UserSessionManager @Inject constructor(private val app: App) {
private var _userCoroutineScope: UserCoroutineScope? = null
private val userCoroutineScope: CoroutineScope
get() = createUserCoroutineScope()
fun resetSession() {
_userCoroutineScope?.cancel()
_userCoroutineScope = null
userComponent = null
}
fun createSession(id: String) {
val session = UserSession(id)
userComponent = app.appComponent
.userComponentFactory()
.create(session, userCoroutineScope)
}
private fun createUserCoroutineScope(): UserCoroutineScope {
return _userCoroutineScope ?: UserCoroutineScope(CoroutineScope(SupervisorJob()))
.also { _userCoroutineScope = it }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment