Skip to content

Instantly share code, notes, and snippets.

@soxjke soxjke/AuthManager.swift
Last active Nov 7, 2017

Embed
What would you like to do?
enum AuthError: Error {
case wrongAuthData,
case wrongDeauthData
}
class AuthManager {
let networkManager: // injected dependency
func deauthenticateUser(user: User) -> SignalProducer<User, AuthError> {
// perhaps go to network:
return SignalProducer { (observer, disposable) in
let netOperationToken = networkManager.deauthenticate(user: user) { (user, error) in
if let error = error {
observer.send(error: error)
} else {
observer.send(user)
observer.sendCompleted()
}
}
return AnyDisposable {
networkManager.cancel(netOperationToken)
}
}
}
func authenticateUser(user: User) -> SignalProducer<User, AuthError> {
// pretty same shit
}
}
class AuthManager2 {
private let innerAuthenticatedUsers = MutableProperty<[User], NoError>([])
public let authenticatedUsers: = Property<[User], NoError>(capturing: innerAuthenticatedUsers)
public func authenticate(user: User) {
// at some point of time:
innerAuthenticatedUsers.value = innerAuthenticatedUsers.value + [user]
}
}
// client code:
AuthManager2.shared.authenticatedUsers.producer.startWithValues { (authenticatedUsersArray) in
// here will go array of authenticated users updates
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.