@stoprocent/noble 2.0.0 → 2.0.1
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
|
@@ -162,6 +162,10 @@ class Noble extends EventEmitter {
|
|
|
162
162
|
|
|
163
163
|
async waitForPoweredOnAsync (timeout = 10000) {
|
|
164
164
|
return new Promise((resolve, reject) => {
|
|
165
|
+
if (this.state === 'poweredOn') {
|
|
166
|
+
resolve();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
165
169
|
const timeoutId = setTimeout(() => {
|
|
166
170
|
reject(new Error('Timeout waiting for Noble to be powered on'));
|
|
167
171
|
}, timeout);
|
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.0.
|
|
9
|
+
"version": "2.0.1",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "https://github.com/stoprocent/noble.git"
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"node-gyp-build": "^4.8.1"
|
|
34
34
|
},
|
|
35
35
|
"optionalDependencies": {
|
|
36
|
-
"@stoprocent/bluetooth-hci-socket": "^2.
|
|
36
|
+
"@stoprocent/bluetooth-hci-socket": "^2.2.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@babel/eslint-parser": "^7.27.0",
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/test/noble.test.js
CHANGED
|
@@ -380,6 +380,48 @@ describe('noble', () => {
|
|
|
380
380
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
381
381
|
});
|
|
382
382
|
|
|
383
|
+
describe('waitForPoweredOnAsync', () => {
|
|
384
|
+
test('should resolve when state changes to poweredOn', async () => {
|
|
385
|
+
// Set initial state to something other than poweredOn
|
|
386
|
+
noble._state = 'poweredOff';
|
|
387
|
+
|
|
388
|
+
// Create promise but don't await it yet
|
|
389
|
+
const promise = noble.waitForPoweredOnAsync();
|
|
390
|
+
|
|
391
|
+
// Emit state change event to poweredOn
|
|
392
|
+
noble.emit('stateChange', 'poweredOn');
|
|
393
|
+
|
|
394
|
+
// Now await the promise - it should resolve
|
|
395
|
+
await expect(promise).resolves.toBeUndefined();
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
test('should resolve immediately if state is already poweredOn', async () => {
|
|
399
|
+
// Set initial state to poweredOn
|
|
400
|
+
noble._state = 'poweredOn';
|
|
401
|
+
|
|
402
|
+
// Multiple concurrent calls should resolve immediately
|
|
403
|
+
const promise1 = noble.waitForPoweredOnAsync();
|
|
404
|
+
const promise2 = noble.waitForPoweredOnAsync();
|
|
405
|
+
const promise3 = noble.waitForPoweredOnAsync();
|
|
406
|
+
|
|
407
|
+
// Both promises should resolve
|
|
408
|
+
await expect(promise1).resolves.toBeUndefined();
|
|
409
|
+
await expect(promise2).resolves.toBeUndefined();
|
|
410
|
+
await expect(promise3).resolves.toBeUndefined();
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
test('should reject if timeout occurs before state changes to poweredOn', async () => {
|
|
414
|
+
// Set initial state to something other than poweredOn
|
|
415
|
+
noble._state = 'poweredOff';
|
|
416
|
+
|
|
417
|
+
// Set a very short timeout
|
|
418
|
+
const promise = noble.waitForPoweredOnAsync(10);
|
|
419
|
+
|
|
420
|
+
// Promise should reject after timeout
|
|
421
|
+
await expect(promise).rejects.toThrow('Timeout waiting for Noble to be powered on');
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
|
|
383
425
|
test('should change address', () => {
|
|
384
426
|
const address = 'newAddress';
|
|
385
427
|
noble._onAddressChange(address);
|