[][src]Trait acteur::Listen

pub trait Listen<M: Debug> where
    Self: Service
{ #[must_use] pub fn handle<'life0, 'life1, 'async_trait>(
        &'life0 self,
        message: M,
        system: &'life1 ServiceAssistant<Self>
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: 'async_trait
; }

This Trait allow Services to receive messages.This is the most efficient way to process messages as it doesn't require to respond the message.

If you want to respond to messages, use the Serve trait.

This trait is compatible with Serve trait as you can implement, for the same message, both traits. This trait will be executed when using the "call" or "call_sync" method from Acteur or the "call" method from ActorAssistant.

use acteur::{Acteur, Service, Listen, ServiceAssistant, ServiceConfiguration};
use async_std::sync::Mutex;

#[derive(Debug)]
struct EmployeeExpensesCalculator {
    // Services have concurrency, therefore, your state needs to be under a Mutex.
    // Highly recommended using an async Mutex instead of Rust Mutex
    employee_expenses: Mutex<f32>,
}

#[async_trait::async_trait]
impl Service for EmployeeExpensesCalculator {
    async fn initialize(system: &ServiceAssistant<Self>) -> (Self, ServiceConfiguration) {
        let service = EmployeeExpensesCalculator {
            employee_expenses: Mutex::new(0.0),
        };
        let service_conf = ServiceConfiguration::default();
        (service, service_conf)
    }
}

#[derive(Debug)]
struct EmployeeHired(f32);

#[async_trait::async_trait]
impl Listen<EmployeeHired> for EmployeeExpensesCalculator {
    async fn handle(&self, message: EmployeeHired, _: &ServiceAssistant<Self>) {
        *self.employee_expenses.lock().await += message.0;
    }
}
fn main() {
    let sys = Acteur::new();
    sys.send_to_service_sync::<EmployeeExpensesCalculator, _>(EmployeeHired(55000.0));
    sys.stop();
    sys.wait_until_stopped();
}

Required methods

#[must_use]pub fn handle<'life0, 'life1, 'async_trait>(
    &'life0 self,
    message: M,
    system: &'life1 ServiceAssistant<Self>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
    'life0: 'async_trait,
    'life1: 'async_trait,
    Self: 'async_trait, 
[src]

Loading content...

Implementors

Loading content...