Skip to content

Commit

Permalink
Observable API for KVO
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Oct 15, 2018
1 parent 87f51da commit bbfdc9b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Sources/Foundation/NSObject+Jetpack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ extension JetpackExtensions where Base: NSObject {
return DisposeBag()
}
}

public func observed<Value>(_ sourceKeyPath: KeyPath<Base,Value>) -> Observable<Value>
{
return Observable { observer in
let observation = self.base.observe(sourceKeyPath, options: [.initial, .new]) { object, change in
guard let newValue = change.newValue else { return }
observer(newValue)
}
return BlockDisposable {
observation.invalidate()
}
}
}
}

extension NSObject: JetpackExtensionsProvider {}
Expand Down

0 comments on commit bbfdc9b

Please sign in to comment.