Skip to content

Commit d13dff4

Browse files
committed
refactor: restructure events
1 parent 3477cdd commit d13dff4

File tree

8 files changed

+83
-67
lines changed

8 files changed

+83
-67
lines changed

src/commands.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export function addCommmands(plugin: ObsidianGit) {
4646
leaf = leafs.first()!;
4747
}
4848
await app.workspace.revealLeaf(leaf);
49-
app.workspace.trigger("obsidian-git:refresh");
5049
},
5150
});
5251
plugin.addCommand({
@@ -68,7 +67,6 @@ export function addCommmands(plugin: ObsidianGit) {
6867
leaf = leafs.first()!;
6968
}
7069
await app.workspace.revealLeaf(leaf);
71-
app.workspace.trigger("obsidian-git:refresh");
7270
},
7371
});
7472

src/main.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ export default class ObsidianGit extends Plugin {
6262
branchBar?: BranchStatusBar;
6363
state: PluginState = {
6464
gitAction: CurrentGitAction.idle,
65-
loading: false,
6665
offlineMode: false,
6766
};
6867
lastPulledFiles: FileStatusResult[];
6968
gitReady = false;
7069
promiseQueue: PromiseQueue = new PromiseQueue(this);
7170
autoCommitDebouncer: Debouncer<[], void> | undefined;
72-
loading = false;
7371
cachedStatus: Status | undefined;
7472
// Used to store the path of the file that is currently shown in the diff view.
7573
lastDiffViewState: Record<string, unknown> | undefined;
@@ -84,6 +82,7 @@ export default class ObsidianGit extends Plugin {
8482
}
8583

8684
async updateCachedStatus(): Promise<Status> {
85+
this.app.workspace.trigger("obsidian-git:loading-status");
8786
this.cachedStatus = await this.gitManager.status();
8887
if (this.cachedStatus.conflicted.length > 0) {
8988
this.localStorage.setConflict(true);
@@ -93,6 +92,10 @@ export default class ObsidianGit extends Plugin {
9392
await this.branchBar?.display();
9493
}
9594

95+
this.app.workspace.trigger(
96+
"obsidian-git:status-changed",
97+
this.cachedStatus
98+
);
9699
return this.cachedStatus;
97100
}
98101

@@ -111,14 +114,11 @@ export default class ObsidianGit extends Plugin {
111114
gitViews.some((leaf) => !(leaf.isDeferred ?? false)) ||
112115
historyViews.some((leaf) => !(leaf.isDeferred ?? false))
113116
) {
114-
this.loading = true;
115-
this.app.workspace.trigger("obsidian-git:view-refresh");
116-
117117
await this.updateCachedStatus().catch((e) => this.displayError(e));
118-
this.loading = false;
119-
this.app.workspace.trigger("obsidian-git:view-refresh");
120118
}
121119

120+
this.app.workspace.trigger("obsidian-git:refreshed");
121+
122122
// We don't put a line authoring refresh here, as it would force a re-loading
123123
// of the line authoring feature - which would lead to a jumpy editor-view in the
124124
// ui after every rename event.
@@ -285,8 +285,6 @@ export default class ObsidianGit extends Plugin {
285285
leaf = leafs.first()!;
286286
}
287287
await this.app.workspace.revealLeaf(leaf);
288-
289-
this.app.workspace.trigger("obsidian-git:refresh");
290288
}
291289
);
292290

