Code Monkey home page Code Monkey logo

Comments (7)

tylerlong avatar tylerlong commented on July 28, 2024

If I create an extra logic and move the second dispatch there, it works like a charm.

https://github.com/tylerlong/hello-async/blob/2f037cd96da276a446822cfc1530113af1cb070d/redux-logic/src/logics.js

So I guess I am right in concluding that you can only invoke dispatch once in each logic.

Do you think this limitation is necessary? Do you think it's a big disadvantage for this library?

from redux-logic.

tylerlong avatar tylerlong commented on July 28, 2024

I tried done, it doesn't work:

process ({ getState, action }, dispatch, done) {
    const id = nextNotificationId++
    dispatch(showNotification(id, action.text))
    setTimeout(() => {
      dispatch(hideNotification(action.id))
      done()
    }, 5000)
  }

from redux-logic.

tylerlong avatar tylerlong commented on July 28, 2024

I also tried

processOptions: {
    dispatchMultiple: true // turn on multiple dispatch mode
  }

It doesn't work either.

from redux-logic.

tylerlong avatar tylerlong commented on July 28, 2024

It is trivial with RxJS:

image

But this project's goal is:

"I wrote the rxjs code so you won't have to."

I hope this issue could help you to find a balance between abstraction and flexibility.

from redux-logic.

jeffbski avatar jeffbski commented on July 28, 2024

@tylerlong Thanks for creating the issue.

I've put together a jsfiddle that shows how you might do this with redux-logic.

You would use the dispatch and done to perform multiple dispatch, otherwise it assumes a single dispatch. I'm not sure why it didn't work for you when you tried this, but you might compare your own code against the jsfiddle or the snippet below.

http://jsfiddle.net/ezp58328/

let nextNotificationId = 0
const showNotificationWithTimeoutLogic = createLogic({
  type: 'SHOW_NOTIFICATION_WITH_TIMEOUT',
  process ({ getState, action }, dispatch, done) { // include done for multidispatch
    const id = nextNotificationId++
    dispatch(showNotification(id, action.text))
    setTimeout(() => {
      dispatch(hideNotification(id))
      done();
    }, 5000)
  }
});

You could also return an observable if you would prefer to use them. I want people to be able to use whatever they are comfortable with. (callbacks, promises, observables, async/await).

let nextNotificationId = 0
const showNotificationWithTimeoutLogic = createLogic({
  type: 'SHOW_NOTIFICATION_WITH_TIMEOUT',
  process ({ getState, action }) { // can just return an observable too
    const id = nextNotificationId++
    return Observable.merge(
      Observable.of(showNotification(id, action.text)),
      Observable.of(hideNotification(id)).delay(5000)
    );
  }
});

It is a challenge to find the right abstractions for people. I'm always looking to improve them, so I do appreciate advice and suggestions.

from redux-logic.

tylerlong avatar tylerlong commented on July 28, 2024

@jeffbski

I tried again and it did work! I remember that I tried everything possible. Maybe I made some mistake or I forgot to compile the code or clear the cache. I've no idea what was wrong but now it works like a charm!

Here is the sample project I created: https://github.com/tylerlong/hello-async/tree/master/redux-logic Feel free to send me PR if you want to change anything there.

Feel free to close this issue and thank you for the help!

from redux-logic.

jeffbski avatar jeffbski commented on July 28, 2024

Fantastic. I'm glad it worked this time.

I like your project hello-async. It is really good to be able to see different ways to accomplish things side by side.

I'm going to check it out more thoroughly when I have time, but it looks like a great idea.

from redux-logic.

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.