Skip to content

Commit

Permalink
refactor: remove unnecessary promises (#2496)
Browse files Browse the repository at this point in the history
  • Loading branch information
ert78gb authored Jan 18, 2025
1 parent 8031f0e commit c1d6b48
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
18 changes: 8 additions & 10 deletions packages/uhk-agent/src/services/device.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,18 +851,16 @@ export class DeviceService {

public async stopPollUhkDevice(): Promise<void> {
this.logService.misc('[DeviceService] stop poll UHK Device');
return new Promise<void>(async resolve => {
this._pollerAllowed = false;
this._pollerAllowed = false;

while (true) {
if (!this._uhkDevicePolling) {
this.logService.misc('[DeviceService] stopped poll UHK Device');
return resolve();
}

await snooze(100);
while (true) {
if (!this._uhkDevicePolling) {
this.logService.misc('[DeviceService] stopped poll UHK Device');
return;
}
});

await snooze(100);
}
}

private async changeKeyboardLayout(event: Electron.IpcMainEvent, args): Promise<void> {
Expand Down
51 changes: 24 additions & 27 deletions packages/uhk-usb/src/uhk-hid-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,36 +437,33 @@ export class UhkHidDevice {
* @returns {Promise<Buffer>}
*/
public async write(buffer: Buffer): Promise<Buffer> {
return new Promise<Buffer>(async (resolve, reject) => {
try {
const device = await this.getDevice();
const reportId = this.getReportId();

this.logService.setUsbReportId(reportId);
const sendData = this.getTransferData(buffer, reportId);
this.logService.usb('[UhkHidDevice] USB[W]:', bufferToString(sendData));
device.write(sendData);
await snooze(1);
const receivedData = device.readTimeout(1000);
const logString = bufferToString(receivedData);
this.logService.usb('[UhkHidDevice] USB[R]:', logString);

if (reportId) {
receivedData.shift();
}

if (receivedData[0] !== 0) {
return reject(new Error(`Communications error with UHK. Response code: ${receivedData[0]}`));
}
try {
const device = await this.getDevice();
const reportId = this.getReportId();

this.logService.setUsbReportId(reportId);
const sendData = this.getTransferData(buffer, reportId);
this.logService.usb('[UhkHidDevice] USB[W]:', bufferToString(sendData));
device.write(sendData);
await snooze(1);
const receivedData = device.readTimeout(1000);
const logString = bufferToString(receivedData);
this.logService.usb('[UhkHidDevice] USB[R]:', logString);

if (reportId) {
receivedData.shift();
}

return resolve(Buffer.from(receivedData));
} catch (err) {
this.logService.error('[UhkHidDevice] Transfer error: ', err);
this.close();
return reject(err);
if (receivedData[0] !== 0) {
throw new Error(`Communications error with UHK. Response code: ${receivedData[0]}`);
}

});
return Buffer.from(receivedData);
} catch (err) {
this.logService.error('[UhkHidDevice] Transfer error: ', err);
this.close();
throw err;
}
}

/**
Expand Down

0 comments on commit c1d6b48

Please sign in to comment.