Add support for non-updating state properties
This commit is contained in:
parent
ee92f63f86
commit
681a51110d
@ -18,8 +18,10 @@ public struct State<Value>: StateProtocol {
|
|||||||
}
|
}
|
||||||
nonmutating set {
|
nonmutating set {
|
||||||
rawValue = newValue
|
rawValue = newValue
|
||||||
content.update = true
|
if !blockUpdates {
|
||||||
StateManager.updateViews(force: forceUpdates)
|
content.update = true
|
||||||
|
StateManager.updateViews(force: forceUpdates)
|
||||||
|
}
|
||||||
writeValue?(newValue)
|
writeValue?(newValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,7 +49,10 @@ public struct State<Value>: StateProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Whether to force update the views when the value changes.
|
/// Whether to force update the views when the value changes.
|
||||||
var forceUpdates: Bool
|
var forceUpdates = false
|
||||||
|
|
||||||
|
/// Whether to block updates.
|
||||||
|
var blockUpdates = false
|
||||||
|
|
||||||
/// The closure for initializing the state property's value.
|
/// The closure for initializing the state property's value.
|
||||||
var getInitialValue: () -> Value
|
var getInitialValue: () -> Value
|
||||||
@ -67,21 +72,35 @@ public struct State<Value>: StateProtocol {
|
|||||||
self.forceUpdates = forceUpdates
|
self.forceUpdates = forceUpdates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Initialize a property representing a state in the view with an autoclosure.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - wrappedValue: The wrapped value.
|
||||||
|
/// - blockUpdates: Whether updates to this state value should not update the UI.
|
||||||
|
///
|
||||||
|
/// This can be useful for storing data and reading this data on special occasions, e.g. on startup.
|
||||||
|
public init(wrappedValue: @autoclosure @escaping () -> Value, blockUpdates: Bool) {
|
||||||
|
getInitialValue = wrappedValue
|
||||||
|
self.blockUpdates = blockUpdates
|
||||||
|
}
|
||||||
|
|
||||||
/// Initialize a property representing a state in the view with an explicit closure.
|
/// Initialize a property representing a state in the view with an explicit closure.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - wrappedValue: Get the wrapped value.
|
/// - wrappedValue: Get the wrapped value.
|
||||||
/// - writeValue: Perform additional operations when the value changes.
|
/// - writeValue: Perform additional operations when the value changes.
|
||||||
/// - forceUpdates: Whether to force update all available views when the property gets modified.
|
/// - forceUpdates: Whether to force update all available views when the property gets modified.
|
||||||
|
/// - blockUpdates: Whether updates to this state value should not update the UI.
|
||||||
///
|
///
|
||||||
/// This initializer can be used to get data from the disk.
|
/// This initializer can be used e.g. to get data from the disk.
|
||||||
public init(
|
public init(
|
||||||
wrappedValue: @escaping () -> Value,
|
wrappedValue: @escaping () -> Value,
|
||||||
writeValue: ((Value) -> Void)? = nil,
|
writeValue: ((Value) -> Void)? = nil,
|
||||||
forceUpdates: Bool = false
|
forceUpdates: Bool = false,
|
||||||
|
blockUpdates: Bool = false
|
||||||
) {
|
) {
|
||||||
getInitialValue = wrappedValue
|
getInitialValue = wrappedValue
|
||||||
self.writeValue = writeValue
|
self.writeValue = writeValue
|
||||||
self.forceUpdates = forceUpdates
|
self.forceUpdates = forceUpdates
|
||||||
|
self.blockUpdates = blockUpdates
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the initial value.
|
/// Get the initial value.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user