[embeddable] fix race condition in useStateFromPublishingSubject (#216522)

Related to https://github.com/elastic/kibana/pull/216399

PR
* updates `useStateFromPublishingSubject` to require `subject`, thus,
removing complexities of setting up subscription when `subject` is
optionally provided.
* Updates `useStateFromPublishingSubject` to setup subscription with
`useMemo` to avoid timing issues.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Nathan Reese 2025-04-03 09:35:59 -06:00 committed by GitHub
parent 4c3274d3a2
commit 5a3c2c0f05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 102 additions and 96 deletions

View file

@ -12,8 +12,8 @@ import { css } from '@emotion/react';
import { ReactEmbeddableFactory } from '@kbn/embeddable-plugin/public';
import { i18n } from '@kbn/i18n';
import {
getViewModeSubject,
initializeTitleManager,
useInheritedViewMode,
useStateFromPublishingSubject,
} from '@kbn/presentation-publishing';
import React from 'react';
@ -78,7 +78,9 @@ export const markdownEmbeddableFactory: ReactEmbeddableFactory<
Component: () => {
// get state for rendering
const content = useStateFromPublishingSubject(content$);
const viewMode = useInheritedViewMode(api) ?? 'view';
const viewMode = useStateFromPublishingSubject(
getViewModeSubject(api) ?? new BehaviorSubject('view')
);
const { euiTheme } = useEuiTheme();
return viewMode === 'edit' ? (