make placeholder-finding code consistent across container types

This commit is contained in:
Greg Johnston 2024-04-20 07:08:51 -04:00
parent b53e4d8ff8
commit 464f157186
4 changed files with 24 additions and 38 deletions

View file

@ -1,4 +1,7 @@
use crate::renderer::Renderer;
use crate::{
renderer::{CastFrom, Renderer},
view::{Position, PositionState},
};
use std::{cell::RefCell, rc::Rc};
#[derive(Debug)]
@ -48,4 +51,15 @@ where
pub fn set(&self, node: R::Node) {
*self.0.borrow_mut() = node;
}
pub fn next_placeholder(&self, position: &PositionState) -> R::Placeholder {
if position.get() == Position::FirstChild {
self.child();
} else {
self.sibling();
}
let marker = self.current();
position.set(Position::NextChild);
R::Placeholder::cast_from(marker).unwrap()
}
}

View file

@ -184,10 +184,8 @@ where
Either::Right(right.hydrate::<FROM_SERVER>(cursor, position))
}
};
cursor.sibling();
let marker = cursor.current().to_owned();
let marker = Rndr::Placeholder::cast_from(marker).unwrap();
position.set(Position::NextChild);
let marker = cursor.next_placeholder(position);
EitherState { state, marker }
}
}
@ -309,10 +307,8 @@ where
}
});
cursor.sibling();
let marker = cursor.current().to_owned();
let marker = Rndr::Placeholder::cast_from(marker).unwrap();
position.set(Position::NextChild);
let marker = cursor.next_placeholder(position);
EitherKeepAliveState {
showing_b,
a,
@ -512,10 +508,7 @@ macro_rules! tuples {
let state = match self {
$([<EitherOf $num>]::$ty(this) => [<EitherOf $num>]::$ty(this.hydrate::<FROM_SERVER>(cursor, position)),)*
};
cursor.sibling();
let marker = cursor.current().to_owned();
let marker = Rndr::Placeholder::cast_from(marker).unwrap();
position.set(Position::NextChild);
let marker = cursor.next_placeholder(position);
Self::State { marker, state }
}
}

View file

@ -184,15 +184,7 @@ where
.map(|s| s.hydrate::<FROM_SERVER>(cursor, position))
.map_err(|e| any_error::throw(e.into()));
// pull the placeholder
if position.get() == Position::FirstChild {
cursor.child();
} else {
cursor.sibling();
}
let placeholder = cursor.current().to_owned();
let placeholder = R::Placeholder::cast_from(placeholder).unwrap();
position.set(Position::NextChild);
let placeholder = cursor.next_placeholder(position);
ResultState { placeholder, state }
}

View file

@ -105,15 +105,7 @@ where
// hydrate the state, if it exists
let state = self.map(|s| s.hydrate::<FROM_SERVER>(cursor, position));
// pull the placeholder
if position.get() == Position::FirstChild {
cursor.child();
} else {
cursor.sibling();
}
let placeholder = cursor.current().to_owned();
let placeholder = R::Placeholder::cast_from(placeholder).unwrap();
position.set(Position::NextChild);
let placeholder = cursor.next_placeholder(position);
OptionState { placeholder, state }
}
@ -327,13 +319,8 @@ where
.map(|child| child.hydrate::<FROM_SERVER>(cursor, position))
.collect();
if position.get() == Position::FirstChild {
cursor.child();
} else {
cursor.sibling();
}
let marker = cursor.current().to_owned();
let marker = R::Placeholder::cast_from(marker).unwrap();
let marker = cursor.next_placeholder(position);
VecState { states, marker }
}
}