Add set up function to model
This commit is contained in:
parent
07e433fa27
commit
0973ea579d
@ -49,6 +49,11 @@ public protocol Model {
|
|||||||
|
|
||||||
/// Data about the model's state value.
|
/// Data about the model's state value.
|
||||||
var model: ModelData? { get set }
|
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 {
|
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.
|
/// Update the model.
|
||||||
/// - Parameter setModel: Update the model in this closure.
|
/// - Parameter setModel: Update the model in this closure.
|
||||||
public func setModel(_ setModel: (inout Self) -> Void) {
|
public func setModel(_ setModel: (inout Self) -> Void) {
|
||||||
guard let data = model else {
|
guard let data = model else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var model = self
|
var model = StateManager.getState(id: data.id) as? Self ?? self
|
||||||
setModel(&model)
|
setModel(&model)
|
||||||
StateManager.setState(id: data.id, value: model)
|
StateManager.setState(id: data.id, value: model)
|
||||||
StateManager.updateState(id: data.id)
|
StateManager.updateState(id: data.id)
|
||||||
|
|||||||
@ -39,6 +39,7 @@ public struct State<Value>: StateProtocol {
|
|||||||
let initialValue = getInitialValue()
|
let initialValue = getInitialValue()
|
||||||
if var model = initialValue as? Model {
|
if var model = initialValue as? Model {
|
||||||
model.model = .init(id: id, force: forceUpdates)
|
model.model = .init(id: id, force: forceUpdates)
|
||||||
|
model.setup()
|
||||||
StateManager.setState(id: id, value: model)
|
StateManager.setState(id: id, value: model)
|
||||||
StateManager.addConstantID(id)
|
StateManager.addConstantID(id)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user