Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Easier observing of bindings changing data
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Mar 31, 2024
1 parent a53b4e0 commit c596504
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Sources/Adwaita/Model/Data Flow/Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public struct Binding<Value> {
}
nonmutating set {
setValue(newValue)
for handler in handlers {
handler(newValue)
}
}
}

Expand All @@ -64,6 +67,8 @@ public struct Binding<Value> {
private let getValue: () -> Value
/// The closure for settings the value.
private let setValue: (Value) -> Void
/// Handlers observing whether the binding changes.
private var handlers: [(Value) -> Void] = []

/// Get a property of any content of a `Binding` as a `Binding`.
/// - Parameter dynamicMember: The path to the member.
Expand Down Expand Up @@ -96,4 +101,13 @@ public struct Binding<Value> {
}
}

/// Observe whether data is changed over this binding.
/// - Parameter handler: The handler.
/// - Returns: The binding.
public func onSet(_ handler: @escaping (Value) -> Void) -> Self {
var newSelf = self
newSelf.handlers.append(handler)
return newSelf
}

}

0 comments on commit c596504

Please sign in to comment.