diff --git a/Sources/Model/Data Flow/State.swift b/Sources/Model/Data Flow/State.swift index 6216223..a4d27cf 100644 --- a/Sources/Model/Data Flow/State.swift +++ b/Sources/Model/Data Flow/State.swift @@ -36,24 +36,11 @@ public struct State: StateProtocol { public var rawValue: Value { get { guard let value = content.value as? Value else { - let initialValue = getInitialValue() - let storage = StateContent.Storage(value: initialValue) - if var model = initialValue as? Model { - model.model = .init(storage: storage, force: forceUpdates) - model.setup() - content.storage = storage - content.value = model - } else { - content.storage = storage - } - return initialValue + return initialValue() } return value } nonmutating set { - if content.storage == nil { - _ = rawValue - } content.value = newValue } } @@ -77,4 +64,25 @@ public struct State: StateProtocol { self.forceUpdates = forceUpdates } + /// Get the initial value. + /// - Returns: The initial value. + func initialValue() -> Value { + let initialValue = getInitialValue() + let storage = StateContent.Storage(value: initialValue) + if var model = initialValue as? Model { + model.model = .init(storage: storage, force: forceUpdates) + model.setup() + content.storage = storage + content.value = model + } else { + content.storage = storage + } + return initialValue + } + + /// Set the storage up. + func setup() { + _ = initialValue() + } + } diff --git a/Sources/Model/Data Flow/StateProtocol.swift b/Sources/Model/Data Flow/StateProtocol.swift index 681c510..54f1e6d 100644 --- a/Sources/Model/Data Flow/StateProtocol.swift +++ b/Sources/Model/Data Flow/StateProtocol.swift @@ -12,5 +12,7 @@ protocol StateProtocol { /// The state content. var content: StateContent { get } + /// Set the storage up. + func setup() } diff --git a/Sources/View/StateWrapper.swift b/Sources/View/StateWrapper.swift index 81e94c9..0991eb8 100644 --- a/Sources/View/StateWrapper.swift +++ b/Sources/View/StateWrapper.swift @@ -69,6 +69,9 @@ struct StateWrapper: ConvenienceWidget { let content = content().storage(data: data, type: type) let storage = ViewStorage(content.pointer, content: [.mainContent: [content]]) storage.state = state + for element in state { + element.value.setup() + } return storage }