Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
Change storing listener by numeric address representation
Browse files Browse the repository at this point in the history
Before `getRaw` was used to determine an array key for a group address. This
method returns an array of bytes, which is transformed to a string when used
as array key. `getNumber` returns an integer representation for the group
address and feels more correct for a usage as an array key.
  • Loading branch information
micha149 committed Dec 23, 2014
1 parent 3203da4 commit 0fa6b58
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function Connection(driver) {
*/
Connection.prototype._onWriteMessage = function(message) {

var rawAddress = message.getDestination().getRaw(),
var rawAddress = message.getDestination().getNumber(),
callbacks = this._listeners[rawAddress];

if (callbacks) {
Expand Down Expand Up @@ -115,7 +115,7 @@ Connection.prototype.send = function(msg, callback) {
Connection.prototype.on = function (address, callback) {
var driver = this._driver,
listeners = this._listeners,
rawAddress = address.getRaw();
rawAddress = address.getNumber();

if (!driver.isConnected()) {
driver.connect();
Expand All @@ -134,7 +134,7 @@ Connection.prototype.on = function (address, callback) {
*/
Connection.prototype.off = function(address, callback) {
var listeners = this._listeners,
rawAddress = address.getRaw();
rawAddress = address.getNumber();

if (!callback) {
listeners[rawAddress] = [];
Expand Down
16 changes: 8 additions & 8 deletions test/ConnectionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('Connection', function() {
var driver = sinon.createStubInstance(Driver),
address = sinon.createStubInstance(GroupAddress),
msg = sinon.createStubInstance(Message),
rawAddress = [0x22, 0x9b],
numAddress = 2563,
callback = sinon.spy(),
connection;

Expand All @@ -139,7 +139,7 @@ describe('Connection', function() {

connection = new Connection(driver);

address.getRaw.returns(rawAddress);
address.getNumber.returns(numAddress);
msg.getDestination.returns(address);
msg.getCommand.returns('write');

Expand All @@ -155,7 +155,7 @@ describe('Connection', function() {
var driver = sinon.createStubInstance(Driver),
address = sinon.createStubInstance(GroupAddress),
msg = sinon.createStubInstance(Message),
rawAddress = [0x22, 0x9b],
numAddress = 8859,
callbackA = sinon.spy(),
callbackB = sinon.spy(),
connection;
Expand All @@ -166,7 +166,7 @@ describe('Connection', function() {

connection = new Connection(driver);

address.getRaw.returns(rawAddress);
address.getNumber.returns(numAddress);
msg.getDestination.returns(address);

connection.on(address, callbackA);
Expand All @@ -193,11 +193,11 @@ describe('Connection', function() {

this.createGroupAddress = function() {
var address = sinon.createStubInstance(GroupAddress);
address.getRaw.returns([0x22, addressCounter++]);
address.getNumber.returns(addressCounter++);
return address;
}
};

})
});

it("removes all listeners for a given address", function() {
var callbackA = sinon.spy(),
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('Connection', function() {

expect(callbackA).not.to.to.be.called;
expect(callbackB).to.to.be.called;
})
});

});

Expand Down

0 comments on commit 0fa6b58

Please sign in to comment.