Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Created November 14, 2019 13:38
Show Gist options
  • Save manuelvicnt/19307282cbf287cfe74e1d9576139fd8 to your computer and use it in GitHub Desktop.
Save manuelvicnt/19307282cbf287cfe74e1d9576139fd8 to your computer and use it in GitHub Desktop.
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class FirebaseAuthStateUserDataSource(...) : AuthStateUserDataSource {
private val channel = ConflatedBroadcastChannel<Result<AuthenticatedUserInfo>>()
private val listener: ((FirebaseAuth) -> Unit) = { auth ->
// Data processing logic
// Send the current user for observers
if (!channel.isClosedForSend) {
channel.offer(Success(FirebaseUserInfo(auth.currentUser)))
} else {
unregisterListener()
}
}
@Synchronized
override fun getBasicUserInfo(): Flow<Result<AuthenticatedUserInfo>> {
if (!isListening) {
firebase.addAuthStateListener(listener)
isListening = true
}
return channel.asFlow()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment