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

Add "Property" struct #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add "Property" struct #79

wants to merge 2 commits into from

Conversation

nalexn
Copy link

@nalexn nalexn commented Aug 25, 2019

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
Copy link
Author

nalexn commented Aug 25, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant