refactor: write `lastVisited` and `query` at the same time

In addition, the `settings.write` method now uses shallow merge by default
This commit is contained in:
futengda 2020-07-31 11:39:37 +08:00
parent b6e791f7d0
commit ecb9bb2428
2 changed files with 5 additions and 10 deletions

View File

@ -183,14 +183,9 @@ export class VscodeHttpProvider extends HttpProvider {
}), }),
]) ])
let promise = Promise.resolve() settings.write({
if (startPath) { lastVisited: startPath || lastVisited, // If startpath is undefined, then fallback to lastVisited
promise = settings.write({ lastVisited: startPath }) query: route.query,
}
// `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) { if (!this.isDev) {

View File

@ -30,9 +30,9 @@ export class SettingsProvider<T> {
/** /**
* Write settings combined with current settings. On failure log a warning. * 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<T>, shallow?: boolean): Promise<void> { public async write(settings: Partial<T>, shallow = true): Promise<void> {
try { try {
const oldSettings = await this.read() const oldSettings = await this.read()
const nextSettings = shallow ? Object.assign({}, oldSettings, settings) : extend(oldSettings, settings) const nextSettings = shallow ? Object.assign({}, oldSettings, settings) : extend(oldSettings, settings)