Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/noble.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,12 @@ class Noble extends EventEmitter {
const self = this;
const scan = (state) => {
if (state !== 'poweredOn') {
self.once('stateChange', scan.bind(self));
const boundScan = scan.bind(self);
self.once('stateChange', boundScan);
const error = new Error(`Could not start scanning, state is ${state} (not poweredOn)`);

if (typeof callback === 'function') {
self.removeListener('stateChange', boundScan);
callback(error);
} else {
throw error;
Expand Down
20 changes: 19 additions & 1 deletion test/noble.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,24 @@ describe('noble', () => {
);
expect(mockBindings.startScanning).not.toHaveBeenCalled();
});

test('should not cause MaxListenersExceededWarning warnings after repeated unauthorized errors', async () => {
noble._state = 'unauthorized';

const promise = noble.waitForPoweredOnAsync(0);

for (let i = 0; i < 10; i++) {
try {
await noble.startScanningAsync([]);
} catch (error) {
}
}

const finalListenerCount = noble.listenerCount('stateChange');

await expect(promise).rejects.toThrow('Timeout waiting for Noble to be powered on');
expect(finalListenerCount).toBeLessThanOrEqual(1);
});
});

describe('stopScanning', () => {
Expand Down Expand Up @@ -421,7 +439,7 @@ describe('noble', () => {
await expect(promise).rejects.toThrow('Timeout waiting for Noble to be powered on');
});

test('should not cause MaxListenersExceededWarning with multiple timeout calls', async () => {
test('should not cause MaxListenersExceededWarning warnings with multiple timeout calls', async () => {
noble._state = 'poweredOff';

const promises = [];
Expand Down