Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sheet): dependency error #2650

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,27 @@ export class FormulaDependencyTree extends Disposable {
override dispose(): void {
super.dispose();

this.children.forEach((tree) => {
tree.dispose();
});
// this.children.forEach((tree) => {
// tree.dispose();
// });

this.children = [];

this.rangeList = [];

this.parents = [];

this.node?.dispose();
}

disposeWithChildren() {
this.children.forEach((tree) => {
tree.disposeWithChildren();
});

this.dispose();
}

resetState() {
this._state = FDtreeStateType.DEFAULT;
}
Expand Down Expand Up @@ -316,7 +327,7 @@ export class FormulaDependencyTreeCache extends Disposable {
dependenceTree.inRangeData(range)
) {
treeList.forEach((tree) => {
if (tree === dependenceTree) {
if (tree === dependenceTree || tree.children.includes(dependenceTree)) {
return true;
}
tree.pushChildren(dependenceTree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ export class FormulaDependencyGenerator extends Disposable {
if (dependencyTreeCache.size() > treeList.length) {
allTree = this._dependencyManagerService.buildDependencyTree(treeList);
} else {
allTree = this._dependencyManagerService.buildDependencyTree(dependencyTreeCache);
allTree = this._dependencyManagerService.buildDependencyTree(dependencyTreeCache, treeList);
}

for (let i = 0, len = allTree.length; i < len; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export class CalculateFormulaService extends Disposable {

for (let i = 0; i < cycleReferenceCount; i++) {
await this._execute();

FORMULA_REF_TO_ARRAY_CACHE.clear();

const isCycleDependency = this._runtimeService.isCycleDependency();
if (!isCycleDependency) {
break;
Expand All @@ -124,8 +127,6 @@ export class CalculateFormulaService extends Disposable {

this._executionCompleteListener$.next(this._runtimeService.getAllRuntimeData());

FORMULA_REF_TO_ARRAY_CACHE.clear();

CELL_INVERTED_INDEX_CACHE.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface IDependencyManagerService {

getAllTree(): FormulaDependencyTree[];

buildDependencyTree(shouldBeBuildTrees: FormulaDependencyTree[] | FormulaDependencyTreeCache): FormulaDependencyTree[];
buildDependencyTree(shouldBeBuildTrees: FormulaDependencyTree[] | FormulaDependencyTreeCache, dependencyTrees?: FormulaDependencyTree[]): FormulaDependencyTree[];

clearDependencyForTree(shouldBeClearTree: Nullable<FormulaDependencyTree>): void;

Expand Down Expand Up @@ -124,12 +124,17 @@ export class DependencyManagerService extends Disposable implements IDependencyM
return trees;
}

buildDependencyTree(shouldBeBuildTrees: FormulaDependencyTree[] | FormulaDependencyTreeCache): FormulaDependencyTree[] {
buildDependencyTree(shouldBeBuildTrees: FormulaDependencyTree[] | FormulaDependencyTreeCache, dependencyTrees?: FormulaDependencyTree[]): FormulaDependencyTree[] {
const allTrees = this.getAllTree();
if (shouldBeBuildTrees.length === 0) {
return allTrees;
}
this._buildDependencyTree(allTrees, shouldBeBuildTrees);
if (shouldBeBuildTrees instanceof FormulaDependencyTreeCache) {
this._buildDependencyTree(allTrees, shouldBeBuildTrees, dependencyTrees || []);
} else {
this._buildDependencyTree(allTrees, shouldBeBuildTrees, shouldBeBuildTrees);
}

return allTrees;
}

Expand All @@ -138,13 +143,13 @@ export class DependencyManagerService extends Disposable implements IDependencyM
* @param allTrees all FormulaDependencyTree
* @param shouldBeBuildTrees FormulaDependencyTree[] | FormulaDependencyTreeCache
*/
private _buildDependencyTree(allTrees: FormulaDependencyTree[], shouldBeBuildTrees: FormulaDependencyTree[] | FormulaDependencyTreeCache) {
private _buildDependencyTree(allTrees: FormulaDependencyTree[], shouldBeBuildTrees: FormulaDependencyTree[] | FormulaDependencyTreeCache, dependencyTrees: FormulaDependencyTree[]) {
allTrees.forEach((tree) => {
if (shouldBeBuildTrees instanceof FormulaDependencyTreeCache) {
shouldBeBuildTrees.dependency(tree);
} else {
shouldBeBuildTrees.forEach((shouldBeBuildTree) => {
if (tree === shouldBeBuildTree) {
if (tree === shouldBeBuildTree || shouldBeBuildTree.children.includes(tree)) {
return true;
}

Expand All @@ -154,6 +159,19 @@ export class DependencyManagerService extends Disposable implements IDependencyM
});
}
});

// Build the reverse dependency relationship between the trees.
allTrees.forEach((tree) => {
dependencyTrees.forEach((dependencyTree) => {
if (tree === dependencyTree || tree.children.includes(dependencyTree)) {
return true;
}

if (tree.dependency(dependencyTree)) {
tree.pushChildren(dependencyTree);
}
});
});
}

/**
Expand All @@ -178,7 +196,7 @@ export class DependencyManagerService extends Disposable implements IDependencyM
child.parents = child.parents.filter((parent) => parent !== shouldBeClearTree);
});

this._buildDependencyTree(parents, children);
this._buildDependencyTree(parents, children, children);

shouldBeClearTree.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ import {
} from '@univerjs/sheets';
import { Inject, Injector } from '@wendellhu/redi';

import { map, merge } from 'rxjs';
import { filter, map, merge } from 'rxjs';
import { IEditorService } from '@univerjs/ui';
import type { IRefRangeWithPosition } from './utils/offset-formula-data';
import { removeFormulaData } from './utils/offset-formula-data';
import { getFormulaReferenceMoveUndoRedo } from './utils/ref-range-formula';
Expand Down Expand Up @@ -171,6 +172,7 @@ export class UpdateFormulaController extends Disposable {
@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel,
@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
@Inject(SelectionManagerService) private _selectionManagerService: SelectionManagerService,
@IEditorService private readonly _editorService: IEditorService,
@Inject(Injector) readonly _injector: Injector
) {
super();
Expand Down Expand Up @@ -221,6 +223,8 @@ export class UpdateFormulaController extends Disposable {
this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET),
this._univerInstanceService.getTypeOfUnitAdded$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC),
this._univerInstanceService.getTypeOfUnitAdded$<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE)
).pipe(
filter((unit) => this._editorService.getEditor(unit.getUnitId()) == null)
).subscribe((unit) => this._handleUnitAdded(unit))
)
);
Expand All @@ -231,6 +235,8 @@ export class UpdateFormulaController extends Disposable {
this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET).pipe(map((sheet) => sheet.getUnitId())),
this._univerInstanceService.getTypeOfUnitDisposed$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC).pipe(map((doc) => doc.getUnitId())),
this._univerInstanceService.getTypeOfUnitDisposed$<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE).pipe(map((slide) => slide.getUnitId()))
).pipe(
filter((unitId) => this._editorService.getEditor(unitId) == null)
).subscribe((id) => this._handleRemoveSheetMutation(id))
)
);
Expand Down
Loading