Add interval task to multi-thread example

This commit is contained in:
Denis Kolodin 2018-06-03 12:51:50 +03:00
parent 04591f1763
commit b0df296fa8
3 changed files with 30 additions and 14 deletions

View File

@ -30,7 +30,7 @@ where
fn update(&mut self, msg: Self::Message, env: &mut Env<CTX, Self>) -> ShouldRender {
match msg {
Msg::SendToThread => {
env.as_ref().send(worker::Msg::RequestDataFromServer);
env.as_ref().send(worker::Request::GetDataFromServer);
}
}
true

View File

@ -1,42 +1,58 @@
use std::time::Duration;
use yew::prelude::*;
// TODO use yew::services::{IntervalService, FetchService, Task};
use yew::services::Task;
use yew::services::interval::IntervalService;
use yew::services::fetch::FetchService;
#[derive(Serialize, Deserialize, Debug)]
pub enum Msg {
RequestDataFromServer,
pub enum Request {
GetDataFromServer,
}
impl Message for Msg {
impl Message for Request {
}
pub enum Msg {
Updating,
}
pub struct Worker {
interval: IntervalService,
task: Box<Task>,
fetch: FetchService,
}
impl Agent for Worker {
type Message = Msg;
type Input = Msg;
type Input = Request;
type Output = Msg;
fn create(link: AgentLink<Self>) -> Self {
let mut interval = IntervalService::new();
let duration = Duration::from_secs(3);
let callback = link.send_back(|_| Msg::Updating);
let task = interval.spawn(duration, callback);
Worker {
interval,
task: Box::new(task),
fetch: FetchService::new(),
}
}
fn update(&mut self, msg: Self::Message) {
match msg {
Msg::Updating => {
info!("Tick...");
}
}
}
fn handle(&mut self, msg: Self::Input) {
info!("Incoming: {:?}", msg);
info!("Request: {:?}", msg);
match msg {
Msg::RequestDataFromServer => {
/*
* let callback = env.send_back(|_| ___);
* let request = ...;
* self.fetch.fetch(callback);
*/
},
Request::GetDataFromServer => {
}
}
}
}

View File

@ -327,7 +327,7 @@ impl<AGN: Agent> AgentLink<AGN> {
}
/// This method sends messages back to the component's loop.
pub fn send_back<F, IN>(&mut self, function: F) -> Callback<IN>
pub fn send_back<F, IN>(&self, function: F) -> Callback<IN>
where
F: Fn(IN) -> AGN::Message + 'static,
{