Repository navigation
Description
Describe the bug
Hey I think I found an issue in the @wordpress/data package. I'm running into a situation where I'd like to use a variable saved on the store's state in a resolver, and at the same time I'd like this resolver to be invalidated every time this store variable is changed.
I have two variables in my state:
const initialState = {
list: [],
page: 1
}
I then have selectors for both of those values, and in addition, I have a resolver called getList that gets the new version of the list. The getList resolver is using the page variable, to know which page of the list to grab. In addition, the resolver has a shouldInvalidate function, that invalidates that resolver whenever a CHANGE_PAGE action is issued.
What I'm trying to get to, is a store that will automatically update the list, whenever the page is changed. This part works just fine, I change the page, and the resolver does get called.
But where things break, is that this getList resolver is called with the previous version of the page variable, rather than the new one.
After some debugging I think I understand the order of execution to be:
CHANGE_PAGEaction is issued,- Internally this call is triggering the ResolversCacheMiddleware (https://github.com/WordPress/gutenberg/blob/master/packages/data/src/resolvers-cache-middleware.js), which checks if there are any resolvers on this store, and if any of them should be invalidated,
- My
shouldInvalidatefunction is returning true, because it is set to invalidate whenCHANGE_PAGEaction is called, - Since the resolver is invalidated, internally the code is calling the
applyWithSelectfunction in theAppcomponent, I assume to recalculate the state of this component, after the resolver has changed, - This function is internally calling
select( 'liststore' ).getList(), - This call is triggering the
fulfillmethod to be called in theliststorestore, as the resolver was just invalidated, - Resolver tries to obtain the
pagevalue, but since theCHANGE_PAGEaction wasn't completed yet, it is still set to the previous value.
Now, my question is if that is an intended behavior, and I should structure my app in some other way?
Or is this a bug?
Below I'm pasting the code of the store:
import { registerStore, select } from "@wordpress/data";
const REPLACE = "REPLACE";
const CHANGE_PAGE = "CHANGE_PAGE";
const LIST_FETCH = "LIST_FETCH";
const initialState = {
list: [],
page: 1
};
const storeActions = {
replace(list) {
return {
type: REPLACE,
payload: list
};
},
changePage(page) {
return {
type: CHANGE_PAGE,
payload: page
};
},
fetch(page) {
return {
type: LIST_FETCH,
page
};
}
};
export default registerStore("liststore", {
reducer(state = initialState, action) {
// eslint-disable-next-line default-case
switch (action.type) {
case REPLACE:
return {
...state,
list: [...action.payload]
};
case CHANGE_PAGE:
return {
...state,
page: action.payload
};
}
return state;
},
actions: storeActions,
selectors: {
getList(state) {
return state.list;
},
getPage(state) {
return state.page;
}
},
controls: {
async LIST_FETCH(action) {
console.log("Calling LIST_FETCH with", action.page);
return new Array(action.page).fill(0).map((q, i) => i + 1);
}
},
resolvers: {
getList: {
*fulfill() {
const page = select("liststore").getPage();
const list = yield storeActions.fetch(page);
return storeActions.replace(list);
},
shouldInvalidate: action => {
return action.type === CHANGE_PAGE;
}
}
}
});To reproduce
I also created a small repo that presents this issue: https://github.com/domderen/wp-data-store
Expected behavior
I would expect the CHANGE_PAGE action to complete before invalidating the resolver.
Desktop (please complete the following information):
- OS: MacOS
- Browser Chrome
- Version 78.0.3904.108
Additional context
@wordpress/datapackage version: 4.10.0

Activity
paaljoachim commentedon Jan 18, 2021
Hey @domderen
Thank you for creating this issue!
Much time has passed.
Is this issue still valid?
Thanks.
domderen commentedon Jan 24, 2021
Hey @paaljoachim
Thanks for getting back to me on this one. I wanted to check if this issue is still valid with the newest version of
@wordpress/datapackage, and so I updated my app to use version4.26.2. The only change I saw in the docs that I had to make to my app, was replacingregisterStorewithcreateReduxStore+registerin my store.Unfortunately, after making those updates, it seems that my app is currently not working with the newest version of React. I'm getting this error:
And it seems to be referencing an issue in the
withSelectcomponent. I'm attaching below a screenshot of the issue I'm getting.Am I missing some required changes to my app, to make it work with React 17? Or is there a known compatibility issue between React 17.0.1 & @wordpress/data 4.26.2?
I also tried starting a new project via
npx create-react-app new-appand moving my store there, to see if maybe the issue was somewhere else, but I got the same error.Thanks in advance for your help!
paaljoachim commentedon Jan 24, 2021
Hi
Unfortunately I am not able to answer. I will ping @gziolo and @noahtallen perhaps they might have a solution.
Thanks!
kisscodes1909 commentedon Oct 7, 2022
I also had a similar situation, multiple APIs were called and returned incorrect data.
Do you have any advice for us?
https://www.awesomescreenshot.com/video/11571190?key=a0a17800dbe445fe079b2e3f4fe221f8
jordesign commentedon Aug 16, 2023
Just checking back on this to see if it is still an issue for you @kisscodes1909 and @domderen ?
jordesign commentedon Aug 29, 2023
With no further activity on this - I'll close the issue, but we can reopen if it continues to be an issue.