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

Refactor actions and resolvers with wp.data controls #8992

Closed
5 tasks
aaemnnosttv opened this issue Jul 9, 2024 · 8 comments
Closed
5 tasks

Refactor actions and resolvers with wp.data controls #8992

aaemnnosttv opened this issue Jul 9, 2024 · 8 comments
Labels
P1 Medium priority Team S Issues for Squad 1 Type: Infrastructure Engineering infrastructure & tooling

Comments

@aaemnnosttv
Copy link
Collaborator

aaemnnosttv commented Jul 9, 2024

Feature Description

With our recent upgrade of @wordpress/data, the package now includes a set of controls which are essentially utility actions that every store will have associated controls for.

These are

  • select – Dispatches a control action for triggering a synchronous registry select.
  • resolveSelect – Dispatches a control action for triggering and resolving a registry select.
  • dispatch – Dispatches a control action for triggering a registry dispatch.

These are actions meaning we would yield these within our own actions/resolvers instead of using workarounds to getRegistry and commonActions.await just to get access to select/registrySelect or dispatch and await the result.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • Include the new controls from WP Data (select, resolveSelect, and dispatch) in our googlesitekit-data package

Implementation Brief

  • Update the datastore of the AdSense module to use the new builtInControls instead of the getRegistry common actions. Follow-up issues should be filed to continue to the next module, unless there is time to fit another module in same issue (based on the issue estimate).
  • Update assets/js/googlesitekit/data/index.js
    • Add import for controls to the existing imports list from @wordpress/data, and alias it as builtInControls
    • Add builtInControls to the export
      // Attach some WordPress data functions to the registry so third-party
      // developers can use them without importing '@wordpress/data'.
      Data.createRegistryControl = createRegistryControl;
      Data.createRegistrySelector = createRegistrySelector;
      Data.useSelect = useSelect;
      Data.useDispatch = useDispatch;
      Data.useRegistry = useRegistry;
      Data.withSelect = withSelect;
      Data.withDispatch = withDispatch;
      Data.RegistryProvider = RegistryProvider;
      export {
      combineStores,
      commonActions,
      commonControls,
      commonStore,
      createReducer,
      useInViewSelect,
      createRegistryControl,
      createRegistrySelector,
      useSelect,
      useDispatch,
      useRegistry,
      withSelect,
      withDispatch,
      RegistryProvider,
      };
  • Include the built-in controls from our data package import { builtInControls } from 'googlesitekit-data' and replace the usage of await, registry and its select, resolveSelect and dispatch in the actions and resolvers, with the builtin controls:
    • Builtin controls need to be yielded to return the value and can be used in following way: yield select( storeKey, selectorName, ...args ) , so replace registry.select with yield builtInControls.select, registry.resolveSelect with yield builtInControls.resolveSelect, and registry.dispatch with yield builtInControls.dispatch
    • Imported builtInControls object will hold all three controls {select, resolveSelect, dispatch}
    • This should be replaced where possible, which includes usage from our commonActions, we should leave it if it is a passed prop from createRegistryControl, like here and here
      const homeURL = registry.select( CORE_SITE ).getHomeURL();
      const ampMode = registry.select( CORE_SITE ).getAMPMode();
  • Mark
    export const commonActions = {
    as @deprecated in JSDoc; no new usage of these actions should be introduced.
  • File follow-up issues for other datastores/modules to be migrated, similar to AdSense.

Test Coverage

  • There shouldn't be test updates needed, since things should continue to work as expected

QA Brief

  • No QA needed; this just updates the APIs exposed via googlesitekit-data.

Changelog entry

  • Add new WordPress Data controls to googlesitekit-data.
@aaemnnosttv aaemnnosttv added P1 Medium priority Type: Infrastructure Engineering infrastructure & tooling labels Jul 9, 2024
@binnieshah binnieshah added Next Up Issues to prioritize for definition Team S Issues for Squad 1 labels Jul 12, 2024
@zutigrm zutigrm assigned zutigrm and unassigned zutigrm Jul 12, 2024
@tofumatt tofumatt self-assigned this Jul 15, 2024
@tofumatt
Copy link
Collaborator

@zutigrm Let's do the opposite, starting with one module. That way the number of files/modules/etc. to test/review is smaller, and the QA can be scoped to testing Analytics behaviour.

If during development there's time to work on more modules they can be added I think.

It's missing from the ACs, but part of this change should include us exporting these new controls from googlesitekit-data, which is also where we should be importing them from.

I think we can mirror the @wordpress/data exports and export things as controls, but if you wanted to alias it to builtInControls so it can be easily imported without every file needing to do { controls as builtInControls } that'd be nice, given how many of our files already have a const controls variable. 👍🏻

Builtin controls can be used in following way: select|resolveSelect|dispatch( storeKey, selectorName, ...args ) , so const propertyID = registry.select( MODULES_ANALYTICS_4 ).getPropertyID() would be replaced with: const propertyID = builtinControls.select( MODULES_ANALYTICS_4, 'getPropertyID' ), etc.

I get what this means but it's a bit hard to read/parse. I think it's fine to write something like:

Replace registry.select with builtInControls.select, registry.resolveSelect with builtInControls.resolveSelect, and registry.dispatch with builtInControls.dispatch.

@tofumatt tofumatt assigned zutigrm and unassigned tofumatt Jul 15, 2024
@aaemnnosttv
Copy link
Collaborator Author

  • Builtin controls can be used in following way: select|resolveSelect|dispatch( storeKey, selectorName, ...args ) , so const propertyID = registry.select( MODULES_ANALYTICS_4 ).getPropertyID() would be replaced with: const propertyID = builtinControls.select( MODULES_ANALYTICS_4, 'getPropertyID' ), etc

I just want to flag that the example here is incorrect. The controls export is an object of (unbound) action creators in Redux terms. These are then mixed-in with their associated controls (to handle their respective actions) when registering a store.

As mentioned in the description:

These are actions meaning we would yield these within our own actions/resolvers instead of using workarounds to getRegistry and commonActions.await just to get access to select/registrySelect or dispatch and await the result.

Calling the function returns the raw action object, so we need to yield them to dispatch them on the current store.

wpd.controls.select('store/foo', 'getBar', 'argBaz', 'argBuzz')
// returns:
{
  type: '@@data/SELECT',
  storeKey: 'store/foo',
  selectorName: 'getBar',
  args: [ 'argBaz', 'argBuzz' ]
}

@zutigrm
Copy link
Collaborator

zutigrm commented Jul 18, 2024

Thanks @aaemnnosttv indeed, I updated to reference that they should be yielded to return the result.

@tofumatt Thanks for the suggestion, I update IB to mention adding alias for controls to our data package, and applied suggested wording.

@zutigrm zutigrm assigned tofumatt and unassigned zutigrm Jul 18, 2024
@binnieshah binnieshah removed the Next Up Issues to prioritize for definition label Jul 18, 2024
@tofumatt
Copy link
Collaborator

We should be explicit in the IB that not all registry.select calls can be replaced. For instance, any code inside a createRegistryControl is fine to leave as-is (eg.

const homeURL = registry.select( CORE_SITE ).getHomeURL();
const ampMode = registry.select( CORE_SITE ).getAMPMode();
).

Let's also make a plan to mark getRegistry as deprecated as part of this issue to discourage future use, as the ACs call for it.

@tofumatt tofumatt assigned zutigrm and unassigned tofumatt Jul 23, 2024
@zutigrm
Copy link
Collaborator

zutigrm commented Jul 23, 2024

Thanks @tofumatt , that's a good spot. I amended the IB to include those points. I suggested the depreciation error for eslint rule to ensure it is not used. Let me know what you think

@zutigrm zutigrm assigned tofumatt and unassigned zutigrm Jul 23, 2024
@tofumatt
Copy link
Collaborator

I meant a JSDoc @deprecated notice for now; once all actions are converted we should remove commonActions instead of marking it as deprecated, but we'll do that once all datastores are migrated.

I adjusted the IB to account for this, and increased the estimate slightly in case any issues are encountered; if they aren't, more work can be done on other stores.

IB ✅

@tofumatt tofumatt removed their assignment Jul 24, 2024
@binnieshah binnieshah added the Next Up Issues to prioritize for definition label Jul 26, 2024
@binnieshah binnieshah removed the Next Up Issues to prioritize for definition label Aug 9, 2024
@tofumatt tofumatt self-assigned this Aug 12, 2024
@tofumatt
Copy link
Collaborator

After a discussion with @aaemnnosttv, @eugene-manuilov, and @nfmohit, we're actually going to change the scope of this issue after looking at the resulting PR (#9234), which ends up being a worse API 😅

I'm just updating that PR and will move it to CR soon; I've updated the ACs accordingly.

@aaemnnosttv
Copy link
Collaborator Author

No QA to do here, moving to approval 👍

@aaemnnosttv aaemnnosttv removed their assignment Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 Medium priority Team S Issues for Squad 1 Type: Infrastructure Engineering infrastructure & tooling
Projects
None yet
Development

No branches or pull requests

4 participants