mirror of
https://github.com/pointfreeco/swift-composable-architecture.git
synced 2025-12-14 20:35:56 +01:00
23 lines
522 B
Swift
23 lines
522 B
Swift
import Dispatch
|
|
|
|
func mainActorNow<R: Sendable>(execute block: @MainActor @Sendable () -> R) -> R {
|
|
if DispatchQueue.getSpecific(key: key) == value {
|
|
return MainActor._assumeIsolated {
|
|
block()
|
|
}
|
|
} else {
|
|
return DispatchQueue.main.sync {
|
|
MainActor._assumeIsolated {
|
|
block()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private let key: DispatchSpecificKey<UInt8> = {
|
|
let key = DispatchSpecificKey<UInt8>()
|
|
DispatchQueue.main.setSpecific(key: key, value: value)
|
|
return key
|
|
}()
|
|
private let value: UInt8 = 0
|