-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added KVO support via
NSObject.currentValuePublisher(for:)
- Loading branch information
1 parent
26beaa5
commit 5c20b7f
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
Sources/CombineExtensions/CurrentValuePublisher/CurrentValuePublisher+KVO.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Foundation | ||
|
||
public protocol KVOCurrentValuePublishing {} | ||
|
||
extension NSObject: KVOCurrentValuePublishing {} | ||
|
||
extension KVOCurrentValuePublishing where Self: NSObject { | ||
|
||
/// Returns a `CurrentValuePublisher` that tracks the current value of a KVO-compliant property. | ||
/// | ||
/// - Parameter keyPath: The key path of the property to observe. | ||
/// - Returns: A `CurrentValuePublisher` that tracks the property's value. | ||
/// | ||
/// This implementation follows the approach of `NSObject.publisher(for:options:)` from Foundation, | ||
/// previously available in the [swift-corelibs-foundation](https://bit.ly/nsobject-keyvalueobserving) | ||
/// open source repository. | ||
public func currentValuePublisher<Value>( | ||
for keyPath: KeyPath<Self, Value> | ||
) -> CurrentValuePublisher<Value, Never> { | ||
return CurrentValuePublisher( | ||
initial: self[keyPath: keyPath], | ||
upstream: self.publisher(for: keyPath, options: .new) | ||
) | ||
} | ||
|
||
} |