Skip to content

Commit 3fda9fe

Browse files
committed
Coderabbit refactoring
1 parent 8f425c3 commit 3fda9fe

File tree

1 file changed

+45
-53
lines changed

1 file changed

+45
-53
lines changed

src/js/protocols/TauriSerial.js

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -90,38 +90,56 @@ class TauriSerial extends EventTarget {
9090
console.log(`${logHead} Device monitoring stopped`);
9191
}
9292

93+
/**
94+
* Convert the raw portsMap from the plugin into our standardized port objects.
95+
* @private
96+
*/
97+
_convertPortsMapToArray(portsMap) {
98+
return Object.entries(portsMap).map(([path, info]) => {
99+
let vendorId = undefined;
100+
let productId = undefined;
101+
102+
if (info.vid) {
103+
vendorId = typeof info.vid === "number" ? info.vid : Number.parseInt(info.vid, 10);
104+
}
105+
if (info.pid) {
106+
productId = typeof info.pid === "number" ? info.pid : Number.parseInt(info.pid, 10);
107+
}
108+
109+
return {
110+
path,
111+
displayName: this.getDisplayName(path, vendorId, productId),
112+
vendorId,
113+
productId,
114+
serialNumber: info.serial_number,
115+
};
116+
});
117+
}
118+
119+
/**
120+
* Filter ports to only include known Betaflight-compatible devices.
121+
* @private
122+
*/
123+
_filterToKnownDevices(ports) {
124+
return ports.filter((port) => {
125+
// Only include ports with known vendor IDs (Betaflight-compatible devices)
126+
if (!port.vendorId || !port.productId) {
127+
return false;
128+
}
129+
// Check if this device is in our known devices list
130+
return serialDevices.some((d) => d.vendorId === port.vendorId && d.productId === port.productId);
131+
});
132+
}
133+
93134
async checkDeviceChanges() {
94135
try {
95136
const portsMap = await invoke("plugin:serialplugin|available_ports");
96137

97138
// Convert to our format
98-
const allPorts = Object.entries(portsMap).map(([path, info]) => {
99-
let vendorId = undefined;
100-
let productId = undefined;
101-
102-
if (info.vid) {
103-
vendorId = typeof info.vid === "number" ? info.vid : Number.parseInt(info.vid, 10);
104-
}
105-
if (info.pid) {
106-
productId = typeof info.pid === "number" ? info.pid : Number.parseInt(info.pid, 10);
107-
}
108-
109-
return {
110-
path,
111-
displayName: this.getDisplayName(path, vendorId, productId),
112-
vendorId,
113-
productId,
114-
serialNumber: info.serial_number,
115-
};
116-
});
139+
const allPorts = this._convertPortsMapToArray(portsMap);
117140

118141
// Filter to only known devices
119-
const currentPorts = allPorts.filter((port) => {
120-
if (!port.vendorId || !port.productId) {
121-
return false;
122-
}
123-
return serialDevices.some((d) => d.vendorId === port.vendorId && d.productId === port.productId);
124-
});
142+
const currentPorts = this._filterToKnownDevices(allPorts);
125143

126144
// Check for removed devices
127145
const removedPorts = this.ports.filter(
@@ -157,36 +175,10 @@ class TauriSerial extends EventTarget {
157175
const portsMap = await invoke("plugin:serialplugin|available_ports");
158176

159177
// Convert the object map to array
160-
const allPorts = Object.entries(portsMap).map(([path, info]) => {
161-
// The plugin returns vid/pid as decimal strings like "1155", "22336"
162-
let vendorId = undefined;
163-
let productId = undefined;
164-
165-
if (info.vid) {
166-
vendorId = typeof info.vid === "number" ? info.vid : Number.parseInt(info.vid, 10);
167-
}
168-
if (info.pid) {
169-
productId = typeof info.pid === "number" ? info.pid : Number.parseInt(info.pid, 10);
170-
}
171-
172-
return {
173-
path,
174-
displayName: this.getDisplayName(path, vendorId, productId),
175-
vendorId,
176-
productId,
177-
serialNumber: info.serial_number,
178-
};
179-
});
178+
const allPorts = this._convertPortsMapToArray(portsMap);
180179

181180
// Filter to only known devices
182-
this.ports = allPorts.filter((port) => {
183-
// Only include ports with known vendor IDs (Betaflight-compatible devices)
184-
if (!port.vendorId || !port.productId) {
185-
return false;
186-
}
187-
// Check if this device is in our known devices list
188-
return serialDevices.some((d) => d.vendorId === port.vendorId && d.productId === port.productId);
189-
});
181+
this.ports = this._filterToKnownDevices(allPorts);
190182

191183
console.log(`${logHead} Found ${this.ports.length} serial ports (filtered from ${allPorts.length})`);
192184
return this.ports;

0 commit comments

Comments
 (0)