Skip to content

Commit

Permalink
Merge pull request #339 from prey/location-callbacks
Browse files Browse the repository at this point in the history
Location callbacks handling
  • Loading branch information
javo authored Oct 31, 2017
2 parents b7f4e9f + a880042 commit 48a4f70
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/agent/triggers/location/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var join = require('path').join,
Emitter = require('events').EventEmitter;

var emitter,
loc_callback,
loc_callbacks = [],
current = {},
checking = false;

Expand All @@ -29,17 +29,24 @@ var trigger_event = function(type, new_location) {
}

var fetch_location = function(type, callback) {
if (callback) loc_callback = callback;
if (callback) loc_callbacks.push(callback);
if (checking) return;

var done = function(err, coords) {
if (loc_callback) {
loc_callback(null, coords);
if (loc_callbacks.length >= 1) {
fire_callbacks(err, coords);
}
loc_callback = null;
loc_callbacks = [];
checking = false;
}

var fire_callbacks = function(err, coords) {
var list = loc_callbacks;
list.forEach(function(fn) {
fn(err, coords);
});
}

checking = true;
geo.fetch_location(function(err, coords) {
if (err || !coords || !emitter)
Expand All @@ -63,7 +70,10 @@ exports.start = function(opts, cb) {
else fetch_location(callback);
});

fetch_location('client-start');
// Give some time to get initial providers before ask for location
setTimeout(function() {
fetch_location('client-start');
}, 2000)

emitter = new Emitter();
cb(null, emitter);
Expand Down

0 comments on commit 48a4f70

Please sign in to comment.