-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: broadcast events with streams
- Loading branch information
1 parent
d0acd80
commit 60002c8
Showing
11 changed files
with
77 additions
and
141 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ authors = [ "Paul-Nicolas Madelaine <[email protected]>" ] | |
typhon-types.workspace = true | ||
actix-cors.workspace = true | ||
actix-files.workspace = true | ||
actix-web-actors.workspace = true | ||
actix-web.workspace = true | ||
actix.workspace = true | ||
age.workspace = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use typhon_types::Event; | ||
|
||
use futures_core::stream::Stream; | ||
use tokio::sync::mpsc::*; | ||
|
||
pub struct EventLogger { | ||
senders: Vec<Sender<Event>>, | ||
shutdown: bool, | ||
} | ||
|
||
impl EventLogger { | ||
pub fn new() -> Self { | ||
Self { | ||
senders: Vec::new(), | ||
shutdown: false, | ||
} | ||
} | ||
|
||
pub async fn log(&mut self, e: Event) { | ||
let mut senders: Vec<Sender<Event>> = Vec::new(); | ||
for sender in self.senders.drain(..) { | ||
match sender.send(e.clone()).await { | ||
Ok(()) => senders.push(sender), | ||
Err(_) => (), | ||
}; | ||
} | ||
self.senders = senders; | ||
} | ||
|
||
pub fn listen(&mut self) -> Option<impl Stream<Item = Event>> { | ||
if self.shutdown { | ||
None | ||
} else { | ||
let (sender, mut receiver) = channel(256); | ||
self.senders.push(sender); | ||
Some(async_stream::stream! { | ||
while let Some(e) = receiver.recv().await { | ||
yield e; | ||
} | ||
}) | ||
} | ||
} | ||
|
||
pub fn shutdown(&mut self) { | ||
self.shutdown = true; | ||
self.senders.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters