Skip to content

Commit c11db08

Browse files
authored
feat: Return the channel selection on the reducer apply (#26)
1 parent 11a0932 commit c11db08

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dioxus-radio"
3-
version = "0.5.3"
3+
version = "0.6.0"
44
edition = "2021"
55
description = "Fully-typed global state management with a topics subscription system for Dioxus 🧬"
66
readme = "./README.md"

src/hooks/use_radio.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,15 @@ where
360360
pub fn write_with_channel_selection(
361361
&mut self,
362362
cb: impl FnOnce(&mut Value) -> ChannelSelection<Channel>,
363-
) {
363+
) -> ChannelSelection<Channel> {
364364
let value = self.antenna.peek().station.value.write_unchecked();
365365
let mut guard = RadioGuard {
366366
channels: Vec::default(),
367367
antenna: self.antenna,
368368
value,
369369
};
370370
let channel_selection = cb(&mut guard.value);
371-
let channel = match channel_selection {
371+
let channel = match channel_selection.clone() {
372372
ChannelSelection::Current => Some(self.antenna.peek().channel.clone()),
373373
ChannelSelection::Silence => None,
374374
ChannelSelection::Select(c) => Some(c),
@@ -379,6 +379,8 @@ where
379379
}
380380
self.antenna.peek().station.cleanup();
381381
}
382+
383+
channel_selection
382384
}
383385

384386
/// Modify the state silently, no component will be notified.
@@ -422,6 +424,24 @@ impl<Channel> ChannelSelection<Channel> {
422424
pub fn silence(&mut self) {
423425
*self = Self::Silence
424426
}
427+
428+
/// Check if it is of type [ChannelSelection::Current]
429+
pub fn is_current(&self) -> bool {
430+
matches!(self, Self::Current)
431+
}
432+
433+
/// Check if it is of type [ChannelSelection::Select] and return the channel.
434+
pub fn is_select(&self) -> Option<&Channel> {
435+
match self {
436+
Self::Select(channel) => Some(channel),
437+
_ => None,
438+
}
439+
}
440+
441+
/// Check if it is of type [ChannelSelection::Silence]
442+
pub fn is_silence(&self) -> bool {
443+
matches!(self, Self::Silence)
444+
}
425445
}
426446

427447
/// Consume the state and subscribe using the given `channel`
@@ -476,8 +496,9 @@ pub trait DataReducer {
476496

477497
pub trait RadioReducer {
478498
type Action;
499+
type Channel;
479500

480-
fn apply(&mut self, action: Self::Action);
501+
fn apply(&mut self, action: Self::Action) -> ChannelSelection<Self::Channel>;
481502
}
482503

483504
impl<
@@ -487,9 +508,10 @@ impl<
487508
> RadioReducer for Radio<Data, Channel>
488509
{
489510
type Action = Action;
511+
type Channel = Channel;
490512

491-
fn apply(&mut self, action: Action) {
492-
self.write_with_channel_selection(|data| data.reduce(action));
513+
fn apply(&mut self, action: Action) -> ChannelSelection<Channel> {
514+
self.write_with_channel_selection(|data| data.reduce(action))
493515
}
494516
}
495517

0 commit comments

Comments
 (0)