Repository navigation
feat(home): tick a build's Auto start box to start it when Desktop opens #2123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { effectScope, type EffectScope } from 'vue' | ||
| import { flushPromises } from '@vue/test-utils' | ||
|
|
||
| import { useAutoStartBuild } from './useAutoStartBuild' | ||
|
|
||
| let saved: unknown | ||
| let settingsChanged: (change: { key: string }) => void | ||
| const unsubscribe = vi.fn() | ||
| const getSetting = vi.fn(async (_key: string): Promise<unknown> => saved) | ||
| const setSetting = vi.fn(async (_key: string, value: unknown): Promise<void> => { | ||
| saved = value | ||
| }) | ||
| let scope: EffectScope | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| saved = 'none' | ||
| ;(window as unknown as { api: unknown }).api = { | ||
| getSetting, | ||
| setSetting, | ||
| onSettingsChanged: vi.fn((callback: (change: { key: string }) => void) => { | ||
| settingsChanged = callback | ||
| return unsubscribe | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| scope?.stop() | ||
| }) | ||
|
|
||
| async function start() { | ||
| scope = effectScope() | ||
| const autoStart = scope.run(() => useAutoStartBuild())! | ||
| await flushPromises() | ||
| return autoStart | ||
| } | ||
|
|
||
| describe('useAutoStartBuild', () => { | ||
| it('reads the Auto-launch on startup setting and ticks only the build it names', async () => { | ||
| saved = 'build-a' | ||
| const autoStart = await start() | ||
|
|
||
| expect(getSetting).toHaveBeenCalledWith('autoLaunchOnStartup') | ||
| expect(autoStart.startsOnOpen('build-a')).toBe(true) | ||
| expect(autoStart.startsOnOpen('build-b')).toBe(false) | ||
| }) | ||
|
|
||
| it('ticks no build for None or Last used build', async () => { | ||
| const autoStart = await start() | ||
| expect(autoStart.startsOnOpen('build-a')).toBe(false) | ||
|
|
||
| saved = 'last' | ||
| settingsChanged({ key: 'autoLaunchOnStartup' }) | ||
| await flushPromises() | ||
|
|
||
| expect(autoStart.startsOnOpen('build-a')).toBe(false) | ||
| }) | ||
|
|
||
| it('moves the tick to a newly ticked build and saves its id in the same setting', async () => { | ||
| saved = 'build-a' | ||
| const autoStart = await start() | ||
|
|
||
| await autoStart.setStartsOnOpen('build-b', true) | ||
|
|
||
| expect(setSetting).toHaveBeenCalledWith('autoLaunchOnStartup', 'build-b') | ||
| expect(autoStart.startsOnOpen('build-b')).toBe(true) | ||
| expect(autoStart.startsOnOpen('build-a')).toBe(false) | ||
| }) | ||
|
|
||
| it('replaces Last used build with the build that is ticked', async () => { | ||
| saved = 'last' | ||
| const autoStart = await start() | ||
|
|
||
| await autoStart.setStartsOnOpen('build-a', true) | ||
|
|
||
| expect(setSetting).toHaveBeenCalledWith('autoLaunchOnStartup', 'build-a') | ||
| }) | ||
|
|
||
| it('sets None when the ticked build is unticked', async () => { | ||
| saved = 'build-a' | ||
| const autoStart = await start() | ||
|
|
||
| await autoStart.setStartsOnOpen('build-a', false) | ||
|
|
||
| expect(setSetting).toHaveBeenCalledWith('autoLaunchOnStartup', 'none') | ||
| expect(autoStart.startsOnOpen('build-a')).toBe(false) | ||
| }) | ||
|
|
||
| it('writes nothing when a build is already in the state asked for', async () => { | ||
| saved = 'build-a' | ||
| const autoStart = await start() | ||
|
|
||
| await autoStart.setStartsOnOpen('build-a', true) | ||
| await autoStart.setStartsOnOpen('build-b', false) | ||
|
|
||
| expect(setSetting).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('follows a change made in Settings without a restart, and ignores other settings', async () => { | ||
| saved = 'build-a' | ||
| const autoStart = await start() | ||
| getSetting.mockClear() | ||
|
|
||
| saved = 'build-b' | ||
| settingsChanged({ key: 'hideCloudFromPicker' }) | ||
| await flushPromises() | ||
| expect(getSetting).not.toHaveBeenCalled() | ||
| expect(autoStart.startsOnOpen('build-a')).toBe(true) | ||
|
|
||
| settingsChanged({ key: 'autoLaunchOnStartup' }) | ||
| await flushPromises() | ||
| expect(autoStart.startsOnOpen('build-b')).toBe(true) | ||
| expect(autoStart.startsOnOpen('build-a')).toBe(false) | ||
| }) | ||
|
|
||
| it('keeps a tick made while the first read is still on its way', async () => { | ||
| let answer: (value: unknown) => void = () => {} | ||
| getSetting.mockImplementationOnce( | ||
| () => | ||
| new Promise((resolve) => { | ||
| answer = resolve | ||
| }) | ||
| ) | ||
| scope = effectScope() | ||
| const autoStart = scope.run(() => useAutoStartBuild())! | ||
|
|
||
| await autoStart.setStartsOnOpen('build-b', true) | ||
| answer('build-a') | ||
| await flushPromises() | ||
|
|
||
| expect(autoStart.startsOnOpen('build-b')).toBe(true) | ||
| expect(autoStart.startsOnOpen('build-a')).toBe(false) | ||
| }) | ||
|
|
||
| it('shows the saved choice again when saving fails', async () => { | ||
| saved = 'build-a' | ||
| const autoStart = await start() | ||
| setSetting.mockRejectedValueOnce(new Error('settings.json is unreadable')) | ||
|
|
||
| await autoStart.setStartsOnOpen('build-b', true) | ||
|
|
||
| expect(autoStart.startsOnOpen('build-a')).toBe(true) | ||
| expect(autoStart.startsOnOpen('build-b')).toBe(false) | ||
| }) | ||
|
|
||
| it('shows the saved choice again when saving and reading it back both fail, so a second tick saves again', async () => { | ||
| saved = 'build-a' | ||
| const autoStart = await start() | ||
| setSetting.mockRejectedValueOnce(new Error('settings.json is unreadable')) | ||
| getSetting.mockRejectedValueOnce(new Error('settings.json is unreadable')) | ||
|
|
||
| await autoStart.setStartsOnOpen('build-b', true) | ||
|
|
||
| expect(autoStart.startsOnOpen('build-a')).toBe(true) | ||
| expect(autoStart.startsOnOpen('build-b')).toBe(false) | ||
|
|
||
| await autoStart.setStartsOnOpen('build-b', true) | ||
|
|
||
| expect(setSetting).toHaveBeenCalledTimes(2) | ||
| expect(setSetting).toHaveBeenLastCalledWith('autoLaunchOnStartup', 'build-b') | ||
| expect(autoStart.startsOnOpen('build-b')).toBe(true) | ||
| }) | ||
|
|
||
| it('keeps a newer tick when the read after a failed save also fails', async () => { | ||
| saved = 'build-a' | ||
| const autoStart = await start() | ||
| let failRead: (error: Error) => void = () => {} | ||
| setSetting.mockRejectedValueOnce(new Error('settings.json is unreadable')) | ||
| getSetting.mockImplementationOnce( | ||
| () => | ||
| new Promise((_resolve, reject) => { | ||
| failRead = reject | ||
| }) | ||
| ) | ||
|
|
||
| const failedSave = autoStart.setStartsOnOpen('build-b', true) | ||
| await flushPromises() | ||
| await autoStart.setStartsOnOpen('build-c', true) | ||
| failRead(new Error('settings.json is unreadable')) | ||
| await failedSave | ||
|
|
||
| expect(autoStart.startsOnOpen('build-c')).toBe(true) | ||
| expect(autoStart.startsOnOpen('build-a')).toBe(false) | ||
| }) | ||
|
|
||
| it('stops listening for setting changes when its page goes away', async () => { | ||
| await start() | ||
|
|
||
| scope.stop() | ||
|
|
||
| expect(unsubscribe).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { onScopeDispose, ref } from 'vue' | ||
|
|
||
| const AUTO_START_SETTING = 'autoLaunchOnStartup' | ||
| const AUTO_START_NONE = 'none' | ||
|
|
||
| export function useAutoStartBuild() { | ||
| const autoStartValue = ref<unknown>(undefined) | ||
| let confirmedValue: unknown = undefined | ||
| let revision = 0 | ||
|
|
||
| async function read(): Promise<void> { | ||
| const seen = ++revision | ||
| try { | ||
| const value = await window.api.getSetting(AUTO_START_SETTING) | ||
| if (seen !== revision) return | ||
| confirmedValue = value | ||
| autoStartValue.value = value | ||
| } catch { | ||
| if (seen === revision) autoStartValue.value = confirmedValue | ||
| } | ||
| } | ||
|
|
||
| const unsubscribe = window.api.onSettingsChanged(({ key }) => { | ||
| if (key === AUTO_START_SETTING) void read() | ||
| }) | ||
| onScopeDispose(unsubscribe) | ||
| void read() | ||
|
|
||
| function startsOnOpen(buildId: string): boolean { | ||
| return autoStartValue.value === buildId | ||
| } | ||
|
|
||
| async function setStartsOnOpen(buildId: string, on: boolean): Promise<void> { | ||
| if (on === startsOnOpen(buildId)) return | ||
| const next = on ? buildId : AUTO_START_NONE | ||
| revision++ | ||
| autoStartValue.value = next | ||
| try { | ||
| await window.api.setSetting(AUTO_START_SETTING, next) | ||
| confirmedValue = next | ||
| } catch { | ||
| await read() | ||
| } | ||
| } | ||
|
|
||
| return { startsOnOpen, setStartsOnOpen } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.