1
1
use futures:: Future ;
2
- use graphql_client_web :: * ;
2
+ use graphql_client :: GraphQLQuery ;
3
3
use lazy_static:: * ;
4
4
use std:: cell:: RefCell ;
5
5
use std:: sync:: Mutex ;
6
6
use wasm_bindgen:: prelude:: * ;
7
+ use wasm_bindgen:: JsCast ;
7
8
use wasm_bindgen_futures:: future_to_promise;
8
9
9
10
#[ derive( GraphQLQuery ) ]
@@ -23,7 +24,7 @@ lazy_static! {
23
24
}
24
25
25
26
fn 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" ) ;
27
28
let variables = puppy_smiles:: Variables {
28
29
after : LAST_ENTRY
29
30
. lock ( )
@@ -48,27 +49,31 @@ fn load_more() -> impl Future<Item = JsValue, Error = JsValue> {
48
49
49
50
fn document ( ) -> web_sys:: Document {
50
51
web_sys:: window ( )
51
- . expect ( "no window" )
52
+ . expect_throw ( "no window" )
52
53
. document ( )
53
- . expect ( "no document" )
54
+ . expect_throw ( "no document" )
54
55
}
55
56
56
57
fn add_load_more_button ( ) {
57
58
let btn = document ( )
58
59
. create_element ( "button" )
59
- . expect ( "could not create button" ) ;
60
+ . expect_throw ( "could not create button" ) ;
60
61
btn. set_inner_html ( "I WANT MORE PUPPIES" ) ;
61
62
let on_click = Closure :: wrap (
62
63
Box :: new ( move || future_to_promise ( load_more ( ) ) ) as Box < FnMut ( ) -> js_sys:: Promise >
63
64
) ;
64
65
btn. add_event_listener_with_callback (
65
66
"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" ) ,
67
71
)
68
- . expect ( "could not add event listener to load more button" ) ;
72
+ . expect_throw ( "could not add event listener to load more button" ) ;
69
73
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" ) ;
72
77
73
78
on_click. forget ( ) ;
74
79
}
@@ -78,27 +83,27 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
78
83
79
84
log ( & format ! ( "response body\n \n {:?}" , response) ) ;
80
85
81
- let parent = document ( ) . body ( ) . expect ( "no body" ) ;
86
+ let parent = document ( ) . body ( ) . expect_throw ( "no body" ) ;
82
87
83
88
let json: graphql_client_web:: Response < puppy_smiles:: ResponseData > = response;
84
89
let response = document ( )
85
90
. create_element ( "div" )
86
- . expect ( "could not create div" ) ;
91
+ . expect_throw ( "could not create div" ) ;
87
92
let mut inner_html = String :: new ( ) ;
88
93
let listings = json
89
94
. data
90
- . expect ( "response data" )
95
+ . expect_throw ( "response data" )
91
96
. reddit
92
- . expect ( "reddit" )
97
+ . expect_throw ( "reddit" )
93
98
. subreddit
94
- . expect ( "puppy smiles subreddit" )
99
+ . expect_throw ( "puppy smiles subreddit" )
95
100
. new_listings ;
96
101
97
102
let new_cursor: Option < String > = listings[ listings. len ( ) - 1 ]
98
103
. as_ref ( )
99
104
. map ( |puppy| puppy. fullname_id . clone ( ) )
100
105
. to_owned ( ) ;
101
- LAST_ENTRY . lock ( ) . unwrap ( ) . replace ( new_cursor) ;
106
+ LAST_ENTRY . lock ( ) . unwrap_throw ( ) . replace ( new_cursor) ;
102
107
103
108
for puppy in & listings {
104
109
if let Some ( puppy) = puppy {
@@ -114,7 +119,7 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
114
119
"# ,
115
120
puppy. title, puppy. url, puppy. title
116
121
)
117
- . expect ( "write to string" ) ;
122
+ . expect_throw ( "write to string" ) ;
118
123
}
119
124
}
120
125
response. set_inner_html ( & format ! (
@@ -123,20 +128,20 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
123
128
) ) ;
124
129
parent
125
130
. append_child ( & response)
126
- . expect ( "could not append response" ) ;
131
+ . expect_throw ( "could not append response" ) ;
127
132
}
128
133
129
- #[ wasm_bindgen]
134
+ #[ wasm_bindgen( start ) ]
130
135
pub fn run ( ) {
131
136
log ( "Hello there" ) ;
132
137
let message_area = document ( )
133
138
. create_element ( "div" )
134
- . expect ( "could not create div" ) ;
139
+ . expect_throw ( "could not create div" ) ;
135
140
message_area. set_inner_html ( "<p>good morning</p>" ) ;
136
- let parent = document ( ) . body ( ) . unwrap ( ) ;
141
+ let parent = document ( ) . body ( ) . unwrap_throw ( ) ;
137
142
parent
138
143
. append_child ( & message_area)
139
- . expect ( "could not append message area" ) ;
144
+ . expect_throw ( "could not append message area" ) ;
140
145
141
146
load_more ( ) ;
142
147
add_load_more_button ( ) ;
0 commit comments