Map third party legacy state to TCA state #352
-
Hi there, I'm noticing that, for understandable reasons, there isn't a recommended way to map some TCA state to a legacy state management system. Like ReSwift for instance. What I came up doing is creating a wrapper for WithViewStore that subscribes to the legacy state manager updates, dispatching a var body: some View {
WithReViewStore(localStore) { legacyState in
localStore.send(.syncState(legacyState))
} content: { viewStore in
VStack (alignment: .center) {
Text(viewStore.someText)
}
}
} The first closure gets called every time the legacy state manager emits an update, making the whole UI redesign itself thanks to the underlaying ViewStore that invalidates the layout on changes. This is the WithReViewStore implementation. struct WithReViewStore<State: Equatable, Action: Equatable, Content> {
private let content: (ViewStore<State, Action>) -> Content
private let globalStoreSubscriber: GlobalStoreSubscriber = .init()
@ObservedObject private var viewStore: ViewStore<State, Action>
init(
_ store: ComposableArchitecture.Store<State, Action>,
onStateChange: @escaping (ViewStore<State, Action>, AppState) -> Void,
@ViewBuilder content: @escaping (ViewStore<State, Action>) -> Content
) {
self.content = content
self.viewStore = ViewStore(store)
globalStoreSubscriber.onStateChange = { [self] newState in
onStateChange(viewStore, newState)
}
}
}
extension WithReViewStore: View where Content: View {
var body: some View {
content(viewStore)
}
} As you can see it's a pretty dumb wrapper which maps the Anyways this doesn't feel that "native" and I'm wondering if there's a better way for TCA to receive updates from external providers. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey @lorenzozanotto, I actually don't know of a way of improving on your current technique 😬. In fact, @stephencelis and I have used a similar technique with a client we consulted for in order to ween them off of ReSwift and onto an in-house, early prototype of TCA (3 years ago!). Once we had this set up we were able to slowly convert old ReSwift features to TCA while building new features in TCA. Good luck! |
Beta Was this translation helpful? Give feedback.
-
Nice 😃 I feel like having this approach would make clear which components are still relying on a legacy state. I'll update this thread if I find a more Composable way tho! Thank you |
Beta Was this translation helpful? Give feedback.
Hey @lorenzozanotto, I actually don't know of a way of improving on your current technique 😬. In fact, @stephencelis and I have used a similar technique with a client we consulted for in order to ween them off of ReSwift and onto an in-house, early prototype of TCA (3 years ago!).
Once we had this set up we were able to slowly convert old ReSwift features to TCA while building new features in TCA.
Good luck!