dioxus_fullstack_hooks/
history.rs1use std::cell::OnceCell;
4
5use dioxus_core::{queue_effect, schedule_update};
6use dioxus_fullstack_protocol::is_hydrating;
7use dioxus_history::History;
8
9fn match_hydration<O>(
12 during_hydration: impl FnOnce() -> O,
13 after_hydration: impl FnOnce() -> O,
14) -> O {
15 if is_hydrating() {
16 let update = schedule_update();
17 queue_effect(move || update());
18 during_hydration()
19 } else {
20 after_hydration()
21 }
22}
23
24#[derive(Clone)]
26pub struct FullstackHistory<H> {
27 initial_route: OnceCell<String>,
28 #[cfg(feature = "server")]
29 in_hydration_context: std::cell::Cell<bool>,
30 history: H,
31}
32
33impl<H> FullstackHistory<H> {
34 pub fn new(history: H) -> Self {
36 Self {
37 initial_route: OnceCell::new(),
38 #[cfg(feature = "server")]
39 in_hydration_context: std::cell::Cell::new(false),
40 history,
41 }
42 }
43
44 pub fn new_server(history: H) -> Self
46 where
47 H: History,
48 {
49 let initial_route = history.current_route();
50 let history = Self::new(history);
51 history.initial_route.set(initial_route).unwrap();
52 history
53 }
54
55 fn initial_route(&self) -> String {
57 let entry = dioxus_fullstack_protocol::serialize_context().create_entry();
58 let route = self.initial_route.get_or_init(|| {
59 entry
60 .get()
61 .expect("Failed to get initial route from hydration context")
62 });
63 #[cfg(feature = "server")]
64 if !self.in_hydration_context.get() {
65 entry.insert(route, std::panic::Location::caller());
66 self.in_hydration_context.set(true);
67 }
68 route.clone()
69 }
70}
71
72impl<H: History> History for FullstackHistory<H> {
73 fn current_prefix(&self) -> Option<String> {
74 self.history.current_prefix()
75 }
76
77 fn can_go_back(&self) -> bool {
78 match_hydration(|| false, || self.history.can_go_back())
79 }
80
81 fn can_go_forward(&self) -> bool {
82 match_hydration(|| false, || self.history.can_go_forward())
83 }
84
85 fn external(&self, url: String) -> bool {
86 self.history.external(url)
87 }
88
89 fn updater(&self, callback: std::sync::Arc<dyn Fn() + Send + Sync>) {
90 self.history.updater(callback)
91 }
92
93 fn include_prevent_default(&self) -> bool {
94 self.history.include_prevent_default()
95 }
96
97 fn current_route(&self) -> String {
98 match_hydration(|| self.initial_route(), || self.history.current_route())
99 }
100
101 fn go_back(&self) {
102 self.history.go_back();
103 }
104
105 fn go_forward(&self) {
106 self.history.go_forward();
107 }
108
109 fn push(&self, route: String) {
110 self.history.push(route);
111 }
112
113 fn replace(&self, path: String) {
114 self.history.replace(path);
115 }
116}