Skip to content

Commit 369c95c

Browse files
committed
Adds new SubscriptionManager helper class
Adopts it in the Home view to manage repo events
1 parent 54f3988 commit 369c95c

File tree

2 files changed

+58
-40
lines changed

2 files changed

+58
-40
lines changed

src/system/subscriptionManager.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
type Status = 'started' | 'paused' | 'stopped';
2+
3+
export class SubscriptionManager<T> {
4+
private _status: Status = 'stopped';
5+
get status(): Status {
6+
return this._status;
7+
}
8+
9+
private _subscription: { dispose: () => void } | undefined;
10+
11+
constructor(
12+
public readonly source: T,
13+
private readonly subscribe: (source: T) => { dispose: () => void },
14+
) {}
15+
16+
dispose(): void {
17+
this.stop();
18+
}
19+
20+
start(): void {
21+
if (this._subscription != null && this._status === 'started') return;
22+
23+
this._subscription = this.subscribe(this.source);
24+
this._status = 'started';
25+
}
26+
27+
pause(): void {
28+
this.stop('paused');
29+
}
30+
31+
resume(): void {
32+
this.start();
33+
}
34+
35+
private stop(status?: Status): void {
36+
this._subscription?.dispose();
37+
this._subscription = undefined;
38+
this._status = status ?? 'stopped';
39+
}
40+
}

src/webviews/home/homeWebview.ts

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import type { Deferrable } from '../../system/function/debounce';
6363
import { debounce } from '../../system/function/debounce';
6464
import { filterMap } from '../../system/iterable';
6565
import { getSettledValue } from '../../system/promise';
66+
import { SubscriptionManager } from '../../system/subscriptionManager';
6667
import type { ShowInCommitGraphCommandArgs } from '../plus/graph/protocol';
6768
import type { Change } from '../plus/patchDetails/protocol';
6869
import type { IpcMessage } from '../protocol';
@@ -115,10 +116,6 @@ const emptyDisposable = Object.freeze({
115116
},
116117
});
117118

118-
interface RepositorySubscription {
119-
repo: Repository;
120-
subscription?: Disposable;
121-
}
122119
interface RepositoryBranchData {
123120
repo: Repository;
124121
branches: GitBranch[];
@@ -407,10 +404,10 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
407404
}
408405

409406
private hasRepositoryChanged(): boolean {
410-
if (this._repositorySubscription?.repo != null) {
407+
if (this._repositorySubscription?.source != null) {
411408
if (
412-
this._repositorySubscription.repo.etag !== this._etagRepository ||
413-
this._repositorySubscription.repo.etagFileSystem !== this._etagFileSystem
409+
this._repositorySubscription.source.etag !== this._etagRepository ||
410+
this._repositorySubscription.source.etagFileSystem !== this._etagFileSystem
414411
) {
415412
return true;
416413
}
@@ -423,12 +420,12 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
423420

424421
onVisibilityChanged(visible: boolean): void {
425422
if (!visible) {
426-
this.stopRepositorySubscription();
423+
this._repositorySubscription?.pause();
427424

428425
return;
429426
}
430427

431-
this.resumeRepositorySubscription();
428+
this._repositorySubscription?.resume();
432429

433430
if (
434431
this._discovering == null &&
@@ -860,7 +857,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
860857
};
861858
}
862859

863-
private _repositorySubscription: RepositorySubscription | undefined;
860+
private _repositorySubscription: SubscriptionManager<Repository> | undefined;
864861
private selectRepository(repoPath?: string) {
865862
let repo: Repository | undefined;
866863
if (repoPath != null) {
@@ -872,48 +869,29 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
872869
}
873870
}
874871

875-
if (this._repositorySubscription != null) {
876-
this._repositorySubscription.subscription?.dispose();
877-
this._repositorySubscription = undefined;
878-
}
872+
this._repositorySubscription?.dispose();
873+
this._repositorySubscription = undefined;
874+
879875
if (repo != null) {
880-
this._repositorySubscription = {
881-
repo: repo,
882-
subscription: this.subscribeToRepository(repo),
883-
};
876+
this._repositorySubscription = new SubscriptionManager(repo, r => this.subscribeToRepository(r));
877+
// Start the subscription immediately if webview is visible
878+
if (this.host.visible) {
879+
this._repositorySubscription.start();
880+
}
884881
}
885882

886883
return repo;
887884
}
888885

889-
private stopRepositorySubscription() {
890-
if (this._repositorySubscription != null) {
891-
this._repositorySubscription.subscription?.dispose();
892-
this._repositorySubscription.subscription = undefined;
893-
}
894-
}
895-
896-
private resumeRepositorySubscription(force = false) {
897-
if (this._repositorySubscription == null) {
898-
return;
899-
}
900-
901-
if (force || this._repositorySubscription.subscription == null) {
902-
this._repositorySubscription.subscription?.dispose();
903-
this._repositorySubscription.subscription = undefined;
904-
this._repositorySubscription.subscription = this.subscribeToRepository(this._repositorySubscription.repo);
905-
}
906-
}
907-
908886
private resetBranchOverview() {
909887
this._repositoryBranches.clear();
910888

911889
if (!this.host.visible) {
912-
this.stopRepositorySubscription();
890+
this._repositorySubscription?.pause();
913891
return;
914892
}
915893

916-
this.resumeRepositorySubscription(true);
894+
this._repositorySubscription?.resume();
917895
}
918896

919897
private subscribeToRepository(repo: Repository): Disposable {
@@ -977,7 +955,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
977955
this.selectRepository();
978956
}
979957

980-
return this._repositorySubscription?.repo;
958+
return this._repositorySubscription?.source;
981959
}
982960

983961
private _invalidateOverview: 'repo' | 'wip' | undefined;

0 commit comments

Comments
 (0)