chore: continue to clean things up

This commit is contained in:
Jonathan Kelley 2022-12-01 01:08:49 -05:00
parent 85657d3906
commit 11a45c8ef0
5 changed files with 23 additions and 50 deletions

View file

@ -131,11 +131,18 @@ impl<T: std::fmt::Debug> std::fmt::Debug for Event<T> {
/// }
///
/// ```
#[derive(Default)]
pub struct EventHandler<'bump, T = ()> {
pub(super) callback: RefCell<Option<ExternalListenerCallback<'bump, T>>>,
}
impl<T> Default for EventHandler<'_, T> {
fn default() -> Self {
Self {
callback: Default::default(),
}
}
}
type ExternalListenerCallback<'bump, T> = bumpalo::boxed::Box<'bump, dyn FnMut(T) + 'bump>;
impl<T> EventHandler<'_, T> {

View file

@ -489,8 +489,7 @@ impl VirtualDom {
match unsafe { self.run_scope(ScopeId(0)).extend_lifetime_ref() } {
// Rebuilding implies we append the created elements to the root
RenderReturn::Sync(Ok(node)) => {
let m = self.create_scope(ScopeId(0), node);
// self.mutations.push(Mutation::AppendChildren { m });
let _m = self.create_scope(ScopeId(0), node);
}
// If an error occurs, we should try to render the default error component and context where the error occured
RenderReturn::Sync(Err(e)) => panic!("Cannot catch errors during rebuild {:?}", e),

View file

@ -1,9 +1,7 @@
export function main() {
let root = window.document.getElementById("main");
console.log("loading!");
if (root != null) {
window.interpreter = new Interpreter(root);
console.log("properly loaded!");
window.ipc.postMessage(serializeIpcMessage("initialize"));
}
}
@ -18,7 +16,6 @@ class ListenerMap {
}
create(event_name, element, handler, bubbles) {
console.log("creating listener for", event_name, element, handler, bubbles);
if (bubbles) {
if (this.global[event_name] === undefined) {
this.global[event_name] = {};
@ -81,7 +78,6 @@ export class Interpreter {
}
SaveTemplate(nodes, name) {
this.templates[name] = nodes;
console.log(this.templates);
}
MountToRoot() {
this.AppendChildren(this.stack.length - 1);
@ -131,8 +127,7 @@ export class Interpreter {
}
}
CreateRawText(text) {
const node = document.createTextNode(text);
this.stack.push(node);
this.stack.push(document.createTextNode(text));
}
CreateTextNode(text, root) {
const node = document.createTextNode(text);
@ -218,8 +213,6 @@ export class Interpreter {
}
}
handleEdits(edits) {
console.log("handling edits", edits, this.stack.length);
for (let edit of edits) {
this.handleEdit(edit);
}
@ -253,7 +246,6 @@ export class Interpreter {
this.stack.push(node);
}
handleEdit(edit) {
console.log(edit);
switch (edit.type) {
case "AppendChildren":
this.AppendChildren(edit.m);
@ -261,27 +253,9 @@ export class Interpreter {
case "AssignId":
this.AssignId(edit.path, edit.id);
break;
case "CreateElement":
this.CreateElement(edit.name);
break;
case "CreateElementNs":
this.CreateElementNs(edit.name, edit.namespace);
break;
case "CreatePlaceholder":
this.CreatePlaceholder(edit.id);
break;
case "CreateStaticText":
this.CreateStaticText(edit.value)
break;
case "CreateStaticPlaceholder":
this.CreateStaticPlaceholder();
break;
case "CreateTextPlaceholder":
this.CreateTextPlaceholder();
break;
case "CreateStaticText":
this.CreateRawText(edit.value);
break;
case "CreateTextNode":
this.CreateTextNode(edit.value);
break;
@ -291,9 +265,6 @@ export class Interpreter {
case "LoadTemplate":
this.LoadTemplate(edit.name, edit.index, edit.id);
break;
case "SaveTemplate":
this.SaveTemplate(edit.name, edit.m);
break;
case "PushRoot":
this.PushRoot(edit.id);
break;
@ -318,15 +289,9 @@ export class Interpreter {
case "SetAttribute":
this.SetAttribute(edit.id, edit.name, edit.value, edit.ns);
break;
case "SetStaticAttribute":
this.SetStaticAttribute(edit.name, edit.value, edit.ns);
break;
case "SetBoolAttribute":
this.SetAttribute(edit.id, edit.name, edit.value, edit.ns);
break;
case "SetInnerText":
console.log("Set inner text?");
break;
case "RemoveAttribute":
this.RemoveAttribute(edit.id, edit.name, edit.ns);
break;
@ -745,8 +710,6 @@ function is_element_node(node) {
}
function event_bubbles(event) {
console.log("event_bubbles", event);
switch (event) {
case "copy":
return true;

View file

@ -64,8 +64,9 @@ impl WebsysDom {
}
fn create_template_node(&self, v: &TemplateNode) -> web_sys::Node {
use TemplateNode::*;
match v {
TemplateNode::Element {
Element {
tag,
namespace,
attrs,
@ -86,6 +87,12 @@ impl WebsysDom {
} = attr
{
match namespace {
Some(ns) if *ns == "style" => el
.dyn_ref::<HtmlElement>()
.unwrap()
.style()
.set_property(name, value)
.unwrap(),
Some(ns) => el.set_attribute_ns(Some(ns), name, value).unwrap(),
None => el.set_attribute(name, value).unwrap(),
}
@ -99,9 +106,9 @@ impl WebsysDom {
el.dyn_into().unwrap()
}
TemplateNode::Text(t) => self.document.create_text_node(t).dyn_into().unwrap(),
TemplateNode::DynamicText(_) => self.document.create_text_node("p").dyn_into().unwrap(),
TemplateNode::Dynamic(_) => {
Text(t) => self.document.create_text_node(t).dyn_into().unwrap(),
DynamicText(_) => self.document.create_text_node("p").dyn_into().unwrap(),
Dynamic(_) => {
let el = self.document.create_element("pre").unwrap();
el.toggle_attribute("hidden");
el.dyn_into().unwrap()
@ -112,7 +119,6 @@ impl WebsysDom {
pub fn apply_edits(&mut self, mut edits: Vec<Mutation>) {
use Mutation::*;
let i = &self.interpreter;
for edit in edits.drain(..) {
match edit {
AssignId { path, id } => i.AssignId(path, id.0 as u32),
@ -145,9 +151,6 @@ impl WebsysDom {
RemoveEventListener { name, id } => i.RemoveEventListener(name, id.0 as u32),
Remove { id } => i.Remove(id.0 as u32),
PushRoot { id } => i.PushRoot(id.0 as u32),
// Mutation::RemoveEventListener { root, name: event } => self
// .interpreter
// .RemoveEventListener(root, event, event_bubbles(event)),
}
}
}

View file

@ -197,6 +197,8 @@ pub async fn run_with_props<T: 'static>(root: fn(Scope<T>) -> Element, root_prop
websys_dom.load_templates(&edits.templates);
websys_dom.apply_edits(edits.edits);
// the mutations come back with nothing - we need to actually mount them
websys_dom.mount();
let mut work_loop = ric_raf::RafLoop::new();
@ -243,7 +245,6 @@ pub async fn run_with_props<T: 'static>(root: fn(Scope<T>) -> Element, root_prop
let deadline = work_loop.wait_for_idle_time().await;
// run the virtualdom work phase until the frame deadline is reached
// let deadline = gloo_timers::future::sleep(Duration::from_millis(10000));
let edits = dom.render_with_deadline(deadline).await;
// wait for the animation frame to fire so we can apply our changes