Skip to content

Commit

Permalink
more work for lazy bindings (noble#606)
Browse files Browse the repository at this point in the history
* more work for lazy bindings
  • Loading branch information
jacobrosenthal authored and monteslu committed May 11, 2017
1 parent a94b0a5 commit dfc2e4e
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions lib/noble.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var Descriptor = require('./descriptor');

function Noble(bindings) {
this.initialized = false;
this.state = 'unknown';
this.address = 'unknown';

this.address = 'unknown';
this._state = 'unknown';
this._bindings = bindings;
this._peripherals = {};
this._services = {};
Expand Down Expand Up @@ -48,20 +48,35 @@ function Noble(bindings) {
}
}.bind(this));

//lazy init bindings on first new listener, should be on stateChange
this.on('newListener', function(event) {
if (event === 'stateChange' && !this.initialized) {
this._bindings.init();
this.initialized = true;
}
}.bind(this));

//or lazy init bindings if someone attempts to get state first
Object.defineProperties(this, {
state: {
get: function () {
if (!this.initialized) {
this._bindings.init();
this.initialized = true;
}
return this._state;
}
}
});

}

util.inherits(Noble, events.EventEmitter);

Noble.prototype.onStateChange = function(state) {
debug('stateChange ' + state);

this.state = state;
this._state = state;

this.emit('stateChange', state);
};
Expand All @@ -73,25 +88,36 @@ Noble.prototype.onAddressChange = function(address) {
};

Noble.prototype.startScanning = function(serviceUuids, allowDuplicates, callback) {
if (this.state !== 'poweredOn') {
var error = new Error('Could not start scanning, state is ' + this.state + ' (not poweredOn)');

if (typeof callback === 'function') {
callback(error);
var scan = function(state) {
if (state !== 'poweredOn') {
var error = new Error('Could not start scanning, state is ' + state + ' (not poweredOn)');

if (typeof callback === 'function') {
callback(error);
} else {
throw error;
}
} else {
throw error;
}
} else {
if (callback) {
this.once('scanStart', function(filterDuplicates) {
callback(null, filterDuplicates);
});
}
if (callback) {
this.once('scanStart', function(filterDuplicates) {
callback(null, filterDuplicates);
});
}

this._discoveredPeripheralUUids = [];
this._allowDuplicates = allowDuplicates;
this._discoveredPeripheralUUids = [];
this._allowDuplicates = allowDuplicates;

this._bindings.startScanning(serviceUuids, allowDuplicates);
this._bindings.startScanning(serviceUuids, allowDuplicates);
}
};

//if bindings still not init, do it now
if (!this.initialized) {
this._bindings.init();
this.initialized = true;
this.once('stateChange', scan.bind(this));
}else{
scan.call(this, this._state);
}
};

Expand All @@ -104,7 +130,9 @@ Noble.prototype.stopScanning = function(callback) {
if (callback) {
this.once('scanStop', callback);
}
this._bindings.stopScanning();
if(this._bindings && this.initialized){
this._bindings.stopScanning();
}
};

Noble.prototype.onScanStop = function() {
Expand Down

0 comments on commit dfc2e4e

Please sign in to comment.