Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate API requests #6

Closed
simenbrekken opened this issue Feb 27, 2015 · 3 comments
Closed

Duplicate API requests #6

simenbrekken opened this issue Feb 27, 2015 · 3 comments

Comments

@simenbrekken
Copy link

This is probably a more generic Flux question, but with getter actions how do you handle async race conditions?

E.g. an action is dispatched twice, both times the action contacts a (slow) REST API and you need to make sure that the last dispatched action supercedes the first so that a single
successHandler/failureHandler is invoked.

@acdlite
Copy link
Owner

acdlite commented Feb 27, 2015

Hmm, it's hard for me to know how to answer this without more specifics. One possible solution would be to pass a timestamp along with the payload so your store handler can determine how to deal with multiple actions:

// actions.js
async getThing() {
  let thing = await RESTUtils.fetchThing();
  return {
    thing,
    timestamp: Date.now()
  };
}

// store.js
handleGetThing({ thing, timestamp }) {
  if (timestamp > this.latestTimestamp) {
    this.latestTimestamp = timestamp;
    // do something
  } else {
    // do something else
  }
}

(This isn't something I've actually tried; just an idea.)

@simenbrekken
Copy link
Author

On second thought these kinds of race conditions are probably best handled in the actual API module by aborting duplicate requests, that way the success/failure counterparts of an action will never get fired and thus don't need to be explicitly handled.

@ivan-kleshnin
Copy link

@simenbrekken as long as your backend is clustered this is not so easy.
I wouldn't rely on backend to implement debounce for you, because you waste you network traffic and processor ticks to perform useless work even if duplicate requests are thrown away.

@acdlite do we have any API to temporary unsubscribe from subsequent events on actionBegin and resubscribe on actionCompleted / actionFailed? Or somehow debounce events in other way. This should be at Action level.

This demoes again how custom event architectures loose against Rx beauty...

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

No branches or pull requests

3 participants