From 3492c24cc533e9088e8b12f929f5d922c8787cba Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Wed, 4 May 2022 01:59:39 -0700 Subject: web: move opt logic to c --- web/static/dartboat.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'web/static') diff --git a/web/static/dartboat.js b/web/static/dartboat.js index eaf76da..70e4677 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -7,6 +7,8 @@ function scheduleCCall(f, ms, ...args) { } function toCString(str) { // caller must free + if (!str) return 0; + const size = lengthBytesUTF8(str) + 1; const ptr = _malloc(size); stringToUTF8(str, ptr, size); @@ -90,21 +92,12 @@ function promptHandle(command, data) { if (str_d) _free(str_d); } -function setOpt(opt, val, update_opts = true) { - if (update_opts) localStorage.setItem(`dartboat_${opt}`, val); - - if (opt === 'delay' || opt === 'stdev') - Module[`_set_${opt}`](Number(val)); +function readOpt(opt) { // caller must free + return toCString(localStorage.getItem(UTF8ToString(opt))); } -function readOpts() { - let val; - - if ((val = localStorage.getItem('dartboat_delay'))) - setOpt('delay', val, false); - - if ((val = localStorage.getItem('dartboat_stdev'))) - setOpt('stdev', val, false); +function storeOpt(opt, val) { + localStorage.setItem(UTF8ToString(opt), UTF8ToString(val)); } function boatAfloat() { @@ -121,7 +114,8 @@ document.addEventListener('DOMContentLoaded', () => { promptHandle(...x.split(':', 2))); $$('[data-command]').forEach(x => x.addEventListener('click', f)); - f = e => setOpt(e.target.dataset.setting, e.target.value); + f = e => _set_opt(toCString(e.target.dataset.setting), + toCString(e.target.value)); $$('[data-setting]').forEach(x => x.addEventListener('change', f)); f = e => $(`#${e.target.dataset.modal}`).style.display = 'block'; -- cgit v1.2.3-70-g09d2