From b2d4389f26a9062344b176a9574fc6f950deb37c Mon Sep 17 00:00:00 2001 From: Luke Frisken Date: Sat, 30 May 2020 23:21:33 +0400 Subject: [PATCH 1/3] code comments for Scheduler and ComponentLink --- yew/src/html/scope.rs | 47 ++++++++++++++++++++++++++++++++++--------- yew/src/scheduler.rs | 5 +++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/yew/src/html/scope.rs b/yew/src/html/scope.rs index db7964ebed1..acfa97ee87a 100644 --- a/yew/src/html/scope.rs +++ b/yew/src/html/scope.rs @@ -162,7 +162,11 @@ impl Scope { scheduler().push_comp(ComponentRunnableType::Destroy, Box::new(destroy)); } - /// Send a message to the component + /// Send a message to the component. + /// + /// Please be aware that currently this method + /// immediately/synchronously schedules a call to the + /// [Component](Component) interface. pub fn send_message(&self, msg: T) where T: Into, @@ -172,14 +176,23 @@ impl Scope { /// Send a batch of messages to the component. /// - /// This is useful for reducing re-renders of the components because the messages are handled - /// together and the view function is called only once if needed. + /// This is useful for reducing re-renders of the components + /// because the messages are handled together and the view + /// function is called only once if needed. + /// + /// Please be aware that currently this method + /// immediately/synchronously schedules calls to the + /// [Component](Component) interface. pub fn send_message_batch(&self, messages: Vec) { self.update(ComponentUpdate::MessageBatch(messages), false); } - /// Creates a `Callback` which will send a message to the linked component's - /// update method when invoked. + /// Creates a `Callback` which will send a message to the linked + /// component's update method when invoked. + /// + /// Please be aware that currently the result of this callback + /// immediately/synchronously schedules a call to the + /// [Component](Component) interface. pub fn callback(&self, function: F) -> Callback where M: Into, @@ -193,8 +206,12 @@ impl Scope { closure.into() } - /// Creates a `Callback` from a FnOnce which will send a message to the linked - /// component's update method when invoked. + /// Creates a `Callback` from a FnOnce which will send a message + /// to the linked component's update method when invoked. + /// + /// Please be aware that currently the result of this callback + /// will immediately/synchronously schedule calls to the + /// [Component](Component) interface. pub fn callback_once(&self, function: F) -> Callback where M: Into, @@ -208,8 +225,12 @@ impl Scope { Callback::once(closure) } - /// Creates a `Callback` which will send a batch of messages back to the linked - /// component's update method when invoked. + /// Creates a `Callback` which will send a batch of messages back + /// to the linked component's update method when invoked. + /// + /// Please be aware that currently the results of these callbacks + /// will immediately/synchronously schedule calls to the + /// [Component](Component) interface. pub fn batch_callback(&self, function: F) -> Callback where F: Fn(IN) -> Vec + 'static, @@ -236,6 +257,8 @@ struct ComponentState { } impl ComponentState { + /// Creates a new `ComponentState`, also invokes the `create()` + /// method on component to create it. fn new( element: Element, ancestor: Option, @@ -255,6 +278,9 @@ impl ComponentState { } } +/// A `Runnable` task which calls the creates the `ComponentState` (if +/// there is none) and invokes the `create()` method on a `Component` +/// to create it. struct CreateComponent where COMP: Component, @@ -285,6 +311,7 @@ where } } +/// A `Runnable` task which calls the `update()` method on a `Component`. struct UpdateComponent where COMP: Component, @@ -334,6 +361,7 @@ where } } +/// A `Runnable` task which calls the `rendered()` method on a `Component`. struct RenderedComponent where COMP: Component, @@ -362,6 +390,7 @@ where } } +/// A `Runnable` task which calls the `destroy()` method on a `Component`. struct DestroyComponent where COMP: Component, diff --git a/yew/src/scheduler.rs b/yew/src/scheduler.rs index 5e25310f5fb..e57379891c1 100644 --- a/yew/src/scheduler.rs +++ b/yew/src/scheduler.rs @@ -24,6 +24,7 @@ pub(crate) trait Runnable { /// This is a global scheduler suitable to schedule and run any tasks. #[derive(Clone)] pub(crate) struct Scheduler { + /// This lock is used to prevent recursion in [Scheduler#start()](Scheduler#start()) lock: Rc>, main: Shared>>, component: ComponentScheduler, @@ -97,6 +98,10 @@ impl Scheduler { } pub(crate) fn start(&self) { + // The lock here is used to prevent recursion. It is assumed + // that if the lock here fails to acquire, it is because this + // `start()` method is being called recursively in a + // `runnable.run()`. if let Ok(_lock) = self.lock.try_borrow_mut() { while let Some(runnable) = self.next_runnable() { runnable.run(); From 3f0bfbf5ed1ab7d21b4ea456902a9003a0992566 Mon Sep 17 00:00:00 2001 From: Luke Frisken Date: Sun, 31 May 2020 10:21:36 +0400 Subject: [PATCH 2/3] Fix typos, reformat code, address pr comments --- yew/src/html/scope.rs | 16 ++++++++-------- yew/src/scheduler.rs | 7 +++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/yew/src/html/scope.rs b/yew/src/html/scope.rs index acfa97ee87a..182b2f7256e 100644 --- a/yew/src/html/scope.rs +++ b/yew/src/html/scope.rs @@ -166,7 +166,7 @@ impl Scope { /// /// Please be aware that currently this method /// immediately/synchronously schedules a call to the - /// [Component](Component) interface. + /// [Component](Component) interface. Check out pub fn send_message(&self, msg: T) where T: Into, @@ -192,7 +192,7 @@ impl Scope { /// /// Please be aware that currently the result of this callback /// immediately/synchronously schedules a call to the - /// [Component](Component) interface. + /// [Component](Component) interface. pub fn callback(&self, function: F) -> Callback where M: Into, @@ -211,7 +211,7 @@ impl Scope { /// /// Please be aware that currently the result of this callback /// will immediately/synchronously schedule calls to the - /// [Component](Component) interface. + /// [Component](Component) interface. pub fn callback_once(&self, function: F) -> Callback where M: Into, @@ -230,7 +230,7 @@ impl Scope { /// /// Please be aware that currently the results of these callbacks /// will immediately/synchronously schedule calls to the - /// [Component](Component) interface. + /// [Component](Component) interface. pub fn batch_callback(&self, function: F) -> Callback where F: Fn(IN) -> Vec + 'static, @@ -257,7 +257,7 @@ struct ComponentState { } impl ComponentState { - /// Creates a new `ComponentState`, also invokes the `create()` + /// Creates a new `ComponentState`, also invokes the `create()` /// method on component to create it. fn new( element: Element, @@ -278,9 +278,9 @@ impl ComponentState { } } -/// A `Runnable` task which calls the creates the `ComponentState` (if -/// there is none) and invokes the `create()` method on a `Component` -/// to create it. +/// A `Runnable` task which creates the `ComponentState` (if there is +/// none) and invokes the `create()` method on a `Component` to create +/// it. struct CreateComponent where COMP: Component, diff --git a/yew/src/scheduler.rs b/yew/src/scheduler.rs index e57379891c1..e3f3a7d5df4 100644 --- a/yew/src/scheduler.rs +++ b/yew/src/scheduler.rs @@ -98,10 +98,9 @@ impl Scheduler { } pub(crate) fn start(&self) { - // The lock here is used to prevent recursion. It is assumed - // that if the lock here fails to acquire, it is because this - // `start()` method is being called recursively in a - // `runnable.run()`. + // The lock here is used to prevent recursion. If the lock + // here fails to acquire, it is because this `start()` method + // is being called recursively in a `runnable.run()`. if let Ok(_lock) = self.lock.try_borrow_mut() { while let Some(runnable) = self.next_runnable() { runnable.run(); From 4a5a4c966e09987e376c3bbe176cd8edc5a8505e Mon Sep 17 00:00:00 2001 From: Luke Frisken Date: Sun, 31 May 2020 17:01:43 +0400 Subject: [PATCH 3/3] resolve pull request comments --- yew/src/html/scope.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/yew/src/html/scope.rs b/yew/src/html/scope.rs index 182b2f7256e..2a624938f41 100644 --- a/yew/src/html/scope.rs +++ b/yew/src/html/scope.rs @@ -164,9 +164,8 @@ impl Scope { /// Send a message to the component. /// - /// Please be aware that currently this method - /// immediately/synchronously schedules a call to the - /// [Component](Component) interface. Check out + /// Please be aware that currently this method synchronously + /// schedules a call to the [Component](Component) interface. pub fn send_message(&self, msg: T) where T: Into, @@ -180,9 +179,8 @@ impl Scope { /// because the messages are handled together and the view /// function is called only once if needed. /// - /// Please be aware that currently this method - /// immediately/synchronously schedules calls to the - /// [Component](Component) interface. + /// Please be aware that currently this method synchronously + /// schedules calls to the [Component](Component) interface. pub fn send_message_batch(&self, messages: Vec) { self.update(ComponentUpdate::MessageBatch(messages), false); } @@ -191,8 +189,8 @@ impl Scope { /// component's update method when invoked. /// /// Please be aware that currently the result of this callback - /// immediately/synchronously schedules a call to the - /// [Component](Component) interface. + /// synchronously schedules a call to the [Component](Component) + /// interface. pub fn callback(&self, function: F) -> Callback where M: Into, @@ -210,7 +208,7 @@ impl Scope { /// to the linked component's update method when invoked. /// /// Please be aware that currently the result of this callback - /// will immediately/synchronously schedule calls to the + /// will synchronously schedule calls to the /// [Component](Component) interface. pub fn callback_once(&self, function: F) -> Callback where @@ -229,7 +227,7 @@ impl Scope { /// to the linked component's update method when invoked. /// /// Please be aware that currently the results of these callbacks - /// will immediately/synchronously schedule calls to the + /// will synchronously schedule calls to the /// [Component](Component) interface. pub fn batch_callback(&self, function: F) -> Callback where