diff --git a/x-pack/plugins/ml/public/application/app.tsx b/x-pack/plugins/ml/public/application/app.tsx index 3df176ff25cb41..8b50d794d070d1 100644 --- a/x-pack/plugins/ml/public/application/app.tsx +++ b/x-pack/plugins/ml/public/application/app.tsx @@ -13,7 +13,6 @@ import { Storage } from '../../../../../src/plugins/kibana_utils/public'; import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public'; import { setDependencyCache, clearCache } from './util/dependency_cache'; -import { setLicenseCache } from './license'; import { MlSetupDependencies, MlStartDependencies } from '../plugin'; import { MlRouter } from './routing'; @@ -80,14 +79,11 @@ export const renderApp = ( deps.kibanaLegacy.loadFontAwesome(); - const mlLicense = setLicenseCache(deps.licensing); - appMountParams.onAppLeave((actions) => actions.default()); ReactDOM.render(, appMountParams.element); return () => { - mlLicense.unsubscribe(); clearCache(); ReactDOM.unmountComponentAtNode(appMountParams.element); }; diff --git a/x-pack/plugins/ml/public/application/license/check_license.tsx b/x-pack/plugins/ml/public/application/license/check_license.tsx index 3584ee8fbee4b6..583eec7d75414d 100644 --- a/x-pack/plugins/ml/public/application/license/check_license.tsx +++ b/x-pack/plugins/ml/public/application/license/check_license.tsx @@ -5,6 +5,7 @@ */ import { LicensingPluginSetup } from '../../../../licensing/public'; +import { MlLicense } from '../../../common/license'; import { MlClientLicense } from './ml_client_license'; let mlLicense: MlClientLicense | null = null; @@ -16,9 +17,12 @@ let mlLicense: MlClientLicense | null = null; * @param {LicensingPluginSetup} licensingSetup * @returns {MlClientLicense} */ -export function setLicenseCache(licensingSetup: LicensingPluginSetup) { +export function setLicenseCache( + licensingSetup: LicensingPluginSetup, + postInitFunctions?: Array<(lic: MlLicense) => void> +) { mlLicense = new MlClientLicense(); - mlLicense.setup(licensingSetup.license$); + mlLicense.setup(licensingSetup.license$, postInitFunctions); return mlLicense; } diff --git a/x-pack/plugins/ml/public/plugin.ts b/x-pack/plugins/ml/public/plugin.ts index 7f7544a44efa7f..1659d8fa4f0f56 100644 --- a/x-pack/plugins/ml/public/plugin.ts +++ b/x-pack/plugins/ml/public/plugin.ts @@ -31,6 +31,7 @@ import { registerEmbeddables } from './embeddables'; import { UiActionsSetup } from '../../../../src/plugins/ui_actions/public'; import { registerMlUiActions } from './ui_actions'; import { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public'; +import { setLicenseCache } from './application/license'; export interface MlStartDependencies { data: DataPublicPluginStart; @@ -67,29 +68,52 @@ export class MlPlugin implements Plugin { const [coreStart, pluginsStart] = await core.getStartServices(); const kibanaVersion = this.initializerContext.env.packageInfo.version; const { renderApp } = await import('./application/app'); - return renderApp( - coreStart, - { - data: pluginsStart.data, - share: pluginsStart.share, - kibanaLegacy: pluginsStart.kibanaLegacy, - security: pluginsSetup.security, - licensing: pluginsSetup.licensing, - management: pluginsSetup.management, - usageCollection: pluginsSetup.usageCollection, - licenseManagement: pluginsSetup.licenseManagement, - home: pluginsSetup.home, - embeddable: pluginsSetup.embeddable, - uiActions: pluginsSetup.uiActions, - kibanaVersion, + + // Creates a mutable object containing + // a boilerplate callback function which + // we'll update with the real callback once + // the license listener mounts the app. + const returnRef = { callback: () => {} }; + + // We pass the code to mount the app as a `postInitFunctions` + // callback to setLicenseCache/MlLicense.setup() so we make + // sure the app get mounted only after we received the + // license information. + const mlLicense = setLicenseCache(pluginsSetup.licensing, [ + () => { + returnRef.callback = renderApp( + coreStart, + { + data: pluginsStart.data, + share: pluginsStart.share, + kibanaLegacy: pluginsStart.kibanaLegacy, + security: pluginsSetup.security, + licensing: pluginsSetup.licensing, + management: pluginsSetup.management, + usageCollection: pluginsSetup.usageCollection, + licenseManagement: pluginsSetup.licenseManagement, + home: pluginsSetup.home, + embeddable: pluginsSetup.embeddable, + uiActions: pluginsSetup.uiActions, + kibanaVersion, + }, + { + element: params.element, + appBasePath: params.appBasePath, + onAppLeave: params.onAppLeave, + history: params.history, + } + ); }, - { - element: params.element, - appBasePath: params.appBasePath, - onAppLeave: params.onAppLeave, - history: params.history, - } - ); + ]); + + // Finally we return the unmount callback which includes + // unsubscribing from the license listener as well as + // the overall unmount callback from renderApp(). + return () => { + mlLicense.unsubscribe(); + returnRef.callback(); + }; }, });