1#![allow(unused)]
29use crate::{
30 context::ContextStack,
31 render::{ComponentDrawer, ComponentUpdater},
32};
33use std::{
34 any::Any,
35 pin::Pin,
36 task::{Context, Poll},
37};
38mod use_context;
39pub use use_context::*;
40mod use_events;
41pub use use_events::*;
42mod use_future;
43pub use use_future::*;
44mod use_state;
45pub use use_state::*;
46mod use_memo;
47pub use use_memo::*;
48mod use_effect;
49pub use use_effect::*;
50mod use_insert_before;
51pub use use_insert_before::*;
52mod use_size;
53pub use use_size::*;
54
55#[cfg(feature = "router")]
56mod use_router;
57#[cfg(feature = "router")]
58pub use use_router::*;
59
60pub trait Hook: Unpin + Send {
68 fn poll_change(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<()> {
69 Poll::Pending
70 }
71
72 fn pre_component_update(&mut self, _updater: &mut ComponentUpdater) {}
73 fn post_component_update(&mut self, _updater: &mut ComponentUpdater) {}
74
75 fn pre_component_draw(&mut self, _drawer: &mut ComponentDrawer) {}
76 fn post_component_draw(&mut self, _drawer: &mut ComponentDrawer) {}
77}
78
79pub(crate) trait AnyHook: Hook {
80 fn any_self_mut(&mut self) -> &mut dyn Any;
81}
82
83impl<T: Hook + 'static> AnyHook for T {
84 fn any_self_mut(&mut self) -> &mut dyn Any {
85 self
86 }
87}
88
89impl Hook for Vec<Box<dyn AnyHook>> {
90 fn poll_change(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll<()> {
91 let mut is_ready = false;
92 for hook in self.iter_mut() {
93 if Pin::new(&mut **hook).poll_change(_cx).is_ready() {
94 is_ready = true;
95 }
96 }
97
98 if is_ready {
99 Poll::Ready(())
100 } else {
101 Poll::Pending
102 }
103 }
104
105 fn pre_component_update(&mut self, _updater: &mut ComponentUpdater) {
106 for hook in self.iter_mut() {
107 hook.pre_component_update(_updater);
108 }
109 }
110
111 fn post_component_update(&mut self, _updater: &mut ComponentUpdater) {
112 for hook in self.iter_mut() {
113 hook.post_component_update(_updater);
114 }
115 }
116
117 fn pre_component_draw(&mut self, _updater: &mut ComponentDrawer) {
118 for hook in self.iter_mut() {
119 hook.pre_component_draw(_updater);
120 }
121 }
122
123 fn post_component_draw(&mut self, _updater: &mut ComponentDrawer) {
124 for hook in self.iter_mut() {
125 hook.post_component_draw(_updater);
126 }
127 }
128}
129
130pub struct Hooks<'a, 'b: 'a> {
142 hooks: &'a mut Vec<Box<dyn AnyHook>>,
143 first_update: bool,
144 hook_index: usize,
145 pub(crate) context: Option<&'a ContextStack<'b>>,
146}
147
148impl<'a> Hooks<'a, '_> {
149 pub(crate) fn new(hooks: &'a mut Vec<Box<dyn AnyHook>>, first_update: bool) -> Self {
150 Self {
151 hooks,
152 first_update,
153 hook_index: 0,
154 context: None,
155 }
156 }
157
158 pub fn with_context_stack<'c, 'd>(
159 &'c mut self,
160 context: &'c ContextStack<'d>,
161 ) -> Hooks<'c, 'd> {
162 Hooks {
163 hooks: self.hooks,
164 first_update: self.first_update,
165 hook_index: self.hook_index,
166 context: Some(context),
167 }
168 }
169
170 pub fn use_hook<F, H>(&mut self, f: F) -> &mut H
171 where
172 F: FnOnce() -> H,
173 H: Hook + Unpin + 'static,
174 {
175 if self.first_update {
176 self.hooks.push(Box::new(f()));
177 }
178 let idx = self.hook_index;
179 self.hook_index += 1;
180
181 self.hooks
182 .get_mut(idx)
183 .and_then(|hook| hook.any_self_mut().downcast_mut::<H>())
184 .expect("Hook type mismatch, ensure the hook is of the correct type")
185 }
186}