Skip to content

Commit

Permalink
Move adding persisted data to the enumeration stage before any proces…
Browse files Browse the repository at this point in the history
…sing is done
  • Loading branch information
kylebonnici committed Oct 4, 2024
1 parent bce2590 commit 6ea983b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
44 changes: 43 additions & 1 deletion src/Device/deviceLister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import logger from '../logging';
import type { AppThunk, RootState } from '../store';
import simplifyDevice from '../telemetry/simplifyDevice';
import telemetry from '../telemetry/telemetry';
import {
getPersistedIsFavorite,
getPersistedNickname,
getPersistedSerialPortSettings,
} from '../utils/persistentStore';
import {
clearWaitForDevice,
clearWaitForDeviceTimeout,
Expand Down Expand Up @@ -161,6 +166,36 @@ const removeDeviceFromList =
* library for available traits. Whenever devices are attached/detached, this
* will dispatch AddDevice or removeDevice and trigger events.
*/

const setPersistedData = (device: Device) => {
if (device.serialNumber) {
const newDevice = { ...device };
newDevice.favorite = getPersistedIsFavorite(device.serialNumber);
newDevice.nickname = getPersistedNickname(device.serialNumber);

const persistedSerialPortSettings = getPersistedSerialPortSettings(
device.serialNumber
);

if (persistedSerialPortSettings) {
const path =
newDevice.serialPorts?.[persistedSerialPortSettings.vComIndex]
?.comName;

if (path) {
newDevice.persistedSerialPortOptions = {
...persistedSerialPortSettings.serialPortOptions,
path,
};
}
}

return newDevice;
}

return device;
};

export const startWatchingDevices =
(
deviceListing: DeviceTraits,
Expand Down Expand Up @@ -404,13 +439,20 @@ export const startWatchingDevices =
const operation = await NrfutilDeviceLib.list(
deviceListing,
d => {
d = d.map(setPersistedData);
d.forEach(onDeviceArrived);
autoSelectDeviceCLI(d, doSelectDevice);
},
error => {
logger.error(error);
},
{ onDeviceArrived, onDeviceLeft }
{
onDeviceArrived: d => {
d = setPersistedData(d);
onDeviceArrived(d);
},
onDeviceLeft,
}
);

stopNrfutilDevice = (callback?: () => void) => {
Expand Down
38 changes: 2 additions & 36 deletions src/Device/deviceSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import { DeviceInfo } from '../../nrfutil/device';
import { NrfutilDevice } from '../../nrfutil/device/common';
import type { RootState } from '../store';
import {
getPersistedIsFavorite,
getPersistedNickname,
getPersistedSerialPortSettings,
persistIsFavorite,
persistNickname,
persistSerialPortSettings as persistSerialPortSettingsToStore,
Expand Down Expand Up @@ -69,35 +66,6 @@ const initialState: DeviceState = {
devices: [],
};

const setPersistedData = (device: Device) => {
if (device.serialNumber) {
const newDevice = { ...device };
newDevice.favorite = getPersistedIsFavorite(device.serialNumber);
newDevice.nickname = getPersistedNickname(device.serialNumber);

const persistedSerialPortSettings = getPersistedSerialPortSettings(
device.serialNumber
);

if (persistedSerialPortSettings) {
const path =
newDevice.serialPorts?.[persistedSerialPortSettings.vComIndex]
?.comName;

if (path) {
newDevice.persistedSerialPortOptions = {
...persistedSerialPortSettings.serialPortOptions,
path,
};
}
}

return newDevice;
}

return device;
};

const slice = createSlice({
name: 'device',
initialState,
Expand Down Expand Up @@ -130,12 +98,10 @@ const slice = createSlice({
item.serialNumber === action.payload.serialNumber ||
item.id === action.payload.id
);

const device = setPersistedData(action.payload);
if (index !== -1) {
state.devices[index] = device;
state.devices[index] = action.payload;
} else {
state.devices.push(device);
state.devices.push(action.payload);
}
},

Expand Down

0 comments on commit 6ea983b

Please sign in to comment.