Add advanced State initializer
This commit is contained in:
parent
60b100626b
commit
94211b5c87
@ -20,6 +20,7 @@ public struct State<Value>: StateProtocol {
|
|||||||
rawValue = newValue
|
rawValue = newValue
|
||||||
content.update = true
|
content.update = true
|
||||||
StateManager.updateViews(force: forceUpdates)
|
StateManager.updateViews(force: forceUpdates)
|
||||||
|
writeValue?(newValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,19 +52,38 @@ public struct State<Value>: StateProtocol {
|
|||||||
/// The closure for initializing the state property's value.
|
/// The closure for initializing the state property's value.
|
||||||
var getInitialValue: () -> Value
|
var getInitialValue: () -> Value
|
||||||
|
|
||||||
|
/// Perform additional operations when the value changes.
|
||||||
|
var writeValue: ((Value) -> Void)?
|
||||||
|
|
||||||
/// The content.
|
/// The content.
|
||||||
let content: StateContent = .init()
|
let content: StateContent = .init()
|
||||||
|
|
||||||
/// Initialize a property representing a state in the view with an autoclosure.
|
/// Initialize a property representing a state in the view with an autoclosure.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - wrappedValue: The wrapped value.
|
/// - wrappedValue: The wrapped value.
|
||||||
/// - id: An explicit identifier.
|
|
||||||
/// - 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.
|
||||||
public init(wrappedValue: @autoclosure @escaping () -> Value, forceUpdates: Bool = false) {
|
public init(wrappedValue: @autoclosure @escaping () -> Value, forceUpdates: Bool = false) {
|
||||||
getInitialValue = wrappedValue
|
getInitialValue = wrappedValue
|
||||||
self.forceUpdates = forceUpdates
|
self.forceUpdates = forceUpdates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Initialize a property representing a state in the view with an explicit closure.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - wrappedValue: Get the wrapped value.
|
||||||
|
/// - writeValue: Perform additional operations when the value changes.
|
||||||
|
/// - forceUpdates: Whether to force update all available views when the property gets modified.
|
||||||
|
///
|
||||||
|
/// This initializer can be used to get data from the disk.
|
||||||
|
public init(
|
||||||
|
wrappedValue: @escaping () -> Value,
|
||||||
|
writeValue: ((Value) -> Void)? = nil,
|
||||||
|
forceUpdates: Bool = false
|
||||||
|
) {
|
||||||
|
getInitialValue = wrappedValue
|
||||||
|
self.writeValue = writeValue
|
||||||
|
self.forceUpdates = forceUpdates
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the initial value.
|
/// Get the initial value.
|
||||||
/// - Returns: The initial value.
|
/// - Returns: The initial value.
|
||||||
func initialValue() -> Value {
|
func initialValue() -> Value {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user