From 8b46aac3585d5ce413d0efaf84e8e34471ad87a4 Mon Sep 17 00:00:00 2001 From: Maxim Vorobjov Date: Fri, 1 Nov 2019 23:30:52 +0300 Subject: [PATCH] Allow to extend agent communication from external crates (#721) * 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 --- src/agent.rs | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index 421b93b7858..4c3285828df 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -18,23 +18,33 @@ use stdweb::Value; #[allow(unused_imports)] use stdweb::{_js_impl, js}; -#[derive(Serialize, Deserialize)] -enum ToWorker { +/// Serializable messages to worker +#[derive(Serialize, Deserialize, Debug)] +pub enum ToWorker { + /// 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 { +/// Serializable messages sent by worker to consumer +#[derive(Serialize, Deserialize, Debug)] +pub enum FromWorker { /// 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 fn pack(&self) -> Vec; + /// Unpack deserializable message of byte slice fn unpack(data: &[u8]) -> Self; } @@ -713,12 +723,13 @@ impl Clone for AgentScope { } impl AgentScope { - 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) { + /// Schedule message for sending to agent + pub fn send(&self, update: AgentUpdate) { let envelope = AgentEnvelope { shared_agent: self.shared_agent.clone(), update, @@ -728,7 +739,15 @@ impl AgentScope { } } -trait Responder { +impl Default for AgentScope { + fn default() -> Self { + Self::new() + } +} + +/// Defines communication from Worker to Consumers +pub trait Responder { + /// Implementation for communication channel from Worker to Consumers fn response(&self, id: HandlerId, output: AGN::Output); } @@ -753,7 +772,7 @@ pub struct AgentLink { impl AgentLink { /// Create link for a scope. - fn connect(scope: &AgentScope, responder: T) -> Self + pub fn connect(scope: &AgentScope, responder: T) -> Self where T: Responder + 'static, { @@ -804,12 +823,20 @@ impl AgentRunnable { } } -enum AgentUpdate { +/// Local Agent messages +#[derive(Debug)] +pub enum AgentUpdate { + /// Request to create link Create(AgentLink), + /// 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, }