Skip to content

[@wordpress/data] Store resolver called before state is settled #19132

Closed as not planned

Description

@domderen

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:

  1. CHANGE_PAGE action is issued,
  2. 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,
  3. My shouldInvalidate function is returning true, because it is set to invalidate when CHANGE_PAGE action is called,
  4. Since the resolver is invalidated, internally the code is calling the applyWithSelect function in the App component, I assume to recalculate the state of this component, after the resolver has changed,
  5. This function is internally calling select( 'liststore' ).getList(),
  6. This call is triggering the fulfill method to be called in the liststore store, as the resolver was just invalidated,
  7. Resolver tries to obtain the page value, but since the CHANGE_PAGE action 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.

Screenshots
Screen+Recording+2019-12-13+at+06 04+PM

Desktop (please complete the following information):

  • OS: MacOS
  • Browser Chrome
  • Version 78.0.3904.108

Additional context

  • @wordpress/data package version: 4.10.0

Activity

  1. added
    [Package] Data
    Needs Testing
    on Dec 14, 2019
  2. added
    Needs Technical Feedback
    and removed
    Needs Testing
    on Jul 7, 2020
  3. paaljoachim commented on Jan 18, 2021

    @paaljoachim
    Contributor

    Hey @domderen

    Thank you for creating this issue!
    Much time has passed.
    Is this issue still valid?
    Thanks.

  4. domderen commented on Jan 24, 2021

    @domderen
    Author

    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/data package, and so I updated my app to use version 4.26.2. The only change I saw in the docs that I had to make to my app, was replacing registerStore with createReduxStore + register in 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:

    Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
    1. You might have mismatching versions of React and the renderer (such as React DOM)
    2. You might be breaking the Rules of Hooks
    3. You might have more than one copy of React in the same app
    See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
    

    And it seems to be referencing an issue in the withSelect component. 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-app and 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!

    Zrzut ekranu 2021-01-24 o 17 21 09

  5. paaljoachim commented on Jan 24, 2021

    @paaljoachim
    Contributor

    Hi

    Unfortunately I am not able to answer. I will ping @gziolo and @noahtallen perhaps they might have a solution.
    Thanks!

  6. kisscodes1909 commented on Oct 7, 2022

    @kisscodes1909

    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

  7. added
    [Type] Bug
    [Status] Needs More Info
    on Aug 16, 2023
  8. jordesign commented on Aug 16, 2023

    @jordesign
    Contributor

    Just checking back on this to see if it is still an issue for you @kisscodes1909 and @domderen ?

  9. jordesign commented on Aug 29, 2023

    @jordesign
    Contributor

    With no further activity on this - I'll close the issue, but we can reopen if it continues to be an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Technical Feedback[Package] Data[Status] Needs More Info[Type] Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions