From 70957a3741f13f10d45671ef42ade8de19a11abc Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Thu, 13 Jul 2023 00:03:47 -0400 Subject: [PATCH] Fixes workspaces repo add/remove event handling --- src/git/gitProviderService.ts | 24 ++++++++++++----- src/views/nodes/repositoriesNode.ts | 5 ++-- src/views/nodes/viewNode.ts | 25 ++++++++++++++---- src/views/nodes/workspaceNode.ts | 51 +++++++++++++++++++++++++++++-------- 4 files changed, 79 insertions(+), 26 deletions(-) diff --git a/src/git/gitProviderService.ts b/src/git/gitProviderService.ts index fc8eedf..af96a90 100644 --- a/src/git/gitProviderService.ts +++ b/src/git/gitProviderService.ts @@ -620,17 +620,18 @@ export class GitProviderService implements Disposable { const added: Repository[] = []; for (const repository of repositories) { - if (this._repositories.add(repository)) { + this._repositories.add(repository); + if (!repository.closed) { added.push(repository); } } this.updateContext(); - if (added.length === 0) return; - - // Defer the event trigger enough to let everything unwind - queueMicrotask(() => this.fireRepositoriesChanged(added)); + if (added.length) { + // Defer the event trigger enough to let everything unwind + queueMicrotask(() => this.fireRepositoriesChanged(added)); + } } finally { deferred.fulfill(); } @@ -2400,15 +2401,24 @@ export class GitProviderService implements Disposable { Logger.log(scope, `Repository found in '${repoUri.toString(true)}'`); const repositories = provider.openRepository(root?.folder, repoUri, false, undefined, closed); + + const added: Repository[] = []; + for (const repository of repositories) { this._repositories.add(repository); + if (!repository.closed) { + added.push(repository); + } } this._pendingRepositories.delete(key); this.updateContext(); - // Send a notification that the repositories changed - queueMicrotask(() => this.fireRepositoriesChanged(repositories)); + + if (added.length) { + // Send a notification that the repositories changed + queueMicrotask(() => this.fireRepositoriesChanged(added)); + } repository = repositories.length === 1 ? repositories[0] : this.getRepository(uri); return repository; diff --git a/src/views/nodes/repositoriesNode.ts b/src/views/nodes/repositoriesNode.ts index d4ccb4b..58f6d5f 100644 --- a/src/views/nodes/repositoriesNode.ts +++ b/src/views/nodes/repositoriesNode.ts @@ -7,7 +7,6 @@ import { debug } from '../../system/decorators/log'; import { debounce, szudzikPairing } from '../../system/function'; import { Logger } from '../../system/logger'; import type { ViewsWithRepositoriesNode } from '../viewBase'; -import { WorkspacesView } from '../workspacesView'; import { MessageNode } from './common'; import { RepositoryNode } from './repositoryNode'; import type { ViewNode } from './viewNode'; @@ -31,7 +30,7 @@ export class RepositoriesNode extends SubscribeableViewNode { +export class WorkspaceNode extends SubscribeableViewNode { constructor( uri: GitUri, view: WorkspacesView, @@ -22,6 +26,22 @@ export class WorkspaceNode extends ViewNode { this._uniqueId = getViewNodeId('workspace', this.context); } + override dispose() { + super.dispose(); + this.resetChildren(); + } + + private resetChildren() { + if (this._children == null) return; + + for (const child of this._children) { + if ('dispose' in child) { + child.dispose(); + } + } + this._children = undefined; + } + override get id(): string { return this._uniqueId; } @@ -131,17 +151,26 @@ export class WorkspaceNode extends ViewNode { return item; } - override refresh() { + @gate() + @debug() + override refresh(reset: boolean = false) { if (this._children == null) return; - if (this._children.length) { - for (const child of this._children) { - if ('dispose' in child) { - child.dispose(); - } - } + if (reset) { + this.resetChildren(); } + } - this._children = undefined; + protected override etag(): number { + return this.view.container.git.etag; + } + + @debug() + protected subscribe(): Disposable | Promise { + return Disposable.from(this.view.container.git.onDidChangeRepositories(this.onRepositoriesChanged, this)); + } + + private onRepositoriesChanged(_e: RepositoriesChangeEvent) { + void this.triggerChange(true); } }