mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-01-20 00:43:04 +00:00
feat: debounce connection suspension
This commit is contained in:
@@ -16,7 +16,7 @@ const KEY_COUNTS = {
|
||||
ONE: 90,
|
||||
LITE: 67,
|
||||
X: 256,
|
||||
}
|
||||
} as const
|
||||
|
||||
if (browser && navigator.serial === undefined && import.meta.env.TAURI_FAMILY !== undefined) {
|
||||
await import("./tauri-serial")
|
||||
@@ -51,12 +51,19 @@ export class CharaDevice {
|
||||
|
||||
private lock?: Promise<true>
|
||||
|
||||
private readonly suspendDebounce = 100
|
||||
private suspendDebounceId?: number
|
||||
|
||||
version!: SemVer
|
||||
company!: "CHARACHORDER"
|
||||
device!: "ONE" | "LITE" | "X"
|
||||
chipset!: "M0" | "S2"
|
||||
keyCount!: 90 | 67 | 256
|
||||
|
||||
get portInfo() {
|
||||
return this.port.getInfo()
|
||||
}
|
||||
|
||||
constructor(private readonly baudRate = 115200) {}
|
||||
|
||||
async init(manual = false) {
|
||||
@@ -165,10 +172,18 @@ export class CharaDevice {
|
||||
const exec = new Promise<T>(async resolve => {
|
||||
let result!: T
|
||||
try {
|
||||
await this.wake()
|
||||
if (this.suspendDebounceId) {
|
||||
clearTimeout(this.suspendDebounceId)
|
||||
} else {
|
||||
await this.wake()
|
||||
}
|
||||
result = await callback(send, read)
|
||||
} finally {
|
||||
await this.suspend()
|
||||
this.suspendDebounceId = setTimeout(() => {
|
||||
// cannot be locked here as all the code until clearTimeout is sync
|
||||
console.assert(this.lock === undefined)
|
||||
this.lock = this.suspend().then(() => true)
|
||||
}, this.suspendDebounce) as any
|
||||
this.lock = undefined
|
||||
resolve(result)
|
||||
}
|
||||
|
||||
12
src/lib/serial/updater.ts
Normal file
12
src/lib/serial/updater.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export async function updateDevice(port: SerialPort) {
|
||||
await port.open({
|
||||
baudRate: 115200,
|
||||
dataBits: 8,
|
||||
stopBits: 1,
|
||||
parity: "none",
|
||||
bufferSize: 255,;
|
||||
})
|
||||
|
||||
const writer = port.writable!.getWriter()
|
||||
const reader = port.readable!.getReader()
|
||||
}
|
||||
Reference in New Issue
Block a user