feat: Begins more work on async integration
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
|
||||
/// A property wrapper that tracks changes of a property.
|
||||
///
|
||||
/// This allows values to only publish changes if they have changed since the
|
||||
/// last time they were recieved.
|
||||
@propertyWrapper
|
||||
public struct TrackedChanges<Value> {
|
||||
|
||||
|
||||
/// The current tracking state.
|
||||
private var tracking: TrackingState
|
||||
|
||||
/// The current wrapped value.
|
||||
private var value: Value
|
||||
|
||||
/// Used to check if a new value is equal to an old value.
|
||||
private var isEqual: (Value, Value) -> Bool
|
||||
|
||||
|
||||
/// Access to the underlying property that we are wrapping.
|
||||
public var wrappedValue: Value {
|
||||
get { value }
|
||||
set {
|
||||
@@ -16,7 +25,13 @@ public struct TrackedChanges<Value> {
|
||||
tracking = .needsProcessed
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Create a new property that tracks it's changes.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - wrappedValue: The value that we are wrapping.
|
||||
/// - needsProcessed: Whether this value needs processed (default = false).
|
||||
/// - isEqual: Method to compare old values against new values.
|
||||
public init(
|
||||
wrappedValue: Value,
|
||||
needsProcessed: Bool = false,
|
||||
@@ -26,12 +41,18 @@ public struct TrackedChanges<Value> {
|
||||
self.tracking = needsProcessed ? .needsProcessed : .hasProcessed
|
||||
self.isEqual = isEqual
|
||||
}
|
||||
|
||||
|
||||
/// Represents whether a wrapped value has changed and needs processed or not.
|
||||
enum TrackingState {
|
||||
|
||||
/// The state when nothing has changed and we've already processed the current value.
|
||||
case hasProcessed
|
||||
|
||||
/// The state when the value has changed and has not been processed yet.
|
||||
case needsProcessed
|
||||
}
|
||||
|
||||
|
||||
/// Check whether the value needs processed.
|
||||
public var needsProcessed: Bool {
|
||||
get { tracking == .needsProcessed }
|
||||
set {
|
||||
@@ -42,7 +63,7 @@ public struct TrackedChanges<Value> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public var projectedValue: Self {
|
||||
get { self }
|
||||
set { self = newValue }
|
||||
@@ -54,7 +75,12 @@ extension TrackedChanges: Equatable where Value: Equatable {
|
||||
lhs.wrappedValue == rhs.wrappedValue
|
||||
&& lhs.needsProcessed == rhs.needsProcessed
|
||||
}
|
||||
|
||||
|
||||
/// Create a new property that tracks it's changes, using the default equality check.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - wrappedValue: The value that we are wrapping.
|
||||
/// - needsProcessed: Whether this value needs processed (default = false).
|
||||
public init(
|
||||
wrappedValue: Value,
|
||||
needsProcessed: Bool = false
|
||||
|
||||
Reference in New Issue
Block a user