Skip to content

Commit

Permalink
[Workspace] Support getting workspace client from coreStart (opensear…
Browse files Browse the repository at this point in the history
…ch-project#7501)

* Support to get workspace client from coreStart

Signed-off-by: yubonluo <[email protected]>

* Changeset file for PR opensearch-project#7501 created/updated

* Support to get workspace client from coreStart

Signed-off-by: yubonluo <[email protected]>

* fix test error

Signed-off-by: yubonluo <[email protected]>

* delete irrelevant modification

Signed-off-by: yubonluo <[email protected]>

* optimize the code

Signed-off-by: yubonluo <[email protected]>

* optimize the code

Signed-off-by: yubonluo <[email protected]>

* optimize the code

Signed-off-by: yubonluo <[email protected]>

---------

Signed-off-by: yubonluo <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Co-authored-by: Yulong Ruan <[email protected]>
  • Loading branch information
3 people authored and Qxisylolo committed Aug 20, 2024
1 parent 29d4308 commit a1f1666
Show file tree
Hide file tree
Showing 20 changed files with 283 additions and 137 deletions.
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

0 comments on commit a1f1666

Please sign in to comment.