Skip to content

Commit

Permalink
Core Data: Handle dynamic entity contexts using a proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Aug 22, 2019
1 parent 9988855 commit 217bcad
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,33 @@ import { useSelect, useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { defaultEntities } from './entities';
import { defaultEntities, kinds } from './entities';

const entities = defaultEntities.reduce(
( acc, entity ) => {
const entities = {
...defaultEntities.reduce( ( acc, entity ) => {
if ( ! acc[ entity.kind ] ) {
acc[ entity.kind ] = {};
}
acc[ entity.kind ][ entity.name ] = { context: createContext() };
return acc;
},
{ postType: { post: { context: createContext() } } }
);
}, {} ),
...kinds.reduce( ( acc, kind ) => {
acc[ kind.name ] = new Proxy(
{},
{
get( target, name ) {
if ( Reflect.has( target, name ) ) {
return Reflect.get( ...arguments );
}
const value = { context: createContext() };
Reflect.set( target, name, value );
return value;
},
}
);
return acc;
}, {} ),
};

/**
* Context provider component for providing
Expand Down

0 comments on commit 217bcad

Please sign in to comment.