Skip to content

Commit

Permalink
Support to get workspace client from coreStart
Browse files Browse the repository at this point in the history
Signed-off-by: yubonluo <[email protected]>
  • Loading branch information
yubonluo committed Jul 26, 2024
1 parent 236488d commit 7566a22
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 139 deletions.
10 changes: 8 additions & 2 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import {
HandlerParameters,
} from './context';
import { Branding } from '../types';
import { WorkspacesStart, WorkspacesSetup } from './workspace';
import { WorkspacesStart, WorkspacesSetup, IWorkspaceClient } from './workspace';

export type { Logos } from '../common';
export { PackageInfo, EnvironmentMode } from '../server/types';
Expand Down Expand Up @@ -385,6 +385,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();
}
}
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

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion src/plugins/saved_objects_management/public/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@ export { extractExportDetails, SavedObjectsExportResultDetails } from './extract
export { createFieldList } from './create_field_list';
export { getAllowedTypes } from './get_allowed_types';
export { filterQuery } from './filter_query';
export { duplicateSavedObjects } from './duplicate_saved_objects';
Loading

0 comments on commit 7566a22

Please sign in to comment.