Git Product home page Git Product logo

Comments (6)

militeev avatar militeev commented on May 5, 2024

In case you need to launch async process in your action dispatcher, consider emitting action upon completion of async call, and have another action dispatcher handle this action.

For instance, you might have UPDATE_ITEM action calling your REST API and ITEM_UPDATED action emitted upon success (and optionally ITEM_UPDATE_FAILED on failure).

This way the code is cleaner and easier to read and write tests against. With Uniflow, we encourage you to use actions as much as possible instead of using promises or callbacks.

from uniflow-polymer.

eterna2 avatar eterna2 commented on May 5, 2024

Thank you for ur advice. Can I further ask what would u recommend if I am waiting for multiple events to complete? (I am retrieving multiple resources)

E.g. I am using Promise.all now.

from uniflow-polymer.

eterna2 avatar eterna2 commented on May 5, 2024

Sorry, I am still pretty new to this. I am imagining a few solutions.

  1. emit an action after each request, then the subsequent action will set a flag for the resource and check the state of all the other resources flags, and only emit a complete if everything is completed?

  2. Wrap all requests inside 1 action and use a promise.all to emit a completed action?

from uniflow-polymer.

eterna2 avatar eterna2 commented on May 5, 2024

Sorry just one last question.

Should I keep the dispatched action (the object that is passed into the methods) immutable?

Because since each action is synchronous, if I allow the dispatched action to be mutable, I can use the action-dispatcher as middleware - modifying the data before updating the state.

from uniflow-polymer.

militeev avatar militeev commented on May 5, 2024

Very good questions.

As to waiting for multiple events to complete, we've implemented one solution in our internal version of the library. I think it's a bit too complex and specific, so I wasn't sure about porting it to open-sourced repo. It's quite close to what you're describing in your option (1), though.

For your second question about keeping action property object immutable the answer is simple: you don't have to, and in fact you can have multiple action dispatchers acting like middleware and updating action object with new information for future actions. The order in which actions are processed is defined by the order dispatchers are declared.

from uniflow-polymer.

eterna2 avatar eterna2 commented on May 5, 2024

Thank you very much!

from uniflow-polymer.

Related Issues (14)

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.