Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace] Support getting workspace client from coreStart #7501

Merged
Merged
2 changes: 2 additions & 0 deletions changelogs/fragments/7501.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
refactor:
- [Workspace] Support getting workspaces client from coreStart ([#7501](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7501))
8 changes: 7 additions & 1 deletion src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,12 @@ export {

export { __osdBootstrap__ } from './osd_bootstrap';

export { WorkspacesStart, WorkspacesSetup, WorkspacesService, WorkspaceObject } from './workspace';
export {
WorkspacesStart,
WorkspacesSetup,
WorkspacesService,
WorkspaceObject,
IWorkspaceClient,
} from './workspace';

export { debounce } from './utils';
1 change: 1 addition & 0 deletions src/core/public/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export {
WorkspacesService,
WorkspacesSetup,
WorkspaceObject,
IWorkspaceClient,
} from './workspaces_service';
6 changes: 5 additions & 1 deletion src/core/public/workspace/workspaces_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { BehaviorSubject } from 'rxjs';
import type { PublicMethodsOf } from '@osd/utility-types';

import { WorkspacesService, WorkspaceObject } from './workspaces_service';
import { WorkspacesService, WorkspaceObject, IWorkspaceClient } from './workspaces_service';

const createWorkspacesSetupContractMock = () => {
const currentWorkspaceId$ = new BehaviorSubject<string>('');
Expand All @@ -18,6 +18,7 @@ const createWorkspacesSetupContractMock = () => {
workspaceList$,
currentWorkspace$,
initialized$,
setClient: jest.fn(),
};
};

Expand All @@ -26,11 +27,14 @@ const createWorkspacesStartContractMock = () => {
const workspaceList$ = new BehaviorSubject<WorkspaceObject[]>([]);
const currentWorkspace$ = new BehaviorSubject<WorkspaceObject | null>(null);
const initialized$ = new BehaviorSubject<boolean>(false);
const client$ = new BehaviorSubject<IWorkspaceClient | null>(null);

return {
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
initialized$,
client$,
};
};

Expand Down
19 changes: 18 additions & 1 deletion src/core/public/workspace/workspaces_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { WorkspacesService, WorkspacesStart } from './workspaces_service';
import {
WorkspacesService,
WorkspacesSetup,
WorkspacesStart,
IWorkspaceClient,
} from './workspaces_service';

describe('WorkspacesService', () => {
let workspaces: WorkspacesService;
let workspacesStart: WorkspacesStart;
let workspacesSetUp: WorkspacesSetup;

beforeEach(() => {
workspaces = new WorkspacesService();
workspacesSetUp = workspaces.setup();
workspacesStart = workspaces.start();
});

Expand All @@ -31,6 +38,16 @@ describe('WorkspacesService', () => {
expect(workspacesStart.workspaceList$.value.length).toBe(0);
});

it('client$ is not set by default', () => {
expect(workspacesStart.client$.value).toBe(null);
});

it('client is updated when set client', () => {
const client: IWorkspaceClient = { copy: jest.fn() };
workspacesSetUp.setClient(client);
expect(workspacesStart.client$.value).toEqual(client);
});

it('currentWorkspace is updated when currentWorkspaceId changes', () => {
expect(workspacesStart.currentWorkspace$.value).toBe(null);

Expand Down
19 changes: 17 additions & 2 deletions src/core/public/workspace/workspaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { CoreService, WorkspaceAttribute } from '../../types';

export type WorkspaceObject = WorkspaceAttribute & { readonly?: boolean };

export interface IWorkspaceClient {
copy(objects: any[], targetWorkspace: string, includeReferencesDeep?: boolean): Promise<any>;
}

interface WorkspaceObservables {
/**
* Indicates the current activated workspace id, the value should be changed every time
Expand Down Expand Up @@ -43,14 +47,20 @@ enum WORKSPACE_ERROR {
WORKSPACE_IS_STALE = 'WORKSPACE_IS_STALE',
}

export type WorkspacesSetup = WorkspaceObservables;
export type WorkspacesStart = WorkspaceObservables;
export type WorkspacesSetup = WorkspaceObservables & {
setClient: (client: IWorkspaceClient) => void;
};

export type WorkspacesStart = WorkspaceObservables & {
client$: BehaviorSubject<IWorkspaceClient | null>;
};

export class WorkspacesService implements CoreService<WorkspacesSetup, WorkspacesStart> {
private currentWorkspaceId$ = new BehaviorSubject<string>('');
private workspaceList$ = new BehaviorSubject<WorkspaceObject[]>([]);
private currentWorkspace$ = new BehaviorSubject<WorkspaceObject | null>(null);
private initialized$ = new BehaviorSubject<boolean>(false);
private client$ = new BehaviorSubject<IWorkspaceClient | null>(null);

constructor() {
combineLatest([this.initialized$, this.workspaceList$, this.currentWorkspaceId$]).subscribe(
Expand Down Expand Up @@ -87,6 +97,9 @@ export class WorkspacesService implements CoreService<WorkspacesSetup, Workspace
currentWorkspace$: this.currentWorkspace$,
workspaceList$: this.workspaceList$,
initialized$: this.initialized$,
setClient: (client: IWorkspaceClient) => {
this.client$.next(client);
},
};
}

Expand All @@ -96,6 +109,7 @@ export class WorkspacesService implements CoreService<WorkspacesSetup, Workspace
currentWorkspace$: this.currentWorkspace$,
workspaceList$: this.workspaceList$,
initialized$: this.initialized$,
client$: this.client$,
};
}

Expand All @@ -104,5 +118,6 @@ export class WorkspacesService implements CoreService<WorkspacesSetup, Workspace
this.currentWorkspaceId$.unsubscribe();
this.workspaceList$.unsubscribe();
this.initialized$.unsubscribe();
this.client$.unsubscribe();
}
}

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

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

1 change: 0 additions & 1 deletion src/plugins/saved_objects_management/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export {
ProcessedImportResponse,
processImportResponse,
FailedImport,
duplicateSavedObjects,
getSavedObjectLabel,
} from './lib';
export { SavedObjectRelation, SavedObjectWithMetadata, SavedObjectMetadata } from './types';
Expand Down
Loading
Loading