Skip to content

Commit

Permalink
Added queue mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
tmuguet committed Oct 6, 2017
1 parent 731e3ca commit 6edd382
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 83 deletions.
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const sources_js = [
"src/js/jquery.shepherd.js",
"src/js/maths.js",
"src/js/utils.js",
"src/js/queue.js",
"src/js/state.js",
"src/js/cache.js",

Expand Down
33 changes: 33 additions & 0 deletions src/js/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(function ($) {
var queues = 0;

$.fn.clearCompute = function () {
return this.each(function () {
queues -= $(this).queue().length;
$(this).clearQueue();
});
};

$.fn.startCompute = function (cb) {
return this.each(function () {
$.State.setComputing(true);
queues++;
$(this).queue(cb);
});
};

$.fn.endCompute = function (next) {
return this.each(function () {
queues--;
next();

if (queues == 0)
$.State.setComputing(false);
});
};

$.Queue = {
size: () => queues,
};

})(jQuery);
20 changes: 8 additions & 12 deletions src/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,12 @@

geojson.addData(_geometry);

deferred.notify({ progress: 0.5, step: 'Route calculée' });
_this._add(geojson, start, end, index, 'auto')
.progress(function (progress) {
progress.progress = 0.5 + progress.progress / 2;
deferred.notify(progress);
})
.progress(deferred.notify)
.done(deferred.resolve)
.fail(deferred.reject);

deferred.notify({ step: 'Route calculée' });
} else {
deferred.rejectWith({ error: 'Impossible d\'obtenir la route: pas de résultats fournis' });
}
Expand All @@ -162,7 +160,7 @@
deferred.rejectWith({ error: 'Impossible d\'obtenir la route: ' + error.message });
},
};
deferred.notify({ progress: 0, status: 'Calcul de la route...' });
deferred.notify({ start: true, total: 1, status: 'Calcul de la route...' });
Gp.Services.route(options);
});
},
Expand All @@ -173,7 +171,7 @@
return $.Deferred(function () {
const deferred = this; // jscs:ignore safeContextKeyword

deferred.notify({ progress: 0, status: 'Calcul de la route...' });
deferred.notify({ start: true, total: 1, status: 'Calcul de la route...' });

const c1 = start.getLatLng().roundE8();
const c2 = end.getLatLng().roundE8();
Expand All @@ -197,14 +195,12 @@
snakingSpeed: 1000,
});

deferred.notify({ progress: 0.5, step: 'Route calculée' });
_this._add(geojson, start, end, index, 'straight')
.progress(function (progress) {
progress.progress = 0.5 + progress.progress / 2;
deferred.notify(progress);
})
.progress(deferred.notify)
.done(deferred.resolve)
.fail(deferred.reject);

deferred.notify({ step: 'Route calculée' });
});
},
};
Expand Down
44 changes: 15 additions & 29 deletions src/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,11 @@ window.onload = function () {
addMarker({ latlng: $.Track.getFirstMarker().getLatLng() });
}
},
}, {
stateName: 'computing',
icon: 'fa-spinner fa-pulse',
title: 'Fermer la boucle (calcul en cours...)',
},
],
});
$('body').on('map2gpx:modechange map2gpx:computingchange map2gpx:markerschange', function (e) {
if (e.computing) {
closeLoop.state('computing');
closeLoop.disable();
} else {
closeLoop.state('loaded');
closeLoop.setEnabled((e.mode !== null && $.Track.hasRoutes() && !$.Track.isImport()));
}
closeLoop.setEnabled((e.mode !== null && $.Track.hasRoutes() && !$.Track.isImport() && !$.Track.isLoop()));
});

