Skip to content

Commit

Permalink
Device: Change cleanup approach
Browse files Browse the repository at this point in the history
Disconnect cannot be used for cleanup as it actually has the property of
disconnecting already connected devices.

This commit removes listener removal from disconnect and places it in its own
function that should be called on a Device object after instantiating it
and before discarding it.
  • Loading branch information
tuxedoxt committed Jan 15, 2024
1 parent 9ec2b04 commit 89b0bb8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Adapter {

/**
* Init a device instance and returns it.
* Caller is responsible of calling cleanup() on the
* returned Device before discarding it to free resources.
* @param {string} uuid - Device Name.
* @async
* @returns {Device}
Expand Down
9 changes: 8 additions & 1 deletion src/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Device extends EventEmitter {
*/
async disconnect () {
await this.helper.callMethod('Disconnect')
this.helper.removeListeners()
await this.cleanup()
}

/**
Expand All @@ -142,6 +142,13 @@ class Device extends EventEmitter {

return `${name} [${address}]`
}

/**
* Cleanup when discarding device object
*/
async cleanup () {
this.helper.removeListeners()
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ declare namespace NodeBle {
disconnect(): Promise<void>;
gatt(): Promise<GattServer>;
toString(): Promise<string>;
cleanup(): Promise<void>;

on(event: 'connect', listener: (state: ConnectionState) => void): this;
on(event: 'disconnect', listener: (state: ConnectionState) => void): this;
Expand Down

0 comments on commit 89b0bb8

Please sign in to comment.