Crate orsomafo

source ·
Expand description

§Orsomafofo

Orsomafofo is a event dispatcher

Events are dispatchable across threads. Handlers are executed asynchronously

§Example


// Event must be
// - serializable
// - deserializable
// - cloneable
#[derive(Clone, Debug,serde::Serialize, serde::Deserialize )] // Event must be cloneable
struct MyEvent;

impl orsomafo::Dispatchable for MyEvent {} // MyEvent is now dispatchable

 // create a handler
 #[derive(Default)]  // Event handler must implement default
 struct MyEventHandler;
    
 #[orsomafo::async_trait]
  impl orsomafo::EventHandler for MyEventHandler {
       // called when event from "MyEvent" is dispatched
       async fn handle(&self, dispatched: &DispatchedEvent)  {
          let event: MyEvent = dispatched.the_event().unwrap();  // Get the instance of "MyEvent"
          println!("handled my event: {:#?}",event);
       }
   }

 #[tokio::main]
 async fn main() {
   MyEvent::subscribe::<MyEventHandler>().await;

   let event = MyEvent;
   event.dispatch_event();

   // The following line is use to pause the application for
  // few milliseconds. This will allow us to handle all dispatched events.
  // In a full application, this line wil not be require.
  sleep(Duration::from_millis(100)).await;

}

§Example (The long way)


// Event must be
// - serializable
// - deserializable
// - cloneable
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] // Event must be clonable
struct MyEvent;

impl orsomafo::Dispatchable for MyEvent {} // MyEvent is now dispatchable

 // create a handler
 #[derive(Default)]  // Event handler must implement default
 struct MyEventHandler;
    
 #[orsomafo::async_trait]
  impl orsomafo::EventHandler for MyEventHandler {
       // called when event from "MyEvent" is dispatched
       async fn handle(&self, dispatched: &DispatchedEvent)  {
          let event: MyEvent = dispatched.the_event().unwrap();  // Get the instance of "MyEvent"
          println!("handled my event: {:#?}",event);
       }
   }

 #[tokio::main]
 async fn main() {
  _ =  EventDispatcherBuilder::new()
        .listen::<MyEvent, MyEventHandler>() // Register "MyEventHandler" for "MyEvent"
        .build().await;

   let event = MyEvent;
   event.dispatch_event();

   // The following line is use to pause the application for
  // few milliseconds. This will allow us to handle all dispatched events.
  // In a full application, this line wil not be require.
  sleep(Duration::from_millis(100)).await;
}

Re-exports§

Structs§

Traits§

Functions§

Attribute Macros§