js & rust code for key inputs

This commit is contained in:
Scott Richmond 2025-07-05 23:33:39 -04:00
parent d39113d755
commit 4f9ebdaeb1
5 changed files with 29 additions and 11 deletions

View File

@ -85,11 +85,16 @@ function io_poller () {
outbox = []
}
if (ready && running) {
if (keys_down.size > 0) bundle_keys()
worker.postMessage(outbox)
outbox = []
}
}
function bundle_keys () {
outbox.push({verb: "Keys", data: keys_down})
}
function start_io_polling () {
io_interval_id = setInterval(io_poller, 100)
}
@ -102,17 +107,19 @@ export function run (source) {
return "TODO: handle this? should not be running"
}
// start the vm
// wrapping the Run message in an array for the worker
worker.postMessage([{verb: "Run", data: source}])
// reset all my state
// update state for this run
code = source
running = true
// reset the rest of my state
outbox = []
ludus_console = ""
ludus_commands = []
ludus_result = null
code = source
running = true
ready = false
keys_down = new Set();
// start the polling loop loop
// start the polling loop
start_io_polling()
}

View File

@ -403,7 +403,7 @@ function __wbg_get_imports() {
_assertBoolean(ret);
return ret;
};
imports.wbg.__wbindgen_closure_wrapper8073 = function() { return logError(function (arg0, arg1, arg2) {
imports.wbg.__wbindgen_closure_wrapper8075 = function() { return logError(function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 354, __wbg_adapter_20);
return ret;
}, arguments) };

BIN
pkg/rudus_bg.wasm (Stored with Git LFS)

Binary file not shown.

View File

@ -34,7 +34,7 @@ pub enum MsgIn {
Input(String),
Fetch(String, f64, String),
Kill,
Keyboard(Vec<String>),
Key(Vec<String>),
}
impl std::fmt::Display for MsgIn {
@ -64,12 +64,12 @@ impl MsgIn {
Value::tuple(vec![url, result_tuple])
}
MsgIn::Kill => Value::Nothing,
MsgIn::Keyboard(downkeys) => {
MsgIn::Key(downkeys) => {
let mut vector = Vector::new();
for key in downkeys {
vector.push_back(Value::String(Rc::new(key)));
}
Value::List(Box::new(vector))
Value::list(vector)
}
}
}

View File

@ -255,6 +255,7 @@ pub struct Buffers {
fetch_out: Value,
fetch_in: Value,
input: Value,
keys_down: Value,
}
impl Buffers {
@ -265,6 +266,7 @@ impl Buffers {
fetch_out: prelude.get(&Key::Keyword("fetch_outbox")).unwrap().clone(),
fetch_in: prelude.get(&Key::Keyword("fetch_inbox")).unwrap().clone(),
input: prelude.get(&Key::Keyword("input")).unwrap().clone(),
keys_down: prelude.get(&Key::Keyword("keys_down")).unwrap().clone(),
}
}
@ -288,6 +290,10 @@ impl Buffers {
self.fetch_in.as_box()
}
pub fn keys_down (&self) -> Rc<RefCell<Value>> {
self.keys_down.as_box()
}
}
#[derive(Debug, Clone, PartialEq)]
@ -465,13 +471,18 @@ impl World {
inbox_rc.replace(reply);
}
fn register_keys(&mut self, keys: Value) {
let keys_down_rc = self.buffers.keys_down();
keys_down_rc.replace(keys);
}
fn fill_buffers(&mut self, inbox: Vec<MsgIn>) {
for msg in inbox {
match msg {
MsgIn::Input(str) => self.fill_input(str),
MsgIn::Kill => self.kill_signal = true,
MsgIn::Fetch(..) => self.fetch_reply(msg.into_value()),
_ => todo!()
MsgIn::Key(..) => self.register_keys(msg.into_value()),
}
}
}