Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some simple proposed renames #751

Merged
merged 4 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/multi_thread/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/multi_thread/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/multi_thread/src/native_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/routing/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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);
}

Expand Down
66 changes: 33 additions & 33 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,25 @@ where
let scope = AgentScope::<T>::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<u8>| {
let msg = ToWorker::<T::Input>::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
Expand Down Expand Up @@ -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)
}
Expand All @@ -304,7 +304,7 @@ struct SlabResponder<AGN: Agent> {
}

impl<AGN: Agent> Responder<AGN> for SlabResponder<AGN> {
fn response(&self, id: HandlerId, output: AGN::Output) {
fn respond(&self, id: HandlerId, output: AGN::Output) {
locate_callback_and_respond::<AGN>(&self.slab, id, output);
}
}
Expand All @@ -330,7 +330,7 @@ struct ContextBridge<AGN: Agent> {

impl<AGN: Agent> Bridge<AGN> for ContextBridge<AGN> {
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);
}
}
Expand All @@ -346,11 +346,11 @@ impl<AGN: Agent> Drop for ContextBridge<AGN> {
}
};

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::<LocalAgent<AGN>>();
}
Expand All @@ -368,9 +368,9 @@ impl Discoverer for Job {
let scope = AgentScope::<AGN>::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)
Expand All @@ -384,7 +384,7 @@ struct CallbackResponder<AGN: Agent> {
}

impl<AGN: Agent> Responder<AGN> for CallbackResponder<AGN> {
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);
}
Expand All @@ -396,16 +396,16 @@ struct JobBridge<AGN: Agent> {

impl<AGN: Agent> Bridge<AGN> for JobBridge<AGN> {
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<AGN: Agent> Drop for JobBridge<AGN> {
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);
}
}
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -733,7 +733,7 @@ impl<AGN: Agent> AgentScope<AGN> {
AgentScope { shared_agent }
}
/// Schedule message for sending to agent
pub fn send(&self, update: AgentUpdate<AGN>) {
pub fn send(&self, update: AgentLifecycleEvent<AGN>) {
let envelope = AgentEnvelope {
shared_agent: self.shared_agent.clone(),
update,
Expand All @@ -752,13 +752,13 @@ impl<AGN: Agent> Default for AgentScope<AGN> {
/// 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);
fn respond(&self, id: HandlerId, output: AGN::Output);
}

struct WorkerResponder {}

impl<AGN: Agent> Responder<AGN> 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! {
Expand Down Expand Up @@ -787,8 +787,8 @@ impl<AGN: Agent> AgentLink<AGN> {
}

/// 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.
Expand All @@ -799,7 +799,7 @@ impl<AGN: Agent> AgentLink<AGN> {
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()
}
Expand Down Expand Up @@ -828,7 +828,7 @@ impl<AGN> AgentRunnable<AGN> {

/// Local Agent messages
#[derive(Debug)]
pub enum AgentUpdate<AGN: Agent> {
pub enum AgentLifecycleEvent<AGN: Agent> {
/// Request to create link
Create(AgentLink<AGN>),
/// Internal Agent message
Expand All @@ -845,7 +845,7 @@ pub enum AgentUpdate<AGN: Agent> {

struct AgentEnvelope<AGN: Agent> {
shared_agent: Shared<AgentRunnable<AGN>>,
update: AgentUpdate<AGN>,
update: AgentLifecycleEvent<AGN>,
}

impl<AGN> Runnable for AgentEnvelope<AGN>
Expand All @@ -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()
Expand Down