Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the unbound memory growth of unidirectional bindings. #176

Merged
merged 1 commit into from
Dec 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public protocol PropertyProtocol: class, BindingSourceProtocol {

extension PropertyProtocol {
@discardableResult
public func observe(_ observer: Observer<Value, NoError>) -> Disposable? {
return producer.observe(observer)
public func observe(_ observer: Observer<Value, NoError>, during lifetime: Lifetime) -> Disposable? {
return producer.observe(observer, during: lifetime)
}
}

Expand Down
35 changes: 17 additions & 18 deletions Sources/UnidirectionalBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,29 @@ infix operator <~ : BindingPrecedence
public protocol BindingSourceProtocol {
associatedtype Value
associatedtype Error: Swift.Error

/// Observe the binding source by sending any events to the given observer.
@discardableResult
func observe(_ observer: Observer<Value, Error>) -> Disposable?
func observe(_ observer: Observer<Value, Error>, during lifetime: Lifetime) -> Disposable?
}

extension Signal: BindingSourceProtocol { }
extension Signal: BindingSourceProtocol {
@discardableResult
public func observe(_ observer: Observer, during lifetime: Lifetime) -> Disposable? {
return self.take(during: lifetime).observe(observer)
}
}

extension SignalProducer: BindingSourceProtocol {
@discardableResult
public func observe(_ observer: Observer<Value, Error>) -> Disposable? {
public func observe(_ observer: ProducedSignal.Observer, during lifetime: Lifetime) -> Disposable? {
var disposable: Disposable!

startWithSignal { signal, signalDisposable in
disposable = signalDisposable
signal.observe(observer)
self
.take(during: lifetime)
.startWithSignal { signal, signalDisposable in
disposable = signalDisposable
signal.observe(observer)
}

return disposable
Expand Down Expand Up @@ -93,12 +100,8 @@ public func <~
} else {
observer = Observer(value: { [weak target] in target?.consume($0) })
}

let disposable = source.observe(observer)
if let disposable = disposable {
target.lifetime.ended.observeCompleted { disposable.dispose() }
}
return disposable

return source.observe(observer, during: target.lifetime)
}

/// Binds a source to a target, updating the target's value to the latest
Expand Down Expand Up @@ -146,11 +149,7 @@ public func <~
observer = Observer(value: { [weak target] in target?.consume(Target.Value(reconstructing: $0)) })
}

let disposable = source.observe(observer)
if let disposable = disposable {
target.lifetime.ended.observeCompleted { disposable.dispose() }
}
return disposable
return source.observe(observer, during: target.lifetime)
}

/// A binding target that can be used with the `<~` operator.
Expand Down