americanexpress / fetchye

✨ If you know how to use Fetch, you know how to use Fetchye [fetch-yae]. Simple React Hooks, Centralized Cache, Infinitely Extensible.
Apache License 2.0
43 stars 22 forks source link

In flight Cache hits in oneFetchye (or the underlying makeServerFetchye) don't return a shared promise #93

Open code-forger opened 7 months ago

code-forger commented 7 months ago

🐞 Bug Report

Describe the bug

If two requests are made to the same data, if the second request is made while the first request is still "in flight" it will return both "error" and "data" as undefined instead of the promise to the inflight request.

This is because during a second call to the same data, while the first call is 'in flight':

This block will be skipped, because we do have a cacheSelector via oneFetchye: https://github.com/americanexpress/fetchye/blob/main/packages/fetchye/src/makeServerFetchye.js#L42

This block will be skipped, because the request is loading: https://github.com/americanexpress/fetchye/blob/main/packages/fetchye/src/makeServerFetchye.js#L54

The function will then "syncronously" return the data, error, and run fields: https://github.com/americanexpress/fetchye/blob/main/packages/fetchye/src/makeServerFetchye.js#L64

However data and error will be undefined because the original request is still in flight.

To Reproduce

Define two sequenced functions (in psuedo code):

const getUserPlaylists = (dispatch) => {
    const { data: token } = await dispatch(oneFetchye(".../logIn"));
    const { data: playlists } = await dispatch(oneFetchye(".../readUserPlaylists"));
    return playlists;
}

const getUserPreferences = (dispatch) => {
    const { data: token } = await dispatch(oneFetchye(".../logIn"));
    const { data: preferences } = await dispatch(oneFetchye(".../readUserPreferences"));
    return preferences;
}

Then race them:

return Promise.all([dispatch(getUserPlaylists), dispatch(getUserPreferences)]);

Note: This bug is also present if you are calling for the same data from multiple one-app modules.

Whichever call to logIn happens second will "synchronously" receive undefined as the token.

Expected behavior

Both calls to get the token properly return a promise to the data from the single call made

oneamexbot commented 3 months ago

This issue is stale because it has been open 30 days with no activity.