Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed May 26, 2023
1 parent e77db45 commit f40a2a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
49 changes: 18 additions & 31 deletions extension/src/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export class Setup
const webviewMessages = new WebviewMessages(
() => this.getWebview(),
() => this.initializeGit(),
(offline: boolean) => this.setStudioOffline(offline)
(offline: boolean) => this.updateStudioOffline(offline)
)
this.dispose.track(
this.onDidReceivedWebviewMessage(message =>
Expand Down Expand Up @@ -673,46 +673,24 @@ export class Setup
}

private async setStudioValues() {
if (!this.getCliCompatible()) {
const cwd = this.getCwd()

if (!cwd) {
this.studioAccessToken = undefined
this.shareLiveToStudio = undefined
return
}

if (this.dvcRoots.length !== 1) {
const cwd = getFirstWorkspaceFolder()
if (!cwd) {
this.studioAccessToken = undefined
this.shareLiveToStudio = undefined
return
}

;[this.studioAccessToken] = await Promise.all([
this.accessConfig(cwd, ConfigKey.STUDIO_TOKEN),
this.getStudioShareLive(cwd)
])
return
}

;[this.studioAccessToken] = await Promise.all([
this.accessConfig(this.dvcRoots[0], ConfigKey.STUDIO_TOKEN),
this.getStudioShareLive(this.dvcRoots[0])
])
}

private async getStudioShareLive(cwd: string) {
this.shareLiveToStudio =
;[this.studioAccessToken, this.shareLiveToStudio] = await Promise.all([
this.accessConfig(cwd, ConfigKey.STUDIO_TOKEN),
(await this.accessConfig(cwd, ConfigKey.STUDIO_OFFLINE)) !== 'true'
])
}

private async setStudioOffline(shareLive: boolean) {
private async updateStudioOffline(shareLive: boolean) {
const offline = !shareLive
if (!this.getCliCompatible()) {
return
}

const cwd =
this.dvcRoots.length === 1 ? this.dvcRoots[0] : getFirstWorkspaceFolder()
const cwd = this.getCwd()

if (!cwd) {
return
Expand All @@ -726,6 +704,15 @@ export class Setup
)
}

private getCwd() {
if (!this.getCliCompatible()) {
return
}
return this.dvcRoots.length === 1
? this.dvcRoots[0]
: getFirstWorkspaceFolder()
}

private accessConfig(cwd: string, ...args: Args) {
return this.accessDvc(AvailableCommands.CONFIG, cwd, ...args)
}
Expand Down
8 changes: 4 additions & 4 deletions extension/src/setup/webview/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import { openUrl } from '../../vscode/external'
export class WebviewMessages {
private readonly getWebview: () => BaseWebview<TSetupData> | undefined
private readonly initializeGit: () => void
private readonly setStudioOffline: (offline: boolean) => Promise<void>
private readonly updateStudioOffline: (offline: boolean) => Promise<void>

constructor(
getWebview: () => BaseWebview<TSetupData> | undefined,
initializeGit: () => void,
setStudioOffline: (shareLive: boolean) => Promise<void>
updateStudioOffline: (shareLive: boolean) => Promise<void>
) {
this.getWebview = getWebview
this.initializeGit = initializeGit
this.setStudioOffline = setStudioOffline
this.updateStudioOffline = updateStudioOffline
}

public sendWebviewMessage({
Expand Down Expand Up @@ -102,7 +102,7 @@ export class WebviewMessages {
RegisteredCommands.REMOVE_STUDIO_ACCESS_TOKEN
)
case MessageFromWebviewType.SET_STUDIO_SHARE_EXPERIMENTS_LIVE:
return this.setStudioOffline(message.payload)
return this.updateStudioOffline(message.payload)
case MessageFromWebviewType.OPEN_EXPERIMENTS_WEBVIEW:
return commands.executeCommand(RegisteredCommands.EXPERIMENT_SHOW)
case MessageFromWebviewType.REMOTE_ADD:
Expand Down

0 comments on commit f40a2a1

Please sign in to comment.