Skip to content

Commit

Permalink
style: 🚨 fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
meisZWFLZ committed Dec 17, 2023
1 parent 9322205 commit bf56048
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/Vex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ export enum SerialDeviceType {

CORTEX_PROGCBL = 10,
CORTEX_BRAIN = 11,
// Jerry says this was copied from the official source code ¯\_(ツ)_/¯
// eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
CORTEX_JOYSTK = 12,

VEXNET_KEY_20 = 7,
Expand Down
2 changes: 1 addition & 1 deletion src/VexConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ export class V5SerialConnection extends VexSerialConnection {
let lastBlock = false;

while (!lastBlock) {
var tmpbuf;
let tmpbuf;
if (buf.byteLength - bufferOffset > bufferChunkSize) {
tmpbuf = buf.subarray(bufferOffset, bufferOffset + bufferChunkSize);
} else {
Expand Down
44 changes: 29 additions & 15 deletions src/VexDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function sleepUntilAsync(
const stopper = setTimeout(() => {
stopped = true;
resolve(false);
});
}, timeout);
const checker = (val: boolean): void => {
if (stopped) return;

Expand Down Expand Up @@ -182,7 +182,11 @@ class V5SerialDeviceProxyHandler implements ProxyHandler<V5SerialDeviceState> {
}
}

set<K extends keyof V5SerialDeviceState>(target: V5SerialDeviceState, key: K, value: V5SerialDeviceState[K] ): boolean {
set<K extends keyof V5SerialDeviceState>(
target: V5SerialDeviceState,
key: K,
value: V5SerialDeviceState[K],
): boolean {
const oldValue = target[key];
if (oldValue === value) {
return true;
Expand Down Expand Up @@ -246,7 +250,7 @@ class V5SerialDeviceState {
},
];

devices: ISmartDeviceInfo[] = [];
devices: Array<ISmartDeviceInfo | undefined> = [];
isFieldControllerConnected = false;
matchMode: MatchMode = "disabled";
radio = {
Expand Down Expand Up @@ -833,22 +837,24 @@ export class V5SmartDevice {
this.deviceIndex = index;
}

protected getDeviceInfo(): ISmartDeviceInfo | undefined {
return this.state.devices[this.deviceIndex];
}

get isAvailable(): boolean {
return this.state.devices[this.deviceIndex] !== undefined;
return this.getDeviceInfo() !== undefined;
}

get port(): number {
return this.deviceIndex;
}

get type(): SmartDeviceType {
return this.isAvailable
? this.state.devices[this.deviceIndex].type
: SmartDeviceType.EMPTY;
return this.getDeviceInfo()?.type ?? SmartDeviceType.EMPTY;
}

get version(): number {
return this.isAvailable ? this.state.devices[this.deviceIndex].version : 0;
return this.getDeviceInfo()?.version ?? 0;
}
}

Expand Down Expand Up @@ -1010,28 +1016,34 @@ export class V5SerialDevice extends VexSerialDevice {
this.connection = undefined;
}

/**
* @param timeout defaults to 0. If timeout is 0, then it will attempt to reconnect forever
* @returns
*/
async reconnect(timeout: number = 0): Promise<boolean> {
if (this.isConnected) return true;
if (timeout < 0) return false;

const endTime = new Date().getTime() + timeout;

if (this._isReconnecting) {
let successB4Timeout;
let successBeforeTimeout;
do {
successB4Timeout = await sleepUntil(
successBeforeTimeout = await sleepUntil(
() => !this._isReconnecting,
timeout === 0 ? 1000 : timeout,
);
} while (timeout === 0 && !successB4Timeout);
// eslint-disable-next-line no-unmodified-loop-condition
} while (timeout === 0 && !successBeforeTimeout);

if (this.isConnected) return true;
if (!successB4Timeout) return false;
if (!successBeforeTimeout) return false;
}

this._isReconnecting = true;

while (timeout === 0 || new Date().getTime() > endTime) {
// eslint-disable-next-line no-unmodified-loop-condition
while (timeout === 0 || new Date().getTime() < endTime) {
// console.log("try to reconnect");

let tryIdx = 0;
Expand Down Expand Up @@ -1165,7 +1177,7 @@ export class V5SerialDevice extends VexSerialDevice {

let missingPorts = this.state.devices
.map((d) => d?.port)
.filter((p) => p !== undefined);
.filter((p): p is number => p !== undefined);

for (let i = 0; i < dsPacket.devices.length; i++) {
const device = dsPacket.devices[i];
Expand All @@ -1175,7 +1187,9 @@ export class V5SerialDevice extends VexSerialDevice {
missingPorts = missingPorts.filter((p) => p !== device.port);
}

missingPorts.forEach((port) => delete this.state.devices[port]);
missingPorts.forEach((port) => {
this.state.devices[port] = undefined;
});

return true;
}
Expand Down

0 comments on commit bf56048

Please sign in to comment.