Created
April 8, 2025 16:45
-
-
Save jacobsapps/3ff251dafe479f2c86ef3e6fac633461 to your computer and use it in GitHub Desktop.
Auth Actor 2
This file contains hidden or 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
actor AuthService { | |
var tokenTask: Task<String, Error>? | |
func getAuthToken() async throws -> String { | |
if let localToken = getLocalToken(), | |
!localToken.isExpired { | |
return localToken | |
} | |
if tokenTask == nil { | |
tokenTask = Task(priority: high) { | |
try await refreshAuthToken() | |
} | |
} | |
defer { tokenTask = nil } | |
return try await tokenTask!.value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment