Created
April 9, 2025 07:21
-
-
Save jacobsapps/d8ea53e9511a3ba94e710ae31ae1e655 to your computer and use it in GitHub Desktop.
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 TaskActor<T> { | |
private var cachedTask: Task<T, Error>? | |
func run(_ operation: @escaping @Sendable () async throws -> T) async throws -> T { | |
if cachedTask == nil { | |
cachedTask = Task { | |
try await operation() | |
} | |
} | |
defer { cachedTask = nil } | |
return try await cachedTask!.value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment