Skip to content

Commit

Permalink
v0.3.7: fix #115 (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin authored Jun 7, 2024
2 parents 78ac134 + ab01745 commit 3300110
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 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.6",
"version": "0.3.7",
"bugs": {
"url": "https://github.com/taichunmin/chameleon-ultra.js/issues"
},
Expand Down
18 changes: 7 additions & 11 deletions src/ChameleonUltra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,13 @@ export class ChameleonUltra {
*/
async _writeBuffer (buf: Buffer): Promise<void> {
await this.invokeHook('_writeBuffer', { buf }, async (ctx, next) => {
try {
if (!Buffer.isBuffer(ctx.buf)) throw new TypeError('buf should be a Buffer')
if (!this.isConnected()) await this.connect()
this.logger.send(ChameleonUltraFrame.inspect(ctx.buf))
const writer = (this.port?.writable as any)?.getWriter()
if (_.isNil(writer)) throw new Error('Failed to getWriter(). Did you remember to use adapter plugin?')
await writer.write(ctx.buf)
writer.releaseLock()
} catch (err) {
throw _.merge(new Error(err.message ?? 'Failed to connect'), { originalError: err })
}
if (!Buffer.isBuffer(ctx.buf)) throw new TypeError('buf should be a Buffer')
if (!this.isConnected()) await this.connect()
this.logger.send(ChameleonUltraFrame.inspect(ctx.buf))
const writer = (this.port?.writable as any)?.getWriter()
if (_.isNil(writer)) throw new Error('Failed to getWriter(). Did you remember to use adapter plugin?')
await writer.write(ctx.buf)
writer.releaseLock()
})
}

Expand Down
20 changes: 10 additions & 10 deletions src/plugin/WebbleAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,17 @@ interface AdapterInstallResp {
}

class ChameleonWebbleAdapterRxSource implements UnderlyingSource<Buffer> {
adapter: WebbleAdapter
bufs: Buffer[] = []
controller?: ReadableStreamDefaultController<Buffer>
#controller?: ReadableStreamDefaultController<Buffer>
readonly #adapter: WebbleAdapter

constructor (adapter: WebbleAdapter) { this.adapter = adapter }
constructor (adapter: WebbleAdapter) { this.#adapter = adapter }

start (controller: ReadableStreamDefaultController<Buffer>): void { this.controller = controller }
start (controller: ReadableStreamDefaultController<Buffer>): void { this.#controller = controller }

onNotify (event: any): void {
const buf = this.adapter.Buffer?.from((event?.target?.value as Uint8Array))
this.adapter.logger.webble(`onNotify = ${buf?.toString('hex')}`)
this.controller?.enqueue(buf)
const buf = this.#adapter.Buffer?.fromView((event?.target?.value as DataView))
this.#adapter.logger.webble(`onNotify = ${buf?.toString('hex')}`)
this.#controller?.enqueue(buf)
}
}

Expand All @@ -163,12 +162,13 @@ class ChameleonWebbleAdapterTxSink implements UnderlyingSink<Buffer> {

constructor (adapter: WebbleAdapter) {
this.#adapter = adapter
if (_.isNil(this.#adapter.send)) throw new Error('this.adapter.send can not be null')
if (_.isNil(this.#adapter.Buffer)) throw new Error('this.adapter.Buffer can not be null')
if (_.isNil(this.#adapter.Buffer)) throw new Error('this.#adapter.Buffer can not be null')
this.#Buffer = this.#adapter.Buffer
}

async write (chunk: Buffer): Promise<void> {
if (_.isNil(this.#adapter.send)) throw new Error('this.#adapter.send can not be null')

// 20 bytes are left for the attribute data
// https://stackoverflow.com/questions/38913743/maximum-packet-length-for-bluetooth-le
let buf1: Buffer | null = null
Expand Down

0 comments on commit 3300110

Please sign in to comment.