1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// See: https://github.com/rust-lang/rust/issues/44732#issuecomment-488766871
//
#![ cfg_attr( nightly, feature(doc_cfg, external_doc) ) ]
#![ cfg_attr( nightly, doc(include = "../README.md")  ) ]
#![doc = ""] // empty doc line to handle missing doc warning when the feature is missing.

#![ doc    ( html_root_url = "https://docs.rs/ws_stream_wasm"            ) ]
#![ forbid ( unsafe_code                                                 ) ]
#![ allow  ( clippy::suspicious_else_formatting, clippy::needless_return ) ]


#![ warn
(
	missing_debug_implementations ,
	missing_docs                  ,
	nonstandard_style             ,
	rust_2018_idioms              ,
	trivial_casts                 ,
	trivial_numeric_casts         ,
	unused_extern_crates          ,
	unused_qualifications         ,
	single_use_lifetimes          ,
	unreachable_pub               ,
	variant_size_differences      ,
)]



mod error        ;
mod ws_event     ;
mod ws_message   ;
mod ws_meta      ;
mod ws_state     ;
mod ws_stream    ;
mod ws_stream_io ;

pub use
{
	error        :: { WsErr               } ,
	ws_event     :: { WsEvent, CloseEvent } ,
	ws_message   :: { WsMessage           } ,
	ws_meta      :: { WsMeta              } ,
	ws_state     :: { WsState             } ,
	ws_stream    :: { WsStream            } ,
	ws_stream_io :: { WsStreamIo          } ,
};



mod import
{
	pub(crate) use
	{
		futures              :: { prelude::{ Stream, Sink }, ready                                       } ,
		futures              :: { StreamExt, SinkExt                                                     } ,
		std                  :: { io, collections::VecDeque, fmt, task::{ Context, Waker, Poll }         } ,
		std                  :: { rc::Rc, cell::{ RefCell }, pin::Pin, convert::{ TryFrom, TryInto }     } ,
		js_sys               :: { ArrayBuffer, Uint8Array                                                } ,
		wasm_bindgen         :: { closure::Closure, JsCast, JsValue, UnwrapThrowExt                      } ,
		web_sys              :: { *, BinaryType, Blob, WebSocket, CloseEvent as JsCloseEvt, DomException } ,
		js_sys               :: { Array                                                                  } ,
		pharos               :: { Pharos, Observable, Filter, ObserveConfig, Events                      } ,
		wasm_bindgen_futures :: { spawn_local                                                            } ,
		async_io_stream      :: { IoStream                                                               } ,
		thiserror            :: { Error                                                                  } ,
		send_wrapper         :: { SendWrapper                                                            } ,
	};
}


use import::*;

/// Helper function to reduce code bloat
//
pub(crate) fn notify( pharos: SendWrapper< Rc<RefCell<Pharos<WsEvent>>> >, evt: WsEvent )
{
	let notify = async move
	{
		let mut pharos = pharos.borrow_mut();

		pharos.send( evt ).await

			.map_err( |e| unreachable!( "{:?}", e ) ).unwrap(); // only happens if we closed it.
	};

	spawn_local( notify );
}