Created
May 30, 2020 20:08
-
-
Save pteasima/5d2ef54a7428a3e351065d73b0c719db to your computer and use it in GitHub Desktop.
MissionControl - Debug tools for ComposableArchitecture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MissionControlState<State> { | |
var currentState: State | |
} | |
enum MissionControlAction<Action> { | |
case app(Action) | |
case lldb | |
} | |
struct MissionControl<State, Action, Environment, Content: View>: View { | |
let store: Store<MissionControlState<State>, MissionControlAction<Action>> | |
@ObservedObject var viewStore: ViewStore<ViewState, MissionControlAction<Action>> | |
let content: Content | |
init( | |
initialState: State, | |
reducer: Reducer<State, Action, Environment>, | |
environment: Environment, | |
content: @escaping (Store<State, Action>) -> Content, | |
lldbHook: @escaping (inout State, Environment, inout [Effect<Action, Never>]) -> Void) { | |
let store = Store<MissionControlState<State>, MissionControlAction<Action>>( | |
initialState: MissionControlState( | |
currentState: initialState), | |
reducer: .combine([ | |
.init { state, action, environment in | |
switch action { | |
case .lldb: | |
var effects: [Effect<Action, Never>] = [] | |
lldbHook(&state.currentState, environment, &effects) | |
return Effect.concatenate(effects) | |
.map(MissionControlAction<Action>.app) | |
case .app: | |
return .none | |
} | |
}, | |
reducer.pullback( | |
state: \.currentState, | |
action: /MissionControlAction.app, | |
environment: { $0 }), | |
]), | |
environment: environment | |
) | |
self.store = store | |
viewStore = ViewStore(store.scope(state: { _ in .init() })) | |
self.content = content(store.scope(state: \.currentState, action: MissionControlAction.app)) | |
} | |
var body: some View { | |
ZStack { | |
content | |
VStack(spacing: 20) { | |
Text("Im a debug overlay.") | |
Button(action: { | |
self.viewStore.send(.lldb) | |
}) { | |
Text("Enter Script Mode") | |
.font(.largeTitle) | |
} | |
} | |
.padding() | |
.background(Color.init(.systemGroupedBackground)) | |
} | |
} | |
struct ViewState:Equatable { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Btw I dont think theres a reason not to use
WithViewStore
, its a property either for historical reasons or because I wrote it at 5am 😀