Fix examples: use tasks explicitly

This commit is contained in:
Denis Kolodin 2018-02-10 18:45:05 +03:00
parent 5f056c5c68
commit 99d8131ff7
3 changed files with 16 additions and 9 deletions

View File

@ -6,8 +6,8 @@ extern crate yew;
use yew::prelude::*;
use yew::format::{Nothing, Json};
use yew::services::Task;
use yew::services::fetch::{FetchService, Request, Response};
use yew::services::websocket::{WebSocketService, WebSocketHandle, WebSocketStatus};
use yew::services::fetch::{FetchService, FetchTask, Request, Response};
use yew::services::websocket::{WebSocketService, WebSocketTask, WebSocketStatus};
struct Context {
web: FetchService,
@ -17,7 +17,8 @@ struct Context {
struct Model {
fetching: bool,
data: Option<u32>,
ws: Option<WebSocketHandle>,
ft: Option<FetchTask>,
ws: Option<WebSocketTask>,
}
enum WsAction {
@ -68,6 +69,7 @@ impl Component<Context> for Model {
Model {
fetching: false,
data: None,
ft: None,
ws: None,
}
}
@ -85,7 +87,8 @@ impl Component<Context> for Model {
}
});
let request = Request::get("/data.json").body(Nothing).unwrap();
context.web.fetch(request, callback);
let task = context.web.fetch(request, callback);
self.ft = Some(task);
}
Msg::WsAction(action) => {
match action {
@ -97,8 +100,8 @@ impl Component<Context> for Model {
WebSocketStatus::Closed => WsAction::Lost.into(),
}
});
let handle = context.ws.connect("ws://localhost:9001/", callback, notification);
self.ws = Some(handle);
let task = context.ws.connect("ws://localhost:9001/", callback, notification);
self.ws = Some(task);
}
WsAction::SendData => {
let request = WsRequest {

View File

@ -1,5 +1,5 @@
use yew::format::{Nothing, Json};
use yew::services::fetch::{FetchService, FetchHandle, Request, Response};
use yew::services::fetch::{FetchService, FetchTask, Request, Response};
use yew::html::Callback;
#[derive(Deserialize, Debug)]
@ -28,7 +28,7 @@ impl GravatarService {
}
}
pub fn profile(&mut self, hash: &str, callback: Callback<Result<Profile, ()>>) -> FetchHandle {
pub fn profile(&mut self, hash: &str, callback: Callback<Result<Profile, ()>>) -> FetchTask {
let url = format!("https://gravatar.com/{}", hash);
let handler = move |response: Response<Json<Result<Profile, ()>>>| {
let (_, Json(data)) = response.into_parts();

View File

@ -6,6 +6,7 @@ extern crate stdweb;
extern crate yew;
use yew::prelude::*;
use yew::services::fetch::FetchTask;
// Own services implementation
mod gravatar;
@ -21,6 +22,7 @@ struct Context {
struct Model {
profile: Option<Profile>,
exchanges: Vec<String>,
task: Option<FetchTask>,
}
enum Msg {
@ -37,6 +39,7 @@ impl Component<Context> for Model {
Model {
profile: None,
exchanges: Vec::new(),
task: None,
}
}
@ -44,7 +47,8 @@ impl Component<Context> for Model {
match msg {
Msg::Gravatar => {
let callback = context.send_back(Msg::GravatarReady);
context.gravatar.profile("205e460b479e2e5b48aec07710c08d50", callback);
let task = context.gravatar.profile("205e460b479e2e5b48aec07710c08d50", callback);
self.task = Some(task);
}
Msg::GravatarReady(Ok(profile)) => {
self.profile = Some(profile);