1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::html::AnyScope;
use crate::{Component, Html, Properties};
use scoped_tls_hkt::scoped_thread_local;
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
mod hooks;
pub use hooks::*;
use crate::html::Context;
pub use yew_macro::function_component;
scoped_thread_local!(static mut CURRENT_HOOK: HookState);
type Msg = Box<dyn FnOnce() -> bool>;
type ProcessMessage = Rc<dyn Fn(Msg, bool)>;
struct HookState {
counter: usize,
scope: AnyScope,
process_message: ProcessMessage,
hooks: Vec<Rc<RefCell<dyn std::any::Any>>>,
destroy_listeners: Vec<Box<dyn FnOnce()>>,
}
pub trait FunctionProvider {
type TProps: Properties + PartialEq;
fn run(props: &Self::TProps) -> Html;
}
pub struct FunctionComponent<T: FunctionProvider + 'static> {
_never: std::marker::PhantomData<T>,
hook_state: RefCell<HookState>,
message_queue: MsgQueue,
}
impl<T: FunctionProvider> fmt::Debug for FunctionComponent<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("FunctionComponent<_>")
}
}
impl<T> FunctionComponent<T>
where
T: FunctionProvider,
{
fn with_hook_state<R>(&self, f: impl FnOnce() -> R) -> R {
let mut hook_state = self.hook_state.borrow_mut();
hook_state.counter = 0;
CURRENT_HOOK.set(&mut *hook_state, f)
}
}
impl<T: 'static> Component for FunctionComponent<T>
where
T: FunctionProvider,
{
type Message = Box<dyn FnOnce() -> bool>;
type Properties = T::TProps;
fn create(ctx: &Context<Self>) -> Self {
let scope = AnyScope::from(ctx.link().clone());
let message_queue = MsgQueue::default();
Self {
_never: std::marker::PhantomData::default(),
message_queue: message_queue.clone(),
hook_state: RefCell::new(HookState {
counter: 0,
scope,
process_message: {
let scope = ctx.link().clone();
Rc::new(move |msg, post_render| {
if post_render {
message_queue.push(msg);
} else {
scope.send_message(msg);
}
})
},
hooks: vec![],
destroy_listeners: vec![],
}),
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
msg()
}
fn view(&self, ctx: &Context<Self>) -> Html {
self.with_hook_state(|| T::run(&*ctx.props()))
}
fn rendered(&mut self, ctx: &Context<Self>, _first_render: bool) {
for msg in self.message_queue.drain() {
ctx.link().send_message(msg);
}
}
fn destroy(&mut self, _ctx: &Context<Self>) {
let mut hook_state = self.hook_state.borrow_mut();
for hook in hook_state.destroy_listeners.drain(..) {
hook()
}
}
}
pub(crate) fn get_current_scope() -> Option<AnyScope> {
if CURRENT_HOOK.is_set() {
Some(CURRENT_HOOK.with(|state| state.scope.clone()))
} else {
None
}
}
#[derive(Clone, Default)]
struct MsgQueue(Rc<RefCell<Vec<Msg>>>);
impl MsgQueue {
fn push(&self, msg: Msg) {
self.0.borrow_mut().push(msg);
}
fn drain(&self) -> Vec<Msg> {
self.0.borrow_mut().drain(..).collect()
}
}
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct HookUpdater {
hook: Rc<RefCell<dyn std::any::Any>>,
process_message: ProcessMessage,
}
impl HookUpdater {
pub fn callback<T: 'static, F>(&self, cb: F)
where
F: FnOnce(&mut T) -> bool + 'static,
{
let internal_hook_state = self.hook.clone();
let process_message = self.process_message.clone();
let post_render = false;
process_message(
Box::new(move || {
let mut r = internal_hook_state.borrow_mut();
let hook: &mut T = r
.downcast_mut()
.expect("internal error: hook downcasted to wrong type");
cb(hook)
}),
post_render,
);
}
pub fn post_render<T: 'static, F>(&self, cb: F)
where
F: FnOnce(&mut T) -> bool + 'static,
{
let internal_hook_state = self.hook.clone();
let process_message = self.process_message.clone();
let post_render = true;
process_message(
Box::new(move || {
let mut hook = internal_hook_state.borrow_mut();
let hook: &mut T = hook
.downcast_mut()
.expect("internal error: hook downcasted to wrong type");
cb(hook)
}),
post_render,
);
}
}