Skip to content

Instantly share code, notes, and snippets.

@vnsam
Created January 14, 2020 04:11
Show Gist options
  • Save vnsam/c9357f577c47d0877e3b8ff12138dc7f to your computer and use it in GitHub Desktop.
Save vnsam/c9357f577c47d0877e3b8ff12138dc7f to your computer and use it in GitHub Desktop.
A simple implementation of one way binding of property in Swift.
class Bindable<Value> {
typealias Binder = (Value) -> Void
var binder: Binder?
var value: Value {
didSet {
DispatchQueue.main.async { [weak self] in
guard let self = self else {
return
}
self.binder?(self.value)
}
}
}
init(_ value: Value) {
self.value = value
}
func bind(_ binder: Binder?) {
self.binder = binder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment