Skip to content

Commit 870bfd0

Browse files
author
Akos Kitta
committed
fix: update monitor settings only if it's changed
Closes #375 Signed-off-by: Akos Kitta <[email protected]>
1 parent 9e042ae commit 870bfd0

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

Diff for: arduino-ide-extension/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"dateformat": "^3.0.3",
6969
"deepmerge": "2.0.1",
7070
"electron-updater": "^4.6.5",
71+
"fast-json-stable-stringify": "^2.1.0",
7172
"fast-safe-stringify": "^2.1.1",
7273
"glob": "^7.1.6",
7374
"google-protobuf": "^3.20.1",

Diff for: arduino-ide-extension/src/common/utils.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import stableJsonStringify = require('fast-json-stable-stringify');
2+
13
export const naturalCompare: (left: string, right: string) => number =
24
require('string-natural-compare').caseInsensitive;
35

@@ -13,6 +15,19 @@ export function firstToUpperCase(what: string): string {
1315
return what.charAt(0).toUpperCase() + what.slice(1);
1416
}
1517

16-
export function isNullOrUndefined(what: any): what is undefined | null {
18+
export function isNullOrUndefined(what: unknown): what is undefined | null {
1719
return what === undefined || what === null;
1820
}
21+
22+
export function sameAs(left: unknown, right: unknown): boolean {
23+
if (left === right) {
24+
return true;
25+
}
26+
if (!left) {
27+
return right === undefined;
28+
}
29+
if (!right) {
30+
return false;
31+
}
32+
return stableJsonStringify(left) === stableJsonStringify(right);
33+
}

Diff for: arduino-ide-extension/src/node/monitor-service.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,10 @@ export class MonitorService extends CoreClientAware implements Disposable {
491491
*/
492492
async changeSettings(settings: MonitorSettings): Promise<Status> {
493493
const config = new MonitorPortConfiguration();
494-
const { pluggableMonitorSettings } = settings;
494+
const { pluggableMonitorSettings = {} } = settings;
495495
const reconciledSettings = await this.monitorSettingsProvider.setSettings(
496496
this.monitorID,
497-
pluggableMonitorSettings || {}
497+
pluggableMonitorSettings
498498
);
499499

500500
if (reconciledSettings) {
@@ -512,9 +512,13 @@ export class MonitorService extends CoreClientAware implements Disposable {
512512
connected: !!this.duplex,
513513
serialPort: this.port.address,
514514
},
515-
pluggableMonitorSettings: reconciledSettings,
515+
pluggableMonitorSettings: reconciledSettings ?? pluggableMonitorSettings,
516516
});
517517

518+
if (!reconciledSettings) {
519+
return Status.OK;
520+
}
521+
518522
if (!this.duplex) {
519523
return Status.NOT_CONNECTED;
520524
}

Diff for: arduino-ide-extension/src/node/monitor-settings/monitor-settings-provider-impl.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
longestPrefixMatch,
1818
reconcileSettings,
1919
} from './monitor-settings-utils';
20-
import { ILogger } from '@theia/core';
20+
import { deepClone, ILogger } from '@theia/core';
21+
import { sameAs } from '../../common/utils';
2122

2223
const MONITOR_SETTINGS_FILE = 'pluggable-monitor-settings.json';
2324

@@ -74,14 +75,17 @@ export class MonitorSettingsProviderImpl implements MonitorSettingsProvider {
7475
async setSettings(
7576
monitorId: string,
7677
settings: PluggableMonitorSettings
77-
): Promise<PluggableMonitorSettings> {
78+
): Promise<PluggableMonitorSettings | undefined> {
7879
// wait for the service to complete the init
7980
await this.ready.promise;
8081

81-
const newSettings = this.reconcileSettings(
82-
settings,
83-
this.monitorSettings[monitorId] || {}
84-
);
82+
const defaultSettings = this.monitorSettings[monitorId] || {};
83+
const oldSettings = deepClone(defaultSettings);
84+
const newSettings = this.reconcileSettings(settings, defaultSettings);
85+
if (sameAs(oldSettings, newSettings)) {
86+
// NOOP if no settings change. The `undefined` return value represents this case.
87+
return undefined;
88+
}
8589
this.monitorSettings[monitorId] = newSettings;
8690

8791
await this.writeSettingsToFS();

Diff for: arduino-ide-extension/src/node/monitor-settings/monitor-settings-provider.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { MonitorModel } from '../../browser/monitor-model';
2-
import { PluggableMonitorSetting } from '../../common/protocol';
1+
import type { MonitorModel } from '../../browser/monitor-model';
2+
import type { PluggableMonitorSetting } from '../../common/protocol';
33

44
export type PluggableMonitorSettings = Record<string, PluggableMonitorSetting>;
55
export interface MonitorSettings {
@@ -13,8 +13,12 @@ export interface MonitorSettingsProvider {
1313
monitorId: string,
1414
defaultSettings: PluggableMonitorSettings
1515
): Promise<PluggableMonitorSettings>;
16+
/**
17+
* The promise resolves to `undefined` if no settings change we performed. Otherwise, the reconciled settings value.
18+
*/
19+
// TODO: find a better name for this method
1620
setSettings(
1721
monitorId: string,
1822
settings: PluggableMonitorSettings
19-
): Promise<PluggableMonitorSettings>;
23+
): Promise<PluggableMonitorSettings | undefined>;
2024
}

0 commit comments

Comments
 (0)