Hello
I am trying to save the response of my server into a variable outside of wasm_futures scope to return it to update function.but the value of target variable (response
) won't change.
I am guessing that the problem is in async scope, because send_request
function won't wait for updated response
value and just returning the default vale (noenter
).
do you know how can I solve this problem?
here is my whole code:
#[derive(Properties,PartialEq)]
pub struct Props{
pub url:String,
}
pub struct Page{
inner_text:String
}
pub enum Msg{
GetRequest,
SendRequest
}
impl Component for Page {
type Message=Msg;
type Properties=Props;
fn create(ctx: &Context<Self>) -> Self {
log!(ctx.props().url.clone());
let url = ctx.deref().props().url.clone();
log!(&url);
let request_handle = {
let link = ctx.link().clone();
Interval::new(5000,move|| { link.send_message(Msg::SendRequest)})
};
request_handle.forget();
Self{inner_text:"".to_owned()}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<>
<ToolBar url={"test"}/>
<div>
<div class="container-fluid">
<textarea id="textarea" class={classes!("textarea",css!("white-space:pre-wrap;"))} value={self.inner_text.clone()} >
</textarea>
</div>
</div>
</>
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::GetRequest => todo!(),
Msg::SendRequest =>{
let response = self.send_request( ctx.props().url.clone());
self.inner_text=response.clone();
true
},
}
}
}
impl Page {
pub fn send_request(self: &mut Self ,url:String)->String{
let url = url.clone();
let response = Arc::new(Mutex::new(String::from("noenter")));
let response_clone = Arc::clone(&response);
wasm_bindgen_futures::spawn_local(async move{
let _url = format!("http://127.0.0.1:3000/{}",url);
// log!(&_url);
// log!("sdfasdfas");
let res = Request::get(_url.as_str())
.send()
.await
.unwrap()
.text()
.await
.unwrap();
let mut response = response_clone.lock().unwrap();
*response = res;
});
let response = response.lock().unwrap();
let x = response.clone();
x
}
}