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

Mark Property and MutableProperty as property wrappers. #781

Merged
merged 3 commits into from
May 22, 2020
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
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
31 changes: 31 additions & 0 deletions Sources/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>: PropertyProtocol {
private let _value: () -> Value

Expand All @@ -478,6 +479,16 @@ public final class Property<Value>: PropertyProtocol {
return _value()
}

@inlinable
public var wrappedValue: Value {
return value
}

@inlinable
public var projectedValue: Property<Value> {
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.
Expand Down Expand Up @@ -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<Value>: ComposableMutablePropertyProtocol {
private let token: Lifetime.Token
private let observer: Signal<Value, Never>.Observer
Expand All @@ -663,6 +675,17 @@ public final class MutableProperty<Value>: ComposableMutablePropertyProtocol {
set { modify { $0 = newValue } }
}

@inlinable
public var wrappedValue: Value {
get { value }
set { value = newValue }
}

@inlinable
public var projectedValue: MutableProperty<Value> {
return self
}

/// The lifetime of the property.
public let lifetime: Lifetime

Expand Down Expand Up @@ -696,6 +719,14 @@ public final class MutableProperty<Value>: 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:
Expand Down
31 changes: 31 additions & 0 deletions Tests/ReactiveSwiftTests/PropertySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down