Skip to content

Commit 19a530e

Browse files
committed
Convert shortcut services from .svelte.ts to just .ts
Because $effects are difficult to work with in unit tests.
1 parent bcd6c7c commit 19a530e

14 files changed

+119
-129
lines changed

apps/desktop/src/components/AppUpdater.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ import AppUpdater from '$components/AppUpdater.svelte';
22
import { EventContext } from '$lib/analytics/eventContext';
33
import { PostHogWrapper } from '$lib/analytics/posthog';
44
import { Tauri } from '$lib/backend/tauri';
5+
import { ShortcutService } from '$lib/shortcuts/shortcutService';
56
import { getSettingsdServiceMock } from '$lib/testing/mockSettingsdService';
67
import { UPDATER_SERVICE, UpdaterService } from '$lib/updater/updater';
78
import { render, screen } from '@testing-library/svelte';
89
import { expect, test, describe, vi, beforeEach, afterEach } from 'vitest';
910
import type { Update } from '@tauri-apps/plugin-updater';
1011

1112
describe('AppUpdater', () => {
12-
let tauri: Tauri;
1313
let updater: UpdaterService;
1414
let context: Map<any, any>;
15+
const tauri = new Tauri();
16+
const shortcuts = new ShortcutService(tauri);
1517
const MockSettingsService = getSettingsdServiceMock();
1618
const settingsService = new MockSettingsService();
1719
const eventContext = new EventContext();
1820
const posthog = new PostHogWrapper(settingsService, eventContext);
1921

2022
beforeEach(() => {
2123
vi.useFakeTimers();
22-
tauri = new Tauri();
23-
updater = new UpdaterService(tauri, posthog);
24+
updater = new UpdaterService(tauri, posthog, shortcuts);
2425
context = new Map([[UPDATER_SERVICE._key, updater]]);
2526
vi.spyOn(tauri, 'listen').mockReturnValue(async () => {});
2627
vi.mock('$env/dynamic/public', () => {

apps/desktop/src/components/FileMenuAction.svelte

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
import { goto } from '$app/navigation';
33
import { PROJECTS_SERVICE } from '$lib/project/projectsService';
44
import { clonePath } from '$lib/routes/routes.svelte';
5-
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService.svelte';
5+
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
66
import { inject } from '@gitbutler/shared/context';
7+
import { mergeUnlisten } from '@gitbutler/ui/utils/mergeUnlisten';
78
89
const projectsService = inject(PROJECTS_SERVICE);
910
const shortcutService = inject(SHORTCUT_SERVICE);
1011
11-
shortcutService.on('add-local-repo', async () => {
12-
await projectsService.addProject();
13-
});
14-
15-
shortcutService.on('clone-repo', async () => {
16-
goto(clonePath());
17-
});
12+
$effect(() =>
13+
mergeUnlisten(
14+
shortcutService.on('add-local-repo', async () => {
15+
await projectsService.addProject();
16+
}),
17+
shortcutService.on('clone-repo', async () => {
18+
goto(clonePath());
19+
})
20+
)
21+
);
1822
</script>
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<script lang="ts">
22
import { goto } from '$app/navigation';
33
import { newSettingsPath } from '$lib/routes/routes.svelte';
4-
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService.svelte';
4+
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
55
import { inject } from '@gitbutler/shared/context';
66
77
const shortcutService = inject(SHORTCUT_SERVICE);
8-
shortcutService.on('global-settings', () => {
9-
goto(newSettingsPath());
10-
});
8+
$effect(() =>
9+
shortcutService.on('global-settings', () => {
10+
goto(newSettingsPath());
11+
})
12+
);
1113
</script>

apps/desktop/src/components/KeyboardShortcutsModal.svelte

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService.svelte';
2+
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
33
import { shortcuts } from '$lib/utils/hotkeys';
44
import { inject } from '@gitbutler/shared/context';
55
import Modal from '@gitbutler/ui/Modal.svelte';
@@ -8,9 +8,7 @@
88
let modal: ReturnType<typeof Modal> | undefined = $state();
99
1010
const shortcutService = inject(SHORTCUT_SERVICE);
11-
shortcutService.on('keyboard-shortcuts', () => {
12-
show();
13-
});
11+
$effect(() => shortcutService.on('keyboard-shortcuts', () => show()));
1412
1513
export function show() {
1614
modal?.show();

apps/desktop/src/components/ProjectSettingsMenuAction.svelte

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import { PROJECTS_SERVICE } from '$lib/project/projectsService';
66
import { projectSettingsPath } from '$lib/routes/routes.svelte';
77
import { SETTINGS } from '$lib/settings/userSettings';
8-
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService.svelte';
8+
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
99
import * as events from '$lib/utils/events';
10-
import { unsubscribe } from '$lib/utils/unsubscribe';
1110
import { getEditorUri, openExternalUrl, showFileInFolder } from '$lib/utils/url';
1211
import { inject } from '@gitbutler/shared/context';
13-
import { onMount } from 'svelte';
12+
import { mergeUnlisten } from '@gitbutler/ui/utils/mergeUnlisten';
1413
1514
const { projectId }: { projectId: string } = $props();
1615
@@ -19,46 +18,38 @@
1918
const userSettings = inject(SETTINGS);
2019
const shortcutService = inject(SHORTCUT_SERVICE);
2120
22-
shortcutService.on('project-settings', () => {
23-
goto(projectSettingsPath(projectId));
24-
});
25-
26-
shortcutService.on('open-in-vscode', async () => {
27-
const project = await projectsService.fetchProject(projectId);
28-
if (!project) {
29-
throw new Error(`Project not found: ${projectId}`);
30-
}
31-
openExternalUrl(
32-
getEditorUri({
33-
schemeId: $userSettings.defaultCodeEditor.schemeIdentifer,
34-
path: [vscodePath(project.path)],
35-
searchParams: { windowId: '_blank' }
21+
$effect(() =>
22+
mergeUnlisten(
23+
shortcutService.on('project-settings', () => {
24+
goto(projectSettingsPath(projectId));
25+
}),
26+
shortcutService.on('history', () => {
27+
$showHistoryView = !$showHistoryView;
28+
}),
29+
shortcutService.on('open-in-vscode', async () => {
30+
const project = await projectsService.fetchProject(projectId);
31+
if (!project) {
32+
throw new Error(`Project not found: ${projectId}`);
33+
}
34+
openExternalUrl(
35+
getEditorUri({
36+
schemeId: $userSettings.defaultCodeEditor.schemeIdentifer,
37+
path: [vscodePath(project.path)],
38+
searchParams: { windowId: '_blank' }
39+
})
40+
);
41+
}),
42+
events.on('openHistory', () => {
43+
$showHistoryView = true;
44+
}),
45+
shortcutService.on('show-in-finder', async () => {
46+
const project = await projectsService.fetchProject(projectId);
47+
if (!project) {
48+
throw new Error(`Project not found: ${projectId}`);
49+
}
50+
// Show the project directory in the default file manager (cross-platform)
51+
await showFileInFolder(project.path);
3652
})
37-
);
38-
});
39-
40-
shortcutService.on('show-in-finder', async () => {
41-
const project = await projectsService.fetchProject(projectId);
42-
if (!project) {
43-
throw new Error(`Project not found: ${projectId}`);
44-
}
45-
// Show the project directory in the default file manager (cross-platform)
46-
await showFileInFolder(project.path);
47-
});
48-
49-
shortcutService.on('history', () => {
50-
$showHistoryView = !$showHistoryView;
51-
});
52-
53-
const unsubscribeHistoryButton = unsubscribe(
54-
events.on('openHistory', () => {
55-
$showHistoryView = true;
56-
})
53+
)
5754
);
58-
59-
onMount(() => {
60-
return () => {
61-
unsubscribeHistoryButton();
62-
};
63-
});
6455
</script>
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<script lang="ts">
2-
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService.svelte';
2+
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
33
import { inject } from '@gitbutler/shared/context';
44
55
const shortcutService = inject(SHORTCUT_SERVICE);
66
7-
shortcutService.on('reload', () => {
8-
location.reload();
9-
});
7+
$effect(() => shortcutService.on('reload', () => location.reload()));
108
</script>

apps/desktop/src/components/ShareIssueModal.svelte

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { page } from '$app/stores';
33
import { invoke } from '$lib/backend/ipc';
4-
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService.svelte';
4+
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
55
import * as zip from '$lib/support/dataSharing';
66
import { USER } from '$lib/user/user';
77
import { inject } from '@gitbutler/shared/context';
@@ -130,9 +130,7 @@
130130
modal?.close();
131131
}
132132
133-
shortcutService.on('share-debug-info', () => {
134-
show();
135-
});
133+
$effect(() => shortcutService.on('share-debug-info', () => show()));
136134
</script>
137135

138136
<Modal

apps/desktop/src/components/SwitchThemeMenuAction.svelte

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { SETTINGS } from '$lib/settings/userSettings';
3-
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService.svelte';
3+
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
44
import { initTheme } from '$lib/utils/theme';
55
import { inject } from '@gitbutler/shared/context';
66
@@ -16,7 +16,5 @@
1616
}));
1717
}
1818
19-
shortcutService.on('switch-theme', () => {
20-
updateTheme();
21-
});
19+
$effect(() => shortcutService.on('switch-theme', () => updateTheme()));
2220
</script>

apps/desktop/src/components/ZoomInOutMenuAction.svelte

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<script lang="ts">
22
import { SETTINGS } from '$lib/settings/userSettings';
3-
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService.svelte';
3+
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
44
import { inject } from '@gitbutler/shared/context';
5+
import { mergeUnlisten } from '@gitbutler/ui/utils/mergeUnlisten';
56
import { onMount } from 'svelte';
67
78
const userSettings = inject(SETTINGS);
@@ -24,15 +25,19 @@
2425
userSettings.update((s) => ({ ...s, zoom }));
2526
}
2627
27-
shortcutService.on('zoom-in', () => {
28-
updateZoom(zoom + ZOOM_STEP);
29-
});
30-
shortcutService.on('zoom-out', () => {
31-
updateZoom(zoom - ZOOM_STEP);
32-
});
33-
shortcutService.on('zoom-reset', () => {
34-
updateZoom(DEFAULT_ZOOM);
35-
});
28+
$effect(() =>
29+
mergeUnlisten(
30+
shortcutService.on('zoom-in', () => {
31+
updateZoom(zoom + ZOOM_STEP);
32+
}),
33+
shortcutService.on('zoom-out', () => {
34+
updateZoom(zoom - ZOOM_STEP);
35+
}),
36+
shortcutService.on('zoom-reset', () => {
37+
updateZoom(DEFAULT_ZOOM);
38+
})
39+
)
40+
);
3641
3742
onMount(() => {
3843
if (zoom !== DEFAULT_ZOOM) {

apps/desktop/src/lib/shortcuts/shortcutService.svelte.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)