Skip to content

Commit

Permalink
Merge #42 r=@johnnyman727: Removes unnessary listener removal and add…
Browse files Browse the repository at this point in the history
…s test
  • Loading branch information
tm-rampart committed Oct 10, 2014
2 parents ce31dd3 + 0bdf055 commit b8930d1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
3 changes: 0 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,6 @@ Ambient.prototype._setListening = function(enable, event) {
// stop polling
clearTimeout(self._pollTimeout);
self._pollTimeout = null;
if(this.irqwatcher) {
this.attiny.irq.removeListener('high', this.irqwatcher);
}
}
}
};
Expand Down
82 changes: 82 additions & 0 deletions test/listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var test = require('tinytap');
var tessel = require('tessel');
var ambientlib = require('../');

var portname = process.argv[2] || 'A';
var timeout = 1500;
var triggerVal = 0.001; // light trigger

var t;
var ambient;

test.count(1);

test("Using multiple kinds of listeners", function(tester) {
t = tester;
ambient = ambientlib.use(tessel.port[portname], start);

ambient.on('error', function(err) {
t.fail(err.message);
})
})

function start () {
var numEvents = 2;
var countEvents = 0;

// Set a light data timer
var lightDataTimer = setTimeout(function () {
t.fail('failed to emit a light data event in a reasonable amount of time');
}, timeout);

// Wait for light readings
ambient.on('light', function (data) {
// Clear that light data timer
clearTimeout(lightDataTimer);
// Remove ALL Listeners!
ambient.removeAllListeners('light');
// There was another event
countEvents++;
// If there were x number of events
if(countEvents == numEvents) {
// add a lighttrigger listener
light();
}
});

// Set a sound data tmer
var soundDataTimer = setTimeout(function () {
t.fail('failed to emit a sound data event in a reasonable amount of time')
}, timeout);
// When we get sound
ambient.on('sound', function (data) {
// clear the timeout
clearTimeout(soundDataTimer);
// remove the sound listeners
ambient.removeAllListeners('sound');
// we shouldn't get another event
countEvents++;
// if we do
if(countEvents == numEvents) {
// start the light trigger listener
light();
}
});
}

function light () {
ambient.setLightTrigger(triggerVal, function (err, val) {
var s = new Date();
var failLightTrigger = setTimeout(function () {
t.fail('Light trigger timed out.')
}, timeout);
ambient.on('light-trigger', function (datum) {
// Timeout cancelled
clearTimeout(failLightTrigger);
ambient.clearLightTrigger(function (err, val) {
t.equal(err, null);
t.end();
});
});
});
}

0 comments on commit b8930d1

Please sign in to comment.