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 3 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
8 changes: 4 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,
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
// to merge it with the edits and then propagate it to subscribers
persistedEntity = yield select(
'getEntityRecord',
'getEntityRecordNoResolver',
kind,
name,
recordId
Expand All @@ -379,6 +378,7 @@ export function* saveEntityRecord(
method: recordId ? 'PUT' : 'POST',
data,
} );

epiqueras marked this conversation as resolved.
Show resolved Hide resolved
yield receiveEntityRecords( kind, name, updatedRecord, undefined, true );
}
} catch ( _error ) {
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 get( state.entities.data, [ kind, name, 'queriedData', 'items', key ] );
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
}

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

// should select getEntityRecordNoResolver selector (as opposed to getEntityRecord)
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
// see https://github.com/WordPress/gutenberg/pull/19752#discussion_r368498318
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.selectorName ).toBe( 'getEntityRecordNoResolver' );

epiqueras marked this conversation as resolved.
Show resolved Hide resolved
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SELECT' );

epiqueras marked this conversation as resolved.
Show resolved Hide resolved
expect( fulfillment.next().value.type ).toBe( 'RECEIVE_ITEMS' );
const { value: apiFetchAction } = fulfillment.next( {} );
expect( apiFetchAction.request ).toEqual( {
Expand Down
55 changes: 30 additions & 25 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,43 +21,47 @@ import {
getReferenceByDistinctEdits,
} from '../selectors';

describe( 'getEntityRecord', () => {
it( 'should return undefined for unknown record’s key', () => {
const state = deepFreeze( {
entities: {
data: {
root: {
postType: {
queriedData: {
items: {},
queries: {},
// getEntityRecord and getEntityRecordNoResolver selectors share the same tests
Object.entries( { getEntityRecord, getEntityRecordNoResolver } ).forEach( ( [ name, func ] ) => {
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
// the interpolation is needed due to https://github.com/jest-community/eslint-plugin-jest/issues/203
describe( `${ name }`, () => {
it( 'should return undefined for unknown record’s key', () => {
const state = deepFreeze( {
entities: {
data: {
root: {
postType: {
queriedData: {
items: {},
queries: {},
},
},
},
},
},
},
} );
expect( func( state, 'root', 'postType', 'post' ) ).toBe( undefined );
} );
expect( getEntityRecord( state, 'root', 'postType', 'post' ) ).toBe( undefined );
} );

it( 'should return a record by key', () => {
const state = deepFreeze( {
entities: {
data: {
root: {
postType: {
queriedData: {
items: {
post: { slug: 'post' },
it( 'should return a record by key', () => {
const state = deepFreeze( {
entities: {
data: {
root: {
postType: {
queriedData: {
items: {
post: { slug: 'post' },
},
queries: {},
},
queries: {},
},
},
},
},
},
} );
expect( func( state, 'root', 'postType', 'post' ) ).toEqual( { slug: 'post' } );
} );
expect( getEntityRecord( state, 'root', 'postType', 'post' ) ).toEqual( { slug: 'post' } );
} );
} );

Expand Down