Skip to content

Commit

Permalink
Move buildContext inside use effect
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomThomson committed May 28, 2021
1 parent 0f801a9 commit 60fd0b2
Showing 1 changed file with 31 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,24 @@ export const useDashboardAppState = ({
const { version: kibanaVersion } = initializerContext.env.packageInfo;

/**
* The dashboard build context is a collection of all of the services and props required in subsequent steps to build the dashboard
* from the dashboardId. This build context should be created only once on mount, and doesn't contain any extrenuous services.
* This useEffect triggers when the dashboard ID changes, and is in charge of loading the saved dashboard,
* fetching the initial state, building the Dashboard Container embeddable, and setting up all state syncing.
*/
const dashboardBuildContext = useMemo(() => {
const context: DashboardBuildContext = {
useEffect(() => {
// fetch incoming embeddable from state transfer service.
const incomingEmbeddable = getStateTransfer().getIncomingEmbeddablePackage(
DashboardConstants.DASHBOARDS_ID,
true
);

let canceled = false;
let onDestroy: () => void;

/**
* The dashboard build context is a collection of all of the services and props required in subsequent steps to build the dashboard
* from the dashboardId. This build context doesn't contain any extrenuous services.
*/
const dashboardBuildContext: DashboardBuildContext = {
query,
search,
history,
Expand All @@ -119,40 +132,6 @@ export const useDashboardAppState = ({
$triggerDashboardRefresh: dashboardAppState.$triggerDashboardRefresh,
getLatestDashboardState: () => dashboardAppState.$onDashboardStateChange.value,
};
return context;
}, [
query,
search,
history,
embeddable,
indexPatterns,
notifications,
kibanaVersion,
savedDashboards,
kbnUrlStateStorage,
initializerContext,
isEmbeddedExternally,
dashboardCapabilities,
dispatchDashboardStateChange,
dashboardAppState.$onDashboardStateChange,
dashboardAppState.$triggerDashboardRefresh,
]);

/**
* This useEffect triggers when the dashboard ID changes, and is in charge of loading the saved dashboard,
* fetching the initial state, building the Dashboard Container embeddable, and setting up all state syncing.
*/
useEffect(() => {
const { timefilter } = dashboardBuildContext.query.timefilter;

// fetch incoming embeddable from state transfer service.
const incomingEmbeddable = getStateTransfer().getIncomingEmbeddablePackage(
DashboardConstants.DASHBOARDS_ID,
true
);

let canceled = false;
let onDestroy: () => void;

(async () => {
/**
Expand Down Expand Up @@ -228,6 +207,7 @@ export const useDashboardAppState = ({
* Any time the redux state, or the last saved state changes, compare them, set the unsaved
* changes state, and and push the unsaved changes to session storage.
*/
const { timefilter } = dashboardBuildContext.query.timefilter;
const lastSavedSubscription = combineLatest([
$onLastSavedStateChange,
dashboardAppState.$onDashboardStateChange,
Expand Down Expand Up @@ -305,16 +285,28 @@ export const useDashboardAppState = ({
onDestroy?.();
};
}, [
dashboardAppState.$triggerDashboardRefresh,
dashboardAppState.$onDashboardStateChange,
dispatchDashboardStateChange,
$onLastSavedStateChange,
dashboardSessionStorage,
dashboardBuildContext,
dashboardCapabilities,
isEmbeddedExternally,
kbnUrlStateStorage,
savedObjectsTagging,
initializerContext,
savedDashboardId,
getStateTransfer,
savedDashboards,
usageCollection,
notifications,
indexPatterns,
kibanaVersion,
embeddable,
docTitle,
history,
search,
query,
data,
]);

Expand Down

0 comments on commit 60fd0b2

Please sign in to comment.