AsyncMonitor/Sources/AsyncMonitor/ValueLocker.swift
Sami Samhuri d2b4e0e382
Change the KVO monitoring API
Instead of having a values method that observes and monitors, break out
a values method that returns an AsyncStream and then a monitorValues
method that calls values(for: keyPath).monitor. That method is kind of
superfluous, not sure if it's good to keep it or not.
2025-04-26 17:47:12 -07:00

20 lines
385 B
Swift

import Foundation
final class ValueLocker<Value>: @unchecked Sendable {
private let lock = NSLock()
private var unsafeValue: Value
init(value: Value) {
unsafeValue = value
}
var value: Value {
lock.withLock { unsafeValue }
}
func modify(_ f: (inout Value) -> Void) {
lock.withLock {
f(&unsafeValue)
}
}
}