Coder Social home page Coder Social logo

Comments (7)

seivan avatar seivan commented on May 16, 2024 1

Alright, I think we see this differently. From my PoV it's irrelevant what alters the state.
showing a loading spinner is exactly the same thing as setting a list of tasks.
They both update the UI that's subscribed to the state.

But I'll just maintain this notion myself! Thanks for the code example on canceling, that's useful!

from reactorkit.

devxoul avatar devxoul commented on May 16, 2024

In that case it's recommended to use both Action and Mutation. Action is just an abstraction of an user input from a view and a mutation is a state-manipulator.

enum Action {
  case requestTasks
}

enum Mutation {
  case setLoading(Bool)
  case setTasks([Task])
}

func mutate(action: Observable<Action>) -> Observable<Mutation> {
  switch action {
  case .requestTasks:
    return Observable<Mutation>.concat([
      Observable.just(.setLoading(true)), // .setLoading(true)
      fetchTasksUsingAPI().map { Mutation.setTasks($0) }, // .setTasks([...])
      Observable.just(.setLoading(false)), // .setLoading(false)
    ])
  }
}

You may refer to the actual code: https://github.com/devxoul/Drrrible/blob/f52812943d16a891ace62083adac753c2c30ac87/Drrrible/Sources/ViewControllers/ShotListViewReactor.swift#L44-L53

from reactorkit.

seivan avatar seivan commented on May 16, 2024

Yes, I understand that part. The problem is you're gong to get a lot of messy actions that don't do anything they than toggling visibility of components. Setting the loading spinner is part of loading the tasks. Not two different actions. Except one mutation is async and the other synchronous.

from reactorkit.

seivan avatar seivan commented on May 16, 2024

Btw, I figured out a way to handle the infinite loop problem That Redux-Observables have. Ensure type constraints on your "epic". T: Action -> U:Action where T != U.
That solves the :

"const actionEpic = (action$) => action$; // creates infinite loop"

from reactorkit.

devxoul avatar devxoul commented on May 16, 2024

Setting the loading spinner is part of loading the tasks. Not two different actions.

There's one action and two mutations. It's not "two different actions". One action can occur multiple mutations.

And I didn't see anywhere that occurs infinite loop in ReactorKit. Could you reproduce it?

from reactorkit.

seivan avatar seivan commented on May 16, 2024

@devxoul No, the infinite loop is part of Redux-Observable issue :) not related here, sorry for the confusion!

Yeah but why does setting loading need its own action? As far as I see, those two things are related. There are other cases where you'd want an action to reduce state and async trigger a different action.

I really suggest looking into Redux-Observable, the strength comes from the ability to cancel async actions. https://redux-observable.js.org/docs/recipes/Cancellation.html

from reactorkit.

devxoul avatar devxoul commented on May 16, 2024

Why do we have to expose an Action.requestTasksCompleted to a view? A view's interest is just an Action.requestTasks. Action and Mutation are different. Action just takes user inputs from a view and Mutation just manipulates the state. Their role are different from each other. Of course the Action.requestTasks and setLoading can be related but "setting loading" is not a role of an Action. I would like to separate the responsibility to reduce complexity.

And for cancellation, I think we can make actionSubject public. It may look like:

func mutate(action: Action) -> Observable<Mutation> {
  switch action {
  case .refresh:
    return Observable<Mutation>.concat([
      Observable.just(.setLoading(true)),
      API.requestTasks()
        .map { Mutation.setTasks($0) }
        .takeUnil(self.actionSubject.filter(Action.cancelRefreshing)),
      Observable.just(.setLoading(false)),
    ])
  }
}

I'll take a look at this for another solution 👀

from reactorkit.

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.