Skip to content

Commit

Permalink
fix: update monitor settings only if it's changed
Browse files Browse the repository at this point in the history
Closes #375

Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed Nov 23, 2022
1 parent bfcb518 commit de3794a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions arduino-ide-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"dateformat": "^3.0.3",
"deepmerge": "2.0.1",
"electron-updater": "^4.6.5",
"fast-json-stable-stringify": "^2.1.0",
"fast-safe-stringify": "^2.1.1",
"glob": "^7.1.6",
"google-protobuf": "^3.20.1",
Expand Down
17 changes: 16 additions & 1 deletion arduino-ide-extension/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stableJsonStringify = require('fast-json-stable-stringify');

export const naturalCompare: (left: string, right: string) => number =
require('string-natural-compare').caseInsensitive;

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

export function isNullOrUndefined(what: any): what is undefined | null {
export function isNullOrUndefined(what: unknown): what is undefined | null {
return what === undefined || what === null;
}

export function sameAs(left: unknown, right: unknown): boolean {
if (left === right) {
return true;
}
if (!left) {
return right === undefined;
}
if (!right) {
return false;
}
return stableJsonStringify(left) === stableJsonStringify(right);
}
10 changes: 7 additions & 3 deletions arduino-ide-extension/src/node/monitor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,10 @@ export class MonitorService extends CoreClientAware implements Disposable {
*/
async changeSettings(settings: MonitorSettings): Promise<Status> {
const config = new MonitorPortConfiguration();
const { pluggableMonitorSettings } = settings;
const { pluggableMonitorSettings = {} } = settings;
const reconciledSettings = await this.monitorSettingsProvider.setSettings(
this.monitorID,
pluggableMonitorSettings || {}
pluggableMonitorSettings
);

if (reconciledSettings) {
Expand All @@ -512,9 +512,13 @@ export class MonitorService extends CoreClientAware implements Disposable {
connected: !!this.duplex,
serialPort: this.port.address,
},
pluggableMonitorSettings: reconciledSettings,
pluggableMonitorSettings: reconciledSettings ?? pluggableMonitorSettings,
});

if (!reconciledSettings) {
return Status.OK;
}

if (!this.duplex) {
return Status.NOT_CONNECTED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
longestPrefixMatch,
reconcileSettings,
} from './monitor-settings-utils';
import { ILogger } from '@theia/core';
import { deepClone, ILogger } from '@theia/core';
import { sameAs } from '../../common/utils';

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

Expand Down Expand Up @@ -74,14 +75,17 @@ export class MonitorSettingsProviderImpl implements MonitorSettingsProvider {
async setSettings(
monitorId: string,
settings: PluggableMonitorSettings
): Promise<PluggableMonitorSettings> {
): Promise<PluggableMonitorSettings | undefined> {
// wait for the service to complete the init
await this.ready.promise;

const newSettings = this.reconcileSettings(
settings,
this.monitorSettings[monitorId] || {}
);
const defaultSettings = this.monitorSettings[monitorId] || {};
const oldSettings = deepClone(defaultSettings);
const newSettings = this.reconcileSettings(settings, defaultSettings);
if (sameAs(oldSettings, newSettings)) {
// NOOP if no settings change. The `undefined` return value represents this case.
return undefined;
}
this.monitorSettings[monitorId] = newSettings;

await this.writeSettingsToFS();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MonitorModel } from '../../browser/monitor-model';
import { PluggableMonitorSetting } from '../../common/protocol';
import type { MonitorModel } from '../../browser/monitor-model';
import type { PluggableMonitorSetting } from '../../common/protocol';

export type PluggableMonitorSettings = Record<string, PluggableMonitorSetting>;
export interface MonitorSettings {
Expand All @@ -13,8 +13,12 @@ export interface MonitorSettingsProvider {
monitorId: string,
defaultSettings: PluggableMonitorSettings
): Promise<PluggableMonitorSettings>;
/**
* The promise resolves to `undefined` if no settings change we performed. Otherwise, the reconciled settings value.
*/
// TODO: find a better name for this method
setSettings(
monitorId: string,
settings: PluggableMonitorSettings
): Promise<PluggableMonitorSettings>;
): Promise<PluggableMonitorSettings | undefined>;
}

0 comments on commit de3794a

Please sign in to comment.