L.easyBar([automatedBtn, lineBtn, closeLoop]).addTo(map);
Expand Down Expand Up @@ -252,7 +242,7 @@ window.onload = function () {

$btn.attr('disabled', 'disabled');
$.State.setComputing(true);
$.State.updateComputing({ progress: 0, status: 'Importation en cours...' });
$.State.updateComputing({ start: true, total: 1, status: 'Importation en cours...' });

const reader = new FileReader();

Expand All @@ -269,7 +259,10 @@ window.onload = function () {
$.State.setComputing(false);
},
onSuccess: function (gpx) {
$.State.updateComputing({ progress: 1 / (lines.length + 1), status: 'Récupération des données géographiques en cours...', step: 'Fichier traité' });
$.State.updateComputing([
{ step: 'Fichier traité' },
{ start: true, total: lines.length, status: 'Récupération des données géographiques en cours...' },
]);

$.Track.clear();

Expand All @@ -291,7 +284,6 @@ window.onload = function () {
};

const promises = [];
const progresses = [1];
var startMarker;
$.each(lines, function (idx, track) {
// Add new route+markers
Expand Down Expand Up @@ -329,16 +321,15 @@ window.onload = function () {
marker.bindPopup('<button class="track-delete-button"><i class="fa fa-trash" aria-hidden="true"></i> Supprimer l\'import</button>');
marker.on('popupopen', deleteTrack);

progresses.push(0);
promises.push(track.computeStats().progress(function (progress) {
progresses[idx + 1] = progress.progress;
progress.progress = progresses;
$.State.updateComputing(progress);
}));
promises.push(track.computeStats().progress($.State.updateComputing));

startMarker = marker;
});

$.each(promises, function () {
this.done(() => $.State.updateComputing({}));
});

$.when.apply($, promises).done(function () {
$.Track.eachRoute(function (i, route) {
route.setStyle({ opacity: 0.75 });
Expand Down Expand Up @@ -384,8 +375,8 @@ window.onload = function () {
icon: 'fa-trash',
title: 'Effacer l\'itinéraire',
onClick: function (btn, map) {
$.State.setComputing(true);
$.Track.clear();
$.State.triggerMarkersChanged();
$.State.setComputing(false);
},
}, {
Expand Down Expand Up @@ -474,22 +465,20 @@ window.onload = function () {

$('body').on('map2gpx:computingchange', function (e) {
if (e.computing) {
$.State.updateComputing({ progress: 0, status: 'Calculs en cours...' });
$.State.updateComputing({ start: true, total: 1, status: 'Calculs en cours...' });
$('#data-computing').fadeIn();
} else {
$.State.updateComputing({ progress: 1, status: 'Finalisation...' });
$.State.updateComputing({ end: true, status: 'Finalisation...' });
$.Chart.replot();
$('#data-computing').fadeOut();
}
});

function addMarker(e) {
if ($.State.getMode() === null || $.State.getComputing()) {
if ($.State.getMode() === null) {
return;
}

$.State.setComputing(true);

const marker = L.Marker.routed(e.latlng.roundE8(), {
riseOnHover: true,
draggable: true,
Expand All @@ -500,9 +489,6 @@ window.onload = function () {

marker.add().progress($.State.updateComputing).done(function () {
marker.setOpacity(1);
$.State.setComputing(false);
}).fail(function () {
$.State.setComputing(false);
});

if (!isSmallScreen) {
Expand Down
43 changes: 38 additions & 5 deletions src/js/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
var _mode = null;
var _computing = false;

var _progress = 0;
var _total = 0;

const $h2 = $('#data-computing h2');
const $progress = $('#data-computing-progress');
const $status = $('#data-computing-status');
const $pending = $('#data-computing-pending');

$.State = {};

Expand All @@ -14,21 +18,50 @@
};

$.State.setComputing = function (computing) {
if (computing && !_computing) {
_progress = 0;
_total = 0;
}

_computing = computing;
$('body').trigger($.Event('map2gpx:computingchange', { mode: _mode, computing: _computing }));
};

$.State.updateComputing = function (progress) {
let p = progress.progress;
if (Array.isArray(progress.progress))
p = p.reduce((a, b) => a + b) / p.length;
$.State.updateComputing = function (progress, display = true) {
if (Array.isArray(progress)) {
$.each(progress, function () {
$.State.updateComputing(this, false);
});

$.State.displayComputing();

return;
}

if (progress.start) {
_total += progress.total;
} else if (progress.end) {
_progress = _total;
} else {
_progress++;
}

$progress.text(Math.round(p * 100) + '%');
if ('status' in progress && progress.status)
$status.text(progress.status);
if ('step' in progress && progress.step)
$('<div><small>' + progress.step + '</small></div>').insertAfter($h2).fadeOut(400, function () {this.remove();});

if (display)
$.State.displayComputing();
};

$.State.displayComputing = function () {
let p = 1;
if (_total > 0)
p = _progress / _total;

$progress.text(Math.round(p * 100) + '%');

if (Math.round(p * 100) == 42)
$('<div><small>La grande question sur la vie, l\'univers et le reste répondue</small></div>').insertAfter($h2).fadeOut(400, function () {this.remove();});
};
Expand Down
6 changes: 2 additions & 4 deletions src/js/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,11 @@ L.Layer.include({
const promises = _this._fetchAltitude().concat(_this._fetchSlope());
const total = promises.length;

deferred.notify({ progress: 0, status: 'Récupération des données géographiques...' });
deferred.notify({ start: true, total: total, status: 'Récupération des données géographiques...' });

var i = 0;
$.each(promises, function () {
this.done(function () {
i++;
deferred.notify({ progress: i / (total + 1), step: this.size + ' points récupérés' });
deferred.notify({ step: this.size + ' points récupérés' });
});
});

Expand Down
Loading

0 comments on commit 6edd382

Please sign in to comment.