Skip to content

Commit

Permalink
refactor: restructure events
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Jan 4, 2025
1 parent 3477cdd commit d13dff4
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 67 deletions.
2 changes: 0 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export function addCommmands(plugin: ObsidianGit) {
leaf = leafs.first()!;
}
await app.workspace.revealLeaf(leaf);
app.workspace.trigger("obsidian-git:refresh");
},
});
plugin.addCommand({
Expand All @@ -68,7 +67,6 @@ export function addCommmands(plugin: ObsidianGit) {
leaf = leafs.first()!;
}
await app.workspace.revealLeaf(leaf);
app.workspace.trigger("obsidian-git:refresh");
},
});

Expand Down
19 changes: 9 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ export default class ObsidianGit extends Plugin {
branchBar?: BranchStatusBar;
state: PluginState = {
gitAction: CurrentGitAction.idle,
loading: false,
offlineMode: false,
};
lastPulledFiles: FileStatusResult[];
gitReady = false;
promiseQueue: PromiseQueue = new PromiseQueue(this);
autoCommitDebouncer: Debouncer<[], void> | undefined;
loading = false;
cachedStatus: Status | undefined;
// Used to store the path of the file that is currently shown in the diff view.
lastDiffViewState: Record<string, unknown> | undefined;
Expand All @@ -84,6 +82,7 @@ export default class ObsidianGit extends Plugin {
}

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

this.app.workspace.trigger(
"obsidian-git:status-changed",
this.cachedStatus
);
return this.cachedStatus;
}

Expand All @@ -111,14 +114,11 @@ export default class ObsidianGit extends Plugin {
gitViews.some((leaf) => !(leaf.isDeferred ?? false)) ||
historyViews.some((leaf) => !(leaf.isDeferred ?? false))
) {
this.loading = true;
this.app.workspace.trigger("obsidian-git:view-refresh");

await this.updateCachedStatus().catch((e) => this.displayError(e));
this.loading = false;
this.app.workspace.trigger("obsidian-git:view-refresh");
}

this.app.workspace.trigger("obsidian-git:refreshed");

// We don't put a line authoring refresh here, as it would force a re-loading
// of the line authoring feature - which would lead to a jumpy editor-view in the
// ui after every rename event.
Expand Down Expand Up @@ -285,8 +285,6 @@ export default class ObsidianGit extends Plugin {
leaf = leafs.first()!;
}
await this.app.workspace.revealLeaf(leaf);

this.app.workspace.trigger("obsidian-git:refresh");
}
);

