Skip to content

Commit

Permalink
Fix all examples
Browse files Browse the repository at this point in the history
  • Loading branch information
therustmonk committed Jan 11, 2018
1 parent f84e08c commit 7b758fb
Show file tree
Hide file tree
Showing 11 changed files with 418 additions and 426 deletions.
2 changes: 1 addition & 1 deletion examples/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum Msg {
Bulk(Vec<Msg>),
}

fn update(context: &mut Context, model: &mut Model, msg: Msg) -> ShouldUpdate {
fn update(context: &mut AppContext<Context, Model, Msg>, model: &mut Model, msg: Msg) -> ShouldUpdate {
match msg {
Msg::Increment => {
model.value = model.value + 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/crm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn load_database(context: &mut Context) -> Database {
})
}

fn update(context: &mut Context, model: &mut Model, msg: Msg) -> ShouldUpdate {
fn update(context: &mut AppContext<Context, Model, Msg>, model: &mut Model, msg: Msg) -> ShouldUpdate {
let mut new_scene = None;
match model.scene {
Scene::Initialization => {
Expand Down
151 changes: 72 additions & 79 deletions examples/dashboard/client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate serde_derive;
#[macro_use]
extern crate yew;

use yew::html::*;
use yew::prelude::*;
use yew::format::{Nothing, Json};
use yew::services::Task;
use yew::services::fetch::{FetchService, Method};
Expand Down Expand Up @@ -60,102 +60,95 @@ struct WsResponse {
value: u32,
}

impl Component<Context> for Model {
type Msg = Msg;

fn create(_: &mut ScopeRef<Context, Msg>) -> Self {
Model {
fetching: false,
data: None,
ws: None,
fn update(context: &mut AppContext<Context, Model, Msg>, model: &mut Model, msg: Msg) -> ShouldUpdate {
match msg {
Msg::FetchData => {
model.fetching = true;
let callback = context.send_back(|Json(data)| Msg::FetchReady(data));
context.web.fetch(Method::Get, "./data.json", Nothing, callback);
}
}

fn update(&mut self, msg: Msg, context: &mut ScopeRef<Context, Msg>) {
match msg {
Msg::FetchData => {
self.fetching = true;
let callback = context.send_back(|Json(data)| Msg::FetchReady(data));
context.web.fetch(Method::Get, "./data.json", Nothing, callback);
}
Msg::WsAction(action) => {
match action {
WsAction::Connect => {
let callback = context.send_back(|Json(data)| Msg::WsReady(data));
let notification = context.send_back(|status| {
match status {
WebSocketStatus::Opened => Msg::Ignore,
WebSocketStatus::Closed => WsAction::Lost.into(),
}
});
let handle = context.ws.connect("ws://localhost:9001/", callback, notification);
self.ws = Some(handle);
}
WsAction::SendData => {
let request = WsRequest {
value: 321,
};
self.ws.as_mut().unwrap().send(Json(&request));
}
WsAction::Disconnect => {
self.ws.take().unwrap().cancel();
}
WsAction::Lost => {
self.ws = None;
}
Msg::WsAction(action) => {
match action {
WsAction::Connect => {
let callback = context.send_back(|Json(data)| Msg::WsReady(data));
let notification = context.send_back(|status| {
match status {
WebSocketStatus::Opened => Msg::Ignore,
WebSocketStatus::Closed => WsAction::Lost.into(),
}
});
let handle = context.ws.connect("ws://localhost:9001/", callback, notification);
model.ws = Some(handle);
}
WsAction::SendData => {
let request = WsRequest {
value: 321,
};
model.ws.as_mut().unwrap().send(Json(&request));
}
WsAction::Disconnect => {
model.ws.take().unwrap().cancel();
}
WsAction::Lost => {
model.ws = None;
}
}
Msg::FetchReady(response) => {
self.fetching = false;
self.data = response.map(|data| data.value).ok();
}
Msg::WsReady(response) => {
self.data = response.map(|data| data.value).ok();
}
Msg::Ignore => {
}
}
Msg::FetchReady(response) => {
model.fetching = false;
model.data = response.map(|data| data.value).ok();
}
Msg::WsReady(response) => {
model.data = response.map(|data| data.value).ok();
}
Msg::Ignore => {
return false;
}
}
true
}

fn view(&self) -> Html<Context, Msg> {
html! {
<div>
<nav class="menu",>
<button onclick=|_| Msg::FetchData,>{ "Fetch Data" }</button>
{ self.view_data() }
<button disabled=self.ws.is_some(),
onclick=|_| WsAction::Connect.into(),>{ "Connect To WebSocket" }</button>
<button disabled=self.ws.is_none(),
onclick=|_| WsAction::SendData.into(),>{ "Send To WebSocket" }</button>
<button disabled=self.ws.is_none(),
onclick=|_| WsAction::Disconnect.into(),>{ "Close WebSocket connection" }</button>
</nav>
</div>
}
fn view(model: &Model) -> AppHtml<Context, Model, Msg> {
html! {
<div>
<nav class="menu",>
<button onclick=|_| Msg::FetchData,>{ "Fetch Data" }</button>
{ view_data(&model) }
<button disabled=model.ws.is_some(),
onclick=|_| WsAction::Connect.into(),>{ "Connect To WebSocket" }</button>
<button disabled=model.ws.is_none(),
onclick=|_| WsAction::SendData.into(),>{ "Send To WebSocket" }</button>
<button disabled=model.ws.is_none(),
onclick=|_| WsAction::Disconnect.into(),>{ "Close WebSocket connection" }</button>
</nav>
</div>
}
}

impl Model {
fn view_data(&self) -> Html<Context, Msg> {
if let Some(value) = self.data {
html! {
<p>{ value }</p>
}
} else {
html! {
<p>{ "Data hasn't fetched yet." }</p>
}
fn view_data(model: &Model) -> AppHtml<Context, Model, Msg> {
if let Some(value) = model.data {
html! {
<p>{ value }</p>
}
} else {
html! {
<p>{ "Data hasn't fetched yet." }</p>
}
}
}

fn main() {
yew::initialize();
let app = App::new();
let context = Context {
web: FetchService::new(),
ws: WebSocketService::new(),
};
let app = Scope::new(context);
app.mount_to_body::<Model>();
let model = Model {
fetching: false,
data: None,
ws: None,
};
app.mount(context, model, update, view);
yew::run_loop();
}
138 changes: 66 additions & 72 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern crate yew;
extern crate rand;

use yew::html::*;
use yew::prelude::*;
use std::time::Duration;
use yew::services::Task;
use yew::services::interval::IntervalService;
Expand Down Expand Up @@ -145,83 +145,71 @@ enum Msg {
ToggleCellule(usize)
}

impl Component<Context> for GameOfLife {
type Msg = Msg;

fn create(_: &mut ScopeRef<Context, Msg>) -> Self {
GameOfLife {
cellules: vec![Cellule { life_state: LifeState::Dead }; 2000],
cellules_width: 50,
cellules_height: 40,
job : None
}
}

fn update(&mut self, msg: Msg, context: &mut ScopeRef<Context, Msg>) {
match msg {
Msg::Random => {
self.random_mutate();
println!("Random");
},
Msg::Start => {
let callback = context.send_back(|_| Msg::Step);
let handle = context.interval.spawn(Duration::from_millis(200), callback);
self.job = Some(Box::new(handle));
println!("Start");
},
Msg::Step => {
self.play();
},
Msg::Reset => {
self.reset();
println!("Reset");
},
Msg::Stop => {
if let Some(mut task) = self.job.take() {
task.cancel();
}
self.job = None;
println!("Stop");
},
Msg::ToggleCellule(idx) => {
self.toggle_cellule(idx);
fn update(context: &mut AppContext<Context, GameOfLife, Msg>, gof: &mut GameOfLife, msg: Msg) -> ShouldUpdate {
match msg {
Msg::Random => {
gof.random_mutate();
println!("Random");
},
Msg::Start => {
let callback = context.send_back(|_| Msg::Step);
let handle = context.interval.spawn(Duration::from_millis(200), callback);
gof.job = Some(Box::new(handle));
println!("Start");
},
Msg::Step => {
gof.play();
},
Msg::Reset => {
gof.reset();
println!("Reset");
},
Msg::Stop => {
if let Some(mut task) = gof.job.take() {
task.cancel();
}
gof.job = None;
println!("Stop");
},
Msg::ToggleCellule(idx) => {
gof.toggle_cellule(idx);
}
}
true
}

fn view(&self) -> Html<Context, Msg> {
html! {
<div>
<section class="game-container",>
<header class="app-header",>
<img src="favicon.ico", class="app-logo",/>
<h1 class="app-title",>{ "Game of Life" }</h1>
</header>
<section class="game-area",>
<div class="game-of-life",>
{ for self.cellules.iter().enumerate().map(view_cellule) }
</div>
<div class="game-buttons",>
<button class="game-button", onclick=move|_| Msg::Random,>{ "Random" }</button>
<button class="game-button", onclick=move|_| Msg::Step,>{ "Step" }</button>
<button class="game-button", onclick=move|_| Msg::Start,>{ "Start" }</button>
<button class="game-button", onclick=move|_| Msg::Stop,>{ "Stop" }</button>
<button class="game-button", onclick=move|_| Msg::Reset,>{ "Reset" }</button>
</div>
</section>
fn view(gof: &GameOfLife) -> AppHtml<Context, GameOfLife, Msg> {
html! {
<div>
<section class="game-container",>
<header class="app-header",>
<img src="favicon.ico", class="app-logo",/>
<h1 class="app-title",>{ "Game of Life" }</h1>
</header>
<section class="game-area",>
<div class="game-of-life",>
{ for gof.cellules.iter().enumerate().map(view_cellule) }
</div>
<div class="game-buttons",>
<button class="game-button", onclick=move|_| Msg::Random,>{ "Random" }</button>
<button class="game-button", onclick=move|_| Msg::Step,>{ "Step" }</button>
<button class="game-button", onclick=move|_| Msg::Start,>{ "Start" }</button>
<button class="game-button", onclick=move|_| Msg::Stop,>{ "Stop" }</button>
<button class="game-button", onclick=move|_| Msg::Reset,>{ "Reset" }</button>
</div>
</section>
<footer class="app-footer",>
<strong class="footer-text",>
{ "Game of Life - a yew experiment " }
</strong>
<a href="https://github.com/DenisKolodin/yew", target="_blank",>{ "source" }</a>
</footer>
</div>
}
</section>
<footer class="app-footer",>
<strong class="footer-text",>
{ "Game of Life - a yew experiment " }
</strong>
<a href="https://github.com/DenisKolodin/yew", target="_blank",>{ "source" }</a>
</footer>
</div>
}
}

fn view_cellule((idx, cellule): (usize, &Cellule)) -> Html<Context, Msg> {
fn view_cellule((idx, cellule): (usize, &Cellule)) -> AppHtml<Context, GameOfLife, Msg> {
html! {
<div class=("game-cellule", if cellule.life_state == LifeState::Live { "cellule-live" } else { "cellule-dead" }),
onclick=move |_| Msg::ToggleCellule(idx),> </div>
Expand All @@ -230,10 +218,16 @@ fn view_cellule((idx, cellule): (usize, &Cellule)) -> Html<Context, Msg> {

fn main() {
yew::initialize();
let app = App::new();
let context = Context {
interval: IntervalService::new(),
};
let app = Scope::new(context);
app.mount_to_body::<GameOfLife>();
let gof = GameOfLife {
cellules: vec![Cellule { life_state: LifeState::Dead }; 2000],
cellules_width: 50,
cellules_height: 40,
job : None
};
app.mount(context, gof, update, view);
yew::run_loop();
}
Loading

0 comments on commit 7b758fb

Please sign in to comment.