Skip to content

Commit

Permalink
Reduce copying of search term string by using Rc
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Mar 20, 2020
1 parent 11c5418 commit 7755f68
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/components/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use web_sys::{HtmlElement, KeyboardEvent};
use yew::{
html,
Expand All @@ -19,15 +21,15 @@ pub struct App {
link: ComponentLink<Self>,
input_ref: NodeRef,
router: Box<dyn Bridge<RouteAgent>>,
search_query: String,
search_query: Rc<String>,

_key_listener_handle: KeyListenerHandle,
}

pub enum Msg {
Update,
FocusInput,
Search(String),
Search(Rc<String>),
}

impl Component for App {
Expand All @@ -52,7 +54,7 @@ impl Component for App {
link,
input_ref: NodeRef::default(),
router,
search_query: String::new(),
search_query: Rc::new(String::new()),
_key_listener_handle,
}
}
Expand Down Expand Up @@ -93,7 +95,7 @@ impl Component for App {
html! {
<>
<Header input_ref=self.input_ref.clone()
oninput=self.link.callback(|e: InputData| Msg::Search(e.value)) />
oninput=self.link.callback(|e: InputData| Msg::Search(Rc::new(e.value))) />
<ExtLinks />
<div class="page">
<Router render=render_route />
Expand Down
4 changes: 2 additions & 2 deletions src/components/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use std::{rc::Rc, time::Duration};

use yew::{
html,
Expand Down Expand Up @@ -35,7 +35,7 @@ pub enum Msg {

#[derive(Clone, Properties)]
pub struct Props {
pub search_query: String,
pub search_query: Rc<String>,
}

const BATCH_SIZE: usize = 12;
Expand Down

0 comments on commit 7755f68

Please sign in to comment.