From 64b974420b4cf7ef1f3b57d7c69aa316b69a842a Mon Sep 17 00:00:00 2001 From: Jan Schmidle Date: Mon, 21 Sep 2020 11:25:27 +0200 Subject: [PATCH] linting, testing and async fixes --- lib/hci-socket/hci.js | 8 +++++--- lib/noble.js | 4 ++-- lib/peripheral.js | 16 ++++++++++++---- test/test-peripheral.js | 4 ++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/hci-socket/hci.js b/lib/hci-socket/hci.js index 00d3c08cb..aedea35ed 100644 --- a/lib/hci-socket/hci.js +++ b/lib/hci-socket/hci.js @@ -80,9 +80,11 @@ const Hci = function (options) { this._handleBuffers = {}; - this._deviceId = options.deviceId != null ? parseInt(options.deviceId, 10) : - process.env.NOBLE_HCI_DEVICE_ID ? parseInt(process.env.NOBLE_HCI_DEVICE_ID, 10) : - undefined; + this._deviceId = options.deviceId != null + ? parseInt(options.deviceId, 10) + : process.env.NOBLE_HCI_DEVICE_ID + ? parseInt(process.env.NOBLE_HCI_DEVICE_ID, 10) + : undefined; this._userChannel = (typeof options.userChannel === 'undefined' && options.userChannel) || process.env.HCI_CHANNEL_USER; diff --git a/lib/noble.js b/lib/noble.js index d42170917..80367acf8 100644 --- a/lib/noble.js +++ b/lib/noble.js @@ -99,14 +99,14 @@ Noble.prototype.setScanParameters = function (interval, window, callback) { this.once('scanParametersSet', callback); } this._bindings.setScanParameters(interval, window); -} +}; Noble.prototype.onScanParametersSet = function () { debug('scanParametersSet'); this.emit('scanParametersSet'); }; -Noble.prototype.startScanning = function (serviceUuids, allowDuplicates, callback) { +const startScanning = function (serviceUuids, allowDuplicates, callback) { if (typeof serviceUuids === 'function') { this.emit('warning', 'calling startScanning(callback) is deprecated'); } diff --git a/lib/peripheral.js b/lib/peripheral.js index d023b720c..2e9979f8b 100644 --- a/lib/peripheral.js +++ b/lib/peripheral.js @@ -31,7 +31,7 @@ Peripheral.prototype.toString = function () { }); }; -Peripheral.prototype.connect = function (options, callback) { +const connect = function (options, callback) { if (typeof options === 'function') { callback = options; options = undefined; @@ -46,11 +46,17 @@ Peripheral.prototype.connect = function (options, callback) { this.emit('connect', new Error('Peripheral already connected')); } else { this.state = 'connecting'; + console.log('connecting'); this._noble.connect(this.id, options); } }; -Peripheral.prototype.cancelConnect = function (options, callback) { +Peripheral.prototype.connect = connect; +Peripheral.prototype.connectAsync = function (options) { + return util.promisify((callback) => this.connect(options, callback))(); +}; + +const cancelConnect = function (options, callback) { if (typeof options === 'function') { callback = options; options = undefined; @@ -70,8 +76,10 @@ Peripheral.prototype.cancelConnect = function (options, callback) { } }; -Peripheral.prototype.connect = connect; -Peripheral.prototype.connectAsync = util.promisify(connect); +Peripheral.prototype.cancelConnect = cancelConnect; +Peripheral.prototype.cancelConnectAsync = function (options) { + return util.promisify((callback) => this.cancleConnect(options, callback))(); +}; const disconnect = function (callback) { if (callback) { diff --git a/test/test-peripheral.js b/test/test-peripheral.js index 1d3b9b1c7..a099c1f54 100644 --- a/test/test-peripheral.js +++ b/test/test-peripheral.js @@ -67,7 +67,7 @@ describe('Peripheral', function () { it('should delegate to noble', function () { peripheral.connect(); - mockNoble.connect.calledWithExactly(mockId).should.equal(true); + mockNoble.connect.calledWithExactly(mockId, undefined).should.equal(true); }); it('should callback', function () { @@ -105,7 +105,7 @@ describe('Peripheral', function () { peripheral.emit('connect'); await promise; - mockNoble.connect.calledWithExactly(mockId).should.equal(true); + mockNoble.connect.calledWithExactly(mockId, undefined).should.equal(true); }); });