Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dispatchers #639

Merged
merged 21 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/routing/src/b_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use yew::{html, Bridge, Component, ComponentLink, Html, Renderable, ShouldRender
pub struct BModel {
number: Option<usize>,
sub_path: Option<String>,
router: Box<Bridge<Router<()>>>,
router: Box<dyn Bridge<Router<()>>>,
}

pub enum Msg {
Expand Down
50 changes: 18 additions & 32 deletions examples/routing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

mod b_component;
mod router;
mod router_button;
mod routing;
use b_component::BModel;

use crate::router_button::RouterButton;
use log::info;
use router::Route;
use yew::agent::Bridged;
Expand All @@ -14,15 +16,15 @@ pub enum Child {
A,
B,
PathNotFound(String),
Loading,
}

pub struct Model {
child: Child,
router: Box<Bridge<router::Router<()>>>,
router: Box<dyn Bridge<router::Router<()>>>,
}

pub enum Msg {
NavigateTo(Child),
HandleRoute(Route<()>),
}

Expand All @@ -32,40 +34,21 @@ impl Component for Model {

fn create(_: Self::Properties, mut link: ComponentLink<Self>) -> Self {
let callback = link.send_back(|route: Route<()>| Msg::HandleRoute(route));
let mut router = router::Router::bridge(callback);

// TODO Not sure if this is technically correct. This should be sent _after_ the component has been created.
// I think the `Component` trait should have a hook called `on_mount()`
// that is called after the component has been attached to the vdom.
// It seems like this only works because the JS engine decides to activate the
// router worker logic after the mounting has finished.
router.send(router::Request::GetCurrentRoute);

let router = router::Router::bridge(callback);
Model {
child: Child::A, // This should be quickly overwritten by the actual route.
child: Child::Loading, // This should be quickly overwritten by the actual route.
router,
}
}

fn mounted(&mut self) -> ShouldRender {
// Once the component has successfully mounted, it is safe to send messages to agents.
hgzimmerman marked this conversation as resolved.
Show resolved Hide resolved
self.router.send(router::Request::GetCurrentRoute);
false
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::NavigateTo(child) => {
let path_segments = match child {
Child::A => vec!["a".into()],
Child::B => vec!["b".into()],
Child::PathNotFound(_) => vec!["path_not_found".into()],
};

let route = router::Route {
path_segments,
query: None,
fragment: None,
state: (),
};

self.router.send(router::Request::ChangeRoute(route));
false
}
Msg::HandleRoute(route) => {
info!("Routing: {}", route.to_route_string());
// Instead of each component selecting which parts of the path are important to it,
Expand All @@ -92,8 +75,8 @@ impl Renderable<Model> for Model {
html! {
<div>
<nav class="menu">
<button onclick=|_| Msg::NavigateTo(Child::A)>{ "Go to A" }</button>
<button onclick=|_| Msg::NavigateTo(Child::B)>{ "Go to B" }</button>
<RouterButton text="Go to A" path="/a" />
<RouterButton text="Go to B" path="/b" />
</nav>
<div>
{self.child.view()}
Expand All @@ -105,7 +88,7 @@ impl Renderable<Model> for Model {

impl Renderable<Model> for Child {
fn view(&self) -> Html<Model> {
match *self {
match self {
Child::A => html! {
<>
{"This corresponds to route 'a'"}
Expand All @@ -122,6 +105,9 @@ impl Renderable<Model> for Child {
{format!("Invalid path: '{}'", path)}
</>
},
Child::Loading => html! {
{"Loading"}
},
}
}
}
16 changes: 9 additions & 7 deletions examples/routing/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ where
}
}

fn handle(&mut self, msg: Self::Input, who: HandlerId) {
fn connected(&mut self, id: HandlerId) {
self.subscribers.insert(id);
}

fn handle(&mut self, msg: Self::Input, who: Option<HandlerId>) {
info!("Request: {:?}", msg);
match msg {
Request::ChangeRoute(route) => {
Expand All @@ -166,15 +170,13 @@ where
self.route_service.set_route(&route_string, route.state);
}
Request::GetCurrentRoute => {
let route = Route::current_route(&self.route_service);
self.link.response(who, route.clone());
if let Some(who) = who {
let route = Route::current_route(&self.route_service);
self.link.response(who, route.clone());
}
}
}
}

fn connected(&mut self, id: HandlerId) {
self.subscribers.insert(id);
}
fn disconnected(&mut self, id: HandlerId) {
self.subscribers.remove(&id);
}
Expand Down
81 changes: 81 additions & 0 deletions examples/routing/src/router_button.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! A component wrapping a <button/> tag that changes the route.
jstarry marked this conversation as resolved.
Show resolved Hide resolved
use crate::router::Route;
use crate::router::Router;
use yew::agent::Dispatched;
use yew::prelude::*;

use crate::router::Request;
use yew::agent::Dispatcher;

/// Changes the route when clicked.
pub struct RouterButton {
router: Dispatcher<Router<()>>,
props: Props,
}

pub enum Msg {
Clicked,
}

/// Properties for Routing Components
#[derive(Properties, Default, Clone, Debug, PartialEq)]
pub struct Props {
/// The route that will be set when the component is clicked.
pub path: String,
/// The text to display.
pub text: String,
/// Disable the component.
pub disabled: bool,
/// Classes to be added to component.
pub classes: String,
}

impl Component for RouterButton {
type Message = Msg;
type Properties = Props;

fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
let router = Router::dispatcher();

RouterButton { router, props }
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Clicked => {
let route = Route {
path_segments: self
.props
.path
.split("/")
.skip(1)
.map(|s| s.to_string())
.collect::<Vec<_>>(),
query: None,
state: (),
fragment: None,
};
self.router.send(Request::ChangeRoute(route));
false
}
}
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
self.props = props;
true
}
}

impl Renderable<RouterButton> for RouterButton {
fn view(&self) -> Html<RouterButton> {
html! {
<button
class=self.props.classes.clone(),
onclick=|_| Msg::Clicked,
disabled=self.props.disabled,
>
{&self.props.text}
</button>
}
}
}
Loading