From ecb9bb2428b60fd40b9f58a341e57ab291d2b0de Mon Sep 17 00:00:00 2001 From: futengda Date: Fri, 31 Jul 2020 11:39:37 +0800 Subject: [PATCH] refactor: write `lastVisited` and `query` at the same time In addition, the `settings.write` method now uses shallow merge by default --- src/node/app/vscode.ts | 11 +++-------- src/node/settings.ts | 4 ++-- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/node/app/vscode.ts b/src/node/app/vscode.ts index a41212ca6..8965fb6fe 100644 --- a/src/node/app/vscode.ts +++ b/src/node/app/vscode.ts @@ -183,14 +183,9 @@ export class VscodeHttpProvider extends HttpProvider { }), ]) - let promise = Promise.resolve() - if (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) + settings.write({ + lastVisited: startPath || lastVisited, // If startpath is undefined, then fallback to lastVisited + query: route.query, }) if (!this.isDev) { diff --git a/src/node/settings.ts b/src/node/settings.ts index abb8f63b7..7564b5319 100644 --- a/src/node/settings.ts +++ b/src/node/settings.ts @@ -30,9 +30,9 @@ export class SettingsProvider { /** * Write settings combined with current settings. On failure log a warning. - * Objects will be merged and everything else will be replaced. + * Settings can be shallow or deep merged. */ - public async write(settings: Partial, shallow?: boolean): Promise { + public async write(settings: Partial, shallow = true): Promise { try { const oldSettings = await this.read() const nextSettings = shallow ? Object.assign({}, oldSettings, settings) : extend(oldSettings, settings)