Created
January 14, 2020 04:11
-
-
Save vnsam/c9357f577c47d0877e3b8ff12138dc7f to your computer and use it in GitHub Desktop.
A simple implementation of one way binding of property in Swift.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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