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: do not publish outdated state to subscribers during updates #19752

Merged
merged 4 commits into from
Jan 21, 2020
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
15 changes: 15 additions & 0 deletions docs/designers-developers/developers/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ _Returns_

- `?Object`: The entity record's non transient edits.

<a name="getEntityRecordNoResolver" href="#getEntityRecordNoResolver">#</a> **getEntityRecordNoResolver**

Returns the Entity's record object by key. Doesn't trigger a resolver nor requests the entity from the API if the entity record isn't available in the local state.

_Parameters_

- _state_ `Object`: State tree
- _kind_ `string`: Entity kind.
- _name_ `string`: Entity name.
- _key_ `number`: Record's key

_Returns_

- `?Object`: Record.

<a name="getEntityRecords" href="#getEntityRecords">#</a> **getEntityRecords**

Returns the Entity's records.
Expand Down
15 changes: 15 additions & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,21 @@ _Returns_

- `?Object`: The entity record's non transient edits.

<a name="getEntityRecordNoResolver" href="#getEntityRecordNoResolver">#</a> **getEntityRecordNoResolver**

Returns the Entity's record object by key. Doesn't trigger a resolver nor requests the entity from the API if the entity record isn't available in the local state.

_Parameters_

- _state_ `Object`: State tree
- _kind_ `string`: Entity kind.
- _name_ `string`: Entity name.
- _key_ `number`: Record's key

_Returns_

- `?Object`: Record.

<a name="getEntityRecords" href="#getEntityRecords">#</a> **getEntityRecords**

Returns the Entity's records.
Expand Down
7 changes: 3 additions & 4 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,10 @@ export function* saveEntityRecord(
}
}

// We perform an optimistic update here to clear all the edits that
// will be persisted so that if the server filters them, the new
// filtered values are always accepted.
// Get the full local version of the record before the update,
// to merge it with the edits and then propagate it to subscribers
persistedEntity = yield select(
'getEntityRecord',
'getEntityRecordNoResolver',
kind,
name,
recordId
Expand Down
14 changes: 14 additions & 0 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ export function getEntityRecord( state, kind, name, key ) {
return get( state.entities.data, [ kind, name, 'queriedData', 'items', key ] );
}

/**
* Returns the Entity's record object by key. Doesn't trigger a resolver nor requests the entity from the API if the entity record isn't available in the local state.
*
* @param {Object} state State tree
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {number} key Record's key
*
* @return {Object?} Record.
*/
export function getEntityRecordNoResolver( state, kind, name, key ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It bothers me that we're introducing this as a new API. For me resolvers are internal and shouldn't be known to public APIs

Copy link
Contributor

Choose a reason for hiding this comment

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

By nature of the fact that there will always be cases where you want to run a selector locally/without resolvers, resolvers will always have to be somewhat "public."

We should have a registry.selectLocal or something like that, where the returned selectors don't trigger their resolvers.

Copy link
Contributor

Choose a reason for hiding this comment

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

By nature of the fact that there will always be cases where you want to run a selector locally/without resolvers

Why? The concept of resolvers is meant to be entirely transparent to the Component author.
I'd argue that maybe it's needed for internal usage (inside the same store) but not as a public API of that store.

Copy link
Contributor

Choose a reason for hiding this comment

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

Authors might also need to call selectors without triggering their resolvers, E.G., to get cached values and compare them with the new ones, to avoid API calls when there is no network, to keep local changes for things that could have been modified by other clients, etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not certain about that. The whole idea of the selectors/actions is that the component just asks for data and doesn't need to know whether it's async or sync (close to GraphQL approach of things).

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not certain about that. The whole idea of the selectors/actions is that the component just asks for data and doesn't need to know whether it's async or sync (close to GraphQL approach of things).

GraphQL clients expose functions to run queries on the cache.

something which could apply to any selector that has associated resolver

That's what I suggested with a selectLocal or selectCached.

Or maybe we need to avoid triggering resolvers as a result of any control being yielded from an action.

That would force a lot of code duplication between controls and resolvers.

In fact, I wonder if that change alone would be fine, and just omit the last argument from the selector. The documentation generator won't consider the resolver, but I expect the data package will happily pass along all arguments anyways.

Yes, that would also work, albeit it's a bit more manual than just having a selectCached. And would we leave it up to each resolver to decide what to do with the option or would we bail before even calling the resolver?

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we at least mark this s experimental before the beta release?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I found this issue when reviewing and debugging another instance of this issue 🙂

It seems to me that the root cause is that getEntityRecords (plural) fetches the records from network and stores them in local cache, but if I call getEntityRecord (singular) immediately after that, it's a cache miss. And fetches the record again. That shouldn't be happening. The getEntityRecord selector is effectively resolved, the cache contains up-to-date data and a new request shouldn't be fired. A GraphQL cache wouldn't behave this way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I explained that further up. The problem is that this resolver setup doesn't have a trivial way to define relationships between resolvers like this. I guess we would need a new API like getEntityRecord.shouldResolve = ( action, ...args ) => ....

return getEntityRecord( state, kind, name, key );
}

/**
* Returns the entity's record object by key,
* with its attributes mapped to their raw values.
Expand Down
5 changes: 4 additions & 1 deletion packages/core-data/src/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ describe( 'saveEntityRecord', () => {
expect( fulfillment.next( entities ).value.type ).toBe(
'SAVE_ENTITY_RECORD_START'
);

// Should select getEntityRecordNoResolver selector (as opposed to getEntityRecord)
// see https://github.com/WordPress/gutenberg/pull/19752#discussion_r368498318.
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.selectorName ).toBe( 'getEntityRecordNoResolver' );
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'RECEIVE_ITEMS' );
const { value: apiFetchAction } = fulfillment.next( {} );
Expand Down
13 changes: 10 additions & 3 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import deepFreeze from 'deep-freeze';
*/
import {
getEntityRecord,
getEntityRecordNoResolver,
getEntityRecords,
getEntityRecordChangesByRecord,
getEntityRecordNonTransientEdits,
Expand All @@ -20,7 +21,11 @@ import {
getReferenceByDistinctEdits,
} from '../selectors';

describe( 'getEntityRecord', () => {
// getEntityRecord and getEntityRecordNoResolver selectors share the same tests
describe.each( [
[ getEntityRecord ],
[ getEntityRecordNoResolver ],
] )( '%p', ( selector ) => {
it( 'should return undefined for unknown record’s key', () => {
const state = deepFreeze( {
entities: {
Expand All @@ -36,7 +41,7 @@ describe( 'getEntityRecord', () => {
},
},
} );
expect( getEntityRecord( state, 'root', 'postType', 'post' ) ).toBe( undefined );
expect( selector( state, 'root', 'postType', 'post' ) ).toBe( undefined );
} );

it( 'should return a record by key', () => {
Expand All @@ -56,7 +61,9 @@ describe( 'getEntityRecord', () => {
},
},
} );
expect( getEntityRecord( state, 'root', 'postType', 'post' ) ).toEqual( { slug: 'post' } );
expect( selector( state, 'root', 'postType', 'post' ) ).toEqual( {
slug: 'post',
} );
} );
} );

Expand Down