Skip to content

Commit

Permalink
Use unwrap_throw instead of unwrap (#226)
Browse files Browse the repository at this point in the history
* Use unwrap_throw in dom_node.rs

* Update .gitpod.yml

* Transition colors on header

* Use more unwrap_throws
  • Loading branch information
lukechu10 authored Sep 8, 2021
1 parent e9f7527 commit fc640d3
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1,916 deletions.
2 changes: 1 addition & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ports:
onOpen: open-preview

tasks:
- prebuild: cargo test --no-run && cd docs && trunk build # Prebuild tests and docs website
- init: cargo test --no-run && cd docs && trunk build # Prebuild tests and docs website

github:
prebuilds:
Expand Down
40 changes: 20 additions & 20 deletions packages/sycamore/src/generic_node/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl fmt::Debug for DomNode {
} else if let Some(comment) = self.node.dyn_ref::<Comment>() {
format!("<!--{}-->", comment.text_content().unwrap_or_default())
} else {
self.node.to_string().as_string().unwrap()
self.node.to_string().as_string().unwrap_throw()
};
f.debug_tuple("DomNode").field(&outer_html).finish()
}
Expand All @@ -125,7 +125,7 @@ impl fmt::Debug for DomNode {
fn document() -> web_sys::Document {
thread_local! {
/// Cache document since it is frequently accessed to prevent going through js-interop.
static DOCUMENT: web_sys::Document = web_sys::window().unwrap().document().unwrap();
static DOCUMENT: web_sys::Document = web_sys::window().unwrap_throw().document().unwrap_throw();
};
DOCUMENT.with(|document| document.clone())
}
Expand All @@ -134,9 +134,9 @@ impl GenericNode for DomNode {
fn element(tag: &str) -> Self {
let node = document()
.create_element(intern(tag))
.unwrap()
.unwrap_throw()
.dyn_into()
.unwrap();
.unwrap_throw();
DomNode {
id: Default::default(),
node,
Expand All @@ -163,19 +163,19 @@ impl GenericNode for DomNode {
self.node
.unchecked_ref::<Element>()
.set_attribute(intern(name), value)
.unwrap();
.unwrap_throw();
}

fn set_class_name(&self, value: &str) {
self.node.unchecked_ref::<Element>().set_class_name(value);
}

fn set_property(&self, name: &str, value: &JsValue) {
assert!(js_sys::Reflect::set(&self.node, &name.into(), value).unwrap());
assert!(js_sys::Reflect::set(&self.node, &name.into(), value).unwrap_throw());
}

fn append_child(&self, child: &Self) {
self.node.append_child(&child.node).unwrap();
self.node.append_child(&child.node).unwrap_throw();
}

fn first_child(&self) -> Option<Self> {
Expand All @@ -188,22 +188,22 @@ impl GenericNode for DomNode {
fn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>) {
self.node
.insert_before(&new_node.node, reference_node.map(|n| n.node.as_ref()))
.unwrap();
.unwrap_throw();
}

fn remove_child(&self, child: &Self) {
self.node.remove_child(&child.node).unwrap();
self.node.remove_child(&child.node).unwrap_throw();
}

fn replace_child(&self, old: &Self, new: &Self) {
self.node.replace_child(&new.node, &old.node).unwrap();
self.node.replace_child(&new.node, &old.node).unwrap_throw();
}

fn insert_sibling_before(&self, child: &Self) {
self.node
.unchecked_ref::<Element>()
.before_with_node_1(&child.node)
.unwrap();
.unwrap_throw();
}

fn parent_node(&self) -> Option<Self> {
Expand All @@ -228,7 +228,7 @@ impl GenericNode for DomNode {
let closure = Closure::wrap(handler);
self.node
.add_event_listener_with_callback(intern(name), closure.as_ref().unchecked_ref())
.unwrap();
.unwrap_throw();

on_cleanup(move || {
drop(closure);
Expand All @@ -245,7 +245,7 @@ impl GenericNode for DomNode {

fn clone_node(&self) -> Self {
Self {
node: self.node.clone_node_with_deep(true).unwrap(),
node: self.node.clone_node_with_deep(true).unwrap_throw(),
id: Default::default(),
}
}
Expand All @@ -256,10 +256,10 @@ impl GenericNode for DomNode {
///
/// _This API requires the following crate features to be activated: `dom`_
pub fn render(template: impl FnOnce() -> Template<DomNode>) {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let window = web_sys::window().unwrap_throw();
let document = window.document().unwrap_throw();

render_to(template, &document.body().unwrap());
render_to(template, &document.body().unwrap_throw());
}

/// Render a [`Template`] under a `parent` node.
Expand Down Expand Up @@ -297,10 +297,10 @@ pub fn render_to(template: impl FnOnce() -> Template<DomNode>, parent: &Node) {
///
/// _This API requires the following crate features to be activated: `dom`_
pub fn hydrate(template: impl FnOnce() -> Template<DomNode>) {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let window = web_sys::window().unwrap_throw();
let document = window.document().unwrap_throw();

hydrate_to(template, &document.body().unwrap());
hydrate_to(template, &document.body().unwrap_throw());
}

/// Gets the children of an [`Element`] by collecting them into a [`Vec`]. Note that the returned
Expand All @@ -312,7 +312,7 @@ fn get_children(parent: &Element) -> Vec<Element> {
let mut vec = Vec::with_capacity(children_count as usize);

for i in 0..children.length() {
vec.push(children.get_with_index(i).unwrap());
vec.push(children.get_with_index(i).unwrap_throw());
}

vec
Expand Down
16 changes: 9 additions & 7 deletions packages/sycamore/src/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use std::any::{Any, TypeId};

use wasm_bindgen::prelude::*;

use crate::prelude::*;

/// Props for [`Portal`].
Expand All @@ -19,11 +21,11 @@ pub fn portal(props: PortalProps<G>) -> Template<G> {
let PortalProps { children, selector } = props;

if TypeId::of::<G>() == TypeId::of::<DomNode>() {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let window = web_sys::window().unwrap_throw();
let document = window.document().unwrap_throw();
let container = document
.query_selector(selector)
.unwrap()
.unwrap_throw()
.expect("could not find element matching selector");

let children = children.flatten();
Expand All @@ -32,21 +34,21 @@ pub fn portal(props: PortalProps<G>) -> Template<G> {
container
.append_child(
&<dyn Any>::downcast_ref::<DomNode>(child)
.unwrap()
.unwrap_throw()
.inner_element(),
)
.unwrap();
.unwrap_throw();
}

on_cleanup(move || {
for child in &children {
container
.remove_child(
&<dyn Any>::downcast_ref::<DomNode>(child)
.unwrap()
.unwrap_throw()
.inner_element(),
)
.unwrap();
.unwrap_throw();
}
});
} else {
Expand Down
10 changes: 6 additions & 4 deletions packages/sycamore/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ pub(crate) fn run_tasks() {
drop(callback);
} else {
web_sys::window()
.unwrap()
.request_animation_frame(f.borrow().as_ref().unwrap().as_ref().unchecked_ref())
.unwrap_throw()
.request_animation_frame(
f.borrow().as_ref().unwrap_throw().as_ref().unchecked_ref(),
)
.expect("could not register requestAnimationFrame");
}
});
})));

web_sys::window()
.unwrap()
.request_animation_frame(g.borrow().as_ref().unwrap().as_ref().unchecked_ref())
.unwrap_throw()
.request_animation_frame(g.borrow().as_ref().unwrap_throw().as_ref().unchecked_ref())
.expect("could not register requestAnimationFrame");
}

Expand Down
9 changes: 5 additions & 4 deletions packages/sycamore/src/utils/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::rc::Rc;

use ahash::AHashMap;
use wasm_bindgen::UnwrapThrowExt;

use crate::generic_node::GenericNode;
use crate::reactive::create_effect;
Expand Down Expand Up @@ -274,7 +275,7 @@ pub fn reconcile_fragments<G: GenericNode>(parent: &G, a: &mut [G], b: &[G]) {
} else if b_end == b_start {
// Remove.
while a_start < a_end {
if map.is_none() || !map.as_ref().unwrap().contains_key(&a[a_start]) {
if map.is_none() || !map.as_ref().unwrap_throw().contains_key(&a[a_start]) {
parent.remove_child(&a[a_start]);
}
a_start += 1;
Expand Down Expand Up @@ -303,10 +304,10 @@ pub fn reconcile_fragments<G: GenericNode>(parent: &G, a: &mut [G], b: &[G]) {
if map.is_none() {
map = Some(AHashMap::with_capacity(b_end - b_start));
for (i, item) in b.iter().enumerate().take(b_end).skip(b_start) {
map.as_mut().unwrap().insert(item.clone(), i);
map.as_mut().unwrap_throw().insert(item.clone(), i);
}
}
let map = map.as_ref().unwrap();
let map = map.as_ref().unwrap_throw();

let index = map.get(&a[a_start]);
if let Some(index) = index {
Expand All @@ -318,7 +319,7 @@ pub fn reconcile_fragments<G: GenericNode>(parent: &G, a: &mut [G], b: &[G]) {
while i + 1 < a_end && i + 1 < b_end {
i += 1;
t = map.get(&a[i]);
if t.is_none() || *t.unwrap() != *index + sequence {
if t.is_none() || *t.unwrap_throw() != *index + sequence {
break;
}
sequence += 1;
Expand Down
Loading

0 comments on commit fc640d3

Please sign in to comment.