Skip to content

Commit

Permalink
reduce boilerplace code with more enum constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Oct 15, 2024
1 parent 02166dc commit d8c8afe
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 111 deletions.
5 changes: 1 addition & 4 deletions src/lib/dialog/dialogs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ pub fn comp_esc_listener[T](
Keydown,
fn(e, dispatch) {
match e {
@node.RespoEvent::Keyboard(~key, ..) =>
if key == "Escape" {
on_close(dispatch)
}
Keyboard(~key, ..) => if key == "Escape" { on_close(dispatch) }
_ => ()
}
},
Expand Down
16 changes: 8 additions & 8 deletions src/lib/node/alias.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn div[T](
~style : RespoStyle = RespoStyle::new(),
children : Array[RespoNode[T]]
) -> RespoNode[T] {
RespoNode::Element(
Element(
{
name: "div",
attrs,
Expand All @@ -31,7 +31,7 @@ pub fn div_listed[T](
~style : RespoStyle = RespoStyle::new(),
children : Array[(RespoIndexKey, RespoNode[T])]
) -> RespoNode[T] {
RespoNode::Element({ name: "div", attrs, event, style, children })
Element({ name: "div", attrs, event, style, children })
}

pub fn span[T](
Expand All @@ -43,7 +43,7 @@ pub fn span[T](
~style : RespoStyle = RespoStyle::new(),
children : Array[RespoNode[T]]
) -> RespoNode[T] {
RespoNode::Element(
Element(
{
name: "span",
attrs,
Expand All @@ -64,7 +64,7 @@ pub fn button[T](
] = {},
~style : RespoStyle = RespoStyle::new()
) -> RespoNode[T] {
RespoNode::Element({ name: "button", attrs, event, style, children: [] })
Element({ name: "button", attrs, event, style, children: [] })
}

pub fn input[T](
Expand All @@ -75,7 +75,7 @@ pub fn input[T](
] = {},
~style : RespoStyle = RespoStyle::new()
) -> RespoNode[T] {
RespoNode::Element({ name: "input", attrs, event, style, children: [] })
Element({ name: "input", attrs, event, style, children: [] })
}

pub fn textarea[T](
Expand All @@ -86,7 +86,7 @@ pub fn textarea[T](
] = {},
~style : RespoStyle = RespoStyle::new()
) -> RespoNode[T] {
RespoNode::Element({ name: "textarea", attrs, event, style, children: [] })
Element({ name: "textarea", attrs, event, style, children: [] })
}

pub fn br[T](
Expand All @@ -97,7 +97,7 @@ pub fn br[T](
] = {},
~style : RespoStyle = RespoStyle::new()
) -> RespoNode[T] {
RespoNode::Element({ name: "br", attrs, event, style, children: [] })
Element({ name: "br", attrs, event, style, children: [] })
}

pub fn space[T](~width : Int = 1, ~height : Int = 1) -> RespoNode[T] {
Expand All @@ -122,7 +122,7 @@ pub fn create_element[T](
~style : RespoStyle = RespoStyle::new(),
children : Array[RespoNode[T]]
) -> RespoNode[T] {
RespoNode::Element(
Element(
{
name,
attrs,
Expand Down
101 changes: 45 additions & 56 deletions src/lib/node/diff.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ pub fn diff_tree[T](
changes,
)
// crate::util::log!("compare elements: {:?} {:?}", new_child, old_child);
changes.val.push(
DomChange::ReplaceElement(~coord, ~dom_path, node=new_child),
)
changes.val.push(ReplaceElement(~coord, ~dom_path, node=new_child))
collect_effects_outside_in_as!(
new_tree,
coord,
dom_path,
RespoEffectType::Mounted,
Mounted,
changes,
)
}
Expand All @@ -64,15 +62,15 @@ pub fn diff_tree[T](
old_tree,
coord,
dom_path,
RespoEffectType::BeforeUnmount,
BeforeUnmount,
changes,
)
changes.val.push(DomChange::ReplaceElement(~coord, ~dom_path, node=a))
changes.val.push(ReplaceElement(~coord, ~dom_path, node=a))
collect_effects_outside_in_as!(
new_tree,
coord,
dom_path,
RespoEffectType::Mounted,
Mounted,
changes,
)
}
Expand All @@ -81,15 +79,15 @@ pub fn diff_tree[T](
old_tree,
coord,
dom_path,
RespoEffectType::BeforeUnmount,
BeforeUnmount,
changes,
)
changes.val.push(DomChange::ReplaceElement(~coord, ~dom_path, node=a))
changes.val.push(ReplaceElement(~coord, ~dom_path, node=a))
collect_effects_outside_in_as!(
new_tree,
coord,
dom_path,
RespoEffectType::Mounted,
Mounted,
changes,
)
}
Expand All @@ -107,15 +105,15 @@ pub fn diff_tree[T](
old_tree,
coord,
dom_path,
RespoEffectType::BeforeUnmount,
BeforeUnmount,
changes,
)
changes.val.push(DomChange::ReplaceElement(~coord, ~dom_path, node=a))
collect_effects_outside_in_as!(
new_tree,
coord,
dom_path,
RespoEffectType::Mounted,
Mounted,
changes,
)
} else {
Expand Down Expand Up @@ -281,28 +279,28 @@ fn diff_children[T](
return
} else {
let old_entry = old_children[old_tracking_pointer]
let child_coord = [RespoCoord::Key(old_entry.0)]
let child_coord = [Key(old_entry.0)]
let child_dom_path = [cursor]
nested_effects_inside_out_as!(
old_entry.1,
child_coord,
child_dom_path,
RespoEffectType::BeforeUnmount,
BeforeUnmount,
operations,
)
operations.val.push(ChildDomOp::RemoveAt(cursor))
operations.val.push(RemoveAt(cursor))
old_tracking_pointer += 1
}
} else if old_tracking_pointer >= old_children.length() {
let (new_key, new_child) = new_children[new_tracking_pointer]
operations.val.push(ChildDomOp::Append(new_key, new_child))
let child_coord = [RespoCoord::Key(new_key)]
operations.val.push(Append(new_key, new_child))
let child_coord = [Key(new_key)]
let child_dom_path = [cursor]
nested_effects_outside_in_as!(
new_child,
child_coord,
child_dom_path,
RespoEffectType::Mounted,
Mounted,
operations,
)
new_tracking_pointer += 1
Expand All @@ -311,7 +309,7 @@ fn diff_children[T](
let old_entry = old_children[old_tracking_pointer]
if new_entry.0 == old_entry.0 {
let next_coord = coord.copy()
next_coord.push(RespoCoord::Key(new_entry.0))
next_coord.push(Key(new_entry.0))
let next_dom_path = dom_path.copy()
next_dom_path.push(cursor)
diff_tree!(new_entry.1, old_entry.1, next_coord, next_dom_path, changes)
Expand All @@ -323,64 +321,60 @@ fn diff_children[T](
Some(new_entry.0) == old_children.get(old_tracking_pointer + 2).map(fst) ||
Some(new_entry.0) == old_children.get(old_tracking_pointer + 3).map(fst) {
// look ahead for 3 entries, if still not found, regards this as a remove
let child_coord = [RespoCoord::Key(old_entry.0)]
let child_coord = [Key(old_entry.0)]
let child_dom_path = [cursor]
nested_effects_inside_out_as!(
old_entry.1,
child_coord,
child_dom_path,
RespoEffectType::BeforeUnmount,
BeforeUnmount,
operations,
)
operations.val.push(ChildDomOp::RemoveAt(cursor))
operations.val.push(RemoveAt(cursor))
old_tracking_pointer += 1
} else if Some(old_entry.0) ==
new_children.get(new_tracking_pointer + 1).map(fst) ||
Some(old_entry.0) == new_children.get(new_tracking_pointer + 2).map(fst) ||
Some(old_entry.0) == new_children.get(new_tracking_pointer + 3).map(fst) {
if cursor == 0 {
operations.val.push(ChildDomOp::Prepend(new_entry.0, new_entry.1))
operations.val.push(Prepend(new_entry.0, new_entry.1))
} else {
operations.val.push(
ChildDomOp::InsertAfter(cursor - 1, new_entry.0, new_entry.1),
)
operations.val.push(InsertAfter(cursor - 1, new_entry.0, new_entry.1))
}
let child_coord = [RespoCoord::Key(new_entry.0)]
let child_coord = [Key(new_entry.0)]
let child_dom_path = [cursor]
nested_effects_outside_in_as!(
new_entry.1,
child_coord,
child_dom_path,
RespoEffectType::Mounted,
Mounted,
operations,
)
cursor += 1
new_tracking_pointer += 1
} else {
let child_coord = [RespoCoord::Key(old_entry.0)]
let child_coord = [Key(old_entry.0)]
let child_dom_path = [cursor]
nested_effects_inside_out_as!(
old_entry.1,
child_coord,
child_dom_path,
RespoEffectType::BeforeUnmount,
BeforeUnmount,
operations,
)
operations.val.push(ChildDomOp::RemoveAt(cursor))
operations.val.push(RemoveAt(cursor))
if cursor == 0 {
operations.val.push(ChildDomOp::Prepend(new_entry.0, new_entry.1))
operations.val.push(Prepend(new_entry.0, new_entry.1))
} else {
operations.val.push(
ChildDomOp::InsertAfter(cursor - 1, new_entry.0, new_entry.1),
)
operations.val.push(InsertAfter(cursor - 1, new_entry.0, new_entry.1))
}
let child_coord = [RespoCoord::Key(new_entry.0)]
let child_coord = [Key(new_entry.0)]
let child_dom_path = [cursor]
nested_effects_outside_in_as!(
new_entry.1,
child_coord,
child_dom_path,
RespoEffectType::Mounted,
Mounted,
operations,
)
cursor += 1
Expand All @@ -400,38 +394,33 @@ pub fn collect_effects_outside_in_as[T](
changes : Ref[Array[DomChange[T]]]
) -> Unit!RespoCommonError {
match tree {
RespoNode::Component(left) => {
Component(left) => {
let { name, effects, tree } = left
if not(effects.is_empty()) {
changes.val.push(
DomChange::Effect(
~coord,
~dom_path,
~effect_type,
skip_indexes=@hashset.new(),
),
Effect(~coord, ~dom_path, ~effect_type, skip_indexes=@hashset.new()),
)
}
let next_coord = coord.copy()
next_coord.push(RespoCoord::Comp(name))
next_coord.push(Comp(name))
collect_effects_outside_in_as!(
tree, next_coord, dom_path, effect_type, changes,
)
}
RespoNode::Element(right) => {
Element(right) => {
let { children, .. } = right
for idx, pair in children.iter2() {
let (k, child) = pair
let next_coord = coord.copy()
next_coord.push(RespoCoord::Key(RespoIndexKey(k.to_string())))
next_coord.push(Key(RespoIndexKey(k.to_string())))
let next_dom_path = dom_path.copy()
next_dom_path.push(idx.reinterpret_as_uint())
collect_effects_outside_in_as!(
child, next_coord, next_dom_path, effect_type, changes,
)
}
}
RespoNode::Referenced(cell) =>
Referenced(cell) =>
collect_effects_outside_in_as!(
cell, coord, dom_path, effect_type, changes,
)
Expand All @@ -450,7 +439,7 @@ pub fn collect_effects_inside_out_as[T](
RespoNode::Component(left) => {
let { name, effects, tree } = left
let next_coord = coord.copy()
next_coord.push(RespoCoord::Comp(name))
next_coord.push(Comp(name))
collect_effects_inside_out_as!(
tree, next_coord, dom_path, effect_type, changes,
)
Expand All @@ -471,7 +460,7 @@ pub fn collect_effects_inside_out_as[T](
for pair in children.iter() {
let (k, child) = pair
let next_coord = coord.copy()
next_coord.push(RespoCoord::Key(k))
next_coord.push(Key(k))
let next_dom_path = dom_path.copy()
next_dom_path.push(idx.reinterpret_as_uint())
collect_effects_inside_out_as!(
Expand Down Expand Up @@ -500,7 +489,7 @@ fn nested_effects_outside_in_as[T](
let { name, effects, tree } = left
if not(effects.is_empty()) {
operations.val.push(
ChildDomOp::NestedEffect(
NestedEffect(
nested_coord=coord,
nested_dom_path=dom_path,
~effect_type,
Expand All @@ -509,7 +498,7 @@ fn nested_effects_outside_in_as[T](
)
}
let next_coord = coord.copy()
next_coord.push(RespoCoord::Comp(name))
next_coord.push(Comp(name))
nested_effects_outside_in_as!(
tree, next_coord, dom_path, effect_type, operations,
)
Expand All @@ -519,7 +508,7 @@ fn nested_effects_outside_in_as[T](
for pair in children {
let (k, child) = pair
let next_coord = coord.copy()
next_coord.push(RespoCoord::Key(k))
next_coord.push(Key(k))
nested_effects_outside_in_as!(
child, next_coord, dom_path, effect_type, operations,
)
Expand All @@ -544,13 +533,13 @@ fn nested_effects_inside_out_as[T](
Component(left) => {
let { name, effects, tree } = left
let next_coord = coord.copy()
next_coord.push(RespoCoord::Comp(name))
next_coord.push(Comp(name))
nested_effects_inside_out_as!(
tree, next_coord, dom_path, effect_type, operations,
)
if not(effects.is_empty()) {
operations.val.push(
ChildDomOp::NestedEffect(
NestedEffect(
nested_coord=coord,
nested_dom_path=dom_path,
~effect_type,
Expand All @@ -564,7 +553,7 @@ fn nested_effects_inside_out_as[T](
for pair in children {
let (k, child) = pair
let next_coord = coord.copy()
next_coord.push(RespoCoord::Key(k))
next_coord.push(Key(k))
nested_effects_inside_out_as!(
child, next_coord, dom_path, effect_type, operations,
)
Expand Down
Loading

0 comments on commit d8c8afe

Please sign in to comment.