[][src]Trait acteur::Notify

pub trait Notify<M: Debug> where
    Self: Service
{ #[must_use] 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 Assistant.

use acteur::{Acteur, Service, Notify, 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 Notify<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]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, 

Loading content...

Implementors

Loading content...