Signals icon indicating copy to clipboard operation
Signals copied to clipboard

Add "Property" struct

Open nalexn opened this issue 6 years ago • 1 comments

This adds an observable property inspired by Property from ReactiveSwift, and BehaviorSubject from RxSwift.

This implementation of the observable property has an advantage over analogs from RxSwift and ReactiveSwift because it supports defining different Access Levels to setter and getter, which is impossible to achieve in those libraries.

Aiming to maintain the philosophy of simplicity of the Signal, this implementation of the Property doesn't replicate all the functional features inherent to the FRP libraries, such as mapping the value or combining with other properties. Instead, it focuses on the observation feature only.

Signals is a library for creating and observing events. It replaces delegates, actions and NSNotificationCenter with something much more powerful and elegant.

With this addition, the Signals would also offer a replacement for the Key-Value-Observation.

class MyClass {
    private(set) var property = Property(value: "abc")

    func mutateProperty() {
        property.value = "xyz"
    }
}

let object = MyClass()
print("\(object.property.value)") // getter returns "abc"

// Attempt to change the value from outside the class:
object.property.value = "qwe" // Compiler error - setter is private

// Subscribing for new values
object.property.observe(with: self) { value in
    print("\(value)")
}

object.mutateProperty() // triggers the notification with the new value "xyz"

nalexn avatar Aug 25 '19 14:08 nalexn

Hey @artman, please have a look at this. The tests I've added for the Property all pass, but I can see just like the other pull requests, this one fails Travis CI test for iPhone 6

nalexn avatar Aug 25 '19 15:08 nalexn