Skip to content

Commit

Permalink
refactor: derive currentWorkspace in workspaces_service (opensearch-p…
Browse files Browse the repository at this point in the history
…roject#126)

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
  • Loading branch information
ruanyl committed Sep 15, 2023
1 parent a23b39c commit 2d3800b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 89 deletions.

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.

6 changes: 3 additions & 3 deletions src/core/public/workspace/workspaces_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { WorkspaceAttribute } from '..';
const currentWorkspaceId$ = new BehaviorSubject<string>('');
const workspaceList$ = new BehaviorSubject<WorkspaceAttribute[]>([]);
const currentWorkspace$ = new BehaviorSubject<WorkspaceAttribute | null>(null);
const hasFetchedWorkspaceList$ = new BehaviorSubject<boolean>(false);
const initialized$ = new BehaviorSubject<boolean>(false);
const workspaceEnabled$ = new BehaviorSubject<boolean>(false);

const createWorkspacesSetupContractMock = () => ({
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
hasFetchedWorkspaceList$,
initialized$,
workspaceEnabled$,
registerWorkspaceMenuRender: jest.fn(),
});
Expand All @@ -25,7 +25,7 @@ const createWorkspacesStartContractMock = () => ({
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
hasFetchedWorkspaceList$,
initialized$,
workspaceEnabled$,
renderWorkspaceMenu: jest.fn(),
});
Expand Down
47 changes: 41 additions & 6 deletions src/core/public/workspace/workspaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, combineLatest } from 'rxjs';
import { isEqual } from 'lodash';

import { CoreService, WorkspaceAttribute } from '../../types';
import { InternalApplicationStart } from '../application';
import { HttpSetup } from '../http';
Expand All @@ -23,7 +25,11 @@ export interface WorkspaceObservables {
currentWorkspace$: BehaviorSubject<WorkspaceAttribute | null>;
workspaceList$: BehaviorSubject<WorkspaceAttribute[]>;
workspaceEnabled$: BehaviorSubject<boolean>;
hasFetchedWorkspaceList$: BehaviorSubject<boolean>;
initialized$: BehaviorSubject<boolean>;
}

enum WORKSPACE_ERROR_REASON_MAP {
WORKSPACE_STALED = 'WORKSPACE_STALED',
}

/**
Expand All @@ -41,16 +47,45 @@ export class WorkspaceService implements CoreService<WorkspaceSetup, WorkspaceSt
private currentWorkspaceId$ = new BehaviorSubject<string>('');
private workspaceList$ = new BehaviorSubject<WorkspaceAttribute[]>([]);
private currentWorkspace$ = new BehaviorSubject<WorkspaceAttribute | null>(null);
private hasFetchedWorkspaceList$ = new BehaviorSubject<boolean>(false);
private initialized$ = new BehaviorSubject<boolean>(false);
private workspaceEnabled$ = new BehaviorSubject<boolean>(false);
private _renderWorkspaceMenu: WorkspaceMenuRenderFn | null = null;

constructor() {
combineLatest([this.initialized$, this.workspaceList$, this.currentWorkspaceId$]).subscribe(
([workspaceInitialized, workspaceList, currentWorkspaceId]) => {
if (workspaceInitialized) {
const currentWorkspace = workspaceList.find((w) => w && w.id === currentWorkspaceId);

/**
* Do a simple idempotent verification here
*/
if (!isEqual(currentWorkspace, this.currentWorkspace$.getValue())) {
this.currentWorkspace$.next(currentWorkspace ?? null);
}

if (currentWorkspaceId && !currentWorkspace?.id) {
/**
* Current workspace is staled
*/
this.currentWorkspaceId$.error({
reason: WORKSPACE_ERROR_REASON_MAP.WORKSPACE_STALED,
});
this.currentWorkspace$.error({
reason: WORKSPACE_ERROR_REASON_MAP.WORKSPACE_STALED,
});
}
}
}
);
}

public setup(): WorkspaceSetup {
return {
currentWorkspaceId$: this.currentWorkspaceId$,
currentWorkspace$: this.currentWorkspace$,
workspaceList$: this.workspaceList$,
hasFetchedWorkspaceList$: this.hasFetchedWorkspaceList$,
initialized$: this.initialized$,
workspaceEnabled$: this.workspaceEnabled$,
registerWorkspaceMenuRender: (render: WorkspaceMenuRenderFn) =>
(this._renderWorkspaceMenu = render),
Expand All @@ -68,7 +103,7 @@ export class WorkspaceService implements CoreService<WorkspaceSetup, WorkspaceSt
currentWorkspaceId$: this.currentWorkspaceId$,
currentWorkspace$: this.currentWorkspace$,
workspaceList$: this.workspaceList$,
hasFetchedWorkspaceList$: this.hasFetchedWorkspaceList$,
initialized$: this.initialized$,
workspaceEnabled$: this.workspaceEnabled$,
};
return {
Expand All @@ -91,7 +126,7 @@ export class WorkspaceService implements CoreService<WorkspaceSetup, WorkspaceSt
this.currentWorkspaceId$.unsubscribe();
this.workspaceList$.unsubscribe();
this.workspaceEnabled$.unsubscribe();
this.hasFetchedWorkspaceList$.unsubscribe();
this.initialized$.unsubscribe();
this._renderWorkspaceMenu = null;
}
}
4 changes: 2 additions & 2 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export class WorkspacePlugin implements Plugin<{}, {}, WorkspacePluginSetupDeps>
public async setup(core: CoreSetup, { savedObjectsManagement }: WorkspacePluginSetupDeps) {
core.workspaces.workspaceEnabled$.next(true);
core.workspaces.registerWorkspaceMenuRender(renderWorkspaceMenu);

const workspaceClient = new WorkspaceClient(core.http, core.workspaces);
workspaceClient.init();
await workspaceClient.init();

/**
* Retrieve workspace id from url
Expand Down
Loading

0 comments on commit 2d3800b

Please sign in to comment.