Skip to content

Commit 682c108

Browse files
committed
Various code improvements to Global & Workspace View State methods and unit tests.
1 parent c18e20a commit 682c108

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

tests/extensionState.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ describe('ExtensionState', () => {
427427
const result = extensionState.getGlobalViewState();
428428

429429
// Assert
430+
expect(extensionContext.globalState.get).toHaveBeenCalledWith('globalViewState', expect.anything());
430431
expect(result).toStrictEqual(globalViewState);
431432
});
432433

@@ -440,6 +441,7 @@ describe('ExtensionState', () => {
440441
const result = extensionState.getGlobalViewState();
441442

442443
// Assert
444+
expect(extensionContext.globalState.get).toHaveBeenCalledWith('globalViewState', expect.anything());
443445
expect(result).toStrictEqual({
444446
alwaysAcceptCheckoutCommit: false,
445447
issueLinkingConfig: null
@@ -454,6 +456,7 @@ describe('ExtensionState', () => {
454456
const result = extensionState.getGlobalViewState();
455457

456458
// Assert
459+
expect(extensionContext.globalState.get).toHaveBeenCalledWith('globalViewState', expect.anything());
457460
expect(result).toStrictEqual({
458461
alwaysAcceptCheckoutCommit: false,
459462
issueLinkingConfig: null
@@ -509,6 +512,7 @@ describe('ExtensionState', () => {
509512
const result = extensionState.getWorkspaceViewState();
510513

511514
// Assert
515+
expect(extensionContext.workspaceState.get).toHaveBeenCalledWith('workspaceViewState', expect.anything());
512516
expect(result).toStrictEqual(workspaceViewState);
513517
});
514518

@@ -523,6 +527,7 @@ describe('ExtensionState', () => {
523527
const result = extensionState.getWorkspaceViewState();
524528

525529
// Assert
530+
expect(extensionContext.workspaceState.get).toHaveBeenCalledWith('workspaceViewState', expect.anything());
526531
expect(result).toStrictEqual({
527532
findIsCaseSensitive: true,
528533
findIsRegex: false,
@@ -538,6 +543,7 @@ describe('ExtensionState', () => {
538543
const result = extensionState.getWorkspaceViewState();
539544

540545
// Assert
546+
expect(extensionContext.workspaceState.get).toHaveBeenCalledWith('workspaceViewState', expect.anything());
541547
expect(result).toStrictEqual({
542548
findIsCaseSensitive: false,
543549
findIsRegex: false,

web/findWidget.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class FindWidget {
6464
this.caseSensitiveElem = document.getElementById('findCaseSensitive')!;
6565
alterClass(this.caseSensitiveElem, CLASS_ACTIVE, workspaceState.findIsCaseSensitive);
6666
this.caseSensitiveElem.addEventListener('click', () => {
67-
this.view.updateWorkspaceViewState('findIsCaseSensitive', !workspaceState.findIsCaseSensitive);
67+
updateWorkspaceViewState('findIsCaseSensitive', !workspaceState.findIsCaseSensitive);
6868
alterClass(this.caseSensitiveElem, CLASS_ACTIVE, workspaceState.findIsCaseSensitive);
6969
this.clearMatches();
7070
this.findMatches(this.getCurrentHash(), true);
@@ -74,7 +74,7 @@ class FindWidget {
7474
this.regexElem = document.getElementById('findRegex')!;
7575
alterClass(this.regexElem, CLASS_ACTIVE, workspaceState.findIsRegex);
7676
this.regexElem.addEventListener('click', () => {
77-
this.view.updateWorkspaceViewState('findIsRegex', !workspaceState.findIsRegex);
77+
updateWorkspaceViewState('findIsRegex', !workspaceState.findIsRegex);
7878
alterClass(this.regexElem, CLASS_ACTIVE, workspaceState.findIsRegex);
7979
this.clearMatches();
8080
this.findMatches(this.getCurrentHash(), true);
@@ -97,7 +97,7 @@ class FindWidget {
9797
openCdvElem.innerHTML = SVG_ICONS.cdv;
9898
alterClass(openCdvElem, CLASS_ACTIVE, workspaceState.findOpenCommitDetailsView);
9999
openCdvElem.addEventListener('click', () => {
100-
this.view.updateWorkspaceViewState('findOpenCommitDetailsView', !workspaceState.findOpenCommitDetailsView);
100+
updateWorkspaceViewState('findOpenCommitDetailsView', !workspaceState.findOpenCommitDetailsView);
101101
alterClass(openCdvElem, CLASS_ACTIVE, workspaceState.findOpenCommitDetailsView);
102102
this.openCommitDetailsViewForCurrentMatchIfEnabled();
103103
});

web/global.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ declare global {
1515

1616
type Config = GG.GitGraphViewConfig;
1717

18-
var initialState: GG.GitGraphViewInitialState;
19-
var globalState: GG.GitGraphViewGlobalState;
20-
var workspaceState: GG.GitGraphViewWorkspaceState;
18+
const initialState: GG.GitGraphViewInitialState;
19+
const globalState: GG.DeepReadonly<GG.GitGraphViewGlobalState>;
20+
const workspaceState: GG.DeepReadonly<GG.GitGraphViewWorkspaceState>;
2121

2222
type AvatarImageCollection = { [email: string]: string };
2323

web/main.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -772,16 +772,6 @@ class GitGraphView {
772772
}
773773
}
774774

775-
public updateGlobalViewState<K extends keyof GG.GitGraphViewGlobalState>(key: K, value: GG.GitGraphViewGlobalState[K]) {
776-
globalState[key] = value;
777-
sendMessage({ command: 'setGlobalViewState', state: globalState });
778-
}
779-
780-
public updateWorkspaceViewState<K extends keyof GG.GitGraphViewWorkspaceState>(key: K, value: GG.GitGraphViewWorkspaceState[K]) {
781-
workspaceState[key] = value;
782-
sendMessage({ command: 'setWorkspaceViewState', state: workspaceState });
783-
}
784-
785775

786776
/* Renderers */
787777

@@ -1124,7 +1114,7 @@ class GitGraphView {
11241114
} else {
11251115
dialog.showCheckbox('Are you sure you want to checkout commit <b><i>' + abbrevCommit(hash) + '</i></b>? This will result in a \'detached HEAD\' state.', 'Always Accept', false, 'Yes, checkout', (alwaysAccept) => {
11261116
if (alwaysAccept) {
1127-
this.updateGlobalViewState('alwaysAcceptCheckoutCommit', true);
1117+
updateGlobalViewState('alwaysAcceptCheckoutCommit', true);
11281118
}
11291119
checkoutCommit();
11301120
}, target);

web/settingsWidget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ class SettingsWidget {
551551
if (this.repo.issueLinkingConfig !== null) {
552552
this.view.saveRepoStateValue(this.currentRepo, 'issueLinkingConfig', null);
553553
}
554-
this.view.updateGlobalViewState('issueLinkingConfig', config);
554+
updateGlobalViewState('issueLinkingConfig', config);
555555
} else {
556556
this.view.saveRepoStateValue(this.currentRepo, 'issueLinkingConfig', config);
557557
}

web/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,29 @@ function handledEvent(event: Event) {
477477
}
478478

479479

480+
/* State Helpers */
481+
482+
/**
483+
* Update a key-value pair in the Global View State.
484+
* @param key The key identifying the value to update.
485+
* @param value The new value.
486+
*/
487+
function updateGlobalViewState<K extends keyof GG.GitGraphViewGlobalState>(key: K, value: GG.GitGraphViewGlobalState[K]) {
488+
(<GG.DeepWriteable<GG.GitGraphViewGlobalState>>globalState)[key] = value;
489+
sendMessage({ command: 'setGlobalViewState', state: globalState });
490+
}
491+
492+
/**
493+
* Update a key-value pair in the Workspace View State.
494+
* @param key The key identifying the value to update.
495+
* @param value The new value.
496+
*/
497+
function updateWorkspaceViewState<K extends keyof GG.GitGraphViewWorkspaceState>(key: K, value: GG.GitGraphViewWorkspaceState[K]) {
498+
(<GG.DeepWriteable<GG.GitGraphViewWorkspaceState>>workspaceState)[key] = value;
499+
sendMessage({ command: 'setWorkspaceViewState', state: workspaceState });
500+
}
501+
502+
480503
/* VSCode Helpers */
481504

482505
/**

0 commit comments

Comments
 (0)