@@ -42,20 +42,30 @@ use gloo_utils::errors::JsError;
42
42
use pin_project:: { pin_project, pinned_drop} ;
43
43
use std:: pin:: Pin ;
44
44
use std:: task:: { Context , Poll } ;
45
+ use std:: fmt;
46
+ use std:: fmt:: Formatter ;
45
47
use wasm_bindgen:: prelude:: * ;
46
48
use wasm_bindgen:: JsCast ;
47
49
use web_sys:: MessageEvent ;
48
50
49
51
/// Wrapper around browser's EventSource API. Dropping
50
52
/// this will close the underlying event source.
51
- #[ allow( missing_debug_implementations) ]
52
53
#[ derive( Clone ) ]
53
54
pub struct EventSource {
54
55
es : web_sys:: EventSource ,
55
56
}
56
57
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
+
57
68
/// Wrapper around browser's EventSource API.
58
- #[ allow( missing_debug_implementations) ]
59
69
#[ pin_project( PinnedDrop ) ]
60
70
pub struct EventSourceSubscription {
61
71
#[ allow( clippy:: type_complexity) ]
@@ -67,6 +77,15 @@ pub struct EventSourceSubscription {
67
77
message_receiver : mpsc:: UnboundedReceiver < StreamMessage > ,
68
78
}
69
79
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
+
70
89
impl EventSource {
71
90
/// Establish an EventSource.
72
91
///
@@ -91,9 +110,8 @@ impl EventSource {
91
110
/// events without an event field as well as events that have the
92
111
/// specific type `event: message`. It will not trigger on any
93
112
/// 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 ( ) ;
97
115
let ( message_sender, message_receiver) = mpsc:: unbounded ( ) ;
98
116
99
117
let message_callback: Closure < dyn FnMut ( MessageEvent ) > = {
@@ -113,15 +131,14 @@ impl EventSource {
113
131
. map_err ( js_to_js_error) ?;
114
132
115
133
let error_callback: Closure < dyn FnMut ( web_sys:: Event ) > = {
116
- let sender = message_sender. clone ( ) ;
117
134
Closure :: wrap ( Box :: new ( move |e : web_sys:: Event | {
118
135
let is_connecting = e
119
136
. current_target ( )
120
137
. map ( |target| target. unchecked_into :: < web_sys:: EventSource > ( ) )
121
138
. map ( |es| es. ready_state ( ) == web_sys:: EventSource :: CONNECTING )
122
139
. unwrap_or ( false ) ;
123
140
if !is_connecting {
124
- let _ = sender . unbounded_send ( StreamMessage :: ErrorEvent ) ;
141
+ let _ = message_sender . unbounded_send ( StreamMessage :: ErrorEvent ) ;
125
142
} ;
126
143
} ) as Box < dyn FnMut ( web_sys:: Event ) > )
127
144
} ;
0 commit comments