Files
helder/Tests/HelderUITests/Support/Fakes/Locked.swift
T

23 lines
560 B
Swift

import Foundation
/// A value behind a lock, so a fake can be `Sendable` and still be read synchronously from a test.
final class Locked<Value>: @unchecked Sendable {
private let lock = NSLock()
private var value: Value
init(_ value: Value) {
self.value = value
}
var current: Value { lock.withLock { value } }
@discardableResult
func mutate<T>(_ change: (inout Value) throws -> T) rethrows -> T {
lock.lock()
defer { lock.unlock() }
return try change(&value)
}
}
struct FakeError: Error {}