@finos/legend-application-pure-ide
Version:
Legend Pure IDE application core
142 lines • 5.23 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ActionState, assertErrorThrown, guaranteeNonNullable, } from '@finos/legend-shared';
import { action, observable, makeObservable, flow, flowResult } from 'mobx';
export class TreeState {
ideStore;
treeData;
selectedNode;
loadInitialDataState = ActionState.create();
refreshDataState = ActionState.create();
constructor(ideStore) {
makeObservable(this, {
treeData: observable.ref,
loadInitialDataState: observable,
refreshDataState: observable,
initialize: flow,
expandNode: flow,
openNode: flow,
refreshTreeData: flow,
setTreeData: action,
setSelectedNode: action,
});
this.ideStore = ideStore;
}
getTreeData() {
return guaranteeNonNullable(this.treeData, 'Tree data has not been initialized');
}
*initialize() {
if (this.loadInitialDataState.isInProgress) {
this.ideStore.applicationStore.notificationService.notifyWarning('Tree state initialization is in progress');
return;
}
this.loadInitialDataState.inProgress();
try {
this.treeData = this.buildTreeData((yield this.getRootNodes()));
this.loadInitialDataState.pass();
}
catch (error) {
assertErrorThrown(error);
this.ideStore.applicationStore.notificationService.notifyError(error);
this.loadInitialDataState.fail();
}
}
setSelectedNode(node) {
if (node !== this.selectedNode) {
if (this.selectedNode) {
this.selectedNode.isSelected = false;
}
if (node) {
node.isSelected = true;
}
this.selectedNode = node;
this.refreshTree();
}
}
setTreeData(data) {
this.treeData = data;
}
refreshTree() {
this.setTreeData({ ...guaranteeNonNullable(this.treeData) });
}
*expandNode(node) {
if (node.isLoading) {
return;
}
if (this.getTreeData().nodes.has(node.id) && node.childrenIds) {
node.isLoading = false;
node.isOpen = true;
this.refreshTree();
}
else {
node.isLoading = true;
try {
const childNodes = (yield this.getChildNodes(node));
this.processChildNodes(node, childNodes);
node.isOpen = true;
this.refreshTree();
}
catch (error) {
assertErrorThrown(error);
this.ideStore.applicationStore.notificationService.notifyError(error);
}
finally {
node.isLoading = false;
}
}
}
*refreshTreeData() {
if (!this.treeData) {
return;
}
const openingNodeIds = new Set(Array.from(this.getTreeData().nodes.values())
.filter((node) => node.isOpen)
.map((node) => node.id));
const selectedNodeId = this.selectedNode?.id;
this.refreshDataState.inProgress();
try {
this.treeData = this.buildTreeData((yield this.getRootNodes()));
}
catch (error) {
assertErrorThrown(error);
this.ideStore.applicationStore.notificationService.notifyError(error);
this.refreshDataState.fail();
return;
}
const nodesToOpen = this.getTreeData()
.rootIds.map((id) => guaranteeNonNullable(this.getTreeData().nodes.get(id)))
.filter((node) => openingNodeIds.has(node.id));
yield this.refreshOpenNodes(nodesToOpen, openingNodeIds);
if (selectedNodeId && this.getTreeData().nodes.has(selectedNodeId)) {
this.setSelectedNode(guaranteeNonNullable(this.getTreeData().nodes.get(selectedNodeId)));
}
}
async refreshOpenNodes(nodesToOpen, openingNodeIds) {
await Promise.all(nodesToOpen.map((node) => {
openingNodeIds.delete(node.id);
return flowResult(this.expandNode(node)).catch(() => undefined);
}));
nodesToOpen = nodesToOpen
.flatMap((node) => node.childrenIds ?? [])
.map((childId) => guaranteeNonNullable(this.getTreeData().nodes.get(childId)))
.filter((node) => openingNodeIds.has(node.id));
if (nodesToOpen.length) {
return this.refreshOpenNodes(nodesToOpen, openingNodeIds);
}
return Promise.resolve();
}
}
//# sourceMappingURL=TreeState.js.map