Skip to content

Commit 70957a3

Browse files
committed
Fixes workspaces repo add/remove event handling
1 parent 84d1ede commit 70957a3

File tree

4 files changed

+79
-26
lines changed

4 files changed

+79
-26
lines changed

src/git/gitProviderService.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -620,17 +620,18 @@ export class GitProviderService implements Disposable {
620620
const added: Repository[] = [];
621621

622622
for (const repository of repositories) {
623-
if (this._repositories.add(repository)) {
623+
this._repositories.add(repository);
624+
if (!repository.closed) {
624625
added.push(repository);
625626
}
626627
}
627628

628629
this.updateContext();
629630

630-
if (added.length === 0) return;
631-
632-
// Defer the event trigger enough to let everything unwind
633-
queueMicrotask(() => this.fireRepositoriesChanged(added));
631+
if (added.length) {
632+
// Defer the event trigger enough to let everything unwind
633+
queueMicrotask(() => this.fireRepositoriesChanged(added));
634+
}
634635
} finally {
635636
deferred.fulfill();
636637
}
@@ -2400,15 +2401,24 @@ export class GitProviderService implements Disposable {
24002401

24012402
Logger.log(scope, `Repository found in '${repoUri.toString(true)}'`);
24022403
const repositories = provider.openRepository(root?.folder, repoUri, false, undefined, closed);
2404+
2405+
const added: Repository[] = [];
2406+
24032407
for (const repository of repositories) {
24042408
this._repositories.add(repository);
2409+
if (!repository.closed) {
2410+
added.push(repository);
2411+
}
24052412
}
24062413

24072414
this._pendingRepositories.delete(key);
24082415

24092416
this.updateContext();
2410-
// Send a notification that the repositories changed
2411-
queueMicrotask(() => this.fireRepositoriesChanged(repositories));
2417+
2418+
if (added.length) {
2419+
// Send a notification that the repositories changed
2420+
queueMicrotask(() => this.fireRepositoriesChanged(added));
2421+
}
24122422

24132423
repository = repositories.length === 1 ? repositories[0] : this.getRepository(uri);
24142424
return repository;

src/views/nodes/repositoriesNode.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { debug } from '../../system/decorators/log';
77
import { debounce, szudzikPairing } from '../../system/function';
88
import { Logger } from '../../system/logger';
99
import type { ViewsWithRepositoriesNode } from '../viewBase';
10-
import { WorkspacesView } from '../workspacesView';
1110
import { MessageNode } from './common';
1211
import { RepositoryNode } from './repositoryNode';
1312
import type { ViewNode } from './viewNode';
@@ -31,7 +30,7 @@ export class RepositoriesNode extends SubscribeableViewNode<ViewsWithRepositorie
3130
if (this._children == null) return;
3231

3332
for (const child of this._children) {
34-
if (child instanceof RepositoryNode) {
33+
if ('dispose' in child) {
3534
child.dispose();
3635
}
3736
}
@@ -50,7 +49,7 @@ export class RepositoriesNode extends SubscribeableViewNode<ViewsWithRepositorie
5049
}
5150

5251
getTreeItem(): TreeItem {
53-
const isInWorkspacesView = this.view instanceof WorkspacesView;
52+
const isInWorkspacesView = this.view.type === 'workspaces';
5453
const isLinkedWorkspace = isInWorkspacesView && this.view.container.workspaces.currentWorkspaceId != null;
5554
const isCurrentLinkedWorkspace = isLinkedWorkspace && this.view.container.workspaces.currentWorkspace != null;
5655
const item = new TreeItem(

src/views/nodes/viewNode.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,22 @@ export abstract class RepositoriesSubscribeableNode<
703703
super(unknownGitUri, view);
704704
}
705705

706+
override dispose() {
707+
super.dispose();
708+
this.resetChildren();
709+
}
710+
711+
private resetChildren() {
712+
if (this.children == null) return;
713+
714+
for (const child of this.children) {
715+
if ('dispose' in child) {
716+
child.dispose();
717+
}
718+
}
719+
this.children = undefined;
720+
}
721+
706722
override async getSplattedChild() {
707723
if (this.children == null) {
708724
await this.getChildren();
@@ -714,11 +730,10 @@ export abstract class RepositoriesSubscribeableNode<
714730
@gate()
715731
@debug()
716732
override refresh(reset: boolean = false) {
717-
if (reset && this.children != null) {
718-
for (const child of this.children) {
719-
child.dispose();
720-
}
721-
this.children = undefined;
733+
if (this.children == null) return;
734+
735+
if (reset) {
736+
this.resetChildren();
722737
}
723738
}
724739

src/views/nodes/workspaceNode.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import { ThemeIcon, TreeItem, TreeItemCollapsibleState, Uri } from 'vscode';
1+
import { Disposable, ThemeIcon, TreeItem, TreeItemCollapsibleState, Uri } from 'vscode';
2+
import type { RepositoriesChangeEvent } from '../../git/gitProviderService';
23
import { GitUri } from '../../git/gitUri';
34
import type { CloudWorkspace, LocalWorkspace } from '../../plus/workspaces/models';
45
import { WorkspaceType } from '../../plus/workspaces/models';
56
import { createCommand } from '../../system/command';
7+
import { gate } from '../../system/decorators/gate';
8+
import { debug } from '../../system/decorators/log';
69
import type { WorkspacesView } from '../workspacesView';
710
import { CommandMessageNode, MessageNode } from './common';
811
import { RepositoryNode } from './repositoryNode';
9-
import { ContextValues, getViewNodeId, ViewNode } from './viewNode';
12+
import type { ViewNode } from './viewNode';
13+
import { ContextValues, getViewNodeId, SubscribeableViewNode } from './viewNode';
1014
import { WorkspaceMissingRepositoryNode } from './workspaceMissingRepositoryNode';
1115

12-
export class WorkspaceNode extends ViewNode<WorkspacesView> {
16+
export class WorkspaceNode extends SubscribeableViewNode<WorkspacesView> {
1317
constructor(
1418
uri: GitUri,
1519
view: WorkspacesView,
@@ -22,6 +26,22 @@ export class WorkspaceNode extends ViewNode<WorkspacesView> {
2226
this._uniqueId = getViewNodeId('workspace', this.context);
2327
}
2428

29+
override dispose() {
30+
super.dispose();
31+
this.resetChildren();
32+
}
33+
34+
private resetChildren() {
35+
if (this._children == null) return;
36+
37+
for (const child of this._children) {
38+
if ('dispose' in child) {
39+
child.dispose();
40+
}
41+
}
42+
this._children = undefined;
43+
}
44+
2545
override get id(): string {
2646
return this._uniqueId;
2747
}
@@ -131,17 +151,26 @@ export class WorkspaceNode extends ViewNode<WorkspacesView> {
131151
return item;
132152
}
133153

134-
override refresh() {
154+
@gate()
155+
@debug()
156+
override refresh(reset: boolean = false) {
135157
if (this._children == null) return;
136158

137-
if (this._children.length) {
138-
for (const child of this._children) {
139-
if ('dispose' in child) {
140-
child.dispose();
141-
}
142-
}
159+
if (reset) {
160+
this.resetChildren();
143161
}
162+
}
144163

145-
this._children = undefined;
164+
protected override etag(): number {
165+
return this.view.container.git.etag;
166+
}
167+
168+
@debug()
169+
protected subscribe(): Disposable | Promise<Disposable> {
170+
return Disposable.from(this.view.container.git.onDidChangeRepositories(this.onRepositoriesChanged, this));
171+
}
172+
173+
private onRepositoriesChanged(_e: RepositoriesChangeEvent) {
174+
void this.triggerChange(true);
146175
}
147176
}

0 commit comments

Comments
 (0)