Skip to content

Commit

Permalink
v0.3.11: fix disconnect error
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed Jul 2, 2024
1 parent 702d4d8 commit 14b502d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"module": "./dist/index.mjs",
"name": "chameleon-ultra.js",
"type": "commonjs",
"version": "0.3.10",
"version": "0.3.11",
"bugs": {
"url": "https://github.com/taichunmin/chameleon-ultra.js/issues"
},
Expand Down
30 changes: 15 additions & 15 deletions src/ChameleonUltra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function validateMf1BlockKey (block: any, keyType: any, key: any, prefix: string
* </details>
*/
export class ChameleonUltra {
readonly #catchErr: (err: Error) => void
readonly #emitErr: (err: Error) => void
#deviceMode: DeviceMode | null = null
#isDisconnecting: boolean = false
#rxSink?: UltraRxSink | DfuRxSink
Expand Down Expand Up @@ -158,7 +158,7 @@ export class ChameleonUltra {

constructor () {
this.#WritableStream = (globalThis as any)?.WritableStream ?? WritableStream
this.#catchErr = (err: Error): void => { this.emitter.emit('error', _.set(new Error(err.message), 'originalError', err)) }
this.#emitErr = (err: Error): void => { this.emitter.emit('error', _.set(new Error(err.message), 'originalError', err)) }
}

#debug (namespace: string, formatter: any, ...args: [] | any[]): void {
Expand Down Expand Up @@ -214,8 +214,8 @@ export class ChameleonUltra {
* @group Connection Related
*/
async connect (): Promise<void> {
await this.invokeHook('connect', {}, async (ctx, next) => {
try {
try {
await this.invokeHook('connect', {}, async (ctx, next) => {
if (_.isNil(this.port)) throw new Error('this.port is undefined. Did you remember to use adapter plugin?')

// serial.readable pipeTo this.rxSink
Expand All @@ -226,13 +226,13 @@ export class ChameleonUltra {

const connectedAt = await promiseConnected
this.#debug('core', `connected at ${connectedAt.toISOString()}`)
} catch (err) {
err.message = `Failed to connect: ${err.message}`
this.emitter.emit('error', err)
if (this.isConnected()) await this.disconnect(err)
throw err
}
})
})
} catch (err) {
const err1 = _.set(new Error(`Failed to connect: ${err.message}`), 'originalError', err)
this.#emitErr(err1)
await this.disconnect(err1)
throw err1
}
}

/**
Expand All @@ -251,14 +251,14 @@ export class ChameleonUltra {
this.#deviceMode = null
this.#supportedCmds.clear()

const promiseDisconnected = new Promise<[Date, string | undefined]>(resolve => {
const promiseDisconnected: Promise<[Date, string | undefined]> = this.isConnected() ? new Promise(resolve => {
this.emitter.once('disconnected', (disconnected: Date, reason?: string) => { resolve([disconnected, reason]) })
})
}) : Promise.resolve([new Date(), err.message])
const isLocked = (): boolean => this.port?.readable?.locked ?? false
if (isLocked()) this.#rxSink?.abortController.abort(err)
while (isLocked()) await sleep(10)
await this.port?.readable?.cancel(err).catch(this.#catchErr)
await this.port?.writable?.close().catch(this.#catchErr)
await this.port?.readable?.cancel(err).catch(this.#emitErr)
await this.port?.writable?.close().catch(this.#emitErr)
delete this.port

const [disconnectedAt, reason] = await promiseDisconnected
Expand Down
17 changes: 8 additions & 9 deletions src/plugin/WebbleAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export default class WebbleAdapter implements ChameleonPlugin {
#isOpen: boolean = false
bluetooth?: typeof bluetooth
Buffer?: typeof Buffer
catchErr: (err: Error) => void
ctrlChar?: BluetoothRemoteGATTCharacteristic
device?: BluetoothDevice
emitErr: (err: Error) => void
name = 'adapter'
packtChar?: BluetoothRemoteGATTCharacteristic
port?: ChameleonSerialPort<Buffer, Buffer>
TransformStream: typeof TransformStream
rxChar?: BluetoothRemoteGATTCharacteristic
TransformStream: typeof TransformStream
ultra?: ChameleonUltra
WritableStream: typeof WritableStream

Expand All @@ -38,7 +38,7 @@ export default class WebbleAdapter implements ChameleonPlugin {
this.bluetooth = navigator?.bluetooth
this.WritableStream = (globalThis as any)?.WritableStream ?? WritableStream
this.TransformStream = (globalThis as any)?.TransformStream ?? TransformStream
this.catchErr = (err: Error): void => { this.ultra?.emitter.emit('error', _.set(new Error(err.message), 'originalError', err)) }
this.emitErr = (err: Error): void => { this.ultra?.emitter.emit('error', _.set(new Error(err.message), 'originalError', err)) }
}

#debug (formatter: any, ...args: [] | any[]): void {
Expand Down Expand Up @@ -66,14 +66,14 @@ export default class WebbleAdapter implements ChameleonPlugin {
this.device = await this.bluetooth?.requestDevice({
filters: BLE_SCAN_FILTERS,
optionalServices: [DFU_SERV_UUID, ULTRA_SERV_UUID],
})
}).catch(err => { throw _.set(new Error(err.message), 'originalError', err) })
if (_.isNil(this.device)) throw new Error('no device')
this.device.addEventListener('gattserverdisconnected', () => { void ultra.disconnect(new Error('Webble gattserverdisconnected')) })
this.#debug(`device selected, name = ${this.device.name ?? 'null'}, id = ${this.device.id}`)

for (let i = 0; i < 100; i++) {
if (gattIsConnected()) break
await this.device.gatt?.connect().catch(this.catchErr)
await this.device.gatt?.connect().catch(this.emitErr)
await sleep(100)
}
if (!gattIsConnected()) throw new Error('Failed to connect gatt')
Expand Down Expand Up @@ -134,8 +134,7 @@ export default class WebbleAdapter implements ChameleonPlugin {
ultra.port = this.port
return await next()
} catch (err) {
this.catchErr(err)
await ultra.disconnect(err)
this.emitErr(err)
throw err
}
})
Expand Down Expand Up @@ -195,7 +194,7 @@ class UltraRxSink implements UnderlyingSink<Buffer> {
await this.#adapter.rxChar.writeValueWithoutResponse(buf2.buffer)
}
} catch (err) {
this.#adapter.catchErr(err)
this.#adapter.emitErr(err)
throw err
}
}
Expand Down Expand Up @@ -231,7 +230,7 @@ class DfuRxSink implements UnderlyingSink<Buffer> {
await this.#adapter.ctrlChar.writeValueWithResponse(chunk.buffer)
}
} catch (err) {
this.#adapter.catchErr(err)
this.#adapter.emitErr(err)
throw err
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/plugin/WebserialAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class WebserialAdapter implements ChameleonPlugin {
#isOpen: boolean = false
name = 'adapter'
port?: SerialPort1
readonly #catchErr: (err: Error) => void
readonly #emitErr: (err: Error) => void
readonly #serial: typeof serial
readonly #TransformStream: typeof TransformStream
readonly #WritableStream: typeof WritableStream
Expand All @@ -32,7 +32,7 @@ export default class WebserialAdapter implements ChameleonPlugin {
this.#TransformStream = (globalThis as any)?.TransformStream ?? TransformStream
this.#WritableStream = (globalThis as any)?.WritableStream ?? WritableStream
this.#serial = navigator.serial ?? ('usb' in navigator ? serial : null)
this.#catchErr = (err: Error): void => { this.ultra?.emitter.emit('error', _.set(new Error(err.message), 'originalError', err)) }
this.#emitErr = (err: Error): void => { this.ultra?.emitter.emit('error', _.set(new Error(err.message), 'originalError', err)) }
}

#debug (formatter: any, ...args: [] | any[]): void {
Expand Down Expand Up @@ -89,16 +89,16 @@ export default class WebserialAdapter implements ChameleonPlugin {
}
return await next()
} catch (err) {
this.#debug(err)
this.#emitErr(err)
throw err
}
})

ultra.addHook('disconnect', async (ctx: any, next: () => Promise<unknown>) => {
if (ultra.$adapter !== adapter || _.isNil(this.port)) return await next() // 代表已經被其他 adapter 接管

await next().catch(this.#catchErr)
await this.port.close().catch(this.#catchErr)
await next().catch(this.#emitErr)
await this.port.close().catch(this.#emitErr)
this.#isOpen = false
this.#isDfu = false
delete this.port
Expand Down

0 comments on commit 14b502d

Please sign in to comment.