@@ -424,7 +422,6 @@ export default class ObsidianGit extends Plugin {
424422

425423
unloadPlugin() {
426424
this.gitReady = false;
427-
this.app.workspace.trigger("obsidian-git:refresh");
428425

429426
this.lineAuthoringFeature.deactivateFeature();
430427
this.automaticsManager.unload();
@@ -1041,6 +1038,7 @@ export default class ObsidianGit extends Plugin {
10411038
if (selectedBranch != undefined) {
10421039
await this.gitManager.checkout(selectedBranch);
10431040
this.displayMessage(`Switched to ${selectedBranch}`);
1041+
this.app.workspace.trigger("obsidian-git:refresh");
10441042
await this.branchBar?.display();
10451043
return selectedBranch;
10461044
}
@@ -1148,6 +1146,7 @@ export default class ObsidianGit extends Plugin {
11481146
new Notice(
11491147
"All local changes have been discarded. New files remain untouched."
11501148
);
1149+
this.app.workspace.trigger("obsidian-git:refresh");
11511150
}
11521151

11531152
async handleConflict(conflicted?: string[]): Promise<void> {

src/statusBar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class StatusBar {
2525
this.statusBarEl.setAttribute("data-tooltip-position", "top");
2626

2727
plugin.registerEvent(
28-
plugin.app.workspace.on("obsidian-git:refresh", () => {
28+
plugin.app.workspace.on("obsidian-git:refreshed", () => {
2929
this.refreshCommitTimestamp().catch(console.error);
3030
})
3131
);

src/types.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,6 @@ export interface FileStatusResult {
208208
export interface PluginState {
209209
offlineMode: boolean;
210210
gitAction: CurrentGitAction;
211-
/* Currently refreshing the cached status
212-
*/
213-
loading: boolean;
214211
}
215212

216213
export enum CurrentGitAction {
@@ -330,25 +327,52 @@ declare module "obsidian" {
330327
inlineTitleEl: HTMLElement;
331328
}
332329
interface Workspace {
330+
/**
331+
* Emitted when some git action has been completed and plugin has been refreshed
332+
*/
333+
on(
334+
name: "obsidian-git:refreshed",
335+
callback: () => void,
336+
ctx?: unknown
337+
): EventRef;
338+
/**
339+
* Emitted when some git action has been completed and the plugin should refresh
340+
*/
333341
on(
334342
name: "obsidian-git:refresh",
335343
callback: () => void,
336344
ctx?: unknown
337345
): EventRef;
346+
/**
347+
* Emitted when the plugin is currently loading a new cached status.
348+
*/
338349
on(
339-
name: "obsidian-git:view-refresh",
350+
name: "obsidian-git:loading-status",
340351
callback: () => void,
341352
ctx?: unknown
342353
): EventRef;
354+
/**
355+
* Emitted when the HEAD changed.
356+
*/
343357
on(
344358
name: "obsidian-git:head-change",
345359
callback: () => void,
346360
ctx?: unknown
347361
): EventRef;
362+
/**
363+
* Emitted when a new cached status is available.
364+
*/
365+
on(
366+
name: "obsidian-git:status-changed",
367+
callback: (status: Status) => void,
368+
ctx?: unknown
369+
): EventRef;
348370

349371
trigger(name: string, ...data: unknown[]): void;
372+
trigger(name: "obsidian-git:refreshed"): void;
350373
trigger(name: "obsidian-git:refresh"): void;
351-
trigger(name: "obsidian-git:view-refresh"): void;
374+
trigger(name: "obsidian-git:loading-status"): void;
352375
trigger(name: "obsidian-git:head-change"): void;
376+
trigger(name: "obsidian-git:status-changed", status: Status): void;
353377
}
354378
}

src/ui/diff/diffView.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ export default class DiffView extends ItemView {
2121
this.parser = new DOMParser();
2222
this.navigation = true;
2323
this.gitRefreshRef = this.app.workspace.on(
24-
"obsidian-git:refresh",
25-
() => {
26-
this.refresh().catch(console.error);
27-
}
28-
);
29-
this.gitViewRefreshRef = this.app.workspace.on(
30-
"obsidian-git:view-refresh",
24+
"obsidian-git:status-changed",
3125
() => {
3226
this.refresh().catch(console.error);
3327
}

src/ui/diff/splitDiffView.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,7 @@ export default class SplitDiffView extends ItemView {
3939
super(leaf);
4040
this.navigation = true;
4141
this.registerEvent(
42-
this.app.workspace.on("obsidian-git:refresh", () => {
43-
if (!this.mergeView) {
44-
this.createMergeView().catch(console.error);
45-
} else {
46-
this.updateRefEditors().catch(console.error);
47-
}
48-
})
49-
);
50-
this.registerEvent(
51-
this.app.workspace.on("obsidian-git:head-change", () => {
42+
this.app.workspace.on("obsidian-git:status-changed", () => {
5243
if (!this.mergeView) {
5344
this.createMergeView().catch(console.error);
5445
} else {

src/ui/history/historyView.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
}
2727
});
2828
refreshRef = view.app.workspace.on(
29-
"obsidian-git:view-refresh",
29+
"obsidian-git:head-change",
3030
() => void refresh().catch(console.error)
3131
);
3232
refresh().catch(console.error);

src/ui/sourceControl/sourceControl.svelte

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { Platform, setIcon, type EventRef } from "obsidian";
2+
import { Platform, setIcon } from "obsidian";
33
import { SOURCE_CONTROL_VIEW_CONFIG } from "src/constants";
44
import type ObsidianGit from "src/main";
55
import type {
@@ -9,7 +9,6 @@
99
} from "src/types";
1010
import { CurrentGitAction, FileType } from "src/types";
1111
import { arrayProxyWithNewLength, getDisplayPath } from "src/utils";
12-
import { onDestroy } from "svelte";
1312
import { slide } from "svelte/transition";
1413
import { DiscardModal } from "../modals/discardModal";
1514
import FileComponent from "./components/fileComponent.svelte";
@@ -36,19 +35,50 @@
3635
let changesOpen = $state(true);
3736
let stagedOpen = $state(true);
3837
let lastPulledFilesOpen = $state(true);
38+
let unPushedCommits = $state(0);
3939
4040
let showTree = $state(plugin.settings.treeStructure);
41-
let refreshRef: EventRef;
42-
refreshRef = view.app.workspace.on(
43-
"obsidian-git:view-refresh",
44-
() => void refresh().catch(console.error)
41+
view.registerEvent(
42+
view.app.workspace.on(
43+
"obsidian-git:loading-status",
44+
() => (loading = true)
45+
)
46+
);
47+
view.registerEvent(
48+
view.app.workspace.on(
49+
"obsidian-git:status-changed",
50+
() => void refresh().catch(console.error)
51+
)
4552
);
46-
refresh().catch(console.error);
53+
if (view.plugin.cachedStatus == undefined) {
54+
view.plugin.refresh().catch(console.error);
55+
} else {
56+
refresh().catch(console.error);
57+
}
4758
$effect(() => {
4859
buttons.forEach((btn) => setIcon(btn, btn.getAttr("data-icon")!));
4960
});
50-
onDestroy(() => {
51-
view.app.workspace.offref(refreshRef);
61+
62+
$effect(() => {
63+
// highlight push button if there are unpushed commits
64+
buttons.forEach((btn) => {
65+
// when reloading the view from settings change, the btn are null at first
66+
if (!btn || btn.id != "push") return;
67+
if (Platform.isMobile) {
68+
btn.removeClass("button-border");
69+
if (unPushedCommits > 0) {
70+
btn.addClass("button-border");
71+
}
72+
} else {
73+
btn.firstElementChild?.removeAttribute("color");
74+
if (unPushedCommits > 0) {
75+
btn.firstElementChild?.setAttr(
76+
"color",
77+
"var(--text-accent)"
78+
);
79+
}
80+
}
81+
});
5282
});
5383
5484
async function commit() {
@@ -89,29 +119,10 @@
89119
status = undefined;
90120
return;
91121
}
92-
const unPushedCommits = await plugin.gitManager.getUnpushedCommits();
93-
94-
// highlight push button if there are unpushed commits
95-
buttons.forEach((btn) => {
96-
// when reloading the view from settings change, the btn are null at first
97-
if (!btn) return;
98-
if (Platform.isMobile) {
99-
btn.removeClass("button-border");
100-
if (btn.id == "push" && unPushedCommits > 0) {
101-
btn.addClass("button-border");
102-
}
103-
} else {
104-
btn.firstElementChild?.removeAttribute("color");
105-
if (btn.id == "push" && unPushedCommits > 0) {
106-
btn.firstElementChild?.setAttr(
107-
"color",
108-
"var(--text-accent)"
109-
);
110-
}
111-
}
112-
});
122+
unPushedCommits = await plugin.gitManager.getUnpushedCommits();
113123
114124
status = plugin.cachedStatus;
125+
loading = false;
115126
if (
116127
plugin.lastPulledFiles &&
117128
plugin.lastPulledFiles != lastPulledFiles
@@ -150,7 +161,6 @@
150161
changeHierarchy = undefined;
151162
stagedHierarchy = undefined;
152163
}
153-
loading = plugin.loading;
154164
}
155165
156166
function triggerRefresh() {

0 commit comments

Comments
 (0)