Skip to content

Commit

Permalink
Change ComponentLink to alias of Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Feb 29, 2020
1 parent bc197d8 commit 921e146
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 77 deletions.
76 changes: 3 additions & 73 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ mod listener;
mod scope;

pub use listener::*;
pub(crate) use scope::{ComponentUpdate, HiddenScope, Scope};
pub use scope::Scope;
pub(crate) use scope::{ComponentUpdate, HiddenScope};

use crate::callback::Callback;
use crate::virtual_dom::{VChild, VList, VNode};
Expand Down Expand Up @@ -400,78 +401,7 @@ impl EmptyBuilder {
}

/// Link to component's scope for creating callbacks.
pub struct ComponentLink<COMP: Component> {
scope: Scope<COMP>,
}

impl<COMP> ComponentLink<COMP>
where
COMP: Component,
{
/// Create link for a scope.
fn connect(scope: &Scope<COMP>) -> Self {
ComponentLink {
scope: scope.clone(),
}
}

/// This method creates a `Callback` which will send a batch of messages back to the linked
/// component's update method when called.
pub fn batch_callback<F, IN>(&self, function: F) -> Callback<IN>
where
F: Fn(IN) -> Vec<COMP::Message> + 'static,
{
let scope = self.scope.clone();
let closure = move |input| {
let messages = function(input);
scope.send_message_batch(messages);
};
closure.into()
}

/// This method creates a `Callback` which will send a message to the linked component's
/// update method when invoked.
pub fn callback<F, IN>(&self, function: F) -> Callback<IN>
where
F: Fn(IN) -> COMP::Message + 'static,
{
let scope = self.scope.clone();
let closure = move |input| {
let output = function(input);
scope.send_message(output);
};
closure.into()
}

/// This method sends a message to this component to be processed immediately after the
/// component has been updated and/or rendered.
pub fn send_message(&self, msg: COMP::Message) {
self.scope.send_message(msg);
}

/// Sends a batch of messages to the component to be processed immediately after
/// the component has been updated and/or rendered..
///
/// All messages will first be processed by `update`, and if _any_ of them return `true`,
/// then re-render will occur.
pub fn send_message_batch(&self, msgs: Vec<COMP::Message>) {
self.scope.send_message_batch(msgs)
}
}

impl<COMP: Component> fmt::Debug for ComponentLink<COMP> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("ComponentLink<_>")
}
}

impl<COMP: Component> Clone for ComponentLink<COMP> {
fn clone(&self) -> Self {
ComponentLink {
scope: self.scope.clone(),
}
}
}
pub type ComponentLink<COMP> = Scope<COMP>;

/// A bridging type for checking `href` attribute value.
#[derive(Debug)]
Expand Down
35 changes: 31 additions & 4 deletions src/html/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ impl<COMP: Component> Scope<COMP> {
props: COMP::Properties,
) -> Scope<COMP> {
let mut scope = self;
let link = ComponentLink::connect(&scope);
let ready_state = ReadyState {
element,
node_ref,
link,
scope: scope.clone(),
props,
ancestor,
};
Expand Down Expand Up @@ -117,6 +116,34 @@ impl<COMP: Component> Scope<COMP> {
pub fn send_message_batch(&self, messages: Vec<COMP::Message>) {
self.update(ComponentUpdate::MessageBatch(messages));
}

/// This method creates a `Callback` which will send a message to the linked component's
/// update method when invoked.
pub fn callback<F, IN>(&self, function: F) -> Callback<IN>
where
F: Fn(IN) -> COMP::Message + 'static,
{
let scope = self.clone();
let closure = move |input| {
let output = function(input);
scope.send_message(output);
};
closure.into()
}

/// This method creates a `Callback` which will send a batch of messages back to the linked
/// component's update method when called.
pub fn batch_callback<F, IN>(&self, function: F) -> Callback<IN>
where
F: Fn(IN) -> Vec<COMP::Message> + 'static,
{
let scope = self.clone();
let closure = move |input| {
let messages = function(input);
scope.send_message_batch(messages);
};
closure.into()
}
}

enum ComponentState<COMP: Component> {
Expand Down Expand Up @@ -144,14 +171,14 @@ struct ReadyState<COMP: Component> {
element: Element,
node_ref: NodeRef,
props: COMP::Properties,
link: ComponentLink<COMP>,
scope: Scope<COMP>,
ancestor: Option<VNode>,
}

impl<COMP: Component> ReadyState<COMP> {
fn create(self) -> CreatedState<COMP> {
CreatedState {
component: COMP::create(self.props, self.link),
component: COMP::create(self.props, self.scope),
element: self.element,
last_frame: self.ancestor,
node_ref: self.node_ref,
Expand Down

0 comments on commit 921e146

Please sign in to comment.