Skip to content

Commit

Permalink
Add currentWorkspace$ (opensearch-project#15)
Browse files Browse the repository at this point in the history
* feat: add currentWorkspace$

Signed-off-by: SuZhoue-Joe <[email protected]>

* fix: type error

Signed-off-by: SuZhoue-Joe <[email protected]>

* feat: add emit on currentWorkspace$

Signed-off-by: SuZhoue-Joe <[email protected]>

---------

Signed-off-by: SuZhoue-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe authored and Hailong-am committed Jul 18, 2023
1 parent ac27fa5 commit ed7fd63
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/core/public/fatal_errors/fatal_errors_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import type { PublicMethodsOf } from '@osd/utility-types';
import { FatalErrorsService, FatalErrorsSetup } from './fatal_errors_service';
import { BehaviorSubject, Subject } from 'rxjs';
import { BehaviorSubject } from 'rxjs';
import { WorkspaceAttribute } from '../workspace';

const createSetupContractMock = () => {
Expand Down Expand Up @@ -62,12 +62,14 @@ export const fatalErrorsServiceMock = {
};

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

const createWorkspacesSetupContractMock = () => ({
client: {
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
stop: jest.fn(),
enterWorkspace: jest.fn(),
exitWorkspace: jest.fn(),
Expand Down
46 changes: 35 additions & 11 deletions src/core/public/workspace/workspaces_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
import type { PublicContract } from '@osd/utility-types';
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, combineLatest } from 'rxjs';
import { isEqual } from 'lodash';
import { HttpFetchError, HttpFetchOptions, HttpSetup } from '../http';
import { WorkspaceAttribute, WorkspaceFindOptions } from '.';
import { WORKSPACES_API_BASE_URL, WORKSPACE_ERROR_REASON_MAP } from './consts';
Expand Down Expand Up @@ -41,33 +42,56 @@ export class WorkspacesClient {
private http: HttpSetup;
public currentWorkspaceId$ = new BehaviorSubject<string>('');
public workspaceList$ = new BehaviorSubject<WorkspaceAttribute[]>([]);
public currentWorkspace$ = new BehaviorSubject<WorkspaceAttribute | null>(null);
constructor(http: HttpSetup) {
this.http = http;
/**
* Add logic to check if current workspace id is still valid
* If not, remove the current workspace id and notify other subscribers
*/
this.workspaceList$.subscribe(async (workspaceList) => {
const currentWorkspaceId = this.currentWorkspaceId$.getValue();
if (currentWorkspaceId) {
const findItem = workspaceList.find((item) => item.id === currentWorkspaceId);
if (!findItem) {

combineLatest([this.workspaceList$, this.currentWorkspaceId$]).subscribe(
([workspaceList, currentWorkspaceId]) => {
const currentWorkspace = this.findWorkspace([workspaceList, currentWorkspaceId]);

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

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,
});
}
}
});
);

/**
* Initialize workspace list
*/
this.updateWorkspaceListAndNotify();
}

private findWorkspace(payload: [WorkspaceAttribute[], string]): WorkspaceAttribute | null {
const [workspaceList, currentWorkspaceId] = payload;
if (!currentWorkspaceId || !workspaceList || !workspaceList.length) {
return null;
}

const findItem = workspaceList.find((item) => item?.id === currentWorkspaceId);

if (!findItem) {
return null;
}

return findItem;
}

/**
* Add a non-throw-error fetch method for internal use.
*/
Expand Down

0 comments on commit ed7fd63

Please sign in to comment.