11use futures:: Future ;
2- use graphql_client_web :: * ;
2+ use graphql_client :: GraphQLQuery ;
33use lazy_static:: * ;
44use std:: cell:: RefCell ;
55use std:: sync:: Mutex ;
66use wasm_bindgen:: prelude:: * ;
7+ use wasm_bindgen:: JsCast ;
78use wasm_bindgen_futures:: future_to_promise;
89
910#[ derive( GraphQLQuery ) ]
@@ -23,7 +24,7 @@ lazy_static! {
2324}
2425
2526fn load_more ( ) -> impl Future < Item = JsValue , Error = JsValue > {
26- let client = graphql_client_web :: Client :: new ( "https://www.graphqlhub.com/graphql" ) ;
27+ let client = graphql_client :: web :: Client :: new ( "https://www.graphqlhub.com/graphql" ) ;
2728 let variables = puppy_smiles:: Variables {
2829 after : LAST_ENTRY
2930 . lock ( )
@@ -48,27 +49,31 @@ fn load_more() -> impl Future<Item = JsValue, Error = JsValue> {
4849
4950fn document ( ) -> web_sys:: Document {
5051 web_sys:: window ( )
51- . expect ( "no window" )
52+ . expect_throw ( "no window" )
5253 . document ( )
53- . expect ( "no document" )
54+ . expect_throw ( "no document" )
5455}
5556
5657fn add_load_more_button ( ) {
5758 let btn = document ( )
5859 . create_element ( "button" )
59- . expect ( "could not create button" ) ;
60+ . expect_throw ( "could not create button" ) ;
6061 btn. set_inner_html ( "I WANT MORE PUPPIES" ) ;
6162 let on_click = Closure :: wrap (
6263 Box :: new ( move || future_to_promise ( load_more ( ) ) ) as Box < FnMut ( ) -> js_sys:: Promise >
6364 ) ;
6465 btn. add_event_listener_with_callback (
6566 "click" ,
66- js_sys:: Function :: try_from ( & on_click. as_ref ( ) ) . expect ( "on click is not a Function" ) ,
67+ & on_click
68+ . as_ref ( )
69+ . dyn_ref ( )
70+ . expect_throw ( "on click is not a Function" ) ,
6771 )
68- . expect ( "could not add event listener to load more button" ) ;
72+ . expect_throw ( "could not add event listener to load more button" ) ;
6973
70- let doc = document ( ) . body ( ) . expect ( "no body" ) ;
71- doc. append_child ( & btn) . expect ( "could not append button" ) ;
74+ let doc = document ( ) . body ( ) . expect_throw ( "no body" ) ;
75+ doc. append_child ( & btn)
76+ . expect_throw ( "could not append button" ) ;
7277
7378 on_click. forget ( ) ;
7479}
@@ -78,27 +83,27 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
7883
7984 log ( & format ! ( "response body\n \n {:?}" , response) ) ;
8085
81- let parent = document ( ) . body ( ) . expect ( "no body" ) ;
86+ let parent = document ( ) . body ( ) . expect_throw ( "no body" ) ;
8287
8388 let json: graphql_client_web:: Response < puppy_smiles:: ResponseData > = response;
8489 let response = document ( )
8590 . create_element ( "div" )
86- . expect ( "could not create div" ) ;
91+ . expect_throw ( "could not create div" ) ;
8792 let mut inner_html = String :: new ( ) ;
8893 let listings = json
8994 . data
90- . expect ( "response data" )
95+ . expect_throw ( "response data" )
9196 . reddit
92- . expect ( "reddit" )
97+ . expect_throw ( "reddit" )
9398 . subreddit
94- . expect ( "puppy smiles subreddit" )
99+ . expect_throw ( "puppy smiles subreddit" )
95100 . new_listings ;
96101
97102 let new_cursor: Option < String > = listings[ listings. len ( ) - 1 ]
98103 . as_ref ( )
99104 . map ( |puppy| puppy. fullname_id . clone ( ) )
100105 . to_owned ( ) ;
101- LAST_ENTRY . lock ( ) . unwrap ( ) . replace ( new_cursor) ;
106+ LAST_ENTRY . lock ( ) . unwrap_throw ( ) . replace ( new_cursor) ;
102107
103108 for puppy in & listings {
104109 if let Some ( puppy) = puppy {
@@ -114,7 +119,7 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
114119 "# ,
115120 puppy. title, puppy. url, puppy. title
116121 )
117- . expect ( "write to string" ) ;
122+ . expect_throw ( "write to string" ) ;
118123 }
119124 }
120125 response. set_inner_html ( & format ! (
@@ -123,20 +128,20 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
123128 ) ) ;
124129 parent
125130 . append_child ( & response)
126- . expect ( "could not append response" ) ;
131+ . expect_throw ( "could not append response" ) ;
127132}
128133
129- #[ wasm_bindgen]
134+ #[ wasm_bindgen( start ) ]
130135pub fn run ( ) {
131136 log ( "Hello there" ) ;
132137 let message_area = document ( )
133138 . create_element ( "div" )
134- . expect ( "could not create div" ) ;
139+ . expect_throw ( "could not create div" ) ;
135140 message_area. set_inner_html ( "<p>good morning</p>" ) ;
136- let parent = document ( ) . body ( ) . unwrap ( ) ;
141+ let parent = document ( ) . body ( ) . unwrap_throw ( ) ;
137142 parent
138143 . append_child ( & message_area)
139- . expect ( "could not append message area" ) ;
144+ . expect_throw ( "could not append message area" ) ;
140145
141146 load_more ( ) ;
142147 add_load_more_button ( ) ;
0 commit comments