Skip to content

Conversation

@nikitamounier
Copy link
Contributor

No description provided.

@Qata
Copy link
Member

Qata commented Feb 26, 2021

@nikitamounier This good to merge?

@nikitamounier
Copy link
Contributor Author

Yep! Do you like the documentation style?

@nikitamounier
Copy link
Contributor Author

I just have one question – does the state publisher in the middleware have any actual use? Because flatmapping and then mapping it to return a refined action or simply returning a Just with a refined action seems to have the same result. Is it for code clarity?

@Qata
Copy link
Member

Qata commented Feb 26, 2021

The state publisher is to ensure that a long running network task or something like that has the most up to date copy of the state when it’s finished its call.

@nikitamounier
Copy link
Contributor Author

I'm not sure I fully understand, since we don't have actual access to the state itself in the middleware. I always pictured the middleware as two parallel streams (like in a marble diagram) – one of raw actions, and one with the state in which all we can do is call refined actions.

Do you think you could show me a quick example?

@nikitamounier
Copy link
Contributor Author

nikitamounier commented Feb 26, 2021

Whoops, ignore what I said! You can access the most recent state in the map! That's super useful, excuse my oversight. Let me try adding an example of that in the documentation.

@nikitamounier
Copy link
Contributor Author

nikitamounier commented Feb 26, 2021

So would this be correct to add to the documentation – it's a raw action for saving the app state (which if successful returns a refined action none which I made for when an inherently asynchronous action doesn't lead to any state changes):

static let middleware = Middleware<State, Action.Raw, Action.Refined> { state, action -> AnyPublisher<Action.Refined, Never> in
    switch action {
    case .saveState:
        return state.flatMap { appState in
            Just(appState)
        }
        .encode(encoder: JSONEncoder())
        .flatMap { FileManager.default.save(data: $0, to: "appState.json", in: .applicationSupportDirectory) }
        .flatMap { _ in
            state.map { _ in
                return .none
            }
        }
        .catch { error in
            Just(.saveStateError(error))
        }
        .eraseToAnyPublisher()

@Qata
Copy link
Member

Qata commented Mar 2, 2021

Yeah that code is correct. You could have your AppAction.Refined type be optional so you automatically get .some(_) and .none.

@nikitamounier
Copy link
Contributor Author

Do you think that's better than just adding a .none case to the refined actions, which in the reducer just causes a break? I feel like making the Refined type be optional in the middleware would add unnecessary boilerplate to the Middleware declaration, making the most important part (aka which refined action is returned) more cluttered.

@nikitamounier
Copy link
Contributor Author

nikitamounier commented Mar 2, 2021

Also I'm fully finished with the Middleware documentation – so feel free to merge with either just the updated diagram or with the Middleware documentation too :)

@Qata Qata merged commit 3203bbb into ReSwift:master Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants