Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
enable es6 class inheritance
Browse files Browse the repository at this point in the history
resolves noble#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.
  • Loading branch information
maxnowack committed Dec 5, 2016
1 parent 3607ad2 commit d9515a7
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 121 deletions.
113 changes: 110 additions & 3 deletions lib/noble-device.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down
125 changes: 7 additions & 118 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit d9515a7

Please sign in to comment.