You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fast-insiders/client/src/global_state.rs

50 lines
1.5 KiB

use perseus::prelude::*;
use serde::{Deserialize, Serialize};
use crate::api::FastInsidersApi;
use perseus::state::GlobalStateCreator;
pub fn get_global_state_creator() -> GlobalStateCreator {
GlobalStateCreator::new()
.build_state_fn(get_build_state)
.request_state_fn(get_request_state)
.amalgamate_states_fn(amalgamate_states)
}
#[derive(Serialize, Deserialize, ReactiveState)]
#[rx(alias = "AppStateRx")]
pub struct AppState {
pub dark_mode: bool,
pub api: FastInsidersApi,
}
#[engine_only_fn]
pub async fn get_build_state() -> AppState {
AppState {
dark_mode: true,
api: FastInsidersApi::new(""), // It's unfortunately not possible to have a different type
// for the build state and the request state, I would rather
// have left this out
// This will also only ever work as long as we don't need the api
// while building the app, so no SSR!
}
}
#[engine_only_fn]
async fn get_request_state(_req: Request) -> AppState {
use crate::env::Config;
let config = Config::new();
AppState {
dark_mode: true,
api: FastInsidersApi::new(&config.api_url),
}
}
#[engine_only_fn]
async fn amalgamate_states(build_state: AppState, request_state: AppState) -> AppState {
AppState {
dark_mode: build_state.dark_mode,
api: request_state.api,
}
}