Expand Down Expand Up @@ -424,7 +422,6 @@ export default class ObsidianGit extends Plugin {

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

this.lineAuthoringFeature.deactivateFeature();
this.automaticsManager.unload();
Expand Down Expand Up @@ -1041,6 +1038,7 @@ export default class ObsidianGit extends Plugin {
if (selectedBranch != undefined) {
await this.gitManager.checkout(selectedBranch);
this.displayMessage(`Switched to ${selectedBranch}`);
this.app.workspace.trigger("obsidian-git:refresh");
await this.branchBar?.display();
return selectedBranch;
}
Expand Down Expand Up @@ -1148,6 +1146,7 @@ export default class ObsidianGit extends Plugin {
new Notice(
"All local changes have been discarded. New files remain untouched."
);
this.app.workspace.trigger("obsidian-git:refresh");
}

async handleConflict(conflicted?: string[]): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/statusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class StatusBar {
this.statusBarEl.setAttribute("data-tooltip-position", "top");

plugin.registerEvent(
plugin.app.workspace.on("obsidian-git:refresh", () => {
plugin.app.workspace.on("obsidian-git:refreshed", () => {
this.refreshCommitTimestamp().catch(console.error);
})
);
Expand Down
34 changes: 29 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ export interface FileStatusResult {
export interface PluginState {
offlineMode: boolean;
gitAction: CurrentGitAction;
/* Currently refreshing the cached status
*/
loading: boolean;
}

export enum CurrentGitAction {
Expand Down Expand Up @@ -330,25 +327,52 @@ declare module "obsidian" {
inlineTitleEl: HTMLElement;
}
interface Workspace {
/**
* Emitted when some git action has been completed and plugin has been refreshed
*/
on(
name: "obsidian-git:refreshed",
callback: () => void,
ctx?: unknown
): EventRef;
/**
* Emitted when some git action has been completed and the plugin should refresh
*/
on(
name: "obsidian-git:refresh",
callback: () => void,
ctx?: unknown
): EventRef;
/**
* Emitted when the plugin is currently loading a new cached status.
*/
on(
name: "obsidian-git:view-refresh",
name: "obsidian-git:loading-status",
callback: () => void,
ctx?: unknown
): EventRef;
/**
* Emitted when the HEAD changed.
*/
on(
name: "obsidian-git:head-change",
callback: () => void,
ctx?: unknown
): EventRef;
/**
* Emitted when a new cached status is available.
*/
on(
name: "obsidian-git:status-changed",
callback: (status: Status) => void,
ctx?: unknown
): EventRef;

trigger(name: string, ...data: unknown[]): void;
trigger(name: "obsidian-git:refreshed"): void;
trigger(name: "obsidian-git:refresh"): void;
trigger(name: "obsidian-git:view-refresh"): void;
trigger(name: "obsidian-git:loading-status"): void;
trigger(name: "obsidian-git:head-change"): void;
trigger(name: "obsidian-git:status-changed", status: Status): void;
}
}
8 changes: 1 addition & 7 deletions src/ui/diff/diffView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ export default class DiffView extends ItemView {
this.parser = new DOMParser();
this.navigation = true;
this.gitRefreshRef = this.app.workspace.on(
"obsidian-git:refresh",
() => {
this.refresh().catch(console.error);
}
);
this.gitViewRefreshRef = this.app.workspace.on(
"obsidian-git:view-refresh",
"obsidian-git:status-changed",
() => {
this.refresh().catch(console.error);
}
Expand Down
11 changes: 1 addition & 10 deletions src/ui/diff/splitDiffView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,7 @@ export default class SplitDiffView extends ItemView {
super(leaf);
this.navigation = true;
this.registerEvent(
this.app.workspace.on("obsidian-git:refresh", () => {
if (!this.mergeView) {
this.createMergeView().catch(console.error);
} else {
this.updateRefEditors().catch(console.error);
}
})
);
this.registerEvent(
this.app.workspace.on("obsidian-git:head-change", () => {
this.app.workspace.on("obsidian-git:status-changed", () => {
if (!this.mergeView) {
this.createMergeView().catch(console.error);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/history/historyView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}
});
refreshRef = view.app.workspace.on(
"obsidian-git:view-refresh",
"obsidian-git:head-change",
() => void refresh().catch(console.error)
);
refresh().catch(console.error);
Expand Down
72 changes: 41 additions & 31 deletions src/ui/sourceControl/sourceControl.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { Platform, setIcon, type EventRef } from "obsidian";
import { Platform, setIcon } from "obsidian";
import { SOURCE_CONTROL_VIEW_CONFIG } from "src/constants";
import type ObsidianGit from "src/main";
import type {
Expand All @@ -9,7 +9,6 @@
} from "src/types";
import { CurrentGitAction, FileType } from "src/types";
import { arrayProxyWithNewLength, getDisplayPath } from "src/utils";
import { onDestroy } from "svelte";
import { slide } from "svelte/transition";
import { DiscardModal } from "../modals/discardModal";
import FileComponent from "./components/fileComponent.svelte";
Expand All @@ -36,19 +35,50 @@
let changesOpen = $state(true);
let stagedOpen = $state(true);
let lastPulledFilesOpen = $state(true);
let unPushedCommits = $state(0);
let showTree = $state(plugin.settings.treeStructure);
let refreshRef: EventRef;
refreshRef = view.app.workspace.on(
"obsidian-git:view-refresh",
() => void refresh().catch(console.error)
view.registerEvent(
view.app.workspace.on(
"obsidian-git:loading-status",
() => (loading = true)
)
);
view.registerEvent(
view.app.workspace.on(
"obsidian-git:status-changed",
() => void refresh().catch(console.error)
)
);
refresh().catch(console.error);
if (view.plugin.cachedStatus == undefined) {
view.plugin.refresh().catch(console.error);
} else {
refresh().catch(console.error);
}
$effect(() => {
buttons.forEach((btn) => setIcon(btn, btn.getAttr("data-icon")!));
});
onDestroy(() => {
view.app.workspace.offref(refreshRef);
$effect(() => {
// highlight push button if there are unpushed commits
buttons.forEach((btn) => {
// when reloading the view from settings change, the btn are null at first
if (!btn || btn.id != "push") return;
if (Platform.isMobile) {
btn.removeClass("button-border");
if (unPushedCommits > 0) {
btn.addClass("button-border");
}
} else {
btn.firstElementChild?.removeAttribute("color");
if (unPushedCommits > 0) {
btn.firstElementChild?.setAttr(
"color",
"var(--text-accent)"
);
}
}
});
});
async function commit() {
Expand Down Expand Up @@ -89,29 +119,10 @@
status = undefined;
return;
}
const unPushedCommits = await plugin.gitManager.getUnpushedCommits();
// highlight push button if there are unpushed commits
buttons.forEach((btn) => {
// when reloading the view from settings change, the btn are null at first
if (!btn) return;
if (Platform.isMobile) {
btn.removeClass("button-border");
if (btn.id == "push" && unPushedCommits > 0) {
btn.addClass("button-border");
}
} else {
btn.firstElementChild?.removeAttribute("color");
if (btn.id == "push" && unPushedCommits > 0) {
btn.firstElementChild?.setAttr(
"color",
"var(--text-accent)"
);
}
}
});
unPushedCommits = await plugin.gitManager.getUnpushedCommits();
status = plugin.cachedStatus;
loading = false;
if (
plugin.lastPulledFiles &&
plugin.lastPulledFiles != lastPulledFiles
Expand Down Expand Up @@ -150,7 +161,6 @@
changeHierarchy = undefined;
stagedHierarchy = undefined;
}
loading = plugin.loading;
}
function triggerRefresh() {
Expand Down

0 comments on commit d13dff4

Please sign in to comment.