Skip to content

Commit

Permalink
Use impl_wasm_page macro
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Aug 14, 2022
1 parent 4ecd63e commit 383a38e
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 105 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sevenguis_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"

[dependencies]
sevenguis_lib = { path = "../sevenguis_lib" }
polyester = { path = "../../polyester-rs" }
polyester = { path = "../../polyester-rs/polyester" }
serde_json = "1.0.81"
3 changes: 2 additions & 1 deletion sevenguis_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
polyester = { path = "../../polyester-rs" }
polyester = { path = "../../polyester-rs/polyester" }

maud = "0.23.0"
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.81"
3 changes: 2 additions & 1 deletion sevenguis_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
sevenguis_lib = { path = "../sevenguis_lib" }
polyester = { path = "../../polyester-rs" }
polyester = { path = "../../polyester-rs/polyester" }
polyester_macro = { path = "../../polyester-rs/polyester_macro" }
serde = { version = "1.0.137", features = ["derive"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
37 changes: 6 additions & 31 deletions sevenguis_wasm/src/home_page.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
use sevenguis_lib::home_page;
use polyester::page::wasm;
use polyester::page::Page;
use polyester_macro::impl_wasm_page;
use sevenguis_lib::home_page;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct HomePage(home_page::HomePage);

#[wasm_bindgen]
impl HomePage {
#[wasm_bindgen(constructor)]
pub fn new() -> Result<HomePage, JsValue> {
Ok(HomePage(home_page::HomePage {}))
}

#[wasm_bindgen(js_name = id)]
pub fn id(&self) -> Result<String, JsValue> {
Ok(self.0.id().to_string())
}

#[wasm_bindgen(js_name = init)]
pub fn initial_model(&self) -> Result<JsValue, JsValue> {
wasm::init(&self.0)
}

#[wasm_bindgen(js_name = viewBody)]
pub fn view_body(&self, js_model: &JsValue) -> Result<String, JsValue> {
wasm::view_body(&self.0, js_model)
}

#[wasm_bindgen(js_name = getSubscriptions)]
pub fn get_subscriptions(&self, js_model: &JsValue) -> Result<JsValue, JsValue> {
wasm::get_subscriptions(&self.0, js_model)
}
impl_wasm_page!(HomePage);

#[wasm_bindgen(js_name = update)]
pub fn update(&self, js_msg: &JsValue, js_model: &JsValue) -> Result<JsValue, JsValue> {
wasm::update(&self.0, js_msg, js_model)
}
#[wasm_bindgen(js_name = homePage)]
pub fn home_page() -> Result<HomePage, JsValue> {
Ok(HomePage(home_page::HomePage {}))
}
35 changes: 5 additions & 30 deletions sevenguis_wasm/src/temperature_page.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
use polyester::page::wasm;
use polyester::page::Page;
use polyester_macro::impl_wasm_page;
use sevenguis_lib::temperature_page;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct TemperaturePage(temperature_page::TemperaturePage);

#[wasm_bindgen]
impl TemperaturePage {
#[wasm_bindgen(constructor)]
pub fn new() -> Result<TemperaturePage, JsValue> {
Ok(TemperaturePage(temperature_page::TemperaturePage {}))
}

#[wasm_bindgen(js_name = id)]
pub fn id(&self) -> Result<String, JsValue> {
Ok(self.0.id().to_string())
}

#[wasm_bindgen(js_name = init)]
pub fn initial_model(&self) -> Result<JsValue, JsValue> {
wasm::init(&self.0)
}

#[wasm_bindgen(js_name = viewBody)]
pub fn view_body(&self, js_model: &JsValue) -> Result<String, JsValue> {
wasm::view_body(&self.0, js_model)
}

#[wasm_bindgen(js_name = getSubscriptions)]
pub fn get_subscriptions(&self, js_model: &JsValue) -> Result<JsValue, JsValue> {
wasm::get_subscriptions(&self.0, js_model)
}
impl_wasm_page!(TemperaturePage);

#[wasm_bindgen(js_name = update)]
pub fn update(&self, js_msg: &JsValue, js_model: &JsValue) -> Result<JsValue, JsValue> {
wasm::update(&self.0, js_msg, js_model)
}
#[wasm_bindgen(js_name = temperaturePage)]
pub fn temperature_page() -> Result<TemperaturePage, JsValue> {
Ok(TemperaturePage(temperature_page::TemperaturePage {}))
}
45 changes: 10 additions & 35 deletions sevenguis_wasm/src/timer_page.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
use polyester::page::wasm;
use polyester::page::Page;
use polyester_macro::impl_wasm_page;
use sevenguis_lib::timer_page;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct TimerPage(timer_page::TimerPage);

#[wasm_bindgen]
impl TimerPage {
#[wasm_bindgen(constructor)]
pub fn new(js_current_time: &JsValue) -> Result<TimerPage, JsValue> {
let current_time = js_current_time
.into_serde()
.map_err(|err| format!("Failed to decode current time: {}", err))?;

Ok(TimerPage(timer_page::TimerPage {
initial_time: current_time,
}))
}

#[wasm_bindgen(js_name = id)]
pub fn id(&self) -> Result<String, JsValue> {
Ok(self.0.id().to_string())
}

#[wasm_bindgen(js_name = init)]
pub fn init(&self) -> Result<JsValue, JsValue> {
wasm::init(&self.0)
}

#[wasm_bindgen(js_name = viewBody)]
pub fn view_body(&self, js_model: &JsValue) -> Result<String, JsValue> {
wasm::view_body(&self.0, js_model)
}
impl_wasm_page!(TimerPage);

#[wasm_bindgen(js_name = getSubscriptions)]
pub fn get_subscriptions(&self, js_model: &JsValue) -> Result<JsValue, JsValue> {
wasm::get_subscriptions(&self.0, js_model)
}
#[wasm_bindgen(js_name = timerPage)]
pub fn timer_page(js_current_time: &JsValue) -> Result<TimerPage, JsValue> {
let current_time = js_current_time
.into_serde()
.map_err(|err| format!("Failed to decode current time: {}", err))?;

#[wasm_bindgen(js_name = update)]
pub fn update(&self, js_msg: &JsValue, js_model: &JsValue) -> Result<JsValue, JsValue> {
wasm::update(&self.0, js_msg, js_model)
}
Ok(TimerPage(timer_page::TimerPage {
initial_time: current_time,
}))
}
4 changes: 2 additions & 2 deletions sevenguis_web/src/home_page.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import init from "../wasm/sevenguis.js";
import { HomePage } from "../wasm/sevenguis";
import { homePage } from "../wasm/sevenguis";
import { Polyester } from "polyester";
import { defaultDebugConfig } from "polyester/src/logger";

(async () => {
await init("./wasm/sevenguis_bg.wasm");

const polyester = new Polyester(new HomePage(), {
const polyester = new Polyester(homePage(), {
loggerConfig: defaultDebugConfig(),
});

Expand Down
4 changes: 2 additions & 2 deletions sevenguis_web/src/temperature_page.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import init, { TemperaturePage } from "../wasm/sevenguis.js";
import init, { temperaturePage } from "../wasm/sevenguis.js";
import { Polyester } from "polyester";
import { defaultDebugConfig } from "polyester/src/logger";

(async () => {
await init("./wasm/sevenguis_bg.wasm");

const polyester = new Polyester(new TemperaturePage(), {
const polyester = new Polyester(temperaturePage(), {
loggerConfig: defaultDebugConfig(),
});

Expand Down
4 changes: 2 additions & 2 deletions sevenguis_web/src/timer_page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import init, { TimerPage } from "../wasm/sevenguis.js";
import init, { timerPage } from "../wasm/sevenguis.js";
import { Polyester } from "polyester";
import { posixFromMilliseconds } from "polyester/src/time";

Expand All @@ -7,6 +7,6 @@ import { posixFromMilliseconds } from "polyester/src/time";

const currentTime = posixFromMilliseconds(Date.now());

const polyester = new Polyester(new TimerPage(currentTime));
const polyester = new Polyester(timerPage(currentTime));
polyester.init();
})();

0 comments on commit 383a38e

Please sign in to comment.