diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e21437f0..4247a9b93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,25 @@ # master *Please add new entries at the top.* -1. Joining an empty sequence of producers can now send an event on the joined signal producer by providing the `noUpstreamSentinel` parameter. This becomes relevant, when the sequence of producers is calculated from some other Signal and the signal resulting from the joined producers is observed. If no event is sent only when the producers sequence is empty, then the observer gets stalled and e.g. the ui won't update. +1. `Property` and `MutableProperty` can now be used as property wrapper. Note that they remain a reference type container, so it may not be appropriate to use them in types requiring value semantics. (#781) + ```swift + class ViewModel { + @MutableProperty var count: Int = 0 + + func subscribe() { + self.$count.producer.startWithValues { + print("`count` has changed to \(count)") + } + } + + func increment() { + print("count prior to increment: \(count)") + self.$count.modify { $0 += 1 } + } + } + ``` + +1. Joining an empty sequence of producers can now send an event on the joined signal producer by providing the `noUpstreamSentinel` parameter. This becomes relevant, when the sequence of producers is calculated from some other Signal and the signal resulting from the joined producers is observed. If no event is sent only when the producers sequence is empty, then the observer gets stalled and e.g. the ui won't update. (#774, kudos to @rocketnik) # 6.2.1 diff --git a/Sources/Property.swift b/Sources/Property.swift index 8eba65e5e..7ddec4d76 100644 --- a/Sources/Property.swift +++ b/Sources/Property.swift @@ -470,6 +470,7 @@ extension PropertyProtocol where Value == Bool { /// deinitializing. /// /// Note that composed properties do not retain any of its sources. +@propertyWrapper public final class Property: PropertyProtocol { private let _value: () -> Value @@ -478,6 +479,16 @@ public final class Property: PropertyProtocol { return _value() } + @inlinable + public var wrappedValue: Value { + return value + } + + @inlinable + public var projectedValue: Property { + return self + } + /// 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 or has no further changes. @@ -649,6 +660,7 @@ extension Property where Value: OptionalProtocol { /// A mutable property of type `Value` that allows observation of its changes. /// /// Instances of this class are thread-safe. +@propertyWrapper public final class MutableProperty: ComposableMutablePropertyProtocol { private let token: Lifetime.Token private let observer: Signal.Observer @@ -663,6 +675,17 @@ public final class MutableProperty: ComposableMutablePropertyProtocol { set { modify { $0 = newValue } } } + @inlinable + public var wrappedValue: Value { + get { value } + set { value = newValue } + } + + @inlinable + public var projectedValue: MutableProperty { + return self + } + /// The lifetime of the property. public let lifetime: Lifetime @@ -696,6 +719,14 @@ public final class MutableProperty: ComposableMutablePropertyProtocol { box = PropertyBox(initialValue) } + /// Initializes a mutable property that first takes on `initialValue` + /// + /// - parameters: + /// - initialValue: Starting value for the mutable property. + public convenience init(wrappedValue: Value) { + self.init(wrappedValue) + } + /// Atomically replaces the contents of the variable. /// /// - parameters: diff --git a/Tests/ReactiveSwiftTests/PropertySpec.swift b/Tests/ReactiveSwiftTests/PropertySpec.swift index ca879a038..745e84b99 100644 --- a/Tests/ReactiveSwiftTests/PropertySpec.swift +++ b/Tests/ReactiveSwiftTests/PropertySpec.swift @@ -310,9 +310,40 @@ class PropertySpec: QuickSpec { } } } + + it("can be used as property wrapper") { + struct Container { + @MutableProperty var counter = 0 + } + + let container = Container() + expect(container.counter) == 0 + + container.counter += 1 + expect(container.counter) == 1 + + container.$counter.modify { $0 += 2 } + expect(container.counter) == 3 + } } describe("Property") { + it("can be used as property wrapper") { + struct Container { + @Property var counter: Int + } + + let mutableCounter = MutableProperty(0) + let container = Container(counter: Property(capturing: mutableCounter)) + expect(container.counter) == 0 + + mutableCounter.value = 1 + expect(container.counter) == 1 + + mutableCounter.modify { $0 += 2 } + expect(container.counter) == 3 + } + describe("constant property") { it("should have the value given at initialization") { let constantProperty = Property(value: initialPropertyValue)