Comments (2)
If I understand collection, CollectionState should only be there in case you're authenticated, right?
If this is the case, you could use something like
enum AuthenticationState {
case notAuthenticated
case authenticating
case authenticated(CollectionState)
}
With Prisms (https://github.com/SwiftRex/SwiftRex/blob/develop/docs/markdown/ActionEnumProperties.md) you will have a property appState.authenticationState.authenticated
that returns CollectionState?
, being nil when you're not authenticated, and not-nil when you are. Your Middleware could be either generic over CollectionState?
(optional) state, or you could "lift" it from non-optional to optional... There will be a version of this lift in the very next version because I also needed something similar recently... In meantime you can write your own lift or use SomeMiddleware<SomeAction, SomeAction, CollectionState?>.
The problem with this approach is that you can't cache things if you're not authenticated. For example, let's imagine that the user logs off. Do you want your collection state to be reset or keep it in cache? If you want to ensure that it's reset, then the enum solution is perfect because it's IMPOSSIBLE to represent a state where you are not authenticated and there are collection items in memory :)
However, sometimes you may want to keep them there. If this is the case, "isAuthenticated" (boolean or enum, whatever) and CollectionState must be siblings, like in your example. In that case, your middleware state must have both. This can be achieve by two ways:
- tuple
struct AppState {
var authenticatedState: AuthenticationState
var collectionState: CollectionState
var moreProperties: String
var andMore: String
}
// class MyMiddleware has StateType = (authenticatedState: AuthenticationState, collectionState: CollectionState)
MyMiddleware().lift(state: {
(authenticatedState: $0.authenticatedState, collectionState: $0.collectionState)
})
- calculated var
extension AppState {
struct CollectionContext {
var isAuthenticated: Bool
var collectionState: CollectionState
}
var collectionContext: CollectionContext {
CollectionContext(
isAuthenticated: self.authenticatedState == .authenticated,
collectionState: self. collectionState
}
}
// class MyMiddleware has StateType = AppState.CollectionContext
MyMiddleware().lift(state: \.collectionContext)
There's also a third way, using the GatedMiddleware. If you want only to guard state.isAuthenticated, you can "gate" it by some piece of state:
https://github.com/SwiftRex/GatedMiddleware/blob/master/Sources/GatedMiddleware/GatedMiddleware.swift#L461
That way, you first lift your middleware that is generic over CollectionState
to the AppState, then you apply .gated(state: \.isAuthenticated)
and that's it!
Please let me know which one works best for you and in case it's not completely clear, I can go deeper in the examples.
Also, feel free to join our Slack channel that we'll be glad to help.
from swiftrex.
Please reopen in case the answer is not clear.
from swiftrex.
Related Issues (20)
- middleware lift with WritableKeyPath doesn't work on AnyMiddleware HOT 2
- Fix animations from Middleware
- Dead-Letter Queue HOT 1
- Memory leak in LiftMiddleware
- When will develop be the official release? HOT 2
- Lift EffectMiddleware using KeyPath not receiving context
- How to modify AppLifecycleMiddleware to adopt the new MiddlewareProtocol? HOT 2
- Redundant conformance of 'ASAPScheduler' to protocol 'Scheduler'
- Using await/async in middleware HOT 4
- IO monad and stack limit HOT 6
- I think it's time to enable Enabling Concurrency warnings in Xcode HOT 2
- Dynamic store HOT 5
- Migrate away from Travis CI HOT 4
- Design question : Model to State migration HOT 2
- Dispatch actions and run reducers on background thread? HOT 4
- CombineRex : Unexpected PublisherType behavior HOT 3
- Trying to use Effect.promise with Firebase's DynamicLinkComponents.shortenURL HOT 5
- Question about assertionFailure() HOT 5
- Improve Tests
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftrex.