Skip to content

Commit f3c8453

Browse files
authored
code comments for Scheduler and ComponentLink (#1274)
* code comments for Scheduler and ComponentLink * Fix typos, reformat code, address pr comments * resolve pull request comments
1 parent 69cfd6a commit f3c8453

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

yew/src/html/scope.rs

+36-9
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ impl<COMP: Component> Scope<COMP> {
162162
scheduler().push_comp(ComponentRunnableType::Destroy, Box::new(destroy));
163163
}
164164

165-
/// Send a message to the component
165+
/// Send a message to the component.
166+
///
167+
/// Please be aware that currently this method synchronously
168+
/// schedules a call to the [Component](Component) interface.
166169
pub fn send_message<T>(&self, msg: T)
167170
where
168171
T: Into<COMP::Message>,
@@ -172,14 +175,22 @@ impl<COMP: Component> Scope<COMP> {
172175

173176
/// Send a batch of messages to the component.
174177
///
175-
/// This is useful for reducing re-renders of the components because the messages are handled
176-
/// together and the view function is called only once if needed.
178+
/// This is useful for reducing re-renders of the components
179+
/// because the messages are handled together and the view
180+
/// function is called only once if needed.
181+
///
182+
/// Please be aware that currently this method synchronously
183+
/// schedules calls to the [Component](Component) interface.
177184
pub fn send_message_batch(&self, messages: Vec<COMP::Message>) {
178185
self.update(ComponentUpdate::MessageBatch(messages), false);
179186
}
180187

181-
/// Creates a `Callback` which will send a message to the linked component's
182-
/// update method when invoked.
188+
/// Creates a `Callback` which will send a message to the linked
189+
/// component's update method when invoked.
190+
///
191+
/// Please be aware that currently the result of this callback
192+
/// synchronously schedules a call to the [Component](Component)
193+
/// interface.
183194
pub fn callback<F, IN, M>(&self, function: F) -> Callback<IN>
184195
where
185196
M: Into<COMP::Message>,
@@ -193,8 +204,12 @@ impl<COMP: Component> Scope<COMP> {
193204
closure.into()
194205
}
195206

196-
/// Creates a `Callback` from a FnOnce which will send a message to the linked
197-
/// component's update method when invoked.
207+
/// Creates a `Callback` from a FnOnce which will send a message
208+
/// to the linked component's update method when invoked.
209+
///
210+
/// Please be aware that currently the result of this callback
211+
/// will synchronously schedule calls to the
212+
/// [Component](Component) interface.
198213
pub fn callback_once<F, IN, M>(&self, function: F) -> Callback<IN>
199214
where
200215
M: Into<COMP::Message>,
@@ -208,8 +223,12 @@ impl<COMP: Component> Scope<COMP> {
208223
Callback::once(closure)
209224
}
210225

211-
/// Creates a `Callback` which will send a batch of messages back to the linked
212-
/// component's update method when invoked.
226+
/// Creates a `Callback` which will send a batch of messages back
227+
/// to the linked component's update method when invoked.
228+
///
229+
/// Please be aware that currently the results of these callbacks
230+
/// will synchronously schedule calls to the
231+
/// [Component](Component) interface.
213232
pub fn batch_callback<F, IN>(&self, function: F) -> Callback<IN>
214233
where
215234
F: Fn(IN) -> Vec<COMP::Message> + 'static,
@@ -236,6 +255,8 @@ struct ComponentState<COMP: Component> {
236255
}
237256

238257
impl<COMP: Component> ComponentState<COMP> {
258+
/// Creates a new `ComponentState`, also invokes the `create()`
259+
/// method on component to create it.
239260
fn new(
240261
parent: Element,
241262
ancestor: Option<VNode>,
@@ -255,6 +276,9 @@ impl<COMP: Component> ComponentState<COMP> {
255276
}
256277
}
257278

279+
/// A `Runnable` task which creates the `ComponentState` (if there is
280+
/// none) and invokes the `create()` method on a `Component` to create
281+
/// it.
258282
struct CreateComponent<COMP>
259283
where
260284
COMP: Component,
@@ -285,6 +309,7 @@ where
285309
}
286310
}
287311

312+
/// A `Runnable` task which calls the `update()` method on a `Component`.
288313
struct UpdateComponent<COMP>
289314
where
290315
COMP: Component,
@@ -334,6 +359,7 @@ where
334359
}
335360
}
336361

362+
/// A `Runnable` task which calls the `rendered()` method on a `Component`.
337363
struct RenderedComponent<COMP>
338364
where
339365
COMP: Component,
@@ -362,6 +388,7 @@ where
362388
}
363389
}
364390

391+
/// A `Runnable` task which calls the `destroy()` method on a `Component`.
365392
struct DestroyComponent<COMP>
366393
where
367394
COMP: Component,

yew/src/scheduler.rs

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub(crate) trait Runnable {
2424
/// This is a global scheduler suitable to schedule and run any tasks.
2525
#[derive(Clone)]
2626
pub(crate) struct Scheduler {
27+
/// This lock is used to prevent recursion in [Scheduler#start()](Scheduler#start())
2728
lock: Rc<RefCell<()>>,
2829
main: Shared<VecDeque<Box<dyn Runnable>>>,
2930
component: ComponentScheduler,
@@ -97,6 +98,9 @@ impl Scheduler {
9798
}
9899

99100
pub(crate) fn start(&self) {
101+
// The lock here is used to prevent recursion. If the lock
102+
// here fails to acquire, it is because this `start()` method
103+
// is being called recursively in a `runnable.run()`.
100104
if let Ok(_lock) = self.lock.try_borrow_mut() {
101105
while let Some(runnable) = self.next_runnable() {
102106
runnable.run();

0 commit comments

Comments
 (0)