move onsuccess to it's own function

This commit is contained in:
Edward Shen 2021-10-31 12:40:43 -07:00
parent 3bc4541390
commit 1d4d37b6ea
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -177,25 +177,55 @@ async fn fetch_resources(
}; };
let db_open_req = open_idb()?; let db_open_req = open_idb()?;
// On success callback let on_success = Closure::once(Box::new(move |event| {
let on_success = Closure::once(Box::new(move |event: Event| { on_success(&event, &decrypted, &expires);
let transaction: IdbObjectStore = as_idb_db(&event) }));
db_open_req.set_onsuccess(Some(on_success.into_js_value().unchecked_ref()));
db_open_req.set_onerror(Some(
Closure::once(Box::new(|e: Event| log!(e)))
.into_js_value()
.unchecked_ref(),
));
let on_upgrade = Closure::once(Box::new(move |event: Event| {
let db = as_idb_db(&event);
let _obj_store = db.create_object_store("decrypted data").unwrap();
}));
db_open_req.set_onupgradeneeded(Some(on_upgrade.into_js_value().unchecked_ref()));
}
Ok(resp) if resp.status() == StatusCode::NOT_FOUND => {
render_message("Either the paste was burned or it never existed.".into());
}
Ok(resp) if resp.status() == StatusCode::BAD_REQUEST => {
render_message("Invalid paste URL.".into());
}
Ok(err) => {
render_message(err.status_text().into());
}
Err(err) => {
render_message(format!("{}", err).into());
}
}
Ok(())
}
fn on_success(event: &Event, decrypted: &DecryptedData, expires: &str) {
let transaction: IdbObjectStore = as_idb_db(event)
.transaction_with_str_and_mode("decrypted data", IdbTransactionMode::Readwrite) .transaction_with_str_and_mode("decrypted data", IdbTransactionMode::Readwrite)
.unwrap() .unwrap()
.object_store("decrypted data") .object_store("decrypted data")
.unwrap(); .unwrap();
let decrypted_object = match &decrypted { let decrypted_object = match decrypted {
DecryptedData::String(s) => IdbObject::new() DecryptedData::String(s) => IdbObject::new()
.string() .string()
.expiration_text(&expires) .expiration_text(expires)
.data(&JsValue::from_str(s)), .data(&JsValue::from_str(s)),
DecryptedData::Blob(blob) => { DecryptedData::Blob(blob) => IdbObject::new().blob().expiration_text(expires).data(blob),
IdbObject::new().blob().expiration_text(&expires).data(blob)
}
DecryptedData::Image(blob, size) => IdbObject::new() DecryptedData::Image(blob, size) => IdbObject::new()
.image() .image()
.expiration_text(&expires) .expiration_text(expires)
.data(blob) .data(blob)
.extra( .extra(
"file_size", "file_size",
@ -203,17 +233,11 @@ async fn fetch_resources(
.get_appropriate_unit(true) .get_appropriate_unit(true)
.to_string(), .to_string(),
), ),
DecryptedData::Audio(blob) => IdbObject::new() DecryptedData::Audio(blob) => IdbObject::new().audio().expiration_text(expires).data(blob),
.audio() DecryptedData::Video(blob) => IdbObject::new().video().expiration_text(expires).data(blob),
.expiration_text(&expires)
.data(blob),
DecryptedData::Video(blob) => IdbObject::new()
.video()
.expiration_text(&expires)
.data(blob),
DecryptedData::Archive(blob, entries) => IdbObject::new() DecryptedData::Archive(blob, entries) => IdbObject::new()
.archive() .archive()
.expiration_text(&expires) .expiration_text(expires)
.data(blob) .data(blob)
.extra( .extra(
"entries", "entries",
@ -247,35 +271,4 @@ async fn fetch_resources(
.into_js_value() .into_js_value()
.unchecked_ref(), .unchecked_ref(),
)); ));
}) as Box<dyn FnOnce(Event)>);
db_open_req.set_onsuccess(Some(on_success.into_js_value().unchecked_ref()));
db_open_req.set_onerror(Some(
Closure::wrap(Box::new(|e| {
log!(e);
}) as Box<dyn Fn(Event)>)
.into_js_value()
.unchecked_ref(),
));
let on_upgrade = Closure::wrap(Box::new(move |event: Event| {
let db = as_idb_db(&event);
let _obj_store = db.create_object_store("decrypted data").unwrap();
}) as Box<dyn FnMut(Event)>);
db_open_req.set_onupgradeneeded(Some(on_upgrade.into_js_value().unchecked_ref()));
}
Ok(resp) if resp.status() == StatusCode::NOT_FOUND => {
render_message("Either the paste was burned or it never existed.".into());
}
Ok(resp) if resp.status() == StatusCode::BAD_REQUEST => {
render_message("Invalid paste URL.".into());
}
Ok(err) => {
render_message(err.status_text().into());
}
Err(err) => {
render_message(format!("{}", err).into());
}
}
Ok(())
} }