Revert "Use unwrap instead of unwrap_throw so that we get stacktraces"

This reverts commit 6f45e6b774.
This commit is contained in:
Michael Pfaff 2022-05-31 17:09:14 -04:00
parent 6f45e6b774
commit 61d917a69e
Signed by: michael
GPG Key ID: CF402C4A012AA9D4
10 changed files with 70 additions and 69 deletions

View File

@ -49,7 +49,7 @@ struct User {
impl User {
async fn fetch(user: &str) -> Result<Self, JsValue> {
let user = fetch_github(&format!("https://api.github.com/users/{}", user)).await?;
Ok(serde_json::from_str::<Self>(&user).unwrap())
Ok(serde_json::from_str::<Self>(&user).unwrap_throw())
}
}

View File

@ -112,7 +112,7 @@ pub async fn fetch_github(url: &str) -> Result<String, JsValue> {
headers.set("Accept", "application/vnd.github.v3+json")?;
let future = window()
.unwrap()
.unwrap_throw()
.fetch_with_str_and_init(
url,
RequestInit::new()
@ -131,7 +131,7 @@ pub async fn fetch_github(url: &str) -> Result<String, JsValue> {
let value = JsFuture::from(response.text()?)
.await?
.as_string()
.unwrap();
.unwrap_throw();
Ok(value)
}

View File

@ -21,7 +21,7 @@ pub enum Route {
impl Route {
// This could use more advanced URL parsing, but it isn't needed
pub fn from_url(url: &str) -> Self {
let url = Url::new(&url).unwrap();
let url = Url::new(&url).unwrap_throw();
match url.hash().as_str() {
"#/active" => Route::Active,
"#/completed" => Route::Completed,
@ -72,7 +72,7 @@ impl App {
pub fn deserialize() -> Arc<Self> {
local_storage()
.get_item("todos-rust-dominator")
.unwrap()
.unwrap_throw()
.and_then(|state_json| {
serde_json::from_str(state_json.as_str()).ok()
})
@ -80,11 +80,11 @@ impl App {
}
pub fn serialize(&self) {
let state_json = serde_json::to_string(self).unwrap();
let state_json = serde_json::to_string(self).unwrap_throw();
local_storage()
.set_item("todos-rust-dominator", state_json.as_str())
.unwrap();
.unwrap_throw();
}
pub fn route(&self) -> impl Signal<Item = Route> {

View File

@ -126,7 +126,7 @@ impl Todo {
.event(clone!(todo => move |event: events::KeyDown| {
match event.key().as_str() {
"Enter" => {
element.blur().unwrap();
element.blur().unwrap_throw();
},
"Escape" => {
todo.cancel_editing();

View File

@ -3,7 +3,7 @@ use web_sys::{window, Storage};
pub fn local_storage() -> Storage {
window().unwrap().local_storage().unwrap().unwrap()
window().unwrap_throw().local_storage().unwrap_throw().unwrap_throw()
}
#[inline]

View File

@ -11,7 +11,7 @@ use futures_signals::signal::{Signal, SignalExt, WaitFor, MutableSignal, Mutable
use futures_signals::signal_vec::{SignalVec, VecDiff};
use discard::DiscardOnDrop;
use pin_project::pin_project;
use wasm_bindgen::JsCast;
use wasm_bindgen::{JsCast, UnwrapThrowExt};
use wasm_bindgen::closure::Closure;
use web_sys::window;
@ -35,9 +35,9 @@ impl Raf {
fn schedule(callback: &Closure<dyn FnMut(f64)>) -> i32 {
window()
.unwrap()
.unwrap_throw()
.request_animation_frame(callback.as_ref().unchecked_ref())
.unwrap()
.unwrap_throw()
}
let closure = {
@ -46,7 +46,7 @@ impl Raf {
Closure::wrap(Box::new(move |time| {
{
let mut state = state.borrow_mut();
let state = state.as_mut().unwrap();
let state = state.as_mut().unwrap_throw();
state.id = schedule(&state.closure);
}
@ -66,12 +66,12 @@ impl Raf {
impl Drop for Raf {
fn drop(&mut self) {
// The take is necessary in order to prevent an Rc leak
let state = self.state.borrow_mut().take().unwrap();
let state = self.state.borrow_mut().take().unwrap_throw();
window()
.unwrap()
.unwrap_throw()
.cancel_animation_frame(state.id)
.unwrap();
.unwrap_throw();
}
}
@ -128,7 +128,7 @@ impl Signal for Timestamps {
// TODO implement Poll::Ready(None)
fn poll_change(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let mut lock = self.state.lock().unwrap();
let mut lock = self.state.lock().unwrap_throw();
if lock.changed {
lock.changed = false;
@ -164,7 +164,7 @@ pub fn timestamps() -> Timestamps {
lock.states.retain(|state| {
if let Some(state) = state.upgrade() {
let mut lock = state.lock().unwrap();
let mut lock = state.lock().unwrap_throw();
lock.changed = true;
lock.value = Some(time);
@ -367,7 +367,7 @@ impl<A, F, S> SignalVec for AnimatedMap<S, F>
},
VecDiff::UpdateAt { index, value } => {
let index = Self::find_index(&animations, index).unwrap();
let index = Self::find_index(&animations, index).unwrap_throw();
let state = {
let state = &animations[index];
AnimatedMapBroadcaster(state.animation.raw_clone())
@ -379,7 +379,7 @@ impl<A, F, S> SignalVec for AnimatedMap<S, F>
// TODO test this
// TODO should this be treated as a removal + insertion ?
VecDiff::Move { old_index, new_index } => {
let old_index = Self::find_index(&animations, old_index).unwrap();
let old_index = Self::find_index(&animations, old_index).unwrap_throw();
let state = animations.remove(old_index);
@ -391,7 +391,7 @@ impl<A, F, S> SignalVec for AnimatedMap<S, F>
},
VecDiff::RemoveAt { index } => {
let index = Self::find_index(&animations, index).unwrap();
let index = Self::find_index(&animations, index).unwrap_throw();
if Self::should_remove(&mut animations, cx, index) {
Self::remove_index(&mut animations, index)
@ -402,7 +402,7 @@ impl<A, F, S> SignalVec for AnimatedMap<S, F>
},
VecDiff::Pop {} => {
let index = Self::find_last_index(&animations).unwrap();
let index = Self::find_last_index(&animations).unwrap_throw();
if Self::should_remove(&mut animations, cx, index) {
Self::remove_index(&mut animations, index)
@ -533,12 +533,12 @@ impl MutableTimestamps<F> where F: FnMut(f64) {
}
pub fn stop(&self) {
let mut lock = self.animating.lock().unwrap();
let mut lock = self.animating.lock().unwrap_throw();
*lock = None;
}
pub fn start(&self) {
let mut lock = self.animating.lock().unwrap();
let mut lock = self.animating.lock().unwrap_throw();
if let None = animating {
let callback = self.callback.clone();
@ -639,7 +639,7 @@ pub struct MutableAnimation {
impl fmt::Debug for MutableAnimation {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let state = self.inner.state.lock().unwrap();
let state = self.inner.state.lock().unwrap_throw();
fmt.debug_struct("MutableAnimation")
.field("playing", &state.playing)
@ -703,7 +703,7 @@ impl MutableAnimation {
// TODO test the performance of set_neq
if diff >= 1.0 {
{
let mut lock = state.inner.state.lock().unwrap();
let mut lock = state.inner.state.lock().unwrap_throw();
Self::stop_animating(&mut lock);
}
state.inner.value.set_neq(Percentage::new_unchecked(end));
@ -728,7 +728,7 @@ impl MutableAnimation {
pub fn set_duration(&self, duration: f64) {
debug_assert!(duration >= 0.0);
let mut lock = self.inner.state.lock().unwrap();
let mut lock = self.inner.state.lock().unwrap_throw();
if lock.duration != duration {
lock.duration = duration;
@ -738,7 +738,7 @@ impl MutableAnimation {
#[inline]
pub fn pause(&self) {
let mut lock = self.inner.state.lock().unwrap();
let mut lock = self.inner.state.lock().unwrap_throw();
if lock.playing {
lock.playing = false;
@ -748,7 +748,7 @@ impl MutableAnimation {
#[inline]
pub fn play(&self) {
let mut lock = self.inner.state.lock().unwrap();
let mut lock = self.inner.state.lock().unwrap_throw();
if !lock.playing {
lock.playing = true;
@ -765,13 +765,13 @@ impl MutableAnimation {
}
pub fn jump_to(&self, end: Percentage) {
let mut lock = self.inner.state.lock().unwrap();
let mut lock = self.inner.state.lock().unwrap_throw();
Self::_jump_to(&mut lock, &self.inner.value, end);
}
pub fn animate_to(&self, end: Percentage) {
let mut lock = self.inner.state.lock().unwrap();
let mut lock = self.inner.state.lock().unwrap_throw();
if lock.end != end {
if lock.duration <= 0.0 {

View File

@ -40,9 +40,9 @@ extern "C" {
thread_local! {
static WINDOW: Window = web_sys::window().unwrap();
static DOCUMENT: Document = WINDOW.with(|w| w.document().unwrap());
static HISTORY: History = WINDOW.with(|w| w.history().unwrap());
static WINDOW: Window = web_sys::window().unwrap_throw();
static DOCUMENT: Document = WINDOW.with(|w| w.document().unwrap_throw());
static HISTORY: History = WINDOW.with(|w| w.history().unwrap_throw());
}
pub(crate) fn window_event_target() -> EventTarget {
@ -51,7 +51,7 @@ pub(crate) fn window_event_target() -> EventTarget {
}
pub(crate) fn body() -> HtmlElement {
DOCUMENT.with(|d| d.body().unwrap())
DOCUMENT.with(|d| d.body().unwrap_throw())
}
pub(crate) fn ready_state() -> String {
@ -59,12 +59,12 @@ pub(crate) fn ready_state() -> String {
}
pub(crate) fn current_url() -> String {
WINDOW.with(|w| w.location().href().unwrap())
WINDOW.with(|w| w.location().href().unwrap_throw())
}
pub(crate) fn go_to_url(url: &str) {
HISTORY.with(|h| {
h.push_state_with_url(&JsValue::NULL, "", Some(url)).unwrap();
h.push_state_with_url(&JsValue::NULL, "", Some(url)).unwrap_throw();
});
}
@ -72,34 +72,34 @@ pub(crate) fn create_stylesheet() -> CssStyleSheet {
DOCUMENT.with(|document| {
// TODO use createElementNS ?
// TODO use dyn_into ?
let e: HtmlStyleElement = document.create_element("style").unwrap().unchecked_into();
let e: HtmlStyleElement = document.create_element("style").unwrap_throw().unchecked_into();
e.set_type("text/css");
append_child(&document.head().unwrap(), &e);
append_child(&document.head().unwrap_throw(), &e);
// TODO use dyn_into ?
e.sheet().unwrap().unchecked_into()
e.sheet().unwrap_throw().unchecked_into()
})
}
pub(crate) fn make_style_rule(sheet: &CssStyleSheet, selector: &str) -> Result<CssStyleRule, JsValue> {
let rules = sheet.css_rules().unwrap();
let rules = sheet.css_rules().unwrap_throw();
let length = rules.length();
// TODO don't return u32 ?
sheet.insert_rule_with_index(&format!("{}{{}}", selector), length)?;
// TODO use dyn_into ?
Ok(rules.get(length).unwrap().unchecked_into())
Ok(rules.get(length).unwrap_throw().unchecked_into())
}
pub(crate) fn get_element_by_id(id: &str) -> Element {
DOCUMENT.with(|d| d.get_element_by_id(id).unwrap())
DOCUMENT.with(|d| d.get_element_by_id(id).unwrap_throw())
}
pub(crate) fn create_element(name: &str) -> Element {
DOCUMENT.with(|d| d.create_element(name).unwrap())
DOCUMENT.with(|d| d.create_element(name).unwrap_throw())
}
pub(crate) fn create_element_ns(namespace: &str, name: &str) -> Element {
DOCUMENT.with(|d| d.create_element_ns(Some(namespace), name).unwrap())
DOCUMENT.with(|d| d.create_element_ns(Some(namespace), name).unwrap_throw())
}
pub(crate) fn create_text_node(value: &str) -> Text {
@ -123,67 +123,67 @@ pub(crate) fn create_empty_node() -> Node {
// TODO check that the attribute *actually* was changed
pub(crate) fn set_attribute(elem: &Element, key: &str, value: &str) {
elem.set_attribute(key, value).unwrap();
elem.set_attribute(key, value).unwrap_throw();
}
pub(crate) fn set_attribute_ns(elem: &Element, namespace: &str, key: &str, value: &str) {
elem.set_attribute_ns(Some(namespace), key, value).unwrap();
elem.set_attribute_ns(Some(namespace), key, value).unwrap_throw();
}
pub(crate) fn remove_attribute(elem: &Element, key: &str) {
elem.remove_attribute(key).unwrap();
elem.remove_attribute(key).unwrap_throw();
}
pub(crate) fn remove_attribute_ns(elem: &Element, namespace: &str, key: &str) {
elem.remove_attribute_ns(Some(namespace), key).unwrap();
elem.remove_attribute_ns(Some(namespace), key).unwrap_throw();
}
pub(crate) fn add_class(classes: &DomTokenList, value: &str) {
classes.add_1(value).unwrap();
classes.add_1(value).unwrap_throw();
}
pub(crate) fn remove_class(classes: &DomTokenList, value: &str) {
classes.remove_1(value).unwrap();
classes.remove_1(value).unwrap_throw();
}
pub(crate) fn get_style(style: &CssStyleDeclaration, name: &str) -> String {
style.get_property_value(name).unwrap()
style.get_property_value(name).unwrap_throw()
}
pub(crate) fn remove_style(style: &CssStyleDeclaration, name: &str) {
// TODO don't return String ?
style.remove_property(name).unwrap();
style.remove_property(name).unwrap_throw();
}
pub(crate) fn set_style(style: &CssStyleDeclaration, name: &str, value: &str, important: bool) {
let priority = if important { intern("important") } else { intern("") };
style.set_property_with_priority(name, value, priority).unwrap();
style.set_property_with_priority(name, value, priority).unwrap_throw();
}
pub(crate) fn insert_child_before(parent: &Node, child: &Node, other: &Node) {
// TODO don't return Node ?
parent.insert_before(child, Some(other)).unwrap();
parent.insert_before(child, Some(other)).unwrap_throw();
}
pub(crate) fn replace_child(parent: &Node, child: &Node, other: &Node) {
// TODO don't return Node ?
parent.replace_child(child, other).unwrap();
parent.replace_child(child, other).unwrap_throw();
}
pub(crate) fn append_child(parent: &Node, child: &Node) {
// TODO don't return Node ?
parent.append_child(child).unwrap();
parent.append_child(child).unwrap_throw();
}
pub(crate) fn remove_child(parent: &Node, child: &Node) {
// TODO don't return Node ?
parent.remove_child(child).unwrap();
parent.remove_child(child).unwrap_throw();
}
pub(crate) fn focus(elem: &HtmlElement) {
elem.focus().unwrap();
elem.focus().unwrap_throw();
}
pub(crate) fn blur(elem: &HtmlElement) {
elem.blur().unwrap();
elem.blur().unwrap_throw();
}

View File

@ -10,7 +10,7 @@ use futures_signals::signal_vec::SignalVec;
use futures_util::FutureExt;
use futures_channel::oneshot;
use discard::{Discard, DiscardOnDrop};
use wasm_bindgen::{JsValue, JsCast, intern};
use wasm_bindgen::{JsValue, UnwrapThrowExt, JsCast, intern};
use web_sys::{HtmlElement, Node, EventTarget, Element, CssStyleSheet, CssStyleDeclaration, ShadowRoot, ShadowRootMode, ShadowRootInit, Text};
use crate::bindings;
@ -151,7 +151,7 @@ impl Signal for IsWindowLoaded {
receiver,
_event: EventListener::once(bindings::window_event_target(), "load", move |_| {
// TODO test this
sender.send(Some(true)).unwrap();
sender.send(Some(true)).unwrap_throw();
}),
};
@ -159,7 +159,7 @@ impl Signal for IsWindowLoaded {
}
},
IsWindowLoaded::Pending { ref mut receiver, .. } => {
receiver.poll_unpin(cx).map(|x| x.unwrap())
receiver.poll_unpin(cx).map(|x| x.unwrap_throw())
},
IsWindowLoaded::Done {} => {
Poll::Ready(None)
@ -264,13 +264,13 @@ impl Dom {
#[inline]
fn create_element<A>(name: &str) -> A where A: JsCast {
// TODO use unchecked_into in release mode ?
bindings::create_element(intern(name)).dyn_into().unwrap()
bindings::create_element(intern(name)).dyn_into().unwrap_throw()
}
#[inline]
fn create_element_ns<A>(name: &str, namespace: &str) -> A where A: JsCast {
// TODO use unchecked_into in release mode ?
bindings::create_element_ns(intern(namespace), intern(name)).dyn_into().unwrap()
bindings::create_element_ns(intern(namespace), intern(name)).dyn_into().unwrap_throw()
}
@ -707,7 +707,7 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
#[inline]
#[doc(hidden)]
pub fn __internal_shadow_root(&self, mode: ShadowRootMode) -> DomBuilder<ShadowRoot> {
let shadow = self.element.as_ref().attach_shadow(&ShadowRootInit::new(mode)).unwrap();
let shadow = self.element.as_ref().attach_shadow(&ShadowRootInit::new(mode)).unwrap_throw();
DomBuilder::new(shadow)
}

View File

@ -9,6 +9,7 @@ use futures_signals::{cancelable_future, CancelableFutureHandle};
use futures_signals::signal::{Signal, SignalExt};
use futures_signals::signal_vec::{VecDiff, SignalVec, SignalVecExt};
use web_sys::Node;
use wasm_bindgen::UnwrapThrowExt;
use wasm_bindgen_futures::spawn_local;
use crate::bindings;
@ -289,7 +290,7 @@ pub(crate) fn insert_children_signal_vec<A>(element: Node, callbacks: &mut Callb
},
VecDiff::Pop {} => {
let dom = self.children.pop().unwrap();
let dom = self.children.pop().unwrap_throw();
bindings::remove_child(&self.element, &dom.element);

View File

@ -1,6 +1,6 @@
use std::mem::ManuallyDrop;
use wasm_bindgen::{JsCast, intern};
use wasm_bindgen::{JsCast, UnwrapThrowExt, intern};
use wasm_bindgen::closure::Closure;
use discard::Discard;
use web_sys::{EventTarget, Event};
@ -56,7 +56,7 @@ impl Drop for EventListener {
impl Discard for EventListener {
#[inline]
fn discard(mut self) {
let closure = self.closure.take().unwrap();
let closure = self.closure.take().unwrap_throw();
bindings::remove_event(&self.elem, &self.name, self.capture, closure.as_ref().unchecked_ref());
}
}