Skip to content

Commit be23724

Browse files
committed
fix!: render_context position problems in impl CsrElement for SyncedCollection
1 parent f040a29 commit be23724

File tree

3 files changed

+75
-340
lines changed

3 files changed

+75
-340
lines changed

packages/frender-csr/src/render.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ pub trait RenderContext {
1414
fn renderer_mut(&mut self) -> &mut Self::Renderer;
1515
fn log_cursor(&mut self);
1616
fn mark_cursor_skipped(&mut self);
17+
18+
// TODO: remove
19+
fn map_mut_cloned_render_context<Res>(
20+
&mut self,
21+
f: impl FnOnce(&mut <Self::Renderer as RenderWithContext>::RenderContext<'_>) -> Res,
22+
) -> Res;
1723
}

packages/frender-dom/src/csr/web.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ pub struct RenderContext<'a, R: ?Sized> {
169169
}
170170

171171
impl<'a> Cursor<'a> {
172+
fn cloned(&self) -> Self {
173+
Cursor {
174+
position: match &self.position {
175+
CursorPosition::After(cow) => CursorPosition::After(cow.clone()),
176+
CursorPosition::FirstChildOf(cow) => CursorPosition::FirstChildOf(cow.clone()),
177+
},
178+
skipped: self.skipped,
179+
}
180+
}
181+
172182
pub fn cursor_is_at_node(&self, node: &web_sys::Node) -> bool {
173183
match &self.position {
174184
CursorPosition::FirstChildOf(parent) => parent.first_child(),
@@ -195,14 +205,13 @@ impl<'a> Cursor<'a> {
195205
}
196206

197207
pub fn readd_node(&mut self, node: &web_sys::Node, force_reposition: bool) {
198-
if force_reposition {
199-
// #[cfg(debug_assertions)]
200-
// if self.skipped {
201-
// web_sys::console::warn_1(
202-
// &"Dom renderer's cursor can not be skipped when moving node".into(),
203-
// );
204-
// }
208+
if self.skipped {
209+
const MSG: &str = "Dom renderer's cursor can not be skipped when moving node";
210+
web_sys::console::warn_1(&MSG.into());
211+
panic!("{}", MSG)
212+
}
205213

214+
if force_reposition {
206215
match &self.position {
207216
CursorPosition::FirstChildOf(parent) => {
208217
// web_sys::console::log_2(&"FirstChildOf".into(), parent);
@@ -235,6 +244,7 @@ impl<'a> Cursor<'a> {
235244
&"But the cursor is at:".into(),
236245
);
237246
self.log_self();
247+
panic!("cursor is not at node");
238248
}
239249
}
240250

@@ -256,6 +266,16 @@ impl<'a, R: ?Sized + Renderer> crate::render::RenderContext for RenderContext<'a
256266
f(self)
257267
}
258268

269+
fn map_mut_cloned_render_context<Res>(
270+
&mut self,
271+
f: impl FnOnce(&mut <Self::Renderer as RenderWithContext>::RenderContext<'_>) -> Res,
272+
) -> Res {
273+
let cursor = self.cursor.cloned();
274+
let out = f(self);
275+
*self.cursor = cursor;
276+
out
277+
}
278+
259279
fn renderer_mut(&mut self) -> &mut Self::Renderer {
260280
self.renderer
261281
}

0 commit comments

Comments
 (0)