mentorenwahl/frontend/src/agents/logged_in.rs
Dominic Grimm ff2b884d42
All checks were successful
continuous-integration/drone/push Build is passing
Agent oriented logged in state
2022-11-05 21:27:49 +01:00

48 lines
1 KiB
Rust

use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use yew_agent::{Agent, AgentLink, Context, HandlerId};
#[derive(Serialize, Deserialize, Debug)]
pub enum Request {
LoggedIn(bool),
}
pub struct EventBus {
link: AgentLink<Self>,
subscribers: HashSet<HandlerId>,
}
impl Agent for EventBus {
type Reach = Context<Self>;
type Message = ();
type Input = Request;
type Output = bool;
fn create(link: AgentLink<Self>) -> Self {
Self {
link,
subscribers: HashSet::new(),
}
}
fn update(&mut self, _msg: Self::Message) {}
fn handle_input(&mut self, msg: Self::Input, _id: HandlerId) {
match msg {
Request::LoggedIn(x) => {
for sub in self.subscribers.iter() {
self.link.respond(*sub, x);
}
}
}
}
fn connected(&mut self, id: HandlerId) {
self.subscribers.insert(id);
}
fn disconnected(&mut self, id: HandlerId) {
self.subscribers.remove(&id);
}
}