From bf06e36eb7df4a5a84e34416d9ef531a79c4bccc Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sun, 8 Dec 2019 11:03:35 -0800 Subject: [PATCH] Some simple proposed renames (#751) * AgentUpdate -> AgentLifecycleEvent * Responder::response -> Responder::respond * AgentLink::response -> AgentLink::respond * Agent::handle -> Agent::handle_input --- README.md | 4 +- examples/multi_thread/src/context.rs | 4 +- examples/multi_thread/src/job.rs | 4 +- examples/multi_thread/src/native_worker.rs | 4 +- examples/routing/src/router.rs | 10 ++-- src/agent.rs | 66 +++++++++++----------- 6 files changed, 46 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index d7cc42f29..76ddfdca4 100644 --- a/README.md +++ b/README.md @@ -171,10 +171,10 @@ impl Agent for Worker { fn update(&mut self, msg: Self::Message) { /* ... */ } // Handle incoming messages from components of other agents. - fn handle(&mut self, msg: Self::Input, who: HandlerId) { + fn handle_input(&mut self, msg: Self::Input, who: HandlerId) { match msg { Request::Question(_) => { - self.link.response(who, Response::Answer("That's cool!".into())); + self.link.respond(who, Response::Answer("That's cool!".into())); }, } } diff --git a/examples/multi_thread/src/context.rs b/examples/multi_thread/src/context.rs index d5bec7bfe..c93a9461c 100644 --- a/examples/multi_thread/src/context.rs +++ b/examples/multi_thread/src/context.rs @@ -55,11 +55,11 @@ impl Agent for Worker { } } - fn handle(&mut self, msg: Self::Input, who: HandlerId) { + fn handle_input(&mut self, msg: Self::Input, who: HandlerId) { info!("Request: {:?}", msg); match msg { Request::GetDataFromServer => { - self.link.response(who, Response::DataFetched); + self.link.respond(who, Response::DataFetched); } } } diff --git a/examples/multi_thread/src/job.rs b/examples/multi_thread/src/job.rs index f8fda50ec..a9d21aa41 100644 --- a/examples/multi_thread/src/job.rs +++ b/examples/multi_thread/src/job.rs @@ -55,11 +55,11 @@ impl Agent for Worker { } } - fn handle(&mut self, msg: Self::Input, who: HandlerId) { + fn handle_input(&mut self, msg: Self::Input, who: HandlerId) { info!("Request: {:?}", msg); match msg { Request::GetDataFromServer => { - self.link.response(who, Response::DataFetched); + self.link.respond(who, Response::DataFetched); } } } diff --git a/examples/multi_thread/src/native_worker.rs b/examples/multi_thread/src/native_worker.rs index 83f6a809e..723d54cec 100644 --- a/examples/multi_thread/src/native_worker.rs +++ b/examples/multi_thread/src/native_worker.rs @@ -55,11 +55,11 @@ impl Agent for Worker { } } - fn handle(&mut self, msg: Self::Input, who: HandlerId) { + fn handle_input(&mut self, msg: Self::Input, who: HandlerId) { info!("Request: {:?}", msg); match msg { Request::GetDataFromServer => { - self.link.response(who, Response::DataFetched); + self.link.respond(who, Response::DataFetched); } } } diff --git a/examples/routing/src/router.rs b/examples/routing/src/router.rs index fe9915750..60ef1fd1b 100644 --- a/examples/routing/src/router.rs +++ b/examples/routing/src/router.rs @@ -137,13 +137,13 @@ where let mut route = Route::current_route(&self.route_service); route.state = state; for sub in self.subscribers.iter() { - self.link.response(*sub, route.clone()); + self.link.respond(*sub, route.clone()); } } } } - fn handle(&mut self, msg: Self::Input, who: HandlerId) { + fn handle_input(&mut self, msg: Self::Input, who: HandlerId) { info!("Request: {:?}", msg); match msg { Request::ChangeRoute(route) => { @@ -154,7 +154,7 @@ where let route = Route::current_route(&self.route_service); // broadcast it to all listening components for sub in self.subscribers.iter() { - self.link.response(*sub, route.clone()); + self.link.respond(*sub, route.clone()); } } Request::ChangeRouteNoBroadcast(route) => { @@ -163,14 +163,14 @@ where } Request::GetCurrentRoute => { let route = Route::current_route(&self.route_service); - self.link.response(who, route.clone()); + self.link.respond(who, route.clone()); } } } fn connected(&mut self, id: HandlerId) { self.link - .response(id, Route::current_route(&self.route_service)); + .respond(id, Route::current_route(&self.route_service)); self.subscribers.insert(id); } diff --git a/src/agent.rs b/src/agent.rs index 5560c1dca..fe2508de6 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -142,25 +142,25 @@ where let scope = AgentScope::::new(); let responder = WorkerResponder {}; let link = AgentLink::connect(&scope, responder); - let upd = AgentUpdate::Create(link); + let upd = AgentLifecycleEvent::Create(link); scope.send(upd); let handler = move |data: Vec| { let msg = ToWorker::::unpack(&data); match msg { ToWorker::Connected(id) => { - let upd = AgentUpdate::Connected(id); + let upd = AgentLifecycleEvent::Connected(id); scope.send(upd); } ToWorker::ProcessInput(id, value) => { - let upd = AgentUpdate::Input(value, id); + let upd = AgentLifecycleEvent::Input(value, id); scope.send(upd); } ToWorker::Disconnected(id) => { - let upd = AgentUpdate::Disconnected(id); + let upd = AgentLifecycleEvent::Disconnected(id); scope.send(upd); } ToWorker::Destroy => { - let upd = AgentUpdate::Destroy; + let upd = AgentLifecycleEvent::Destroy; scope.send(upd); js! { // Terminates web worker @@ -288,10 +288,10 @@ impl Discoverer for Context { }); if let Some((scope, responder)) = scope_to_init { let agent_link = AgentLink::connect(&scope, responder); - let upd = AgentUpdate::Create(agent_link); + let upd = AgentLifecycleEvent::Create(agent_link); scope.send(upd); } - let upd = AgentUpdate::Connected(bridge.id); + let upd = AgentLifecycleEvent::Connected(bridge.id); bridge.scope.send(upd); Box::new(bridge) } @@ -304,7 +304,7 @@ struct SlabResponder { } impl Responder for SlabResponder { - fn response(&self, id: HandlerId, output: AGN::Output) { + fn respond(&self, id: HandlerId, output: AGN::Output) { locate_callback_and_respond::(&self.slab, id, output); } } @@ -330,7 +330,7 @@ struct ContextBridge { impl Bridge for ContextBridge { fn send(&mut self, msg: AGN::Input) { - let upd = AgentUpdate::Input(msg, self.id); + let upd = AgentLifecycleEvent::Input(msg, self.id); self.scope.send(upd); } } @@ -346,11 +346,11 @@ impl Drop for ContextBridge { } }; - let upd = AgentUpdate::Disconnected(self.id); + let upd = AgentLifecycleEvent::Disconnected(self.id); self.scope.send(upd); if terminate_worker { - let upd = AgentUpdate::Destroy; + let upd = AgentLifecycleEvent::Destroy; self.scope.send(upd); pool.borrow_mut().remove::>(); } @@ -368,9 +368,9 @@ impl Discoverer for Job { let scope = AgentScope::::new(); let responder = CallbackResponder { callback }; let agent_link = AgentLink::connect(&scope, responder); - let upd = AgentUpdate::Create(agent_link); + let upd = AgentLifecycleEvent::Create(agent_link); scope.send(upd); - let upd = AgentUpdate::Connected(SINGLETON_ID); + let upd = AgentLifecycleEvent::Connected(SINGLETON_ID); scope.send(upd); let bridge = JobBridge { scope }; Box::new(bridge) @@ -384,7 +384,7 @@ struct CallbackResponder { } impl Responder for CallbackResponder { - fn response(&self, id: HandlerId, output: AGN::Output) { + fn respond(&self, id: HandlerId, output: AGN::Output) { assert_eq!(id.raw_id(), SINGLETON_ID.raw_id()); self.callback.emit(output); } @@ -396,16 +396,16 @@ struct JobBridge { impl Bridge for JobBridge { fn send(&mut self, msg: AGN::Input) { - let upd = AgentUpdate::Input(msg, SINGLETON_ID); + let upd = AgentLifecycleEvent::Input(msg, SINGLETON_ID); self.scope.send(upd); } } impl Drop for JobBridge { fn drop(&mut self) { - let upd = AgentUpdate::Disconnected(SINGLETON_ID); + let upd = AgentLifecycleEvent::Disconnected(SINGLETON_ID); self.scope.send(upd); - let upd = AgentUpdate::Destroy; + let upd = AgentLifecycleEvent::Destroy; self.scope.send(upd); } } @@ -692,7 +692,7 @@ pub trait Agent: Sized + 'static { fn connected(&mut self, _id: HandlerId) {} /// This method called on every incoming message. - fn handle(&mut self, msg: Self::Input, id: HandlerId); + fn handle_input(&mut self, msg: Self::Input, id: HandlerId); /// This method called on when a new bridge destroyed. fn disconnected(&mut self, _id: HandlerId) {} @@ -733,7 +733,7 @@ impl AgentScope { AgentScope { shared_agent } } /// Schedule message for sending to agent - pub fn send(&self, update: AgentUpdate) { + pub fn send(&self, update: AgentLifecycleEvent) { let envelope = AgentEnvelope { shared_agent: self.shared_agent.clone(), update, @@ -752,13 +752,13 @@ impl Default for AgentScope { /// 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); + fn respond(&self, id: HandlerId, output: AGN::Output); } struct WorkerResponder {} impl Responder for WorkerResponder { - fn response(&self, id: HandlerId, output: AGN::Output) { + fn respond(&self, id: HandlerId, output: AGN::Output) { let msg = FromWorker::ProcessOutput(id, output); let data = msg.pack(); js! { @@ -787,8 +787,8 @@ impl AgentLink { } /// Send response to an agent. - pub fn response(&self, id: HandlerId, output: AGN::Output) { - self.responder.response(id, output); + pub fn respond(&self, id: HandlerId, output: AGN::Output) { + self.responder.respond(id, output); } /// Create a callback which will send a message to the agent when invoked. @@ -799,7 +799,7 @@ impl AgentLink { let scope = self.scope.clone(); let closure = move |input| { let output = function(input); - scope.send(AgentUpdate::Message(output)); + scope.send(AgentLifecycleEvent::Message(output)); }; closure.into() } @@ -828,7 +828,7 @@ impl AgentRunnable { /// Local Agent messages #[derive(Debug)] -pub enum AgentUpdate { +pub enum AgentLifecycleEvent { /// Request to create link Create(AgentLink), /// Internal Agent message @@ -845,7 +845,7 @@ pub enum AgentUpdate { struct AgentEnvelope { shared_agent: Shared>, - update: AgentUpdate, + update: AgentLifecycleEvent, } impl Runnable for AgentEnvelope @@ -858,34 +858,34 @@ where return; } match self.update { - AgentUpdate::Create(link) => { + AgentLifecycleEvent::Create(link) => { this.agent = Some(AGN::create(link)); } - AgentUpdate::Message(msg) => { + AgentLifecycleEvent::Message(msg) => { this.agent .as_mut() .expect("agent was not created to process messages") .update(msg); } - AgentUpdate::Connected(id) => { + AgentLifecycleEvent::Connected(id) => { this.agent .as_mut() .expect("agent was not created to send a connected message") .connected(id); } - AgentUpdate::Input(inp, id) => { + AgentLifecycleEvent::Input(inp, id) => { this.agent .as_mut() .expect("agent was not created to process inputs") - .handle(inp, id); + .handle_input(inp, id); } - AgentUpdate::Disconnected(id) => { + AgentLifecycleEvent::Disconnected(id) => { this.agent .as_mut() .expect("agent was not created to send a disconnected message") .disconnected(id); } - AgentUpdate::Destroy => { + AgentLifecycleEvent::Destroy => { let mut agent = this .agent .take()