Skip to content

Commit

Permalink
fix(dbus): Fix peripheral state
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Crespi committed Mar 25, 2021
1 parent 64d95da commit bd05dc5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 46 deletions.
3 changes: 0 additions & 3 deletions lib/bindings/dbus/Peripheral.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ export declare class DbusPeripheral extends Peripheral {
private propsIface;
private _init;
protected _gatt: DbusGatt;
private isConnecting;
private connecting;
private connectTimeout;
private isDisconnecting;
private disconnecting;
private disconnectTimeout;
constructor(adapter: DbusAdapter, path: string, id: string, addressType: AddressType, address: string, advertisement: Record<string, unknown>, rssi: number);
private init;
private prop;
private isConnected;
connect(): Promise<DbusGatt>;
disconnect(): Promise<void>;
private doneConnecting;
Expand Down
2 changes: 1 addition & 1 deletion lib/bindings/dbus/Peripheral.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 12 additions & 19 deletions lib/bindings/dbus/Peripheral.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/bindings/dbus/Peripheral.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 13 additions & 22 deletions src/bindings/dbus/Peripheral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ export class DbusPeripheral extends Peripheral {

protected _gatt: DbusGatt;

private isConnecting = false;
private connecting: [(gatt: DbusGatt) => void, (error?: Error) => void][] = [];
private connectTimeout: NodeJS.Timer;
private isDisconnecting = false;

private disconnecting: [() => void, (error?: Error) => void][] = [];
private disconnectTimeout: NodeJS.Timer;

Expand Down Expand Up @@ -57,25 +56,19 @@ export class DbusPeripheral extends Peripheral {
return rawProp.value;
}

private async isConnected() {
return this.prop<boolean>(I_BLUEZ_DEVICE, 'Connected');
}

public async connect(): Promise<DbusGatt> {
if (await this.isConnected()) {
if (this._state === 'connected') {
return;
}

if (this.isDisconnecting) {
if (this._state === 'disconnecting') {
throw new Error(`Device is currently disconnecting, cannot connect`);
}

if (this.isConnecting) {
if (this._state === 'connecting') {
return new Promise<DbusGatt>((resolve, reject) => this.connecting.push([resolve, reject]));
}

this.connecting = [];
this.isConnecting = true;
this._state = 'connecting';

await this.init();

Expand Down Expand Up @@ -114,20 +107,18 @@ export class DbusPeripheral extends Peripheral {
}

public async disconnect(): Promise<void> {
if (!(await this.isConnected())) {
if (this._state === 'disconnected') {
return;
}

if (this.isConnecting) {
if (this._state === 'connecting') {
throw new Error(`Device is currently connecting, cannot disconnect`);
}

if (this.isDisconnecting) {
if (this.state === 'disconnecting') {
return new Promise<void>((resolve, reject) => this.disconnecting.push([resolve, reject]));
}

this.disconnecting = [];
this.isDisconnecting = true;
this._state = 'disconnecting';

await this.init();

Expand Down Expand Up @@ -159,11 +150,11 @@ export class DbusPeripheral extends Peripheral {
}

private doneConnecting(error?: Error) {
if (!this.isConnecting) {
if (this._state !== 'connecting') {
return;
}

this.isConnecting = false;
this._state = 'connected';
clearTimeout(this.connectTimeout);

if (error) {
Expand All @@ -176,11 +167,11 @@ export class DbusPeripheral extends Peripheral {
this.connecting = [];
}
private doneDisconnecting(error?: Error) {
if (!this.isDisconnecting) {
if (this._state !== 'disconnecting') {
return;
}

this.isDisconnecting = false;
this._state = 'disconnected';
clearTimeout(this.disconnectTimeout);

if (error) {
Expand Down

0 comments on commit bd05dc5

Please sign in to comment.