From d9515a751cb475942706ba10f7f359a95c9c34a6 Mon Sep 17 00:00:00 2001 From: Max Nowack Date: Mon, 5 Dec 2016 19:40:58 +0100 Subject: [PATCH] enable es6 class inheritance resolves #23 It seems that the tests are broken even without my change. This should be fixed before merging to ensure the compatibility to older versions. --- lib/noble-device.js | 113 +++++++++++++++++++++++++++++++++++++-- lib/util.js | 125 +++----------------------------------------- 2 files changed, 117 insertions(+), 121 deletions(-) diff --git a/lib/noble-device.js b/lib/noble-device.js index 9a1a317..889c734 100644 --- a/lib/noble-device.js +++ b/lib/noble-device.js @@ -1,5 +1,5 @@ -var events = require('events'); -var util = require('util'); +var EventEmitter = require('events').EventEmitter; +var util = require('./util'); var GENERIC_ACCESS_UUID = '1800'; var DEVICE_NAME_UUID = '2a00'; @@ -16,7 +16,114 @@ function NobleDevice(peripheral) { this.connectedAndSetUp = false; } -util.inherits(NobleDevice, events.EventEmitter); +util.inherits(NobleDevice, EventEmitter); + +NobleDevice.SCAN_UUIDS = []; +NobleDevice.SCAN_DUPLICATES = false; + +NobleDevice.is = function(peripheral) { + return true; +}; + +NobleDevice.emitter = new EventEmitter(); + +NobleDevice.onDiscover = function(peripheral) { + if (this.is(peripheral)) { + var device = new this(peripheral); + + this.emitter.emit('discover', device); + } +}; + +NobleDevice.onStateChange = function(state) { + if (state === 'poweredOn' && this.emitter.listeners('discover').length > 0) { + this.startScanning(); + } +}; + +NobleDevice.startScanning = function() { + noble.startScanning(this.SCAN_UUIDS, this.SCAN_DUPLICATES); +}; + +NobleDevice.stopScanning = function() { + noble.stopScanning(); +}; + +NobleDevice.discoverAll = function(callback) { + this.emitter.addListener('discover', callback); + + if (this.emitter.listeners('discover').length === 1) { + noble.on('discover', this.onDiscover); + noble.on('stateChange', this.onStateChange); + + if (noble.state === 'poweredOn') { + this.startScanning(); + } + } +}; + +NobleDevice.stopDiscoverAll = function(discoverCallback) { + this.emitter.removeListener('discover', discoverCallback); + + if (this.emitter.listeners('discover').length === 0) { + noble.removeListener('discover', this.onDiscover); + noble.removeListener('stateChange', this.onStateChange); + + this.stopScanning(); + } +}; + +NobleDevice.discover = function(callback) { + console.log(this, this.SCAN_UUIDS, this); + var onDiscover = function(device) { + this.stopDiscoverAll(onDiscover); + + callback(device); + }; + + callback._nobleDeviceOnDiscover = onDiscover; + + this.discoverAll(onDiscover); +}; + +NobleDevice.stopDiscover = function(callback) { + var onDiscover = callback._nobleDeviceOnDiscover; + + if (onDiscover) { + this.stopDiscoverAll(onDiscover); + } +}; + +NobleDevice.discoverWithFilter = function(filter, callback) { + var onDiscoverWithFilter = function(device) { + if (filter(device)) { + this.stopDiscoverAll(onDiscoverWithFilter); + + callback(device); + } + }; + + this.discoverAll(onDiscoverWithFilter); +}; + +NobleDevice.discoverById = function(id, callback) { + this.discoverWithFilter(function(device) { + return (device.id === id); + }, callback); +}; + +// deprecated +NobleDevice.discoverByUuid = function(uuid, callback) { + this.discoverWithFilter(function(device) { + return (device.uuid === uuid); + }, callback); +}; + +NobleDevice.discoverByAddress = function(address, callback) { + this.discoverWithFilter(function(device) { + return (device.address === address); + }, callback); +}; NobleDevice.prototype.onDisconnect = function() { this.connectedAndSetUp = false; diff --git a/lib/util.js b/lib/util.js index 054ce8a..d7cec1b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,125 +1,14 @@ -var events = require('events'); -var util = require('util'); - -var noble = require('noble'); - -var EventEmitter = events.EventEmitter; - -var NobleDevice = require('./noble-device'); - function Util() { } -Util.inherits = function(constructor, superConstructor) { - util.inherits(constructor, superConstructor); - - if (superConstructor === NobleDevice) { - constructor.SCAN_UUIDS = constructor.SCAN_UUIDS || []; - constructor.SCAN_DUPLICATES = constructor.SCAN_DUPLICATES || false; - - constructor.is = constructor.is || function(peripheral) { - return true; - }; - - constructor.emitter = new EventEmitter(); - - constructor.onDiscover = function(peripheral) { - if (constructor.is(peripheral)) { - var device = new constructor(peripheral); - - constructor.emitter.emit('discover', device); - } - }; - - constructor.onStateChange = function(state) { - if (state === 'poweredOn' && constructor.emitter.listeners('discover').length > 0) { - constructor.startScanning(); - } - }; - - constructor.startScanning = function() { - noble.startScanning(constructor.SCAN_UUIDS, constructor.SCAN_DUPLICATES); - }; - - constructor.stopScanning = function() { - noble.stopScanning(); - }; - - constructor.discoverAll = function(callback) { - constructor.emitter.addListener('discover', callback); - - if (constructor.emitter.listeners('discover').length === 1) { - noble.on('discover', constructor.onDiscover); - noble.on('stateChange', constructor.onStateChange); - - if (noble.state === 'poweredOn') { - constructor.startScanning(); - } - } - }; - - constructor.stopDiscoverAll = function(discoverCallback) { - constructor.emitter.removeListener('discover', discoverCallback); - - if (constructor.emitter.listeners('discover').length === 0) { - noble.removeListener('discover', constructor.onDiscover); - noble.removeListener('stateChange', constructor.onStateChange); - - constructor.stopScanning(); - } - }; - - constructor.discover = function(callback) { - var onDiscover = function(device) { - constructor.stopDiscoverAll(onDiscover); - - callback(device); - }; - - callback._nobleDeviceOnDiscover = onDiscover; - - constructor.discoverAll(onDiscover); - }; - - constructor.stopDiscover = function(callback) { - var onDiscover = callback._nobleDeviceOnDiscover; - - if (onDiscover) { - constructor.stopDiscoverAll(onDiscover); - } - }; - - constructor.discoverWithFilter = function(filter, callback) { - var onDiscoverWithFilter = function(device) { - if (filter(device)) { - constructor.stopDiscoverAll(onDiscoverWithFilter); - - callback(device); - } - }; - - constructor.discoverAll(onDiscoverWithFilter); - }; - - constructor.discoverById = function(id, callback) { - constructor.discoverWithFilter(function(device) { - return (device.id === id); - }, callback); - }; - - // deprecated - constructor.discoverByUuid = function(uuid, callback) { - constructor.discoverWithFilter(function(device) { - return (device.uuid === uuid); - }, callback); - }; - - constructor.discoverByAddress = function(address, callback) { - constructor.discoverWithFilter(function(device) { - return (device.address === address); - }, callback); - }; +Util.inherits = function inherits(subClass, superClass) { + if (typeof superClass !== 'function' && superClass !== null) { + throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } + subClass.prototype = Object.create(superClass && superClass.prototype, { + constructor: { value: subClass, enumerable: false, writable: true, configurable: true } + }); + if (superClass) Object.setPrototypeOf(subClass, superClass); }; Util.mixin = function(constructor, mixin, includedMethods, excludedMethods) {