Skip to content

Commit

Permalink
demo: Make auto-update more robust
Browse files Browse the repository at this point in the history
Instead of terminating the loop when encountering an error, handle it
and wait a little longer for the next request. Also we now display the
last time the module got updated which makes it easier to confirm that
the update flow is working.
  • Loading branch information
zeux committed Aug 19, 2024
1 parent 269f466 commit 181192d
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions demo/simplify.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
simplify();
},
autoUpdate: false,
autoUpdateStatus: '',
};

var gui = new GUI({ width: 300 });
Expand All @@ -115,6 +116,7 @@
guiLoad.add(settings, 'loadFile');
guiLoad.add(settings, 'updateModule');
guiLoad.add(settings, 'autoUpdate').onChange(autoReload);
guiLoad.add(settings, 'autoUpdateStatus').listen();

var guiStats = gui.addFolder('Stats');
guiStats.add(settings.stats, 'triangles').listen();
Expand Down Expand Up @@ -333,17 +335,22 @@
function autoReload() {
if (!settings.autoUpdate) return;

var simph = fetch('/js/meshopt_simplifier.module.js?x=' + Date.now(), { method: 'HEAD' });
simph.then(function (r) {
var last = r.headers.get('Last-Modified');
if (last != moduleLastModified) {
moduleLastModified = last;
reload();
simplify();
}
fetch('/js/meshopt_simplifier.module.js?x=' + Date.now(), { method: 'HEAD' })
.then(function (r) {
var last = r.headers.get('Last-Modified');
if (last != moduleLastModified) {
moduleLastModified = last;
reload();
simplify();
}

setTimeout(autoReload, 1000);
});
settings.autoUpdateStatus = new Date(last).toLocaleTimeString();
setTimeout(autoReload, 1000);
})
.catch(function (e) {
settings.autoUpdateStatus = 'error';
setTimeout(autoReload, 5000);
});
}

function update() {
Expand Down

0 comments on commit 181192d

Please sign in to comment.