Skip to content

Commit

Permalink
Rename EmitterToken -> EmitterHandle
Browse files Browse the repository at this point in the history
I think the name is a bit more obvious/consistent.
  • Loading branch information
LucasPickering committed Jan 1, 2025
1 parent 098817a commit 8eb735c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions crates/tui/src/view/common/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
view::{
context::UpdateContext,
draw::{Draw, DrawMetadata},
event::{Child, Emitter, EmitterToken, Event, EventHandler, Update},
event::{Child, Emitter, EmitterHandle, Event, EventHandler, Update},
util::centered_rect,
Component, ViewContext,
},
Expand Down Expand Up @@ -209,7 +209,7 @@ pub struct ModalHandle<T: Emitter> {
/// plumbing but would not actually accomplish anything. Once a modal is
/// closed, it won't be emitting anymore so there's no harm in hanging onto
/// its ID.
emitter: Option<EmitterToken<T::Emitted>>,
emitter: Option<EmitterHandle<T::Emitted>>,
}

impl<T: Emitter> ModalHandle<T> {
Expand All @@ -222,7 +222,7 @@ impl<T: Emitter> ModalHandle<T> {
where
T: 'static + Modal,
{
self.emitter = Some(modal.detach());
self.emitter = Some(modal.handle());
ViewContext::open_modal(modal);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/view/common/text_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl TextBox {
if let Some(debounce) = &self.on_change_debounce {
if self.is_valid() {
// Defer the change event until after the debounce period
let emitter = self.detach();
let emitter = self.handle();
debounce.start(move || emitter.emit(TextBoxEvent::Change));
} else {
debounce.cancel();
Expand Down
6 changes: 3 additions & 3 deletions crates/tui/src/view/component/recipe_pane/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
context::UpdateContext,
draw::{Draw, DrawMetadata, Generate},
event::{
Child, Emitter, EmitterId, EmitterToken, Event, EventHandler,
Child, Emitter, EmitterHandle, EmitterId, Event, EventHandler,
Update,
},
state::fixed_select::FixedSelectState,
Expand Down Expand Up @@ -101,7 +101,7 @@ impl EventHandler for AuthenticationDisplay {
fn update(&mut self, _: &mut UpdateContext, event: Event) -> Update {
let action = event.action();
if let Some(Action::Edit) = action {
self.state.open_edit_modal(self.detach());
self.state.open_edit_modal(self.handle());
} else if let Some(Action::Reset) = action {
self.state.reset_override();
} else if let Some(SaveAuthenticationOverride(value)) =
Expand Down Expand Up @@ -218,7 +218,7 @@ impl State {
/// Open a modal to let the user edit temporary override values
fn open_edit_modal(
&self,
emitter: EmitterToken<SaveAuthenticationOverride>,
emitter: EmitterHandle<SaveAuthenticationOverride>,
) {
let (label, value) = match &self {
Self::Basic {
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/view/component/recipe_pane/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl RawBody {
return;
};

let emitter = self.detach();
let emitter = self.handle();
ViewContext::send_message(Message::FileEdit {
path,
on_complete: Box::new(move |path| {
Expand Down
6 changes: 3 additions & 3 deletions crates/tui/src/view/component/recipe_pane/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
context::UpdateContext,
draw::{Draw, DrawMetadata, Generate},
event::{
Child, Emitter, EmitterId, EmitterToken, Event, EventHandler,
Child, Emitter, EmitterHandle, EmitterId, Event, EventHandler,
Update,
},
state::select::{SelectState, SelectStateEvent, SelectStateEventType},
Expand Down Expand Up @@ -118,7 +118,7 @@ where
// Consume the event even if we have no rows, for
// consistency
if let Some(selected_row) = self.select.data().selected() {
selected_row.open_edit_modal(self.detach());
selected_row.open_edit_modal(self.handle());
}
}
Action::Reset => {
Expand Down Expand Up @@ -266,7 +266,7 @@ impl<K: PersistedKey<Value = bool>> RowState<K> {
}

/// Open a modal to create or edit the value's temporary override
fn open_edit_modal(&self, emitter: EmitterToken<SaveRecipeTableOverride>) {
fn open_edit_modal(&self, emitter: EmitterHandle<SaveRecipeTableOverride>) {
let index = self.index;
ViewContext::open_modal(TextBoxModal::new(
format!("Edit value for {}", self.key),
Expand Down
10 changes: 5 additions & 5 deletions crates/tui/src/view/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ pub trait Emitter {
/// Get a lightweight version of this emitter, with the same type and ID but
/// datched from this lifetime. Useful for both emitting and consuming
/// emissions from across task boundaries, modals, etc.
fn detach(&self) -> EmitterToken<Self::Emitted> {
EmitterToken {
fn handle(&self) -> EmitterHandle<Self::Emitted> {
EmitterHandle {
id: self.id(),
phantom: PhantomData,
}
Expand Down Expand Up @@ -364,20 +364,20 @@ impl Default for EmitterId {
/// A lightweight copy of an emitter that can be passed between threads or
/// callbacks. This has the same emitting capability as the source emitter
/// because it contains its ID and is bound to its emitted event type. Generate
/// via [Emitter::detach].
/// via [Emitter::handle].
///
/// It would be good to impl `!Send` for this type because it shouldn't be
/// passed between threads, but there is one use case where it needs to be Send
/// to be passed to the main loop via Message, but doesn't actually change
/// threads. !Send is more correct because if you try to emit from another
/// thread it'll panic, because the event queue isn't available there.
#[derive(Debug)]
pub struct EmitterToken<T> {
pub struct EmitterHandle<T> {
id: EmitterId,
phantom: PhantomData<T>,
}

impl<T: LocalEvent> Emitter for EmitterToken<T> {
impl<T: LocalEvent> Emitter for EmitterHandle<T> {
type Emitted = T;

fn id(&self) -> EmitterId {
Expand Down

0 comments on commit 8eb735c

Please sign in to comment.