Skip to content

feat: persist route query to local #1920

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 3 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions src/node/app/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ export class VscodeHttpProvider extends HttpProvider {
}),
])

let promise = Promise.resolve()
if (startPath) {
settings.write({
lastVisited: startPath,
})
promise = settings.write({ lastVisited: startPath })
}
// `settings.write` depends on `settings.read` internally. To avoid race conditions, a promise is added here to synchronize.
promise.then(() => {
// the query should not be extends, but should be replaced directly.
settings.write({ query: route.query }, true)
})

if (!this.isDev) {
response.content = response.content.replace(/<!-- PROD_ONLY/g, "").replace(/END_PROD_ONLY -->/g, "")
Expand Down
8 changes: 6 additions & 2 deletions src/node/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from "fs-extra"
import * as path from "path"
import { extend, paths } from "./util"
import { logger } from "@coder/logger"
import { Route } from "./http"

export type Settings = { [key: string]: Settings | string | boolean | number }

Expand Down Expand Up @@ -31,9 +32,11 @@ export class SettingsProvider<T> {
* Write settings combined with current settings. On failure log a warning.
* Objects will be merged and everything else will be replaced.
*/
public async write(settings: Partial<T>): Promise<void> {
public async write(settings: Partial<T>, shallow?: boolean): Promise<void> {
try {
await fs.writeFile(this.settingsPath, JSON.stringify(extend(await this.read(), settings), null, 2))
const oldSettings = await this.read()
const nextSettings = shallow ? Object.assign({}, oldSettings, settings) : extend(oldSettings, settings)
await fs.writeFile(this.settingsPath, JSON.stringify(nextSettings, null, 2))
} catch (error) {
logger.warn(error.message)
}
Expand All @@ -55,6 +58,7 @@ export interface CoderSettings extends UpdateSettings {
url: string
workspace: boolean
}
query: Route["query"]
}

/**
Expand Down