You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I really like TCA and the reducer pattern that makes state updates consistent & easier to track/test, and the subscriptions easier to manage/cancel.
But in a bigger project sometimes we have logic that doesn't directly affect the UI (e.g. business logic to produce intermediate states). We could still use the store from TCA but then that means send() & state updates happen on the main thread which isn't ideal for performance.
So I created a super simple minimalist version of TCA/Redux-like store that's just for logic running on background threads based on actors. The APIs are very close to TCA Store, though it doesn't have any advanced features like scoping, navigation tools, advanced observability tool (some of these are not necessary for none UI use cases anyways), but it has the basics (state, action, reducer, cancellable side effects). It doesn't depend on macros either. Here is a unit test showing how it looks like: https://github.com/ba01ei/swift-background-store/blob/main/Tests/BackgroundStore/BackgroundStoreTests.swift
I'm curious how others think about this approach? Potentially TCA might support background thread use cases in the future so once that happens hopefully code based on this simple solution can migrate to that easily.
I know maybe another option is to use swift-sharing and implement a shared key with custom load/subscribe logic, but I just thought that managing subscriptions through a reducer is nice, especially when the logic gets complex.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I really like TCA and the reducer pattern that makes state updates consistent & easier to track/test, and the subscriptions easier to manage/cancel.
But in a bigger project sometimes we have logic that doesn't directly affect the UI (e.g. business logic to produce intermediate states). We could still use the store from TCA but then that means
send()
& state updates happen on the main thread which isn't ideal for performance.So I created a super simple minimalist version of TCA/Redux-like store that's just for logic running on background threads based on actors. The APIs are very close to TCA Store, though it doesn't have any advanced features like scoping, navigation tools, advanced observability tool (some of these are not necessary for none UI use cases anyways), but it has the basics (state, action, reducer, cancellable side effects). It doesn't depend on macros either. Here is a unit test showing how it looks like:
https://github.com/ba01ei/swift-background-store/blob/main/Tests/BackgroundStore/BackgroundStoreTests.swift
I'm curious how others think about this approach? Potentially TCA might support background thread use cases in the future so once that happens hopefully code based on this simple solution can migrate to that easily.
I know maybe another option is to use swift-sharing and implement a shared key with custom load/subscribe logic, but I just thought that managing subscriptions through a reducer is nice, especially when the logic gets complex.
Beta Was this translation helpful? Give feedback.
All reactions