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 1 commit
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
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