Skip to content

Commit

Permalink
🐛 Fix the macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Dec 19, 2023
1 parent 425ed80 commit 913a85b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
10 changes: 8 additions & 2 deletions packages/boot/tests/register_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[cfg(test)]
mod test {
use serde::{Deserialize, Serialize};
use std::str::FromStr;

use yew::prelude::*;
use yew_router::prelude::*;

Expand Down Expand Up @@ -39,7 +39,13 @@ mod test {

#[tokio::test]
async fn render_on_server() -> anyhow::Result<()> {
let html = App::render_to_string(url::Url::from_str("/")?).await;
let html = App::render_to_string(
"/".to_string(),
AppStates {
color: "#114514".to_string(),
},
)
.await;
println!("{}", html);

Ok(())
Expand Down
14 changes: 7 additions & 7 deletions packages/macro-types/src/register.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#![allow(non_snake_case)]

pub trait DeclRoutes: yew_router::Routable {
fn switch(routes: &Self) -> yew::Html;
pub trait DeclRoutes: ::yew_router::Routable {
fn switch(routes: &Self) -> ::yew::Html;
}

#[derive(Debug, PartialEq, Clone, yew::Properties)]
#[derive(Debug, PartialEq, Clone, ::yew::Properties)]
pub struct AppContext<T>
where
T: PartialEq + Clone + ::serde::Serialize + ::serde::Deserialize<'static>,
{
pub style_manager: stylist::manager::StyleManager,
pub url: url::Url,
pub style_manager: ::stylist::manager::StyleManager,
pub url: String,
pub states: T,
}

#[async_trait::async_trait]
pub trait Application: DeclType {
async fn render_to_string(url: url::Url) -> String;
async fn render_to_string(url: String, status: <Self as DeclType>::AppStates) -> String;
}

pub trait DeclType
where
Self::Routes: DeclRoutes + yew_router::Routable,
Self::Routes: DeclRoutes + ::yew_router::Routable,
Self::AppStates: PartialEq + Clone + ::serde::Serialize + ::serde::Deserialize<'static>,
{
type Routes;
Expand Down
38 changes: 17 additions & 21 deletions packages/macro/src/utils/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn root(input: DeriveApp) -> TokenStream {

quote! {
#[::yew::function_component]
pub fn __HikariApp() -> yew::Html {
pub fn HikariApp() -> yew::Html {
use ::stylist::{manager::StyleManager, yew::ManagerProvider};
use ::yew::prelude::*;
use ::yew_router::BrowserRouter;
Expand All @@ -52,7 +52,7 @@ pub fn root(input: DeriveApp) -> TokenStream {
.expect("Cannot get the global window object")
.document()
.expect("Cannot get the global document object")
.get_element_by_id("__ssr_data")
.get_element_by_id("ssr_data")
.expect("Cannot get the root DOM element");
let page_data = page_data_el.inner_html();
let page_data = {
Expand All @@ -73,15 +73,15 @@ pub fn root(input: DeriveApp) -> TokenStream {
<Suspense {fallback}>
<ManagerProvider manager={style_manager}>
<BrowserRouter>
<__HikariContextShell states={page_data} />
<HikariContextShell states={page_data} />
</BrowserRouter>
</ManagerProvider>
</Suspense>
}
}

#[::yew::function_component]
pub fn __HikariServerApp(
pub fn HikariServerApp(
props: &::hikari_boot::AppContext<<#ident as ::hikari_boot::DeclType>::AppStates>
) -> yew::Html {
use ::stylist::yew::ManagerProvider;
Expand All @@ -93,29 +93,27 @@ pub fn root(input: DeriveApp) -> TokenStream {

let fallback = html! { <div>{"Loading..."}</div> };
let history = AnyHistory::from(MemoryHistory::new());
history
.push(&*props.uri)
.unwrap();
history.push(&props.url);

html! {
<Suspense {fallback}>
<ManagerProvider manager={props.style_manager.clone()}>
<Router history={history}>
<__HikariContextShell states={props.states.to_owned()} />
<HikariContextShell states={props.states.to_owned()} />
</Router>
</ManagerProvider>
</Suspense>
}
}

#[derive(Clone, Debug, PartialEq, ::yew::Properties)]
struct __HikariContextShellProps {
struct HikariContextShellProps {
states: <#ident as ::hikari_boot::DeclType>::AppStates,
}


#[::yew::function_component]
fn __HikariContextShell(states: &__HikariContextShellProps) -> yew::Html {
fn HikariContextShell(states: &HikariContextShellProps) -> yew::Html {
use yew::prelude::*;

type AppStates = <#ident as ::hikari_boot::DeclType>::AppStates;
Expand All @@ -124,23 +122,20 @@ pub fn root(input: DeriveApp) -> TokenStream {

html! {
<ContextProvider<AppStatesContextProviderType> context={ctx.clone()}>
<__HikariContent />
<HikariContent />
</ContextProvider<AppStatesContextProviderType>>
}
}

#[::stylist::yew::styled_component]
pub fn __HikariContent() -> yew::Html {
pub fn HikariContent() -> yew::Html {
use yew::prelude::*;
use yew_router::prelude::*;

html! {
<>
<Switch<<#ident as ::hikari_boot::DeclType>::Routes>
render={<
<#ident as ::hikari_boot::DeclType>::Routes as
::hikari_boot::DeclRoutes>::switch
}
render={|r| <<#ident as ::hikari_boot::DeclType>::Routes as ::hikari_boot::DeclRoutes>::switch(&r)}
/>
</>
}
Expand All @@ -149,16 +144,17 @@ pub fn root(input: DeriveApp) -> TokenStream {
#[automatically_derived]
#[::async_trait::async_trait]
impl ::hikari_boot::Application for #ident {
async fn render_to_string(url: ::url::Url) -> String {
async fn render_to_string(url: String, states: <#ident as ::hikari_boot::DeclType>::AppStates) -> String {
use ::stylist::manager::{render_static, StyleManager};
use ::yew::ServerRenderer;

let (writer, reader) = render_static();
let renderer = ServerRenderer::<__HikariServerApp>::with_props(move || {
let renderer = ServerRenderer::<HikariServerApp>::with_props(move || {
let style_manager = StyleManager::builder().writer(writer).build().unwrap();
<Self as ::hikari_boot::DeclType>::AppStates {
::hikari_boot::AppContext {
style_manager,
url,
states,
}
});
let html_raw = renderer.render().await;
Expand All @@ -176,10 +172,10 @@ pub fn root(input: DeriveApp) -> TokenStream {
<style>{style_raw}</style>
</head>
<body>
<textarea id='__ssr_data' style='display: none;'>{{}}</textarea>
<textarea id='ssr_data' style='display: none;'>{{}}</textarea>
<div id='app'>{html_raw}</div>
<script src='/a.js'></script>
<script>(async () => {{await __wasm_vendor_entry('/a.wasm');(await (new __wasm_vendor_entry.WebHandle())).start();}})()</script>
<script>(async () => {{await wasm_vendor_entry('/a.wasm');(await (new wasm_vendor_entry.WebHandle())).start();}})()</script>
</body>
</html>
")
Expand Down

0 comments on commit 913a85b

Please sign in to comment.