Skip to content

Commit 57b273c

Browse files
authored
fix: missused promises (#2988)
1 parent aff05bf commit 57b273c

8 files changed

Lines changed: 233 additions & 195 deletions

File tree

eslint.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const globalIgnores = [
1111
];
1212

1313
export const typescriptRules = {
14-
'@typescript-eslint/no-misused-promises': 'off',
1514
'@typescript-eslint/no-redundant-type-constituents': 'off',
1615
'@typescript-eslint/no-unnecessary-type-assertion': 'off',
1716
'@typescript-eslint/no-unsafe-argument': 'off',

packages/kboot/src/usb-peripheral.ts

Lines changed: 123 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -55,145 +55,136 @@ export class UsbPeripheral implements Peripheral {
5555
}
5656

5757
async sendCommand(options: CommandOption): Promise<CommandResponse> {
58-
return new Promise<CommandResponse>(async (resolve, reject) => {
59-
try {
60-
await this.open();
61-
validateCommandParams(options.params);
62-
const data = encodeCommandOption(options);
63-
logger('send data %o', `<${convertToHexString(data)}>`);
64-
await this._device.write(data);
65-
const receivedData = await this._device.read(options.timeout || 2000);
66-
logger('received data %o', `<${convertToHexString(receivedData)}>`);
67-
const commandResponse = decodeCommandResponse(receivedData);
68-
logger('command response: %o', commandResponse);
69-
resolve(commandResponse);
70-
} catch (err) {
71-
logger('USB send command communication error %O', err);
58+
try {
59+
await this.open();
60+
validateCommandParams(options.params);
61+
const data = encodeCommandOption(options);
62+
logger('send data %o', `<${convertToHexString(data)}>`);
63+
await this._device.write(data);
64+
const receivedData = await this._device.read(options.timeout || 2000);
65+
logger('received data %o', `<${convertToHexString(receivedData)}>`);
66+
const commandResponse = decodeCommandResponse(receivedData);
67+
logger('command response: %o', commandResponse);
68+
return commandResponse;
69+
} catch (err) {
70+
logger('USB send command communication error %O', err);
71+
72+
throw err;
73+
}
74+
}
7275

73-
return reject(err);
76+
async writeMemory(option: DataOption): Promise<void> {
77+
try {
78+
const command: CommandOption = {
79+
command: Commands.WriteMemory,
80+
hasDataPhase: true,
81+
params: [
82+
...pack(option.startAddress, { bits: 32 }),
83+
...pack(option.data.length, { bits: 32 })
84+
]
85+
};
86+
87+
const firsCommandResponse = await this.sendCommand(command);
88+
if (firsCommandResponse.tag !== ResponseTags.Generic) {
89+
logger('Invalid write memory response! %o', firsCommandResponse);
90+
throw new Error('Invalid write memory response!');
91+
}
92+
93+
if (firsCommandResponse.code !== 0) {
94+
logger('Non zero write memory response! %o', firsCommandResponse);
95+
throw new Error(`Non zero write memory response! Response code: ${firsCommandResponse.code}`);
7496
}
75-
});
76-
}
7797

78-
writeMemory(option: DataOption): Promise<void> {
79-
return new Promise<void>(async (resolve, reject) => {
80-
try {
81-
const command: CommandOption = {
82-
command: Commands.WriteMemory,
83-
hasDataPhase: true,
84-
params: [
85-
...pack(option.startAddress, { bits: 32 }),
86-
...pack(option.data.length, { bits: 32 })
87-
]
88-
};
89-
90-
const firsCommandResponse = await this.sendCommand(command);
91-
if (firsCommandResponse.tag !== ResponseTags.Generic) {
92-
logger('Invalid write memory response! %o', firsCommandResponse);
93-
return reject(new Error('Invalid write memory response!'));
94-
}
95-
96-
if (firsCommandResponse.code !== 0) {
97-
logger('Non zero write memory response! %o', firsCommandResponse);
98-
return reject(new Error(`Non zero write memory response! Response code: ${firsCommandResponse.code}`));
99-
}
100-
101-
for (let i = 0; i < option.data.length; i = i + WRITE_DATA_STREAM_PACKAGE_LENGTH) {
102-
const slice = option.data.slice(i, i + WRITE_DATA_STREAM_PACKAGE_LENGTH);
103-
104-
const writeData = [
105-
2, // USB channel
106-
0,
107-
slice.length,
108-
0, // TODO: What is it?
109-
...slice
110-
];
111-
112-
logger('send data %o', convertToHexString(writeData));
113-
await this._device.write(writeData);
114-
// workaround to prevent main thread blocking
115-
await snooze(1);
116-
}
117-
118-
const receivedData = await this._device.read(option.timeout || 2000);
119-
logger('write memory received data %o', `<${convertToHexString(receivedData)}>`);
120-
const secondCommandResponse = decodeCommandResponse(receivedData);
121-
logger('write memory response: %o', secondCommandResponse);
122-
123-
if (secondCommandResponse.tag !== ResponseTags.Generic) {
124-
logger('Invalid write memory final response %o', secondCommandResponse);
125-
return reject(new Error('Invalid write memory final response!'));
126-
}
127-
128-
if (secondCommandResponse.code !== 0) {
129-
logger('Non zero write memory final response %o', secondCommandResponse);
130-
const msg = `Non zero write memory final response! Response code: ${secondCommandResponse.code}`;
131-
return reject(new Error(msg));
132-
}
133-
134-
resolve();
135-
} catch (err) {
136-
logger('Can not write memory data %O', err);
137-
reject(err);
98+
for (let i = 0; i < option.data.length; i = i + WRITE_DATA_STREAM_PACKAGE_LENGTH) {
99+
const slice = option.data.slice(i, i + WRITE_DATA_STREAM_PACKAGE_LENGTH);
100+
101+
const writeData = [
102+
2, // USB channel
103+
0,
104+
slice.length,
105+
0, // TODO: What is it?
106+
...slice
107+
];
108+
109+
logger('send data %o', convertToHexString(writeData));
110+
await this._device.write(writeData);
111+
// workaround to prevent main thread blocking
112+
await snooze(1);
138113
}
139114

140-
});
115+
const receivedData = await this._device.read(option.timeout || 2000);
116+
logger('write memory received data %o', `<${convertToHexString(receivedData)}>`);
117+
const secondCommandResponse = decodeCommandResponse(receivedData);
118+
logger('write memory response: %o', secondCommandResponse);
119+
120+
if (secondCommandResponse.tag !== ResponseTags.Generic) {
121+
logger('Invalid write memory final response %o', secondCommandResponse);
122+
throw new Error('Invalid write memory final response!');
123+
}
124+
125+
if (secondCommandResponse.code !== 0) {
126+
logger('Non zero write memory final response %o', secondCommandResponse);
127+
const msg = `Non zero write memory final response! Response code: ${secondCommandResponse.code}`;
128+
throw new Error(msg);
129+
}
130+
} catch (err) {
131+
logger('Can not write memory data %O', err);
132+
throw err;
133+
}
141134
}
142135

143-
readMemory(startAddress: number, count: number): Promise<Buffer> {
144-
return new Promise<Buffer>(async (resolve, reject) => {
145-
try {
146-
const command: CommandOption = {
147-
command: Commands.ReadMemory,
148-
params: [
149-
...pack(startAddress, { bits: 32 }),
150-
...pack(count, { bits: 32 })
151-
]
152-
};
153-
154-
const firsCommandResponse = await this.sendCommand(command);
155-
if (firsCommandResponse.tag !== ResponseTags.ReadMemory) {
156-
logger('Invalid read memory response %o', firsCommandResponse);
157-
return reject(new Error('Invalid read memory response!'));
158-
}
159-
160-
if (firsCommandResponse.code !== 0) {
161-
logger('Non zero read memory response %o', firsCommandResponse);
162-
return reject(new Error(`Non zero read memory response! Response code: ${firsCommandResponse.code}`));
163-
}
164-
165-
const byte4Number = firsCommandResponse.raw.slice(12, 15);
166-
const arrivingDataSize = convertLittleEndianNumber(byte4Number);
167-
const memoryData: Array<number> = [];
168-
while (memoryData.length < arrivingDataSize) {
169-
const receivedData = await this._device.read(2000);
170-
logger('received data %o', `<${convertToHexString(receivedData)}>`);
171-
memoryData.push(...receivedData);
172-
// workaround to prevent main thread blocking
173-
await snooze(1);
174-
}
175-
176-
const responseData = await this._device.read(2000);
177-
logger('received data %o', `<${convertToHexString(responseData)}>`);
178-
179-
const secondCommandResponse = decodeCommandResponse(responseData);
180-
if (secondCommandResponse.tag !== ResponseTags.Generic) {
181-
logger('Invalid read memory final response %o', secondCommandResponse);
182-
return reject(new Error('Invalid read memory final response!'));
183-
}
184-
185-
if (secondCommandResponse.code !== 0) {
186-
logger('Non zero read memory final response %o', secondCommandResponse);
187-
const msg = `Non zero read memory final response! Response code: ${secondCommandResponse.code}`;
188-
return reject(new Error(msg));
189-
}
190-
191-
resolve(Buffer.from(memoryData));
192-
} catch (error) {
193-
logger('Read memory error %O', error);
194-
195-
reject(error);
136+
async readMemory(startAddress: number, count: number): Promise<Buffer> {
137+
try {
138+
const command: CommandOption = {
139+
command: Commands.ReadMemory,
140+
params: [
141+
...pack(startAddress, { bits: 32 }),
142+
...pack(count, { bits: 32 })
143+
]
144+
};
145+
146+
const firsCommandResponse = await this.sendCommand(command);
147+
if (firsCommandResponse.tag !== ResponseTags.ReadMemory) {
148+
logger('Invalid read memory response %o', firsCommandResponse);
149+
throw new Error('Invalid read memory response!');
150+
}
151+
152+
if (firsCommandResponse.code !== 0) {
153+
logger('Non zero read memory response %o', firsCommandResponse);
154+
throw new Error(`Non zero read memory response! Response code: ${firsCommandResponse.code}`);
155+
}
156+
157+
const byte4Number = firsCommandResponse.raw.slice(12, 15);
158+
const arrivingDataSize = convertLittleEndianNumber(byte4Number);
159+
const memoryData: Array<number> = [];
160+
while (memoryData.length < arrivingDataSize) {
161+
const receivedData = await this._device.read(2000);
162+
logger('received data %o', `<${convertToHexString(receivedData)}>`);
163+
memoryData.push(...receivedData);
164+
// workaround to prevent main thread blocking
165+
await snooze(1);
166+
}
167+
168+
const responseData = await this._device.read(2000);
169+
logger('received data %o', `<${convertToHexString(responseData)}>`);
170+
171+
const secondCommandResponse = decodeCommandResponse(responseData);
172+
if (secondCommandResponse.tag !== ResponseTags.Generic) {
173+
logger('Invalid read memory final response %o', secondCommandResponse);
174+
throw new Error('Invalid read memory final response!');
175+
}
176+
177+
if (secondCommandResponse.code !== 0) {
178+
logger('Non zero read memory final response %o', secondCommandResponse);
179+
const msg = `Non zero read memory final response! Response code: ${secondCommandResponse.code}`;
180+
throw new Error(msg);
196181
}
197-
});
182+
183+
return Buffer.from(memoryData);
184+
} catch (error) {
185+
logger('Read memory error %O', error);
186+
187+
throw error;
188+
}
198189
}
199190
}

packages/uhk-agent/src/electron-main.ts

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,11 @@ async function createWindow() {
135135
});
136136

137137
// Emitted when the window is closed.
138-
win.on('closed', async () => {
139-
// Dereference the window object, usually you would store windows
140-
// in an array if your app supports multi windows, this is the time
141-
// when you should delete the corresponding element.
142-
logger.misc('[Electron Main] win closed');
143-
win = null;
144-
try {
145-
await deviceService.close();
146-
} catch (error) {
147-
// TODO: Investigate it deeper. It happens on MacOs 15+ sometimes
148-
logger.error('[Electron Main] Error while closing DeviceService when electron has been closed', error);
149-
}
150-
deviceService = null;
151-
appUpdateService = null;
152-
appService = null;
153-
await uhkHidDeviceService.close();
154-
uhkHidDeviceService = null;
155-
sudoService = null;
156-
await smartMacroDocService.stop();
157-
smartMacroDocService = null;
138+
win.on('closed', () => {
139+
windowClosed()
140+
.catch((error) => {
141+
logger.error('[Electron Main] Error while closing window', error);
142+
})
158143
});
159144

160145
win.once('ready-to-show', () => {
@@ -200,6 +185,28 @@ async function createWindow() {
200185
win.on('close', () => saveWindowState(win, logger));
201186
}
202187

188+
async function windowClosed() {
189+
// Dereference the window object, usually you would store windows
190+
// in an array if your app supports multi windows, this is the time
191+
// when you should delete the corresponding element.
192+
logger.misc('[Electron Main] win closed');
193+
win = null;
194+
try {
195+
await deviceService.close();
196+
} catch (error) {
197+
// TODO: Investigate it deeper. It happens on MacOs 15+ sometimes
198+
logger.error('[Electron Main] Error while closing DeviceService when electron has been closed', error);
199+
}
200+
deviceService = null;
201+
appUpdateService = null;
202+
appService = null;
203+
await uhkHidDeviceService.close();
204+
uhkHidDeviceService = null;
205+
sudoService = null;
206+
await smartMacroDocService.stop();
207+
smartMacroDocService = null;
208+
}
209+
203210
if (isSecondInstance) {
204211
app.quit();
205212
} else if (options['capture-oled']) {
@@ -254,7 +261,12 @@ if (isSecondInstance) {
254261
// This method will be called when Electron has finished
255262
// initialization and is ready to create browser windows.
256263
// Some APIs can only be used after this event occurs.
257-
app.on('ready', createWindow);
264+
app.on('ready', () => {
265+
createWindow()
266+
.catch((error) => {
267+
logger.error('[Electron Main] when creating the window: ', error);
268+
});
269+
});
258270

259271
// Quit when all windows are closed.
260272
app.on('window-all-closed', () => {
@@ -264,11 +276,14 @@ if (isSecondInstance) {
264276
app.on('will-quit', () => {
265277
});
266278

267-
app.on('activate', async () => {
279+
app.on('activate', () => {
268280
// On macOS it's common to re-create a window in the app when the
269281
// dock icon is clicked and there are no other windows open.
270282
if (win === null) {
271-
await createWindow();
283+
createWindow()
284+
.catch((error) => {
285+
logger.error('[Electron Main] when activating the app: ', error);
286+
});
272287
}
273288
});
274289

0 commit comments

Comments
 (0)