diff --git a/Sources/ObservableStore/ObservableStore.swift b/Sources/ObservableStore/ObservableStore.swift index 4172523..99850c8 100644 --- a/Sources/ObservableStore/ObservableStore.swift +++ b/Sources/ObservableStore/ObservableStore.swift @@ -93,6 +93,13 @@ extension ModelProtocol { } } +public protocol ModelFactoryProtocol: ModelProtocol { + static func start( + state: Self, + environment: Self.Environment + ) -> Update +} + /// Update represents a state change, together with an `Fx` publisher, /// and an optional `Transaction`. public struct Update { @@ -297,6 +304,17 @@ where Model: ModelProtocol } } +extension Store where Model: ModelFactoryProtocol { + public convenience init( + initial: Model, + environment: Model.Environment + ) { + let update = Model.start(state: initial, environment: environment) + self.init(state: update.state, environment: environment) + self.subscribe(to: update.fx) + } +} + public struct ViewStore: StoreProtocol { private var _send: (ViewModel.Action) -> Void public var state: ViewModel