Skip to content

Commit

Permalink
Add command to reset state (#3421)
Browse files Browse the repository at this point in the history
* Add command to reset state

* Add tests
  • Loading branch information
sroy3 authored Mar 10, 2023
1 parent 9756267 commit 422f7e6
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 5 deletions.
5 changes: 5 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,11 @@
"command": "dvc.views.plotsPathsTree.refreshPlots",
"category": "DVC",
"icon": "$(refresh)"
},
{
"title": "Reset Persisted State and Reload Window",
"command": "dvc.resetState",
"category": "DVC"
}
],
"configuration": {
Expand Down
4 changes: 3 additions & 1 deletion extension/src/commands/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ export enum RegisteredCommands {
ADD_STUDIO_ACCESS_TOKEN = 'dvc.addStudioAccessToken',
UPDATE_STUDIO_ACCESS_TOKEN = 'dvc.updateStudioAccessToken',
REMOVE_STUDIO_ACCESS_TOKEN = 'dvc.removeStudioAccessToken',
EXPERIMENT_VIEW_SHARE_TO_STUDIO = 'dvc.views.experiments.shareExperimentToStudio'
EXPERIMENT_VIEW_SHARE_TO_STUDIO = 'dvc.views.experiments.shareExperimentToStudio',

RESET_STATE = 'dvc.resetState'
}
3 changes: 3 additions & 0 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { LanguageClient } from './languageClient'
import { collectRunningExperimentPids } from './experiments/processExecution/collect'
import { registerPatchCommand } from './patch'
import { DvcViewer } from './cli/dvc/viewer'
import { registerPersistenceCommands } from './persistence/register'
export class Extension extends Disposable {
protected readonly internalCommands: InternalCommands

Expand Down Expand Up @@ -287,6 +288,8 @@ export class Extension extends Disposable {
).contributes.walkthroughs[0].id
)

registerPersistenceCommands(context.workspaceState, this.internalCommands)

void showWalkthroughOnFirstUse(env.isNewAppInstall)
this.dispose.track(recommendRedHatExtensionOnce())

Expand Down
13 changes: 13 additions & 0 deletions extension/src/persistence/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Memento } from 'vscode'
import { resetPersistedState } from './util'
import { RegisteredCommands } from '../commands/external'
import { InternalCommands } from '../commands/internal'

export const registerPersistenceCommands = (
workspaceState: Memento,
internalCommands: InternalCommands
) => {
internalCommands.registerExternalCommand(RegisteredCommands.RESET_STATE, () =>
resetPersistedState(workspaceState)
)
}
49 changes: 49 additions & 0 deletions extension/src/persistence/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { commands } from 'vscode'
import { PersistenceKey } from './constants'
import { resetPersistedState } from './util'
import { buildMockMemento } from '../test/util'
import {
DEFAULT_HEIGHT,
DEFAULT_SECTION_NB_ITEMS_PER_ROW
} from '../plots/webview/contract'

jest.mock('vscode')

const mockedCommands = jest.mocked(commands)
mockedCommands.executeCommand = jest.fn()

describe('Persistence util', () => {
beforeEach(() => {
jest.resetAllMocks()
})

describe('resetPersistedState', () => {
it('should reload the window', async () => {
const workspaceState = buildMockMemento()

await resetPersistedState(workspaceState)

expect(mockedCommands.executeCommand).toHaveBeenCalledWith(
'workbench.action.reloadWindow'
)
})

it('should reset all values from all dvc roots', async () => {
const persistedState = {
[PersistenceKey.PLOT_HEIGHT + 'root1']: DEFAULT_HEIGHT,
[PersistenceKey.PLOT_NB_ITEMS_PER_ROW + 'root2']:
DEFAULT_SECTION_NB_ITEMS_PER_ROW
}
const workspaceState = buildMockMemento(persistedState)

await resetPersistedState(workspaceState)

expect(
workspaceState.get(PersistenceKey.PLOT_HEIGHT + 'root1')
).toBeUndefined()
expect(
workspaceState.get(PersistenceKey.PLOT_NB_ITEMS_PER_ROW + 'root2')
).toBeUndefined()
})
})
})
9 changes: 9 additions & 0 deletions extension/src/persistence/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { commands, Memento } from 'vscode'

export const resetPersistedState = async (workspaceState: Memento) => {
for (const persistenceKey of workspaceState.keys()) {
await workspaceState.update(persistenceKey, undefined)
}

await commands.executeCommand('workbench.action.reloadWindow')
}
2 changes: 2 additions & 0 deletions extension/src/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,6 @@ export interface IEventNamePropertyMapping {
[EventName.ADD_STUDIO_ACCESS_TOKEN]: undefined
[EventName.UPDATE_STUDIO_ACCESS_TOKEN]: undefined
[EventName.REMOVE_STUDIO_ACCESS_TOKEN]: undefined

[EventName.RESET_STATE]: undefined
}
8 changes: 4 additions & 4 deletions extension/src/test/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const buildMockMemento = (
({
get: (key: string, defaultValue: unknown) => values[key] || defaultValue,
keys: () => Object.keys(values),
update: (key: string, value: unknown) =>
new Promise(() => {
values[key] = value
})
update: (key: string, value: unknown) => {
values[key] = value
void Promise.resolve()
}
} as unknown as Memento)

0 comments on commit 422f7e6

Please sign in to comment.