Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions lib/bbc-microbit-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,27 @@ BBCMicrobitIO.prototype.digitalRead = function(pin, handler) {
BBCMicrobitIO.prototype._onDiscover = function(microbit) {
debug('on discover', microbit);

this._microbit = microbit;
//Certain Intel BT Chipsets seem to require a scan stop before connections are allowed, otherwise they'll throw a: Connection Rejected due to Limited Resources
BBCMicrobit.stopScanning(() => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the minimum version of Node.js needed for the () => { syntax? I'm leaning towards just keeping it as an old school function here. Thoughts?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK Node 4.0.0. for arrow functions. But happy to change it...

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a few days so I can't visualise the entire call stack anymore. But if I remember correctly, the discover event listeners are triggered before scanning actually stops on a device level (the actual race condition). The proper solution would probably be to wait for a stopScanning-callback within util.js and then emit discover. But I was hesitant to change a lot in util.js because I didn't have the time to fully understand the effects on other libs depending on noble-device. Also this seems to be an Intel-specific problem, so even more limited scope.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I see what's needed, we're not waiting for the stop scan to complete.

I think this change would be better to have in noble-device.

Change

constructor.discover = function(callback) {
      var onDiscover = function(device) {
        constructor.stopDiscoverAll(onDiscover);

        callback(device);
      };

      callback._nobleDeviceOnDiscover = onDiscover;

      constructor.discoverAll(onDiscover);
    };

to something like

constructor.discover = function(callback) {
      var onDiscover = function(device) {
        constructor.stopDiscoverAll(onDiscover, function() {
          callback(device);
        ).bind(this));
      };

      callback._nobleDeviceOnDiscover = onDiscover;

      constructor.discoverAll(onDiscover);
    };

We'll need to change stopDiscoverAll to include an additional callback parameter. What do you think? Do you have time to PR this?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the minimum version of Node.js required to use the () => { syntax? I'm leaning towards just using the old school function syntax here.

this._microbit = microbit;

this._microbit.on('pinDataChange', this._onPinDataChange.bind(this));
this._microbit.on('pinDataChange', this._onPinDataChange.bind(this));

this._microbit.once('disconnect', function() {
this.emit('disconnect');
}.bind(this));

this._microbit.connectAndSetUp(function() {
this.emit('connect');
this._microbit.once('disconnect', function() {
this.emit('disconnect');
}.bind(this));

this._microbit.subscribePinData(function() {
this.emit('ready');
this._microbit.connectAndSetUp(function(error) {
if (error) {
console.error(error);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we emit the error here instead of logging it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whichever suits your style really :) I had a "throw" initially but felt that was too strong, but thought an emit might be "too weak". Can change.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think emit is the J5 way, it should bubble up the error.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we emit the error here instead of logging it?

}
this.emit('connect');
this._microbit.subscribePinData(function() {
this.emit('ready');
}.bind(this));

}.bind(this));
}.bind(this));
});
};

BBCMicrobitIO.prototype._onPinDataChange = function(pin, value) {
Expand Down