Add set up function to model

This commit is contained in:
david-swift 2024-07-29 16:14:42 +02:00
parent 07e433fa27
commit 0973ea579d
2 changed files with 13 additions and 1 deletions

View File

@ -49,6 +49,11 @@ public protocol Model {
/// Data about the model's state value.
var model: ModelData? { get set }
/// Set the model up.
///
/// At the point this function gets called, the model data is available.
/// Therefore, you can use it for initializing callbacks of children.
mutating func setup()
}
@ -64,13 +69,19 @@ public struct ModelData {
extension Model {
/// Set the model up.
///
/// At the point this function gets called, the model data is available.
/// Therefore, you can use it for initializing callbacks of children.
mutating func setup() { }
/// Update the model.
/// - Parameter setModel: Update the model in this closure.
public func setModel(_ setModel: (inout Self) -> Void) {
guard let data = model else {
return
}
var model = self
var model = StateManager.getState(id: data.id) as? Self ?? self
setModel(&model)
StateManager.setState(id: data.id, value: model)
StateManager.updateState(id: data.id)

View File

@ -39,6 +39,7 @@ public struct State<Value>: StateProtocol {
let initialValue = getInitialValue()
if var model = initialValue as? Model {
model.model = .init(id: id, force: forceUpdates)
model.setup()
StateManager.setState(id: id, value: model)
StateManager.addConstantID(id)
} else {