Coder Social home page Coder Social logo

bindings's Introduction

Bindings for Combine

Unidirectional binding operators and reactive extensions for Cocoa classes.

The operators

Bindings provides two operators: <~, the input binding operator, and ~>, the output binding operator.

Input bindings update the state of your UI

import Bindings
import UIKitBindings

nameLabel.reactive.text <~ viewModel.fullName

Output bindings respond to changes

nameField.reactive.edited ~> viewModel.setName
UIApplication.reactive.didBecomeActiveNotification ~> viewModel.refresh

Common bindings for system classes

In addition to the core operator definitions, Bindings also provides conveniences for using bindings with many system classes. Take a look at the following modules to see what's provided (and consider contributing!):

  • UIKitBindings for apps using UIKit (iOS, iPadOS, Catalyst and more)
  • More to come...

Reactive extensions

It's convenient to group reactive extensions into their own namespace, especially when extending types you don't own with reactive APIs.

Use the ReactiveExtensionProvider protocol to define the .reactive property on your types:

extension MyViewModel: ReactiveExtensionProvider {}

Then add reactive extensions via the Reactive type:

extension UserDefaults {
  var username: String? {
    get { string(forKey: "username") }
    set { set(newValue, forKey: "username") }
  }
}

extension Reactive where Base: UserDefaults {
  var username: NSObject.KeyValueObservingPublisher<Base, String?> {
    base.publisher(for: \.username)
  }
}

UserDefaults.standard.reactive.username.sink { username in
  print("New username: \(username)")
}

Bindings last for the lifetime of their owner

In Combine, the AnyCancellable class controls the lifetime of a subscription. Subscriptions can be automatically cancelled by using the store(in:) method:

import UIKit

class MyViewController: UIViewController {
  @IBOutlet private var usernameLabel: UILabel!
  private var subscriptions: [AnyCancellable] = []

  override func viewDidLoad() {
    super.viewDidLoad()

    UserDefaults.standard.reactive.username
      .sink { [usernameLabel] in usernameLabel?.text = $0 }
      .store(in: &subscriptions)
  }
}

Bindings encapsulates this pattern with the BindingOwner protocol, which automatically defines a subscriptions collection on all conforming classes. The BindingSink subscriber then automatically disposes of the binding subscription when its owner goes out of scope:

extension Reactive where Base: UILabel {
  var text: BindingSink<Base, String?> {
    BindingSink(owner: base) { label, newValue in
      label.text = newValue
    }
  }
}

// automatically cancelled when `usernameLabel` goes out of scope
usernameLabel.reactive.text <~ UserDefaults.standard.reactive.username

Contributing

Have a useful reactive extension in your project? Please consider contributing it back to the community!

For more details, see the CONTRIBUTING document. Thank you, contributors!

License

Bindings is Copyright (c) 2019 thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the LICENSE file.

About

thoughtbot

Bindings is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.

We love open source software! See our other projects or hire us to help build your product.

bindings's People

Contributors

sharplet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bindings's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.