Closed buchanae closed 1 month ago
Is this the "right" way to do this? I'm unsure exactly which globals to keep. Is it reasonable to recreate the root HandleScope and ContextScope in trigger_event?
Yes. You will have to recreate HandleScope each time you want to invoke a listener. You should be able to skip grabbing the context and ContextScope part though.
You can actually store only the callbacks you are interested in using v8::Global<v8::Function>
instead of looking them up from the global scope each time like here:
let global = ctx.global(&mut scope);
let invoke_key = v8::String::new(&mut scope, "__invoke").unwrap().into();
let invoke: v8::Local<v8::Function> = global.get(&mut scope, invoke_key).unwrap().try_into().unwrap();
Thanks, good to know I'm on the right track.
You should be able to skip grabbing the context and ContextScope part though.
I'm not sure what you mean. Most functions seem to require a HandleScope<'_, v8::Context>
expected mutable reference `&mut HandleScope<'_, v8::Context>`
found mutable reference `&mut HandleScope<'_, ()>`
You're right. You can do this instead:
let scope = v8::HandleScope::with_context(isolate, &*self.context)
where self.context
is v8::Global<v8::Context>
I would like to store some event listeners and call them from rust, such as "cursor moved". I've written code like this:
JS:
Is this the "right" way to do this? I'm unsure exactly which globals to keep. Is it reasonable to recreate the root
HandleScope
andContextScope
intrigger_event
?I originally explored other options, such as trying to store a list of listener callback function handles in Rust memory, but I couldn't figure that out, so I ended up defining
__invoke
andlisteners
in Javascript. I imagine something like the DOM has a better way though.Thanks!