Skip to content

Commit

Permalink
Allow to extend agent communication from external crates (yewstack#721)
Browse files Browse the repository at this point in the history
* allow to extend agent communication from external crates

* Fixing build on rustc 1.35

* implement Default for AgentScope with public new

* toml-rs 0.5.5 fixed compatibility with rustc 1.35
  • Loading branch information
dunnock authored and llebout committed Jan 20, 2020
1 parent b10cfc3 commit da9f05d
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,33 @@ use stdweb::Value;
#[allow(unused_imports)]
use stdweb::{_js_impl, js};

#[derive(Serialize, Deserialize)]
enum ToWorker<T> {
/// Serializable messages to worker
#[derive(Serialize, Deserialize, Debug)]
pub enum ToWorker<T> {
/// Client is connected
Connected(HandlerId),
/// Incoming message to Worker
ProcessInput(HandlerId, T),
/// Client is disconnected
Disconnected(HandlerId),
/// Worker should be terminated
Destroy,
}

#[derive(Serialize, Deserialize)]
enum FromWorker<T> {
/// Serializable messages sent by worker to consumer
#[derive(Serialize, Deserialize, Debug)]
pub enum FromWorker<T> {
/// Worker sends this message when `wasm` bundle has loaded.
WorkerLoaded,
/// Outgoing message to consumer
ProcessOutput(HandlerId, T),
}

trait Packed {
/// Message packager, based on serde::Serialize/Deserialize
pub trait Packed {
/// Pack serializable message into Vec<u8>
fn pack(&self) -> Vec<u8>;
/// Unpack deserializable message of byte slice
fn unpack(data: &[u8]) -> Self;
}

Expand Down Expand Up @@ -713,12 +723,13 @@ impl<AGN: Agent> Clone for AgentScope<AGN> {
}

impl<AGN: Agent> AgentScope<AGN> {
fn new() -> Self {
/// Create agent scope
pub fn new() -> Self {
let shared_agent = Rc::new(RefCell::new(AgentRunnable::new()));
AgentScope { shared_agent }
}

fn send(&self, update: AgentUpdate<AGN>) {
/// Schedule message for sending to agent
pub fn send(&self, update: AgentUpdate<AGN>) {
let envelope = AgentEnvelope {
shared_agent: self.shared_agent.clone(),
update,
Expand All @@ -728,7 +739,15 @@ impl<AGN: Agent> AgentScope<AGN> {
}
}

trait Responder<AGN: Agent> {
impl<AGN: Agent> Default for AgentScope<AGN> {
fn default() -> Self {
Self::new()
}
}

/// Defines communication from Worker to Consumers
pub trait Responder<AGN: Agent> {
/// Implementation for communication channel from Worker to Consumers
fn response(&self, id: HandlerId, output: AGN::Output);
}

Expand All @@ -753,7 +772,7 @@ pub struct AgentLink<AGN: Agent> {

impl<AGN: Agent> AgentLink<AGN> {
/// Create link for a scope.
fn connect<T>(scope: &AgentScope<AGN>, responder: T) -> Self
pub fn connect<T>(scope: &AgentScope<AGN>, responder: T) -> Self
where
T: Responder<AGN> + 'static,
{
Expand Down Expand Up @@ -804,12 +823,20 @@ impl<AGN> AgentRunnable<AGN> {
}
}

enum AgentUpdate<AGN: Agent> {
/// Local Agent messages
#[derive(Debug)]
pub enum AgentUpdate<AGN: Agent> {
/// Request to create link
Create(AgentLink<AGN>),
/// Internal Agent message
Message(AGN::Message),
/// Client connected
Connected(HandlerId),
/// Received mesasge from Client
Input(AGN::Input, HandlerId),
/// Client disconnected
Disconnected(HandlerId),
/// Request to destroy agent
Destroy,
}

Expand Down

0 comments on commit da9f05d

Please sign in to comment.