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

Core Data: Mark 'canUser' related actions resolvers as resolved #63435

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ export const canUser =
.join( '/' );

dispatch.receiveUserPermission( key, permissions[ action ] );

// Mark related action resolutions as finished.
if ( action !== requestedAction ) {
dispatch.finishResolution( 'canUser', [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If two useSelect calls try to retrieve two different actions before the resolution, we'll still be triggering two API requests.

A better approach might be to separate this resolver into a sub selector/resolver no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you're saying, in some scenarios, this could invalidate the existing check at the top?

// Prevent resolving the same resource twice.
for ( const relatedAction of retrievedActions ) {
if ( relatedAction === requestedAction ) {
continue;
}
const isAlreadyResolving = hasStartedResolution( 'canUser', [
relatedAction,
resource,
id,
] );
if ( isAlreadyResolving ) {
return;
}
}

A better approach might be to separate this resolver into a sub selector/resolver no?

Do you have an example?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, actually I missed that check and you're right, we seem covered already.

That said, I think we can avoid all of that (both marking as resolved and the check to avoid starting the resolution) by relying on a shared sub resolver/selector.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, I think we can avoid all of that (both marking as resolved and the check to avoid starting the resolution) by relying on a shared sub resolver/selector.

Making a note and happy to follow up on this as "polish step". While I think I have an idea of what you mean by shared sub-resolver/selector. Do you mind sharing an example so we're on the same page 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like:

export const canUser =
	( requestedAction, resource, id ) => async ( { resolveSelect } ) =>  {
		await resolveSelect.getResourcePermissions( resource, id );
	}
		

action,
resource,
id,
] );
}
}
} );
};
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ describe( 'canUser', () => {
};
dispatch = Object.assign( jest.fn(), {
receiveUserPermission: jest.fn(),
finishResolution: jest.fn(),
} );
dispatch.mockReturnValue( ENTITIES );
triggerFetch.mockReset();
Expand Down
Loading