@stoprocent/noble 2.3.5 → 2.3.6

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/lib/noble.js CHANGED
@@ -188,10 +188,12 @@ class Noble extends EventEmitter {
188
188
  const self = this;
189
189
  const scan = (state) => {
190
190
  if (state !== 'poweredOn') {
191
- self.once('stateChange', scan.bind(self));
191
+ const boundScan = scan.bind(self);
192
+ self.once('stateChange', boundScan);
192
193
  const error = new Error(`Could not start scanning, state is ${state} (not poweredOn)`);
193
194
 
194
195
  if (typeof callback === 'function') {
196
+ self.removeListener('stateChange', boundScan);
195
197
  callback(error);
196
198
  } else {
197
199
  throw error;
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.5",
9
+ "version": "2.3.6",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "https://github.com/stoprocent/noble.git"
@@ -184,6 +184,24 @@ describe('noble', () => {
184
184
  );
185
185
  expect(mockBindings.startScanning).not.toHaveBeenCalled();
186
186
  });
187
+
188
+ test('should not cause MaxListenersExceededWarning warnings after repeated unauthorized errors', async () => {
189
+ noble._state = 'unauthorized';
190
+
191
+ const promise = noble.waitForPoweredOnAsync(0);
192
+
193
+ for (let i = 0; i < 10; i++) {
194
+ try {
195
+ await noble.startScanningAsync([]);
196
+ } catch (error) {
197
+ }
198
+ }
199
+
200
+ const finalListenerCount = noble.listenerCount('stateChange');
201
+
202
+ await expect(promise).rejects.toThrow('Timeout waiting for Noble to be powered on');
203
+ expect(finalListenerCount).toBeLessThanOrEqual(1);
204
+ });
187
205
  });
188
206
 
189
207
  describe('stopScanning', () => {
@@ -421,7 +439,7 @@ describe('noble', () => {
421
439
  await expect(promise).rejects.toThrow('Timeout waiting for Noble to be powered on');
422
440
  });
423
441
 
424
- test('should not cause MaxListenersExceededWarning with multiple timeout calls', async () => {
442
+ test('should not cause MaxListenersExceededWarning warnings with multiple timeout calls', async () => {
425
443
  noble._state = 'poweredOff';
426
444
 
427
445
  const promises = [];