From 6b82a83b87bedd6cf66f47d608b8593550968ec5 Mon Sep 17 00:00:00 2001 From: Gordon Brander Date: Wed, 29 Mar 2023 11:13:08 -0400 Subject: [PATCH] Start example --- Sources/ObservableStore/ObservableStore.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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