Skip to content

Commit

Permalink
[Workspace] Feature/workspace service core module (#5092)
Browse files Browse the repository at this point in the history
* feat: add core workspace module (#145)

The core workspace module(WorkspaceService) is a foundational component
that enables the implementation of workspace features within OSD
plugins. The purpose of the core workspace module is to provide
a framework for workspace implementations.

This module does not implement specific workspace
functionality but provides the essential infrastructure for plugins to
extend and customize workspace features, it maintains a shared
workspace state(observables) across the entire application to ensure
a consistent and up-to-date view of workspace-related information to
all parts of the application.

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
Signed-off-by: Yulong Ruan <ruanyu1@gmail.com>
Co-authored-by: Miki <amoo_miki@yahoo.com>
(cherry picked from commit 9e3e3a7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
github-actions[bot] committed Nov 1, 2023
1 parent 603ecff commit 9f656ae
Show file tree
Hide file tree
Showing 24 changed files with 746 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const createStartContractMock = (): jest.Mocked<CapabilitiesStart> => ({
catalogue: {},
management: {},
navLinks: {},
workspaces: {},
}),
});

Expand Down
3 changes: 3 additions & 0 deletions src/core/public/application/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { IUiSettingsClient } from '../ui_settings';
import { SavedObjectsStart } from '../saved_objects';
import { AppCategory } from '../../types';
import { ScopedHistory } from './scoped_history';
import { WorkspacesStart } from '../workspace';

/**
* Accessibility status of an application.
Expand Down Expand Up @@ -334,6 +335,8 @@ export interface AppMountContext {
injectedMetadata: {
getInjectedVar: (name: string, defaultValue?: any) => unknown;
};
/** {@link WorkspacesService} */
workspaces: WorkspacesStart;
};
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/core/public/core_system.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { docLinksServiceMock } from './doc_links/doc_links_service.mock';
import { renderingServiceMock } from './rendering/rendering_service.mock';
import { contextServiceMock } from './context/context_service.mock';
import { integrationsServiceMock } from './integrations/integrations_service.mock';
import { workspacesServiceMock } from './workspace/workspaces_service.mock';
import { coreAppMock } from './core_app/core_app.mock';

