redux-api icon indicating copy to clipboard operation
redux-api copied to clipboard

Fix for concurrent action calls

Open Paxmagister opened this issue 9 years ago • 9 comments

If multiple actions are called to the same endpoint in rapid succession, all the callbacks are pushed onto the same pubsub object. When the first fetch returns, all of the callbacks are called with that data, and subsequent fetch returns call no callbacks. Moving the pubsub instantiation into the individual calls to the api solves this issue.

Paxmagister avatar May 16 '17 19:05 Paxmagister

Concurrent calls for endpoint isn't permitted by design, because you can't update you store concurrent without race condition. And failing test shows it.

lexich avatar May 16 '17 20:05 lexich

I see how that is supposed to stop multiple calls, but it doesn't. I have a lookahead search that passes along the string in the url to the api call. When typing fast enough, the api endpoint is called for each letter typed, and the same response is fed to all the callbacks. This causes the values to be incorrect, however the redux store ends up with the correct values. Correction, the redux store ends up with the values for whatever call returns last.

This is using react-select, with the async options callback.

Paxmagister avatar May 16 '17 21:05 Paxmagister

In this "spam case" you should abort previous request. Now you have 2 variants. Use request method and update store. Or use reset method - but this set initialState to store. But it's point to new feature to abort previous request.

lexich avatar May 17 '17 05:05 lexich

@Paxmagister I made PR. I add support abortable requests. https://github.com/lexich/redux-api/pull/143/files#diff-5a4fb2f7a580b6a02bf1dead9d25e66fR614 Might is be useful for your case. What do you think about API?

lexich avatar May 17 '17 10:05 lexich

That would work for the base functionality I'm going for, thanks. But I would like to cache the results that are returned from the previous api calls, so it would actually be nice if they called the callbacks passed to them. I was thinking something like, only call dispatch to update redux if it was the last call made, but everything else still gets run, and the callbacks are registered per request.

Paxmagister avatar May 17 '17 16:05 Paxmagister

@Paxmagister I have seen your progress and I'm not sure that you on the right way, unfortunately.

lexich avatar May 18 '17 19:05 lexich

@lexich I seem to be having an issue that is related to this. so is it not possible to make multiple action calls with different parameters before the first one comes back? I need to make many calls in a row to the same endpoint but with different ids. is this not possible??

jakeaaron avatar Jun 07 '17 02:06 jakeaaron

you can use pure requests request and after that aggregate this

lexich avatar Jun 07 '17 06:06 lexich

thanks @lexich. so how can we use reducers with request? or is there a way to get concurrent gets to work as expected? I guess I don't understand the issue with updating store with concurrent calls... can we not merge prevState in with the response with something like Immutable?

jakeaaron avatar Jun 07 '17 14:06 jakeaaron