@stoprocent/noble 2.3.4 → 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
@@ -167,6 +167,7 @@ class Noble extends EventEmitter {
167
167
  return;
168
168
  }
169
169
  const timeoutId = setTimeout(() => {
170
+ this.removeListener('stateChange', listener);
170
171
  reject(new Error('Timeout waiting for Noble to be powered on'));
171
172
  }, timeout);
172
173
 
@@ -187,10 +188,12 @@ class Noble extends EventEmitter {
187
188
  const self = this;
188
189
  const scan = (state) => {
189
190
  if (state !== 'poweredOn') {
190
- self.once('stateChange', scan.bind(self));
191
+ const boundScan = scan.bind(self);
192
+ self.once('stateChange', boundScan);
191
193
  const error = new Error(`Could not start scanning, state is ${state} (not poweredOn)`);
192
194
 
193
195
  if (typeof callback === 'function') {
196
+ self.removeListener('stateChange', boundScan);
194
197
  callback(error);
195
198
  } else {
196
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.4",
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', () => {
@@ -420,6 +438,19 @@ describe('noble', () => {
420
438
  // Promise should reject after timeout
421
439
  await expect(promise).rejects.toThrow('Timeout waiting for Noble to be powered on');
422
440
  });
441
+
442
+ test('should not cause MaxListenersExceededWarning warnings with multiple timeout calls', async () => {
443
+ noble._state = 'poweredOff';
444
+
445
+ const promises = [];
446
+ for (let i = 0; i < 11; i++) {
447
+ promises.push(noble.waitForPoweredOnAsync(0).catch(() => {}));
448
+ }
449
+ await Promise.all(promises);
450
+ const finalListenerCount = noble.listenerCount('stateChange');
451
+
452
+ expect(finalListenerCount).toBeLessThanOrEqual(1);
453
+ });
423
454
  });
424
455
 
425
456
  test('should change address', () => {