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

Commit

Permalink
Add advanced State initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Oct 3, 2024
1 parent 60b1006 commit 94211b5
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Sources/Model/Data Flow/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public struct State<Value>: StateProtocol {
rawValue = newValue
content.update = true
StateManager.updateViews(force: forceUpdates)
writeValue?(newValue)
}
}

Expand Down Expand Up @@ -51,19 +52,38 @@ public struct State<Value>: StateProtocol {
/// The closure for initializing the state property's value.
var getInitialValue: () -> Value

/// Perform additional operations when the value changes.
var writeValue: ((Value) -> Void)?

/// The content.
let content: StateContent = .init()

/// Initialize a property representing a state in the view with an autoclosure.
/// - Parameters:
/// - wrappedValue: The wrapped value.
/// - id: An explicit identifier.
/// - forceUpdates: Whether to force update all available views when the property gets modified.
public init(wrappedValue: @autoclosure @escaping () -> Value, forceUpdates: Bool = false) {
getInitialValue = wrappedValue
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.
/// - Returns: The initial value.
func initialValue() -> Value {
Expand Down

0 comments on commit 94211b5

Please sign in to comment.