Skip to content

Commit

Permalink
avoid calling onApiAvailable until after factory.buildEmbeddable retu…
Browse files Browse the repository at this point in the history
…rns (#189304)

Fix timing issue discovered while working on control group,
#188701 (comment).

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
nreese and elasticmachine authored Jul 30, 2024
1 parent 9a7b92a commit cb5dea2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ describe('React embeddables', () => {
},
{}
);
fullApi$.next(fullApi);
fullApi$.complete();
return {
Component: () => <div> TEST DUPLICATE </div>,
api: fullApi,
Expand All @@ -234,6 +232,7 @@ describe('React embeddables', () => {
},
},
});

// render a fake Dashboard to initialize react embeddables
const FakeDashboard = () => {
return (
Expand All @@ -244,7 +243,11 @@ describe('React embeddables', () => {
<div style={{ width: '100%', height: '100px' }} key={panelId}>
<ReactEmbeddableRenderer
type={panel.type}
onApiAvailable={(api) => dashboard.children$.next({ [panelId]: api })}
onApiAvailable={(api) => {
fullApi$.next(api as Api & HasSnapshottableState<{}>);
fullApi$.complete();
dashboard.children$.next({ [panelId]: api });
}}
getParentApi={() => ({
getSerializedStateForChild: () =>
panel.explicitInput as unknown as SerializedPanelState<object> | undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ export const ReactEmbeddableRenderer = <
const setApi = (
apiRegistration: SetReactEmbeddableApiRegistration<SerializedState, RuntimeState, Api>
) => {
const fullApi = {
return {
...apiRegistration,
uuid,
phase$,
parentApi,
type: factory.type,
} as unknown as Api;
onApiAvailable?.(fullApi);
return fullApi;
};

const buildEmbeddable = async () => {
Expand Down Expand Up @@ -194,6 +192,7 @@ export const ReactEmbeddableRenderer = <

try {
const { api, Component } = await buildEmbeddable();
onApiAvailable?.(api);
return React.forwardRef<typeof api>((_, ref) => {
// expose the api into the imperative handle
useImperativeHandle(ref, () => api, []);
Expand Down

0 comments on commit cb5dea2

Please sign in to comment.