Skip to content

Instantly share code, notes, and snippets.

@Ivshti
Created January 13, 2019 13:05
Show Gist options
  • Select an option

  • Save Ivshti/356a38b0e00de0b2e2152ded13f66b11 to your computer and use it in GitHub Desktop.

Select an option

Save Ivshti/356a38b0e00de0b2e2152ded13f66b11 to your computer and use it in GitHub Desktop.
fetch - get a Vec<u8>
let window = web_sys::window().unwrap();
let pr = window.fetch_with_str("https://v3-cinemeta.strem.io/catalog/movie/top.json");
let fut = JsFuture::from(pr)
.and_then(|resp_value| {
// @TODO: reduce number of copies
assert!(resp_value.is_instance_of::<Response>());
let resp: Response = resp_value.dyn_into().unwrap();
let buf_promise = resp.array_buffer().unwrap();
JsFuture::from(buf_promise).map(|buf_val| {
assert!(buf_val.is_instance_of::<ArrayBuffer>());
let typebuf: js_sys::Uint8Array = js_sys::Uint8Array::new(&buf_val);
let mut body = vec![0; typebuf.length() as usize];
typebuf.copy_to(&mut body[..]);
body
})
})
.and_then(move |body| {
let resp: CatalogResponse = serde_json::from_slice(&body).unwrap();
emit(&Action::CatalogReceived(Ok(resp)));
future::ok(())
})
.map_err(|_| ());
spawn_local(fut)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment