Skip to content

Commit

Permalink
Property updates
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Apr 20, 2017
1 parent e67d8bd commit c854aa3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Sources/Foundation/NSObject+Jetpack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fileprivate class PropertyActionHandler<Base: AnyObject, T>: NSObject {
self.getter = getter
self.base = base
self.cleanup = cleanup
property = Property(signal: signal) {
property = Property(signal.asObserver) {
getter(base)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Observable+Commit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Observable {
}

if immediateResult != nil {
let prop = Property<ValueType>(signal: signal) {
let prop = Property<ValueType>(signal.asObserver) {
return immediateResult!
}
return (prop.asObserver, disposable)
Expand Down
18 changes: 14 additions & 4 deletions Sources/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@ import Foundation
public final class Property<T>: Observable {
public typealias ValueType = T

private let signal: Signal<T>
private let observer: Observer<T>
private let getter: ()->T

public var value: T {
return getter()
}

public init(signal: Signal<T>, getter: @escaping ()->T) {
public init(_ observer: Observer<T>, getter: @escaping ()->T) {
self.getter = getter
self.signal = signal
self.observer = observer
}

public convenience init(constant: T) {
self.init(Observer.from(constant), getter: { constant })
}

@discardableResult
public func subscribe(_ observer: @escaping (T) -> Void) -> Disposable {
observer(value)
return signal.subscribe(observer)
return self.observer.subscribe(observer)
}

public func map<U>(_ transform: @escaping (T) -> U) -> Property<U> {
return Property<U>(observer.map(transform)) {
transform(self.getter())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class State<T>: Observable, Bindable {
let signal = Signal<T>()
var v = value

property = Property(signal: signal) {
property = Property(signal.asObserver) {
return v
}
variable = Variable(setter: {
Expand Down

0 comments on commit c854aa3

Please sign in to comment.