wasm_ws/event.rs
1// Copyright (c) 2019-2022 Naja Melan
2// Copyright (c) 2023-2024 Yuki Kishimoto
3// Distributed under the MIT software license
4
5use web_sys::CloseEvent as JsCloseEvt;
6
7use crate::WsErr;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum WsEvent {
11 /// The connection is now Open and ready for use.
12 //
13 Open,
14 /// An error happened on the connection. For more information about when this event
15 /// occurs, see the [HTML Living Standard](https://html.spec.whatwg.org/multipage/web-sockets.html).
16 /// Since the browser is not allowed to convey any information to the client code as to why an error
17 /// happened (for security reasons), as described in the HTML specification, there usually is no extra
18 /// information available. That's why this event has no data attached to it.
19 //
20 Error,
21 /// The connection has started closing, but is not closed yet. You shouldn't try to send messages over
22 /// it anymore. Trying to do so will result in an error.
23 //
24 Closing,
25 /// The connection was closed. The enclosed [`CloseEvent`] has some extra information.
26 //
27 Closed(CloseEvent),
28 /// An error happened, not on the connection, but inside _ws_stream_wasm_. This currently happens
29 /// when an incoming message can not be converted to Rust types, eg. a String message with invalid
30 /// encoding.
31 //
32 WsErr(WsErr),
33}
34
35impl WsEvent {
36 /// Predicate indicating whether this is a [WsEvent::Open] event. Can be used as a filter for the
37 /// event stream obtained with [`pharos::Observable::observe`] on [`WsMeta`](crate::WsMeta).
38 //
39 pub fn is_open(&self) -> bool {
40 matches!(self, Self::Open)
41 }
42
43 /// Predicate indicating whether this is a [WsEvent::Error] event. Can be used as a filter for the
44 /// event stream obtained with [`pharos::Observable::observe`] on [`WsMeta`](crate::WsMeta).
45 //
46 pub fn is_err(&self) -> bool {
47 matches!(self, Self::Error)
48 }
49
50 /// Predicate indicating whether this is a [WsEvent::Closing] event. Can be used as a filter for the
51 /// event stream obtained with [`pharos::Observable::observe`] on [`WsMeta`](crate::WsMeta).
52 //
53 pub fn is_closing(&self) -> bool {
54 matches!(self, Self::Closing)
55 }
56
57 /// Predicate indicating whether this is a [WsEvent::Closed] event. Can be used as a filter for the
58 /// event stream obtained with [`pharos::Observable::observe`] on [`WsMeta`](crate::WsMeta).
59 //
60 pub fn is_closed(&self) -> bool {
61 matches!(self, Self::Closed(_))
62 }
63
64 /// Predicate indicating whether this is a [WsEvent::WsErr] event. Can be used as a filter for the
65 /// event stream obtained with [`pharos::Observable::observe`] on [`WsMeta`](crate::WsMeta).
66 //
67 pub fn is_ws_err(&self) -> bool {
68 matches!(self, Self::WsErr(_))
69 }
70}
71
72/// An event holding information about how/why the connection was closed.
73// We use this wrapper because the web_sys version isn't Send and pharos requires events
74// to be Send.
75#[derive(Clone, Debug, PartialEq, Eq)]
76//
77pub struct CloseEvent {
78 /// The close code.
79 /// See: [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close).
80 //
81 pub code: u16,
82 /// The reason why the connection was closed.
83 /// See: [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close).
84 //
85 pub reason: String,
86 /// Whether the connection was closed cleanly.
87 /// See: [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close).
88 //
89 pub was_clean: bool,
90}
91
92impl From<JsCloseEvt> for CloseEvent {
93 fn from(js_evt: JsCloseEvt) -> Self {
94 Self {
95 code: js_evt.code(),
96 reason: js_evt.reason(),
97 was_clean: js_evt.was_clean(),
98 }
99 }
100}