lps

Crates.iolps
lib.rslps
version0.1.0
sourcesrc
created_at2023-06-14 09:13:35.626457
updated_at2023-06-14 09:13:35.626457
descriptionA local publish-subscribe pattern implementation
homepage
repositoryhttps://github.com/alexanderved/lps
max_upload_size
id889930
size69,039
(alexanderved)

documentation

README

lps

A local publish-subscribe pattern implementation

Example

use lps::*;

use std::sync::Arc;

#[derive(Debug, Clone, Copy)]
struct Msg(i32);

impl Message for Msg {}

struct Pub {
    msg_broker: Arc<dyn MessageBroker>,
}

impl Pub {
    fn new(msg_broker: Arc<dyn MessageBroker>) -> Self {
        Self { msg_broker }
    }
}

impl Publisher for Pub {
    fn message_broker(&self) -> Arc<dyn MessageBroker> {
        Arc::clone(&self.msg_broker)
    }

    fn set_message_broker(&mut self, msg_broker: Arc<dyn MessageBroker>) {
        self.msg_broker = msg_broker;
    }
}

struct Sub {
    sub: Subscription<Msg>,
}

impl Sub {
    fn new() -> Self {
        Self {
            sub: Subscription::unregistered(),
        }
    }
}

impl Subscriber for Sub {
    fn subscribe(&mut self, msg_broker: Arc<dyn MessageBroker>) {
        let _ = self.sub.register(msg_broker);
    }

    fn unsubscribe(&mut self) {
        let _ = self.sub.unregister();
    }

    fn activate(&self) {
        let _ = self.sub.activate();
    }

    fn deactivate(&self) {
        let _ = self.sub.deactivate();
    }

    fn process_messages(&mut self) {
        self.sub
            .message_iter()
            .handle(|msg: Arc<Msg>| {
                println!("The subscriber received a message: {:?}", msg);
            })
            .run();
    }
}

let msg_broker: Arc<dyn MessageBroker> = Arc::new(DefaultMessageBroker::new());

let mut s = Sub::new();
s.subscribe(Arc::clone(&msg_broker));

let p = Pub::new(Arc::clone(&msg_broker));

p.publish(Arc::new(Msg(1)));
p.publish(Arc::new(Msg(2)));
p.publish(Arc::new(Msg(3)));

s.process_messages();

Commit count: 8

cargo fmt