Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loader hack to include components #1

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/button.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
use wasm_bindgen::prelude::*;
use yew::prelude::*;

#[wasm_bindgen(module = "/build/button.js")]
extern "C" {
#[derive(Debug)]
type Button;

// This needs to be added to each component
#[wasm_bindgen(getter, static_method_of = Button)]
fn _dummy_loader() -> JsValue;
}

// call the macro with the type
loader_hack!(Button);

pub struct ButtonComponent {}

pub enum Msg {}
Expand All @@ -9,15 +23,19 @@ impl Component for ButtonComponent {
type Properties = ();

fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
ButtonComponent {}
// call this every time you need the type to be loaded.
// don't worry, this only runs once
Button::ensure_loaded();
Self {}
}

fn update(&mut self, _msg: Self::Message) -> ShouldRender {
false
}

fn change(&mut self, _props: Self::Properties) -> bool { false }

fn change(&mut self, _props: Self::Properties) -> bool {
false
}

fn view(&self) -> Html {
html! {
Expand Down
22 changes: 14 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// this macro is defined here so we can access it in the modules
macro_rules! loader_hack {
($ty:ty) => {
static LOADED: std::sync::Once = std::sync::Once::new();
impl $ty {
fn ensure_loaded() {
LOADED.call_once(|| {
<$ty>::_dummy_loader();
});
}
}
};
}

mod button;
pub use button::ButtonComponent;

use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "/build/button.js")]
extern "C" {
#[derive(Debug)]
type Button;
}