Skip to content

Commit

Permalink
Merge pull request #6 from jucr-io/publish-immediate
Browse files Browse the repository at this point in the history
Field value publishing shouldn't await subscribers
  • Loading branch information
zeenix authored Jul 9, 2024
2 parents 4f23844 + ffae8ec commit 8cabed4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/controller/item_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,11 @@ impl Signal {
let struct_name_caps = struct_name.to_string().to_uppercase();
let method_name_caps = method_name_str.to_uppercase();
let method_name_pascal = snake_to_pascal_case(&method_name_str);
let args_channel_name = Ident::new(
let signal_channel_name = Ident::new(
&format!("{struct_name_caps}_{method_name_caps}_CHANNEL"),
method.span(),
);
let args_publisher_name = Ident::new(
let signal_publisher_name = Ident::new(
&format!("{struct_name_caps}_{method_name_caps}_PUBLISHER"),
method.span(),
);
Expand All @@ -362,12 +362,12 @@ impl Signal {
method.span(),
);

let capacity = super::ALL_CHANNEL_CAPACITY;
let capacity = super::SIGNAL_CHANNEL_CAPACITY;
let max_subscribers = super::BROADCAST_MAX_SUBSCRIBERS;
let max_publishers = super::BROADCAST_MAX_PUBLISHERS;

let declarations = quote! {
static #args_channel_name:
static #signal_channel_name:
embassy_sync::pubsub::PubSubChannel<
embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex,
#args_struct_name,
Expand All @@ -376,7 +376,7 @@ impl Signal {
#max_publishers,
> = embassy_sync::pubsub::PubSubChannel::new();

static #args_publisher_name: embassy_sync::once_lock::OnceLock<embassy_sync::pubsub::publisher::Publisher<
static #signal_publisher_name: embassy_sync::once_lock::OnceLock<embassy_sync::pubsub::publisher::Publisher<
'static,
embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex,
#args_struct_name,
Expand Down Expand Up @@ -404,7 +404,7 @@ impl Signal {

impl #subscriber_struct_name {
pub fn new() -> Option<Self> {
embassy_sync::pubsub::PubSubChannel::subscriber(&#args_channel_name)
embassy_sync::pubsub::PubSubChannel::subscriber(&#signal_channel_name)
.ok()
.map(|subscriber| Self { subscriber })
}
Expand All @@ -425,9 +425,9 @@ impl Signal {

method.block = parse_quote!({
let publisher = embassy_sync::once_lock::OnceLock::get_or_init(
&#args_publisher_name,
&#signal_publisher_name,
// Safety: The publisher is only initialized once.
|| embassy_sync::pubsub::PubSubChannel::publisher(&#args_channel_name).unwrap());
|| embassy_sync::pubsub::PubSubChannel::publisher(&#signal_channel_name).unwrap());
embassy_sync::pubsub::publisher::Pub::publish(
publisher,
#args_struct_name { #(#names),* },
Expand Down
4 changes: 2 additions & 2 deletions src/controller/item_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ impl PublishedField {
previous: value,
new: core::clone::Clone::clone(&self.#field_name),
};
embassy_sync::pubsub::publisher::Pub::publish(
embassy_sync::pubsub::publisher::Pub::publish_immediate(
&self.#publisher_name,
change,
).await;
);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ pub(crate) mod item_impl;
pub(crate) mod item_struct;

const ALL_CHANNEL_CAPACITY: usize = 1;
const SIGNAL_CHANNEL_CAPACITY: usize = 8;
const BROADCAST_MAX_PUBLISHERS: usize = 1;
const BROADCAST_MAX_SUBSCRIBERS: usize = 16;

0 comments on commit 8cabed4

Please sign in to comment.