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

TreeElementBase.getTreeItem() is called twice #1877

Open
bk201- opened this issue Jan 24, 2025 · 0 comments
Open

TreeElementBase.getTreeItem() is called twice #1877

bk201- opened this issue Jan 24, 2025 · 0 comments

Comments

@bk201-
Copy link

bk201- commented Jan 24, 2025

  1. I've registered a command for the tree element node
registerCommandWithTreeNodeUnwrapping('azureDatabases.refresh', refreshTreeElement);

export async function refreshTreeElement(
    context: IActionContext,
    node: AzExtTreeItem | TreeElementWithId,
): Promise<void> {
    ...
    branchDataProvider.refresh(node); // calls refresh
    ...
}

Into BranchDataProvider I register each child in TreeElementStateManager

export class BranchDataProvider
    extends vscode.Disposable
    implements BranchDataProvider<CosmosDBResource, TreeElementWithId>
{
    private readonly onDidChangeTreeDataEmitter = new vscode.EventEmitter<TreeElementWithId| undefined>();

    constructor() {
        super(() => this.onDidChangeTreeDataEmitter.dispose());
    }

    get onDidChangeTreeData(): vscode.Event<TreeElementWithId| undefined> {
        return this.onDidChangeTreeDataEmitter.event;
    }

    async getChildren(element: TreeElementWithId): Promise<TreeElementWithId[]> {
        try {
            const result = await callWithTelemetryAndErrorHandling(
                '',
                async (context: IActionContext) => {
                    const children = (await element.getChildren?.()) ?? [];
                    return children.map((child) => {
                        return ext.state.wrapItemInStateHandling(child, (child: TreeElementWithId) =>
                            this.refresh(child),
                        ) as TreeElementWithId; // THIS FUNCTION CREATES WRAPPED OBJECT with branchItem
                    });
                },
            );

            return result ?? [];
        } catch (error) {
        }
    }

    async getResourceItem(resource: CosmosDBResource): Promise<TreeElementWithId> {
    }

    async getTreeItem(element: TreeElementWithId): Promise<vscode.TreeItem> {
        return element.getTreeItem();
    }

    refresh(element?: TreeElementWithId): void {
        this.onDidChangeTreeDataEmitter.fire(element);
    }
}
  1. When I call a command azureDatabases.refresh the method registerCommand tries to take contextValue. However, due to wrapped object which does not contain contextValue the getTreeItem() calls even if branchItem (real node instance) has this property

Image

  1. Then the method refreshTreeElement calls refreshing the node and getTreeItem calls again (twice)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant