Skip to content

Commit

Permalink
use pubsub ipc messaging pattern instead of event
Browse files Browse the repository at this point in the history
  • Loading branch information
dmackdev committed Nov 16, 2024
1 parent dc0e039 commit 27a6151
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod source;
pub(crate) mod speaker_notes;
pub(crate) mod user;
19 changes: 13 additions & 6 deletions src/input/source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::user::{CommandKeyBindings, KeyBindingsValidationError, UserInput};
use super::{
speaker_notes::SpeakerNotesCommand,
user::{CommandKeyBindings, KeyBindingsValidationError, UserInput},
};
use crate::custom::KeyBindingsConfig;
use iceoryx2::{port::listener::Listener, service::ipc::Service};
use iceoryx2::{port::subscriber::Subscriber, service::ipc::Service};
use serde::Deserialize;
use std::{io, time::Duration};
use strum::EnumDiscriminants;
Expand All @@ -11,14 +14,14 @@ use strum::EnumDiscriminants;
/// happens.
pub struct CommandSource {
user_input: UserInput,
speaker_notes_event_receiver: Option<Listener<Service>>,
speaker_notes_event_receiver: Option<Subscriber<Service, SpeakerNotesCommand, ()>>,
}

impl CommandSource {
/// Create a new command source over the given presentation path.
pub fn new(
config: KeyBindingsConfig,
speaker_notes_event_receiver: Option<Listener<Service>>,
speaker_notes_event_receiver: Option<Subscriber<Service, SpeakerNotesCommand, ()>>,
) -> Result<Self, KeyBindingsValidationError> {
let bindings = CommandKeyBindings::try_from(config)?;
Ok(Self { user_input: UserInput::new(bindings), speaker_notes_event_receiver })
Expand All @@ -30,8 +33,12 @@ impl CommandSource {
pub(crate) fn try_next_command(&mut self) -> io::Result<Option<Command>> {
if let Some(receiver) = self.speaker_notes_event_receiver.as_mut() {
// TODO: Handle Err instead of unwrap.
if let Some(evt) = receiver.try_wait_one().unwrap() {
return Ok(Some(Command::GoToSlide(evt.as_value() as u32)));
if let Some(msg) = receiver.receive().unwrap() {
match msg.payload() {
SpeakerNotesCommand::GoToSlide(idx) => {
return Ok(Some(Command::GoToSlide(*idx)));
}
}
}
}
match self.user_input.poll_next_command(Duration::from_millis(250))? {
Expand Down
5 changes: 5 additions & 0 deletions src/input/speaker_notes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Debug)]
#[repr(C)]
pub enum SpeakerNotesCommand {
GoToSlide(u32),
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub use crate::{
demo::ThemesDemo,
execute::SnippetExecutor,
export::{ExportError, Exporter},
input::source::CommandSource,
input::{source::CommandSource, speaker_notes::SpeakerNotesCommand},
markdown::parse::MarkdownParser,
media::{graphics::GraphicsMode, printer::ImagePrinter, register::ImageRegistry},
presenter::{PresentMode, Presenter, PresenterOptions},
Expand Down
22 changes: 15 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use directories::ProjectDirs;
use iceoryx2::{
node::NodeBuilder,
prelude::ServiceName,
service::{ipc::Service, port_factory::event::PortFactory},
service::{builder::publish_subscribe::Builder, ipc::Service},
};
use presenterm::{
CommandSource, Config, Exporter, GraphicsMode, HighlightThemeSet, ImagePrinter, ImageProtocol, ImageRegistry,
MarkdownParser, PresentMode, PresentationBuilderOptions, PresentationTheme, PresentationThemeSet, Presenter,
PresenterOptions, Resources, SnippetExecutor, Themes, ThemesDemo, ThirdPartyConfigs, ThirdPartyRender,
ValidateOverflows,
PresenterOptions, Resources, SnippetExecutor, SpeakerNotesCommand, Themes, ThemesDemo, ThirdPartyConfigs,
ThirdPartyRender, ValidateOverflows,
};
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -212,10 +212,16 @@ fn overflow_validation(mode: &PresentMode, config: &ValidateOverflows) -> bool {
}
}

fn create_speaker_notes_service() -> Result<PortFactory<Service>, Box<dyn std::error::Error>> {
fn create_speaker_notes_service_builder()
-> Result<Builder<SpeakerNotesCommand, (), Service>, Box<dyn std::error::Error>> {
// TODO: Use a service name that incorporates presenterm and/or the presentation filename/title?
let service_name: ServiceName = "SpeakerNoteEventService".try_into()?;
Ok(NodeBuilder::new().create::<Service>()?.service_builder(&service_name).event().open_or_create()?)
let service = NodeBuilder::new()
.create::<Service>()?
.service_builder(&service_name)
.publish_subscribe::<SpeakerNotesCommand>()
.max_publishers(1);
Ok(service)
}

fn run(mut cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -300,15 +306,17 @@ fn run(mut cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
}
} else {
let speaker_notes_event_receiver = if let Some(SpeakerNotesMode::Receiver) = cli.speaker_notes_mode {
Some(create_speaker_notes_service()?.listener_builder().create()?)
let receiver = create_speaker_notes_service_builder()?.open()?.subscriber_builder().create()?;
Some(receiver)
} else {
None
};
let commands = CommandSource::new(config.bindings.clone(), speaker_notes_event_receiver)?;
options.print_modal_background = matches!(graphics_mode, GraphicsMode::Kitty { .. });

let speaker_notes_event_publisher = if let Some(SpeakerNotesMode::Publisher) = cli.speaker_notes_mode {
Some(create_speaker_notes_service()?.notifier_builder().create()?)
let publisher = create_speaker_notes_service_builder()?.create()?.publisher_builder().create()?;
Some(publisher)
} else {
None
};
Expand Down
14 changes: 9 additions & 5 deletions src/presenter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use iceoryx2::{port::notifier::Notifier, prelude::EventId, service::ipc::Service};
use iceoryx2::{port::publisher::Publisher, service::ipc::Service};

use crate::{
SpeakerNotesCommand,
custom::KeyBindingsConfig,
diff::PresentationDiffer,
execute::SnippetExecutor,
Expand Down Expand Up @@ -54,7 +55,7 @@ pub struct Presenter<'a> {
image_printer: Arc<ImagePrinter>,
themes: Themes,
options: PresenterOptions,
speaker_notes_event_publisher: Option<Notifier<Service>>,
speaker_notes_event_publisher: Option<Publisher<Service, SpeakerNotesCommand, ()>>,
}

impl<'a> Presenter<'a> {
Expand All @@ -70,7 +71,7 @@ impl<'a> Presenter<'a> {
themes: Themes,
image_printer: Arc<ImagePrinter>,
options: PresenterOptions,
speaker_notes_event_publisher: Option<Notifier<Service>>,
speaker_notes_event_publisher: Option<Publisher<Service, SpeakerNotesCommand, ()>>,
) -> Self {
Self {
default_theme,
Expand Down Expand Up @@ -142,8 +143,11 @@ impl<'a> Presenter<'a> {
};
}
if let Some(publisher) = self.speaker_notes_event_publisher.as_mut() {
let current_slide_idx = self.state.presentation().current_slide_index();
publisher.notify_with_custom_event_id(EventId::new(current_slide_idx + 1)).unwrap();
let current_slide_idx = self.state.presentation().current_slide_index() as u32;
// TODO: Replace unwraps.
let sample = publisher.loan_uninit().unwrap();
let sample = sample.write_payload(SpeakerNotesCommand::GoToSlide(current_slide_idx + 1));
sample.send().unwrap();
}
}
}
Expand Down

0 comments on commit 27a6151

Please sign in to comment.