Giter Site home page Giter Site logo

Comments (2)

luizmb avatar luizmb commented on June 19, 2024 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.

luizmb avatar luizmb commented on June 19, 2024

Please reopen in case the answer is not clear.

from swiftrex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.