mirror of
https://github.com/samsonjs/AsyncMonitor.git
synced 2026-03-25 08:25:47 +00:00
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.
20 lines
385 B
Swift
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)
|
|
}
|
|
}
|
|
}
|