Skip to content
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

Add command to reset state #3421

Merged
merged 4 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Q] Should this be returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we ever assign the result. Also there is no result to ever be returned. From there, I think it's more of a stylistic choice.

}
} as unknown as Memento)