Code Monkey home page Code Monkey logo

Comments (10)

Ben-G avatar Ben-G commented on May 30, 2024 1

I believe @agentk was in favor of this as well. If that's the case I'd be happy to deprecate the Reducer protocol with the next release and add support for free functions.

from reswift.

Ben-G avatar Ben-G commented on May 30, 2024

Yes, we could potentially do this! The reducer protocol is an artifact from an older ReSwift version. One of the advantages of using a type instead of a free function is that the error message around type mismatches in the store and the root-reducer is slightly better.

However, our docs have been suggesting using free functions for any but the root reducer function; so it might be more consistent to change this.

I don't have a strong opinion either way. I would love to see the change on a branch to see the impact it would have.

@ReSwift/reswift any opinions on this?

from reswift.

DivineDominion avatar DivineDominion commented on May 30, 2024

A reducer type doesn't have much value in my app TableFlip so far compared to a free function. So I think this is okay. Where the function lives is up to the team anyway -- they could create a Reducers struct with static functions and reference these, for example.

from reswift.

hartbit avatar hartbit commented on May 30, 2024

I also agree that they should be free functions ๐Ÿ‘

from reswift.

Qata avatar Qata commented on May 30, 2024

I like the idea of deprecating Reducer, but what I like even more is turning Reducer into a concrete struct that allows mapping and concatenating Reducers with ease. I've implemented this in ReactiveReSwift and I'm very happy with the results. Here's a reimplementation of the GitHub Example code using this new structure.

let appReducer = Reducer(authenticationReducer, bookmarksReducer, repositoriesReducer)

let authenticationReducer = Reducer<State> { action, state in
    var state = state

    switch action {
    case let action as SetOAuthURL:
        state.authenticationState.oAuthURL = action.oAuthUrl
    case let action as UpdateLoggedInState:
        state.authenticationState.loggedInState = action.loggedInState
    default:
        break
    }

    return state
}

let bookmarksReducer = Reducer<State> { action, state in
    var state = state

    if let action = action as? CreateBookmark {
        let bookmark = (route: action.route, routeSpecificData: action.routeSpecificData)
        state.bookmarks.append(bookmark)
    }

    return state
}

let repositoriesReducer = Reducer<State> { action, state in
    var state = state

    if let action = action as? SetRepostories {
        state.repositories = action.repositories
    }

    return state
}

from reswift.

hartbit avatar hartbit commented on May 30, 2024

from reswift.

DivineDominion avatar DivineDominion commented on May 30, 2024

from reswift.

Ben-G avatar Ben-G commented on May 30, 2024

@Qata agree that this looks nice! One thought: using only functions it's easy to compose reducers that operate on a substate of the overall state. E.g. in the GitHub example we only pass the bookmark state to the bookmark reducer. This enforces separation of concerns within reducers. Do you have an idea if/how this would be possible with your approach?

from reswift.

Ben-G avatar Ben-G commented on May 30, 2024

Finally opened #177, that serves the original request of making Reducer a function type. While I'm a big fan of @Qata suggestion of revamping the middleware API (#175). I think that having the reducer being a function type has advantages for composition that I mentioned above.

You can see an example here: https://github.com/ReSwift/GitHubBrowserExample/blob/master/SwiftFlowGitHubBrowser/Reducers/AppReducer.swift#L16-L19

from reswift.

Ben-G avatar Ben-G commented on May 30, 2024

Shipped with #177.

from reswift.

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.