Skip to content

Commit

Permalink
Lazy ingestManager setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Jul 13, 2020
1 parent af87915 commit 31c3462
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
45 changes: 25 additions & 20 deletions x-pack/plugins/ingest_manager/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface IngestManagerSetup {}
*/
export interface IngestManagerStart {
registerPackageConfigComponent: typeof registerPackageConfigComponent;
success: Promise<true>;
waitForInitialization: () => Promise<true>;
}

export interface IngestManagerSetupDeps {
Expand Down Expand Up @@ -100,27 +100,32 @@ export class IngestManagerPlugin
}

public async start(core: CoreStart): Promise<IngestManagerStart> {
let successPromise: IngestManagerStart['success'];
try {
const permissionsResponse = await core.http.get<CheckPermissionsResponse>(
appRoutesService.getCheckPermissionsPath()
);

if (permissionsResponse?.success) {
successPromise = core.http
.post<PostIngestSetupResponse>(setupRouteService.getSetupPath())
.then(({ isInitialized }) =>
isInitialized ? Promise.resolve(true) : Promise.reject(new Error('Unknown setup error'))
);
} else {
throw new Error(permissionsResponse?.error || 'Unknown permissions error');
}
} catch (error) {
successPromise = Promise.reject(error);
}
let successPromise: ReturnType<IngestManagerStart['waitForInitialization']>;

return {
success: successPromise,
waitForInitialization: () => {
if (!successPromise) {
successPromise = Promise.resolve().then(async () => {
const permissionsResponse = await core.http.get<CheckPermissionsResponse>(
appRoutesService.getCheckPermissionsPath()
);

if (permissionsResponse?.success) {
return core.http
.post<PostIngestSetupResponse>(setupRouteService.getSetupPath())
.then(({ isInitialized }) =>
isInitialized
? Promise.resolve(true)
: Promise.reject(new Error('Unknown setup error'))
);
} else {
throw new Error(permissionsResponse?.error || 'Unknown permissions error');
}
});
}

return successPromise;
},
registerPackageConfigComponent,
};
}
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/security_solution/public/app/home/setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export const Setup: React.FunctionComponent<{
});
};

ingestManager.success.catch((error: Error) => displayToastWithModal(error.message));
ingestManager
.waitForInitialization()
.catch((error: Error) => displayToastWithModal(error.message));
}, [ingestManager, notifications.toasts]);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export const depsStartMock: () => DepsStartMock = () => {

return {
data: dataMock,
ingestManager: { success: Promise.resolve(true), registerPackageConfigComponent },
ingestManager: {
waitForInitialization: () => Promise.resolve(true),
registerPackageConfigComponent,
},
};
};

0 comments on commit 31c3462

Please sign in to comment.