export const MockInjectedMetadataService = injectedMetadataServiceMock.create();
Expand Down Expand Up @@ -145,3 +146,11 @@ export const CoreAppConstructor = jest.fn().mockImplementation(() => MockCoreApp
jest.doMock('./core_app', () => ({
CoreApp: CoreAppConstructor,
}));

export const MockWorkspacesService = workspacesServiceMock.create();
export const WorkspacesServiceConstructor = jest
.fn()
.mockImplementation(() => MockWorkspacesService);
jest.doMock('./workspace', () => ({
WorkspacesService: WorkspacesServiceConstructor,
}));
21 changes: 21 additions & 0 deletions src/core/public/core_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import {
MockIntegrationsService,
CoreAppConstructor,
MockCoreApp,
WorkspacesServiceConstructor,
MockWorkspacesService,
} from './core_system.test.mocks';

import { CoreSystem } from './core_system';
Expand Down Expand Up @@ -99,6 +101,7 @@ describe('constructor', () => {
expect(RenderingServiceConstructor).toHaveBeenCalledTimes(1);
expect(IntegrationsServiceConstructor).toHaveBeenCalledTimes(1);
expect(CoreAppConstructor).toHaveBeenCalledTimes(1);
expect(WorkspacesServiceConstructor).toHaveBeenCalledTimes(1);
});

it('passes injectedMetadata param to InjectedMetadataService', () => {
Expand Down Expand Up @@ -223,6 +226,11 @@ describe('#setup()', () => {
expect(MockIntegrationsService.setup).toHaveBeenCalledTimes(1);
});

it('calls workspaces#setup()', async () => {
await setupCore();
expect(MockWorkspacesService.setup).toHaveBeenCalledTimes(1);
});

it('calls coreApp#setup()', async () => {
await setupCore();
expect(MockCoreApp.setup).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -310,6 +318,11 @@ describe('#start()', () => {
expect(MockIntegrationsService.start).toHaveBeenCalledTimes(1);
});

it('calls workspaces#start()', async () => {
await startCore();
expect(MockWorkspacesService.start).toHaveBeenCalledTimes(1);
});

it('calls coreApp#start()', async () => {
await startCore();
expect(MockCoreApp.start).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -364,6 +377,14 @@ describe('#stop()', () => {
expect(MockIntegrationsService.stop).toHaveBeenCalled();
});

it('calls workspaces.stop()', () => {
const coreSystem = createCoreSystem();

expect(MockWorkspacesService.stop).not.toHaveBeenCalled();
coreSystem.stop();
expect(MockWorkspacesService.stop).toHaveBeenCalled();
});

it('calls coreApp.stop()', () => {
const coreSystem = createCoreSystem();

Expand Down
9 changes: 9 additions & 0 deletions src/core/public/core_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { ContextService } from './context';
import { IntegrationsService } from './integrations';
import { CoreApp } from './core_app';
import type { InternalApplicationSetup, InternalApplicationStart } from './application/types';
import { WorkspacesService } from './workspace';

interface Params {
rootDomElement: HTMLElement;
Expand Down Expand Up @@ -110,6 +111,7 @@ export class CoreSystem {

private readonly rootDomElement: HTMLElement;
private readonly coreContext: CoreContext;
private readonly workspaces: WorkspacesService;
private fatalErrorsSetup: FatalErrorsSetup | null = null;

constructor(params: Params) {
Expand Down Expand Up @@ -138,6 +140,7 @@ export class CoreSystem {
this.rendering = new RenderingService();
this.application = new ApplicationService();
this.integrations = new IntegrationsService();
this.workspaces = new WorkspacesService();

this.coreContext = { coreId: Symbol('core'), env: injectedMetadata.env };

Expand All @@ -160,6 +163,7 @@ export class CoreSystem {
const http = this.http.setup({ injectedMetadata, fatalErrors: this.fatalErrorsSetup });
const uiSettings = this.uiSettings.setup({ http, injectedMetadata });
const notifications = this.notifications.setup({ uiSettings });
const workspaces = this.workspaces.setup();

const pluginDependencies = this.plugins.getOpaqueIds();
const context = this.context.setup({
Expand All @@ -176,6 +180,7 @@ export class CoreSystem {
injectedMetadata,
notifications,
uiSettings,
workspaces,
};

// Services that do not expose contracts at setup
Expand Down Expand Up @@ -220,6 +225,7 @@ export class CoreSystem {
targetDomElement: notificationsTargetDomElement,
});
const application = await this.application.start({ http, overlays });
const workspaces = this.workspaces.start();
const chrome = await this.chrome.start({
application,
docLinks,
Expand All @@ -242,6 +248,7 @@ export class CoreSystem {
overlays,
savedObjects,
uiSettings,
workspaces,
}));

const core: InternalCoreStart = {
Expand All @@ -256,6 +263,7 @@ export class CoreSystem {
overlays,
uiSettings,
fatalErrors,
workspaces,
};

await this.plugins.start(core);
Expand Down Expand Up @@ -303,6 +311,7 @@ export class CoreSystem {
this.chrome.stop();
this.i18n.stop();
this.application.stop();
this.workspaces.stop();
this.rootDomElement.textContent = '';
}
}
8 changes: 8 additions & 0 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import {
HandlerParameters,
} from './context';
import { Branding } from '../types';
import { WorkspacesStart, WorkspacesSetup } from './workspace';

export type { Logos } from '../common';
export { PackageInfo, EnvironmentMode } from '../server/types';
Expand All @@ -102,6 +103,7 @@ export {
StringValidation,
StringValidationRegex,
StringValidationRegexString,
WorkspaceAttribute,
} from '../types';

export {
Expand Down Expand Up @@ -239,6 +241,8 @@ export interface CoreSetup<TPluginsStart extends object = object, TStart = unkno
};
/** {@link StartServicesAccessor} */
getStartServices: StartServicesAccessor<TPluginsStart, TStart>;
/** {@link WorkspacesSetup} */
workspaces: WorkspacesSetup;
}

/**
Expand Down Expand Up @@ -293,6 +297,8 @@ export interface CoreStart {
getInjectedVar: (name: string, defaultValue?: any) => unknown;
getBranding: () => Branding;
};
/** {@link WorkspacesStart} */
workspaces: WorkspacesStart;
}

export {
Expand Down Expand Up @@ -341,3 +347,5 @@ export {
};

export { __osdBootstrap__ } from './osd_bootstrap';

export { WorkspacesStart, WorkspacesSetup } from './workspace';
4 changes: 4 additions & 0 deletions src/core/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
import { savedObjectsServiceMock } from './saved_objects/saved_objects_service.mock';
import { contextServiceMock } from './context/context_service.mock';
import { injectedMetadataServiceMock } from './injected_metadata/injected_metadata_service.mock';
import { workspacesServiceMock } from './workspace/workspaces_service.mock';

export { chromeServiceMock } from './chrome/chrome_service.mock';
export { docLinksServiceMock } from './doc_links/doc_links_service.mock';
Expand All @@ -60,6 +61,7 @@ export { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
export { savedObjectsServiceMock } from './saved_objects/saved_objects_service.mock';
export { scopedHistoryMock } from './application/scoped_history.mock';
export { applicationServiceMock } from './application/application_service.mock';
export { workspacesServiceMock } from './workspace/workspaces_service.mock';

function createCoreSetupMock({
basePath = '',
Expand All @@ -85,6 +87,7 @@ function createCoreSetupMock({
getInjectedVar: injectedMetadataServiceMock.createSetupContract().getInjectedVar,
getBranding: injectedMetadataServiceMock.createSetupContract().getBranding,
},
workspaces: workspacesServiceMock.createSetupContract(),
};

return mock;
Expand All @@ -106,6 +109,7 @@ function createCoreStartMock({ basePath = '' } = {}) {
getBranding: injectedMetadataServiceMock.createStartContract().getBranding,
},
fatalErrors: fatalErrorsServiceMock.createStartContract(),
workspaces: workspacesServiceMock.createStartContract(),
};

return mock;
Expand Down
2 changes: 2 additions & 0 deletions src/core/public/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export function createPluginSetupContext<
getBranding: deps.injectedMetadata.getBranding,
},
getStartServices: () => plugin.startDependencies,
workspaces: deps.workspaces,
};
}

Expand Down Expand Up @@ -168,5 +169,6 @@ export function createPluginStartContext<
getBranding: deps.injectedMetadata.getBranding,
},
fatalErrors: deps.fatalErrors,
workspaces: deps.workspaces,
};
}
3 changes: 3 additions & 0 deletions src/core/public/plugins/plugins_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { CoreSetup, CoreStart, PluginInitializerContext } from '..';
import { docLinksServiceMock } from '../doc_links/doc_links_service.mock';
import { savedObjectsServiceMock } from '../saved_objects/saved_objects_service.mock';
import { contextServiceMock } from '../context/context_service.mock';
import { workspacesServiceMock } from '../workspace/workspaces_service.mock';

export let mockPluginInitializers: Map<PluginName, MockedPluginInitializer>;

Expand Down Expand Up @@ -108,6 +109,7 @@ describe('PluginsService', () => {
injectedMetadata: injectedMetadataServiceMock.createStartContract(),
notifications: notificationServiceMock.createSetupContract(),
uiSettings: uiSettingsServiceMock.createSetupContract(),
workspaces: workspacesServiceMock.createSetupContract(),
};
mockSetupContext = {
...mockSetupDeps,
Expand All @@ -127,6 +129,7 @@ describe('PluginsService', () => {
uiSettings: uiSettingsServiceMock.createStartContract(),
savedObjects: savedObjectsServiceMock.createStartContract(),
fatalErrors: fatalErrorsServiceMock.createStartContract(),
workspaces: workspacesServiceMock.createStartContract(),
};
mockStartContext = {
...mockStartDeps,
Expand Down
6 changes: 6 additions & 0 deletions src/core/public/workspace/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export { WorkspacesStart, WorkspacesService, WorkspacesSetup } from './workspaces_service';
42 changes: 42 additions & 0 deletions src/core/public/workspace/workspaces_service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { BehaviorSubject } from 'rxjs';
import type { PublicMethodsOf } from '@osd/utility-types';

import { WorkspacesService } from './workspaces_service';
import { WorkspaceAttribute } from '..';

const currentWorkspaceId$ = new BehaviorSubject<string>('');
const workspaceList$ = new BehaviorSubject<WorkspaceAttribute[]>([]);
const currentWorkspace$ = new BehaviorSubject<WorkspaceAttribute | null>(null);
const initialized$ = new BehaviorSubject<boolean>(false);

const createWorkspacesSetupContractMock = () => ({
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
initialized$,
});

const createWorkspacesStartContractMock = () => ({
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
initialized$,
});

export type WorkspacesServiceContract = PublicMethodsOf<WorkspacesService>;
const createMock = (): jest.Mocked<WorkspacesServiceContract> => ({
setup: jest.fn().mockReturnValue(createWorkspacesSetupContractMock()),
start: jest.fn().mockReturnValue(createWorkspacesStartContractMock()),
stop: jest.fn(),
});

export const workspacesServiceMock = {
create: createMock,
createSetupContract: createWorkspacesSetupContractMock,
createStartContract: createWorkspacesStartContractMock,
};
Loading

0 comments on commit 9f656ae

Please sign in to comment.