Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created June 28, 2020 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/dff0afaf0e72295cd970f487c42d5230 to your computer and use it in GitHub Desktop.
Save JadenGeller/dff0afaf0e72295cd970f487c42d5230 to your computer and use it in GitHub Desktop.
Delayed Set Binding
@propertyWrapper
struct DebouncedBinding<Value>: DynamicProperty {
@Binding var remote: Value
@State var local: Value? = nil
init(_ remote: Binding<Value>) {
self._remote = remote
}
var wrappedValue: Value {
get {
local ?? remote
}
nonmutating set {
local = newValue
}
}
var projectedValue: Binding<Value> {
Binding(get: { self.wrappedValue }, set: { self.wrappedValue = $0 })
}
func flush() {
if let local = local {
remote = local
self.local = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment