1
- use seed:: browser:: service:: fetch;
1
+ // use seed::browser::service::fetch;
2
2
use seed:: { prelude:: * , * } ;
3
3
use std:: borrow:: Cow ;
4
4
@@ -18,7 +18,7 @@ fn get_request_url() -> impl Into<Cow<'static, str>> {
18
18
19
19
#[ derive( Default ) ]
20
20
pub struct Model {
21
- pub response_data_result : Option < fetch:: ResponseDataResult < String > > ,
21
+ pub fetch_result : Option < fetch:: Result < String > > ,
22
22
pub request_controller : Option < fetch:: RequestController > ,
23
23
pub status : Status ,
24
24
}
@@ -42,31 +42,31 @@ impl Default for Status {
42
42
pub enum Msg {
43
43
SendRequest ,
44
44
AbortRequest ,
45
- Fetched ( fetch:: ResponseDataResult < String > ) ,
45
+ Fetched ( fetch:: Result < String > ) ,
46
46
}
47
47
48
48
pub fn update ( msg : Msg , model : & mut Model , orders : & mut impl Orders < Msg > ) {
49
49
match msg {
50
50
Msg :: SendRequest => {
51
+ let ( request, controller) = Request :: new ( get_request_url ( ) ) . controller ( ) ;
51
52
model. status = Status :: WaitingForResponse ;
52
- model. response_data_result = None ;
53
- let request = fetch:: Request :: new ( get_request_url ( ) )
54
- . controller ( |controller| model. request_controller = Some ( controller) ) ;
55
- orders. perform_cmd ( request. fetch_string_data ( Msg :: Fetched ) ) ;
53
+ model. fetch_result = None ;
54
+ model. request_controller = Some ( controller) ;
55
+ orders. perform_cmd ( async {
56
+ Msg :: Fetched ( async { fetch ( request) . await ?. text ( ) . await } . await )
57
+ } ) ;
56
58
}
57
59
58
60
Msg :: AbortRequest => {
59
- model
60
- . request_controller
61
- . take ( )
62
- . expect ( "AbortRequest: request_controller hasn't been set!" )
63
- . abort ( ) ;
61
+ if let Some ( controller) = & model. request_controller {
62
+ controller. abort ( ) ;
63
+ }
64
64
model. status = Status :: RequestAborted ;
65
65
}
66
66
67
- Msg :: Fetched ( response_data_result ) => {
67
+ Msg :: Fetched ( fetch_result ) => {
68
68
model. status = Status :: ReadyToSendRequest ;
69
- model. response_data_result = Some ( response_data_result ) ;
69
+ model. fetch_result = Some ( fetch_result ) ;
70
70
}
71
71
}
72
72
}
@@ -79,16 +79,22 @@ pub fn view(model: &Model, intro: impl FnOnce(&str, &str) -> Vec<Node<Msg>>) ->
79
79
nodes ! [
80
80
intro( TITLE , DESCRIPTION ) ,
81
81
match model. status {
82
- Status :: ReadyToSendRequest => vec![
83
- view_response_data_result( & model. response_data_result) ,
82
+ Status :: ReadyToSendRequest => nodes![
83
+ model
84
+ . fetch_result
85
+ . as_ref( )
86
+ . map( |result| div![ format!( "{:#?}" , result) ] ) ,
84
87
button![ ev( Ev :: Click , |_| Msg :: SendRequest ) , "Send request" ] ,
85
88
] ,
86
- Status :: WaitingForResponse => vec ![
89
+ Status :: WaitingForResponse => nodes ![
87
90
div![ "Waiting for response..." ] ,
88
91
button![ ev( Ev :: Click , |_| Msg :: AbortRequest ) , "Abort request" ] ,
89
92
] ,
90
- Status :: RequestAborted => vec![
91
- view_response_data_result( & model. response_data_result) ,
93
+ Status :: RequestAborted => nodes![
94
+ model
95
+ . fetch_result
96
+ . as_ref( )
97
+ . map( |result| div![ format!( "{:#?}" , result) ] ) ,
92
98
button![
93
99
attrs! { At :: Disabled => false . as_at_value( ) } ,
94
100
"Request aborted"
@@ -97,28 +103,3 @@ pub fn view(model: &Model, intro: impl FnOnce(&str, &str) -> Vec<Node<Msg>>) ->
97
103
}
98
104
]
99
105
}
100
-
101
- fn view_response_data_result (
102
- response_data_result : & Option < fetch:: ResponseDataResult < String > > ,
103
- ) -> Node < Msg > {
104
- match & response_data_result {
105
- None => empty ! [ ] ,
106
- Some ( Ok ( response_data) ) => div ! [ format!( r#"Response String body: "{}""# , response_data) ] ,
107
- Some ( Err ( fail_reason) ) => view_fail_reason ( fail_reason) ,
108
- }
109
- }
110
-
111
- fn view_fail_reason ( fail_reason : & fetch:: FailReason < String > ) -> Node < Msg > {
112
- if let fetch:: FailReason :: RequestError ( fetch:: RequestError :: DomException ( dom_exception) , _) =
113
- fail_reason
114
- {
115
- if dom_exception. name ( ) == "AbortError" {
116
- return div ! [
117
- div![ format!( r#"Error name: "{}""# , dom_exception. name( ) ) ] ,
118
- div![ format!( r#"Error message: "{}""# , dom_exception. message( ) ) ]
119
- ] ;
120
- }
121
- }
122
- log ! ( "Example_C error:" , fail_reason) ;
123
- empty ! [ ]
124
- }
0 commit comments