@stoprocent/noble 2.3.7 → 2.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -122,14 +122,14 @@ declare module '@stoprocent/noble' {
122
122
  toString(): string;
123
123
 
124
124
  on(event: "connect", listener: (error: Error | undefined) => void): this;
125
- on(event: "disconnect", listener: (error: Error | undefined) => void): this;
125
+ on(event: "disconnect", listener: (reason: string) => void): this;
126
126
  on(event: "rssiUpdate", listener: (rssi: number) => void): this;
127
127
  on(event: "servicesDiscover", listener: (services: Service[]) => void): this;
128
128
  on(event: "mtu", listener: (mtu: number) => void): this;
129
129
  on(event: string, listener: Function): this;
130
130
 
131
131
  once(event: "connect", listener: (error: Error | undefined) => void): this;
132
- once(event: "disconnect", listener: (error: Error | undefined) => void): this;
132
+ once(event: "disconnect", listener: (reason: string) => void): this;
133
133
  once(event: "rssiUpdate", listener: (rssi: number) => void): this;
134
134
  once(event: "servicesDiscover", listener: (services: Service[]) => void): this;
135
135
  once(event: string, listener: Function): this;
@@ -158,20 +158,29 @@ Hci.prototype.init = function (options) {
158
158
  this._socket.on('error', this.onSocketError.bind(this));
159
159
  this._socket.on('state', this.pollIsDevUp.bind(this));
160
160
 
161
- if (this._userChannel) {
162
- this._socket.bindUser(this._deviceId, this._bindParams);
163
- this._socket.start();
164
-
165
- this.reset();
166
- } else {
167
- if (!this._bound) {
168
- this._socket.bindRaw(this._deviceId, this._bindParams);
169
- this._bound = true;
161
+ try {
162
+ // Bind first (either user channel or raw)
163
+ if (this._userChannel) {
164
+ this._socket.bindUser(this._deviceId, this._bindParams);
165
+ } else {
166
+ if (!this._bound) {
167
+ this._socket.bindRaw(this._deviceId, this._bindParams);
168
+ this._bound = true;
169
+ }
170
170
  }
171
+
172
+ // Start and reset (common to both paths)
171
173
  this._socket.start();
172
- this._isStarted = true;
173
174
  this.reset();
174
- this.pollIsDevUp();
175
+
176
+ // Set started flag and poll only for non-userChannel
177
+ if (!this._userChannel) {
178
+ this._isStarted = true;
179
+ this.pollIsDevUp();
180
+ }
181
+ } catch (error) {
182
+ debug(`init error: ${error.message}`);
183
+ this.emit('stateChange', 'unsupported');
175
184
  }
176
185
  };
177
186
 
package/lib/noble.js CHANGED
@@ -431,7 +431,7 @@ class Noble extends EventEmitter {
431
431
  this._bindings.disconnect(peripheralId);
432
432
  }
433
433
 
434
- _onDisconnect (peripheralId, reason) {
434
+ _onDisconnect (peripheralId, reason = 'unknown') {
435
435
  const peripheral = this._peripherals.get(peripheralId);
436
436
 
437
437
  if (peripheral) {
@@ -786,7 +786,7 @@ class Noble extends EventEmitter {
786
786
 
787
787
  async _withDisconnectHandler (peripheralId, operation) {
788
788
  return new Promise((resolve, reject) => {
789
- const disconnectListener = error => reject(error);
789
+ const disconnectListener = reason => reject(new Error(`Disconnected ${reason}`));
790
790
  this.once(`disconnect:${peripheralId}`, disconnectListener);
791
791
 
792
792
  Promise.resolve(operation())
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "license": "MIT",
7
7
  "name": "@stoprocent/noble",
8
8
  "description": "A Node.js BLE (Bluetooth Low Energy) central library.",
9
- "version": "2.3.7",
9
+ "version": "2.3.9",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "https://github.com/stoprocent/noble.git"
@@ -932,4 +932,18 @@ describe('noble', () => {
932
932
  expect(peripheral.state).toBe('disconnected');
933
933
  });
934
934
  });
935
- });
935
+
936
+ describe("_withDisconnectHandler", () => {
937
+ test("resolves operation result", async () => {
938
+ const promise = noble._withDisconnectHandler('peripheralUuid', () => Promise.resolve(1))
939
+ await expect(promise).resolves.toBe(1)
940
+ })
941
+
942
+ test("throws disconnect error if disconnected before resolve", async () => {
943
+ noble._peripherals.set('uuid', {emit: jest.fn()});
944
+ const promise = noble._withDisconnectHandler('uuid', () => Promise.resolve(1))
945
+ noble._onDisconnect('uuid')
946
+ await expect(promise).rejects.toThrow('Disconnected unknown')
947
+ })
948
+ })
949
+ });