Skip to content

Commit

Permalink
[ML] Review feedback: Simplify code structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Jun 26, 2020
1 parent dde9f54 commit c91b10d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 47 deletions.
6 changes: 5 additions & 1 deletion x-pack/plugins/ml/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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';
Expand Down Expand Up @@ -81,9 +82,12 @@ export const renderApp = (

appMountParams.onAppLeave((actions) => actions.default());

ReactDOM.render(<App coreStart={coreStart} deps={deps} />, appMountParams.element);
const mlLicense = setLicenseCache(deps.licensing, [
() => ReactDOM.render(<App coreStart={coreStart} deps={deps} />, appMountParams.element),
]);

return () => {
mlLicense.unsubscribe();
clearCache();
ReactDOM.unmountComponentAtNode(appMountParams.element);
};
Expand Down
68 changes: 22 additions & 46 deletions x-pack/plugins/ml/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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;
Expand Down Expand Up @@ -68,52 +67,29 @@ export class MlPlugin implements Plugin<MlPluginSetup, MlPluginStart> {
const [coreStart, pluginsStart] = await core.getStartServices();
const kibanaVersion = this.initializerContext.env.packageInfo.version;
const { renderApp } = await import('./application/app');

// 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 gets 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,
}
);
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,
},
]);

// 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();
};
{
element: params.element,
appBasePath: params.appBasePath,
onAppLeave: params.onAppLeave,
history: params.history,
}
);
},
});

Expand Down

0 comments on commit c91b10d

Please sign in to comment.