Skip to content

Commit 79b5dfb

Browse files
committed
Address review comments
1 parent c7e46af commit 79b5dfb

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

crates/net/src/eventsource/futures.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,30 @@ use gloo_utils::errors::JsError;
4242
use pin_project::{pin_project, pinned_drop};
4343
use std::pin::Pin;
4444
use std::task::{Context, Poll};
45+
use std::fmt;
46+
use std::fmt::Formatter;
4547
use wasm_bindgen::prelude::*;
4648
use wasm_bindgen::JsCast;
4749
use web_sys::MessageEvent;
4850

4951
/// Wrapper around browser's EventSource API. Dropping
5052
/// this will close the underlying event source.
51-
#[allow(missing_debug_implementations)]
5253
#[derive(Clone)]
5354
pub struct EventSource {
5455
es: web_sys::EventSource,
5556
}
5657

58+
impl fmt::Debug for EventSource {
59+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
60+
f.debug_struct("EventSource")
61+
.field("url", &self.es.url())
62+
.field("with_credentials", &self.es.with_credentials())
63+
.field("ready_state", &self.state())
64+
.finish_non_exhaustive()
65+
}
66+
}
67+
5768
/// Wrapper around browser's EventSource API.
58-
#[allow(missing_debug_implementations)]
5969
#[pin_project(PinnedDrop)]
6070
pub struct EventSourceSubscription {
6171
#[allow(clippy::type_complexity)]
@@ -67,6 +77,15 @@ pub struct EventSourceSubscription {
6777
message_receiver: mpsc::UnboundedReceiver<StreamMessage>,
6878
}
6979

80+
impl fmt::Debug for EventSourceSubscription {
81+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
82+
f.debug_struct("EventSourceSubscription")
83+
.field("event_source", &self.es)
84+
.field("event_type", &self.event_type)
85+
.finish_non_exhaustive()
86+
}
87+
}
88+
7089
impl EventSource {
7190
/// Establish an EventSource.
7291
///
@@ -91,9 +110,8 @@ impl EventSource {
91110
/// events without an event field as well as events that have the
92111
/// specific type `event: message`. It will not trigger on any
93112
/// other event type.
94-
pub fn subscribe(&mut self, event_type: &str) -> Result<EventSourceSubscription, JsError> {
95-
let event_type = event_type.to_string();
96-
113+
pub fn subscribe(&mut self, event_type: impl Into<String>) -> Result<EventSourceSubscription, JsError> {
114+
let event_type = event_type.into();
97115
let (message_sender, message_receiver) = mpsc::unbounded();
98116

99117
let message_callback: Closure<dyn FnMut(MessageEvent)> = {
@@ -113,15 +131,14 @@ impl EventSource {
113131
.map_err(js_to_js_error)?;
114132

115133
let error_callback: Closure<dyn FnMut(web_sys::Event)> = {
116-
let sender = message_sender.clone();
117134
Closure::wrap(Box::new(move |e: web_sys::Event| {
118135
let is_connecting = e
119136
.current_target()
120137
.map(|target| target.unchecked_into::<web_sys::EventSource>())
121138
.map(|es| es.ready_state() == web_sys::EventSource::CONNECTING)
122139
.unwrap_or(false);
123140
if !is_connecting {
124-
let _ = sender.unbounded_send(StreamMessage::ErrorEvent);
141+
let _ = message_sender.unbounded_send(StreamMessage::ErrorEvent);
125142
};
126143
}) as Box<dyn FnMut(web_sys::Event)>)
127144
};

0 commit comments

Comments
 (0)