Last active
November 18, 2023 19:51
-
-
Save kshivang/2b6561c0574a4031ee0e40643e589b8f to your computer and use it in GitHub Desktop.
Switch/Refresh publisher on Firebase Auth new login simple one-liner for rever-ai/CombineFirebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func onLoginRefresher<T>(_ publisherGetter: @escaping () -> AnyPublisher<T, Never> ) -> AnyPublisher<T, Never> { | |
Auth.auth().stateDidChangePublisher | |
.filter { $0 != nil } // User LoggedIn | |
.map { _ in publisherGetter() } | |
.switchToLatest() | |
.eraseToAnyPublisher() | |
} | |
// e.g.: turn userDataPublisher to onLoginRefresher { userDataPublisher() } and done | |
// | |
// onLoginRefresher { userDataPublisher() } | |
// .receive(on: RunLoop.main) | |
// .sink { userData -> do Someting } | |
// .store(in: &cancelBag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rever-ai/CombineFirebase is the major dependency