mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
Updates to using the latest stable gloo, in some cases removing dependencies on `master` branch. Removes `weblog` in favour of `gloo_console`, where applicable.
62 lines
1.3 KiB
Rust
62 lines
1.3 KiB
Rust
use gloo_timers::callback::Interval;
|
|
use serde::{Deserialize, Serialize};
|
|
use yew_agent::{Agent, AgentLink, Context, HandlerId};
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub enum Request {
|
|
GetDataFromServer,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub enum Response {
|
|
DataFetched,
|
|
}
|
|
|
|
pub enum Msg {
|
|
Updating,
|
|
}
|
|
|
|
pub struct Worker {
|
|
link: AgentLink<Worker>,
|
|
_interval: Interval,
|
|
}
|
|
|
|
impl Agent for Worker {
|
|
type Reach = Context<Self>;
|
|
type Message = Msg;
|
|
type Input = Request;
|
|
type Output = Response;
|
|
|
|
fn create(link: AgentLink<Self>) -> Self {
|
|
let duration = 3;
|
|
|
|
let interval = {
|
|
let link = link.clone();
|
|
Interval::new(duration, move || link.send_message(Msg::Updating))
|
|
};
|
|
|
|
Self {
|
|
link,
|
|
_interval: interval,
|
|
}
|
|
}
|
|
|
|
fn update(&mut self, msg: Self::Message) {
|
|
match msg {
|
|
Msg::Updating => {
|
|
log::info!("Tick...");
|
|
}
|
|
}
|
|
}
|
|
|
|
fn handle_input(&mut self, msg: Self::Input, who: HandlerId) {
|
|
log::info!("Request: {:?}", msg);
|
|
match msg {
|
|
Request::GetDataFromServer => {
|
|
// TODO fetch actual data
|
|
self.link.respond(who, Response::DataFetched);
|
|
}
|
|
}
|
|
}
|
|
}
|