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

(WIP) Refactoring out view nodes to better deal with id collisions and storage #3804

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/views/branchesView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class BranchesViewNode extends RepositoriesSubscribeableNode<BranchesView

const splat = repositories.length === 1;
this.children = repositories.map(
r => new BranchesRepositoryNode(GitUri.fromRepoPath(r.path), this.view, this, r, splat),
r => new BranchesRepositoryNode(GitUri.fromRepoPath(r.path), this.view, this, splat, r),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/commitsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class CommitsViewNode extends RepositoriesSubscribeableNode<CommitsView,
const splat = repositories.length === 1;
this.children = repositories.map(
r =>
new CommitsRepositoryNode(GitUri.fromRepoPath(r.path), this.view, this, r, splat, {
new CommitsRepositoryNode(GitUri.fromRepoPath(r.path), this.view, this, splat, r, {
showBranchAndLastFetched: true,
}),
);
Expand Down
2 changes: 1 addition & 1 deletion src/views/contributorsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ContributorsViewNode extends RepositoriesSubscribeableNode<Contribu

const splat = repositories.length === 1;
this.children = repositories.map(
r => new ContributorsRepositoryNode(GitUri.fromRepoPath(r.path), this.view, this, r, splat),
r => new ContributorsRepositoryNode(GitUri.fromRepoPath(r.path), this.view, this, splat, r),
);
}

Expand Down
4 changes: 0 additions & 4 deletions src/views/launchpadView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ export class LaunchpadItemNode extends CacheableChildrenViewNode<'launchpad-item
this.repoPath = repoPath;
}

override get id(): string {
return this._uniqueId;
}

override toClipboard(type?: ClipboardType): string {
const url = this.getUrl();
switch (type) {
Expand Down
4 changes: 0 additions & 4 deletions src/views/nodes/UncommittedFilesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export class UncommittedFilesNode extends ViewNode<'uncommitted-files', ViewsWit
this._uniqueId = getViewNodeId(this.type, this.context);
}

override get id(): string {
return this._uniqueId;
}

get repoPath(): string {
return this.status.repoPath;
}
Expand Down
4 changes: 1 addition & 3 deletions src/views/nodes/abstract/repositoriesSubscribeableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ export abstract class RepositoriesSubscribeableNode<
TView extends View = View,
TChild extends ViewNode = ViewNode,
> extends SubscribeableViewNode<'repositories', TView, TChild> {
protected override splatted = true;

constructor(view: TView) {
super('repositories', unknownGitUri, view);
super('repositories', unknownGitUri, view, undefined, true);
}

override async getSplattedChild() {
Expand Down
14 changes: 2 additions & 12 deletions src/views/nodes/abstract/repositoryFolderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@ export abstract class RepositoryFolderNode<
TView extends View = View,
TChild extends ViewNode = ViewNode,
> extends SubscribeableViewNode<'repo-folder', TView> {
protected override splatted = true;

constructor(
uri: GitUri,
view: TView,
protected override readonly parent: ViewNode,
public readonly repo: Repository,
splatted: boolean,
public readonly repo: Repository,
private readonly options?: { showBranchAndLastFetched?: boolean },
) {
super('repo-folder', uri, view, parent);
super('repo-folder', uri, view, parent, splatted);

this.updateContext({ repository: this.repo });
this._uniqueId = getViewNodeId(this.type, this.context);

this.splatted = splatted;
}

private _child: TChild | undefined;
Expand All @@ -53,10 +49,6 @@ export abstract class RepositoryFolderNode<
this.child = undefined;
}

override get id(): string {
return this._uniqueId;
}

override toClipboard(): string {
return this.repo.path;
}
Expand All @@ -66,8 +58,6 @@ export abstract class RepositoryFolderNode<
}

async getTreeItem(): Promise<TreeItem> {
this.splatted = false;

const branch = await this.repo.git.getBranch();
const ahead = (branch?.state.ahead ?? 0) > 0;
const behind = (branch?.state.behind ?? 0) > 0;
Expand Down
4 changes: 2 additions & 2 deletions src/views/nodes/abstract/subscribeableViewNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export abstract class SubscribeableViewNode<

protected loaded: boolean = false;

constructor(type: Type, uri: GitUri, view: TView, parent?: ViewNode) {
super(type, uri, view, parent);
constructor(type: Type, uri: GitUri, view: TView, parent?: ViewNode, splatted?: boolean) {
super(type, uri, view, parent, splatted);

const disposables = [
weakEvent(this.view.onDidChangeVisibility, this.onVisibilityChanged, this),
Expand Down
98 changes: 87 additions & 11 deletions src/views/nodes/abstract/viewNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,29 @@ export interface AmbientContext {
readonly worktreesByBranch?: Map<string, GitWorktree>;
}

export function getViewNodeId(type: string, context: AmbientContext): string {
export function getViewNodeId(
type: TreeViewNodeTypes | `${TreeViewNodeTypes}+${string}`,
context: AmbientContext,
): string {
switch (type) {
case 'branch':
return `${type}(${context.branch?.id})`;

case 'commit':
return `${type}(${context.commit?.repoPath}|${context.commit?.sha})`;

case 'pullrequest':
return `${type}(${context.pullRequest?.url})`;

case 'commit-file':
return `${type}:(${
context.repository?.path ?? context.branch?.repoPath ?? context.commit?.repoPath
}|${context.file?.path}+${context.file?.status})`;

// case 'results-file':
// return `${type}(${context.file?.path}+${context.file?.status})`;
}

let uniqueness = '';
if (context.root) {
uniqueness += '/root';
Expand Down Expand Up @@ -252,8 +274,22 @@ export abstract class ViewNode<
return types.includes(this.type as unknown as T[number]);
}

public childrenIds = new Set<string>();
public childrenCount = 0;
protected _uniqueId!: string;
protected splatted = false;

private _splatted: boolean;
//** Indicates if this node is only shown as its children, not itself */
get splatted(): boolean {
return this._splatted;
}
set splatted(value: boolean) {
if (this._splatted === value) return;

this._splatted = value;
// this.setId();
}

// NOTE: @eamodio uncomment to track node leaks
// readonly uuid = uuid();

Expand All @@ -262,8 +298,15 @@ export abstract class ViewNode<
// public readonly id: string | undefined,
uri: GitUri,
public readonly view: TView,
protected parent?: ViewNode,
protected parent?: ViewNode | undefined,
//** Indicates if this node is only shown as its children, not itself */
splatted?: boolean,
) {
this._splatted = splatted ?? false;
(parent ?? this).childrenCount++;

// this.setId();

// NOTE: @eamodio uncomment to track node leaks
// queueMicrotask(() => this.view.registerNode(this));
this._uri = uri;
Expand All @@ -277,9 +320,38 @@ export abstract class ViewNode<
// NOTE: @eamodio uncomment to track node leaks
// this.view.unregisterNode(this);
}
private _id!: string;
get id(): string {
if (this._id == null) {
// if (!this.splatted) {
this._id = this._uniqueId ?? `${(this.parent ?? this).childrenCount}:${this.type}`;
// }
}
return this._id;
}

get parentId(): string {
return this.parent?.treeId ?? '~';
}

get treeId(): string {
return this.splatted ? this.parentId : `${this.parentId}/${this.id}`;
}

get id(): string | undefined {
return this._uniqueId;
private setId() {
// if (this.splatted) {
// this._id = undefined!; //this.parent?.id ?? '~';
// } else {
// const { parent } = this;
// const { childrenIds } = parent ?? this;
// this._id = this._uniqueId ?? `${childrenIds.size ?? 0}:${this.type}`;
// if (childrenIds.has(this._id)) {
// debugger;
// // this._id = `${this._id}-${this._uniqueCounter++}`;
// }
// childrenIds.add(this._id);
// }
// console.log('#######', this.type, this.splatted, this._id);
}

private _context: AmbientContext | undefined;
Expand Down Expand Up @@ -339,32 +411,36 @@ export abstract class ViewNode<

getSplattedChild?(): Promise<ViewNode | undefined>;

protected get storedId(): string | undefined {
return this.id;
}

deleteState<T extends StateKey<State> = StateKey<State>>(key?: T): void {
if (this.id == null) {
if (this.storedId == null) {
debugger;
throw new Error('Id is required to delete state');
}
this.view.nodeState.deleteState(this.id, key as string);
this.view.nodeState.deleteState(this.storedId, key as string);
}

getState<T extends StateKey<State> = StateKey<State>>(key: T): StateValue<State, T> | undefined {
if (this.id == null) {
if (this.storedId == null) {
debugger;
throw new Error('Id is required to get state');
}
return this.view.nodeState.getState(this.id, key as string);
return this.view.nodeState.getState(this.storedId, key as string);
}

storeState<T extends StateKey<State> = StateKey<State>>(
key: T,
value: StateValue<State, T>,
sticky?: boolean,
): void {
if (this.id == null) {
if (this.storedId == null) {
debugger;
throw new Error('Id is required to store state');
}
this.view.nodeState.storeState(this.id, key as string, value, sticky);
this.view.nodeState.storeState(this.storedId, key as string, value, sticky);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/views/nodes/abstract/viewRefNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export abstract class ViewRefNode<
uri: GitUri,
view: TView,
protected override readonly parent: ViewNode,
splatted?: boolean,
) {
super(type, uri, view, parent);
super(type, uri, view, parent, splatted);
}

abstract get ref(): TReference;
Expand Down
4 changes: 0 additions & 4 deletions src/views/nodes/autolinkedItemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ export class AutolinkedItemNode extends ViewNode<'autolink', ViewsWithCommits> {
this._uniqueId = getViewNodeId(`${this.type}+${item.id}`, this.context);
}

override get id(): string {
return this._uniqueId;
}

override async toClipboard(type?: ClipboardType): Promise<string> {
const enriched = await this.maybeEnriched;
switch (type) {
Expand Down
4 changes: 0 additions & 4 deletions src/views/nodes/autolinkedItemsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export class AutolinkedItemsNode extends CacheableChildrenViewNode<'autolinks',
this._uniqueId = getViewNodeId(this.type, this.context);
}

override get id(): string {
return this._uniqueId;
}

async getChildren(): Promise<ViewNode[]> {
if (this.children == null) {
const commits = [...this.log.commits.values()];
Expand Down
13 changes: 4 additions & 9 deletions src/views/nodes/branchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export class BranchNode
limit: number | undefined;

private readonly options: Options;
protected override splatted = true;

constructor(
uri: GitUri,
Expand All @@ -81,10 +80,12 @@ export class BranchNode
public readonly root: boolean,
options?: Partial<Options>,
) {
super('branch', uri, view, parent);
super('branch', uri, view, parent, root);

this.updateContext({ repository: repo, branch: branch, root: root });
this._uniqueId = getViewNodeId(this.type, this.context);
// this._uniqueId = getViewNodeId(this.type, this.context);
this._uniqueId = `${this.type}(${this.branch.id})`;

this.limit = this.view.getNodeLastKnownLimit(this);

this.options = {
Expand All @@ -107,10 +108,6 @@ export class BranchNode
this.children = undefined;
}

override get id(): string {
return this._uniqueId;
}

override toClipboard(): string {
return this.branch.name;
}
Expand Down Expand Up @@ -418,8 +415,6 @@ export class BranchNode
}

async getTreeItem(): Promise<TreeItem> {
this.splatted = false;

const parts = await getBranchNodeParts(this.view.container, this.branch, this.current, {
pendingPullRequest: this.getState('pendingPullRequest'),
showAsCommits: this.options.showAsCommits,
Expand Down
4 changes: 0 additions & 4 deletions src/views/nodes/branchOrTagFolderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ export class BranchOrTagFolderNode extends ViewNode<'branch-tag-folder'> {
this._uniqueId = getViewNodeId(`${this.type}+${folderType}+${relativePath ?? folderName}`, this.context);
}

override get id(): string {
return this._uniqueId;
}

override toClipboard(): string {
return this.folderName;
}
Expand Down
6 changes: 1 addition & 5 deletions src/views/nodes/branchTrackingStatusNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,10 @@ export class BranchTrackingStatusNode
branchStatusUpstreamType: upstreamType,
root: root,
});
this._uniqueId = getViewNodeId(this.type, this.context);
// this._uniqueId = getViewNodeId(this.type, this.context);
this.limit = this.view.getNodeLastKnownLimit(this);
}

override get id(): string {
return this._uniqueId;
}

get repoPath(): string {
return this.uri.repoPath!;
}
Expand Down
6 changes: 1 addition & 5 deletions src/views/nodes/branchesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ export class BranchesNode extends CacheableChildrenViewNode<'branches', ViewsWit
protected override readonly parent: ViewNode,
public readonly repo: Repository,
) {
super('branches', uri, view, parent);
super('branches', uri, view, parent, true);

this.updateContext({ repository: repo });
this._uniqueId = getViewNodeId(this.type, this.context);
}

override get id(): string {
return this._uniqueId;
}

get repoPath(): string {
return this.repo.path;
}
Expand Down
Loading
Loading