Skip to content

Commit

Permalink
isReady: consider actions or fields
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Nov 20, 2024
1 parent 8649f93 commit df078fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
25 changes: 20 additions & 5 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ export function unregisterEntityField(
};
}

export function setIsReady( kind: string, name: string ) {
export function setIsReady( kind: string, name: string, part: string ) {
return {
type: 'SET_IS_READY' as const,
kind,
name,
part,
};
}

Expand All @@ -101,15 +102,17 @@ export const registerPostTypeActions =
async ( { registry }: { registry: any } ) => {
const isReady = unlock( registry.select( editorStore ) ).isEntityReady(
'postType',
postType
postType,
'actions'
);
if ( isReady ) {
return;
}

unlock( registry.dispatch( editorStore ) ).setIsReady(
'postType',
postType
postType,
'actions'
);

const postTypeConfig = ( await registry
Expand Down Expand Up @@ -178,8 +181,20 @@ export const registerPostTypeActions =
export const registerPostTypeFields =
( postType: string ) =>
async ( { registry }: { registry: any } ) => {
// TODO: do not register fields if there were already registered.
// Consider the existing isReady state.
const isReady = unlock( registry.select( editorStore ) ).isEntityReady(
'postType',
postType,
'fields'
);
if ( isReady ) {
return;
}

unlock( registry.dispatch( editorStore ) ).setIsReady(
'postType',
postType,
'fields'
);

const fields = [
featuredImageField,
Expand Down
9 changes: 7 additions & 2 deletions packages/editor/src/dataviews/store/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export function getEntityFields( state: State, kind: string, name: string ) {
return state.fields[ kind ]?.[ name ] ?? EMPTY_ARRAY;
}

export function isEntityReady( state: State, kind: string, name: string ) {
return state.isReady[ kind ]?.[ name ];
export function isEntityReady(
state: State,
kind: string,
name: string,
part: string
) {
return state.isReady[ kind ]?.[ name ]?.[ part ];
}
10 changes: 8 additions & 2 deletions packages/editor/src/dataviews/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ type ReduxAction =

export type ActionState = Record< string, Record< string, Action< any >[] > >;
export type FieldsState = Record< string, Record< string, Field< any >[] > >;
export type ReadyState = Record< string, Record< string, boolean > >;
export type ReadyState = Record<
string,
Record< string, Record< string, boolean > >
>;
export type State = {
actions: ActionState;
fields: FieldsState;
Expand All @@ -27,7 +30,10 @@ function isReady( state: ReadyState = {}, action: ReduxAction ) {
...state,
[ action.kind ]: {
...state[ action.kind ],
[ action.name ]: true,
[ action.name ]: {
...( state[ action.kind ]?.[ action.name ] ?? {} ),
[ action.part ]: true,
},
},
};
}
Expand Down

0 comments on commit df078fd

Please sign in to comment.