Skip to content

Commit

Permalink
Implement properties after factoring out protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Jan 16, 2015
1 parent e7748d1 commit aa801c4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
4 changes: 4 additions & 0 deletions ReactiveCocoa.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@
D08C54B71A69A3DB00AD8286 /* Event.swift in Sources */ = {isa = PBXBuildFile; fileRef = D08C54B51A69A3DB00AD8286 /* Event.swift */; };
D08C54B81A69A9D000AD8286 /* SignalProducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D08C54B21A69A2AC00AD8286 /* SignalProducer.swift */; };
D08C54B91A69A9D100AD8286 /* SignalProducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D08C54B21A69A2AC00AD8286 /* SignalProducer.swift */; };
D08C54BA1A69C54300AD8286 /* Property.swift in Sources */ = {isa = PBXBuildFile; fileRef = D08C54B01A69A2AC00AD8286 /* Property.swift */; };
D08C54BB1A69C54400AD8286 /* Property.swift in Sources */ = {isa = PBXBuildFile; fileRef = D08C54B01A69A2AC00AD8286 /* Property.swift */; };
D097CE3919EF7AF5005A201C /* ColdSignal.swift in Sources */ = {isa = PBXBuildFile; fileRef = D097CE3619EF7AF5005A201C /* ColdSignal.swift */; };
D097CE3A19EF7AF5005A201C /* ColdSignal.swift in Sources */ = {isa = PBXBuildFile; fileRef = D097CE3619EF7AF5005A201C /* ColdSignal.swift */; };
D097CE3B19EF7AF5005A201C /* HotSignal.swift in Sources */ = {isa = PBXBuildFile; fileRef = D097CE3719EF7AF5005A201C /* HotSignal.swift */; };
Expand Down Expand Up @@ -1678,6 +1680,7 @@
D037656219EDA41200A782A9 /* RACCommand.m in Sources */,
D037658819EDA41200A782A9 /* RACErrorSignal.m in Sources */,
D03765F619EDA41200A782A9 /* RACSubscriptingAssignmentTrampoline.m in Sources */,
D08C54BA1A69C54300AD8286 /* Property.swift in Sources */,
D037661219EDA41200A782A9 /* RACUnit.m in Sources */,
D03765A019EDA41200A782A9 /* RACKVOTrampoline.m in Sources */,
D037650A19EDA41200A782A9 /* NSInvocation+RACTypeParsing.m in Sources */,
Expand Down Expand Up @@ -1813,6 +1816,7 @@
D037654B19EDA41200A782A9 /* NSUserDefaults+RACSupport.m in Sources */,
D037660F19EDA41200A782A9 /* RACUnarySequence.m in Sources */,
D097CE3C19EF7AF5005A201C /* HotSignal.swift in Sources */,
D08C54BB1A69C54400AD8286 /* Property.swift in Sources */,
D03765FF19EDA41200A782A9 /* RACTargetQueueScheduler.m in Sources */,
D03765DF19EDA41200A782A9 /* RACSignalSequence.m in Sources */,
D037656D19EDA41200A782A9 /* RACDelegateProxy.m in Sources */,
Expand Down
79 changes: 66 additions & 13 deletions ReactiveCocoa/Swift/Property.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,86 @@
/// An abstract class representing a property of type T that allows observation
/// of its changes.
public class Property<T> {
/// Represents a property that allows observation of its changes.
public protocol PropertyType {
typealias Value

/// The current value of the property.
public var value: T { get }
var value: Value { get }

/// A producer for Signals that will send the property's current value,
/// followed by all changes over time, then complete when the property has
/// deinitialized.
public let producer: SignalProducer<T>
/// followed by all changes over time.
var producer: SignalProducer<Value> { get }
}

/// Represents a read-only view to a property of type T that allows observation
/// of its changes.
public struct PropertyOf<T>: PropertyType {
public typealias Value = T

private let _value: () -> T
private let _producer: () -> SignalProducer<T>

public var value: T {
return _value()
}

public var producer: SignalProducer<T> {
return _producer()
}

/// Keeps this class abstract.
private init()
/// Initializes the receiver as a wrapper around the given property.
public init<P: PropertyType where P.Value == T>(property: P) {
_value = { property.value }
_producer = { property.producer }
}
}

/// A mutable property of type T that allows observation of its changes.
///
/// Instances of this class are thread-safe.
public final class MutableProperty<T>: Property<T> {
public final class MutableProperty<T>: PropertyType {
public typealias Value = T

private let observer: Signal<T>.Observer

/// The current value of the property.
///
/// Setting this to a new value will notify all observers of any Signals
/// created from the `values` producer.
public override var value: T { get set }
public var value: T {
get {
return producer.first().value()!
}

set(x) {
sendNext(observer, x)
}
}

/// A producer for Signals that will send the property's current value,
/// followed by all changes over time, then complete when the property has
/// deinitialized.
public let producer: SignalProducer<T>

/// Initializes the property with the given value to start.
public init(_ initialValue: T)
public init(_ initialValue: T) {
let (producer, observer) = SignalProducer<T>.buffer(1)
self.producer = producer
self.observer = observer

value = initialValue
}

deinit {
sendCompleted(observer)
}
}

extension MutableProperty: SinkType {}
extension MutableProperty: SinkType {
public func put(value: T) {
self.value = value
}
}

/*
infix operator <~ {
associativity right
precedence 90
Expand Down Expand Up @@ -59,3 +111,4 @@ public func <~ <T>(property: MutableProperty<T>, producer: SignalProducer<T>)
/// The binding will automatically terminate when either property is
/// deinitialized.
public func <~ <T>(destinationProperty: MutableProperty<T>, sourceProperty: Property<T>)
*/
2 changes: 1 addition & 1 deletion ReactiveCocoa/Swift/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public struct SignalProducer<T> {
///
/// After an `Error` or `Completed` event has been added to the buffer, the
/// observer will not add any further events.
public static func buffer(capacity: Int = Int.max) -> (SignalProducer, Signal<T>.Observer) {
public static func buffer(_ capacity: Int = Int.max) -> (SignalProducer, Signal<T>.Observer) {
precondition(capacity >= 0)

let lock = NSRecursiveLock()
Expand Down

0 comments on commit aa801c4

Please sign in to comment.