start pubsub

This commit is contained in:
eraden 2022-04-30 10:59:20 +02:00
parent 81e75a4888
commit 76d592b918
4 changed files with 54 additions and 1 deletions

9
Cargo.lock generated
View File

@ -195,6 +195,15 @@ dependencies = [
"twoway", "twoway",
] ]
[[package]]
name = "actix-pubsub"
version = "0.1.0"
dependencies = [
"actix 0.13.0",
"actix-rt",
"crossbeam-channel",
]
[[package]] [[package]]
name = "actix-redis" name = "actix-redis"
version = "0.11.0" version = "0.11.0"

View File

@ -1,2 +1,2 @@
[workspace] [workspace]
members = ["api"] members = ["api", "actix-pubsub"]

10
actix-pubsub/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "actix-pubsub"
version = "0.1.0"
edition = "2021"
[dependencies]
actix = { version = "0.13", features = [] }
actix-rt = { version = "2.7", features = [] }
crossbeam-channel = { version = "0.5" }

34
actix-pubsub/src/lib.rs Normal file
View File

@ -0,0 +1,34 @@
use std::sync::Arc;
use crossbeam_channel::{Receiver, Sender};
struct Message<Topic, Payload> {
topic: Topic,
payload: Payload,
}
pub trait PubSubBus<Topic, Payload> {
fn publisher(&self) -> Publisher<Topic, Payload>;
fn subscribe(&self, topic: Topic) -> Subscriber<Topic, Payload>;
}
pub struct PubSub<Topic, Payload> {
tx: Sender<Message<Topic, Payload>>,
rx: Receiver<Message<Topic, Payload>>,
}
pub struct Publisher<Topic, Payload> {}
pub struct Subscriber<Topic, Payload> {}
impl<Topic, Payload> PubSub<Topic, Payload> {
pub fn new() -> Self {
let (tx, rx) = crossbeam_channel::unbounded::<Message<Topic, Payload>>();
Self { rx, tx }
}
}
impl<Topic, Payload> actix::Actor for PubSub<Topic, Payload> {
type Context = actix::Context<Self>;
}