Use Cell instead of RefCell (#166)

This commit is contained in:
Cecile Tonglet 2023-02-07 08:05:16 +01:00 committed by GitHub
parent 5d0bddca6b
commit b71f69ade9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 9 deletions

View file

@ -3,7 +3,8 @@
clippy::needless_update, clippy::needless_update,
clippy::inconsistent_struct_constructor, clippy::inconsistent_struct_constructor,
clippy::type_complexity, clippy::type_complexity,
clippy::derive_partial_eq_without_eq clippy::derive_partial_eq_without_eq,
clippy::uninlined_format_args
)] )]
mod button_group; mod button_group;

View file

@ -31,7 +31,7 @@ pub fn download_from_npm_package(
let version = if version.is_empty() || version == "latest" { let version = if version.is_empty() || version == "latest" {
let info: PackageInfo = let info: PackageInfo =
ureq::get(format!("https://registry.npmjs.org/{}", package_name).as_str()) ureq::get(format!("https://registry.npmjs.org/{package_name}").as_str())
.call()? .call()?
.into_json()?; .into_json()?;

View file

@ -55,7 +55,7 @@ impl Component for App {
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool { fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg { match msg {
Msg::ToggleLight => { Msg::ToggleLight => {
DARK.with(|x| x.replace_with(|&mut x| !x)); DARK.with(|x| x.replace(!x.get()));
} }
Msg::GoToMenu(event, doc_menu) => { Msg::GoToMenu(event, doc_menu) => {
event.prevent_default(); event.prevent_default();
@ -70,7 +70,7 @@ impl Component for App {
} }
fn view(&self, ctx: &Context<Self>) -> Html { fn view(&self, ctx: &Context<Self>) -> Html {
let dark = DARK.with(|x| *x.borrow()); let dark = DARK.with(|x| x.get());
let netlify_badge = if dark { let netlify_badge = if dark {
"https://www.netlify.com/img/global/badges/netlify-color-accent.svg" "https://www.netlify.com/img/global/badges/netlify-color-accent.svg"

View file

@ -4,7 +4,8 @@
dead_code, dead_code,
clippy::derive_partial_eq_without_eq, clippy::derive_partial_eq_without_eq,
// Shadows lints linked to this issue: https://github.com/yewstack/yew/issues/2931 // Shadows lints linked to this issue: https://github.com/yewstack/yew/issues/2931
clippy::let_unit_value clippy::let_unit_value,
clippy::uninlined_format_args
)] )]
mod app; mod app;
@ -39,11 +40,11 @@ mod tree;
pub use app::*; pub use app::*;
pub use example::*; pub use example::*;
pub use logo::*; pub use logo::*;
use std::cell::RefCell; use std::cell::Cell;
thread_local! { thread_local! {
pub static DARK: RefCell<bool> = { pub static DARK: Cell<bool> = {
RefCell::new(web_sys::window() Cell::new(web_sys::window()
.and_then(|x| x.match_media("(prefers-color-scheme: dark)").ok().flatten()) .and_then(|x| x.match_media("(prefers-color-scheme: dark)").ok().flatten())
.map(|x| x.matches()) .map(|x| x.matches())
.unwrap_or(true)) .unwrap_or(true))

View file

@ -63,7 +63,7 @@ impl Component for Example {
onclose={ctx.link().callback(|_| Msg::Close)} onclose={ctx.link().callback(|_| Msg::Close)}
{backdrop} {backdrop}
class={classes!( class={classes!(
DARK.with(|x| x.borrow().then_some("bp3-dark")), DARK.with(|x| x.get().then_some("bp3-dark")),
self.tall.then_some("docs-overlay-example-tall"), self.tall.then_some("docs-overlay-example-tall"),
)} )}
style="left: calc(50vw - 200px); margin: 10vh 0; top: 0; width: 400px;" style="left: calc(50vw - 200px); margin: 10vh 0; top: 0; width: 400px;"