cotis_web/start_up.rs
1//! WASM entry points exported to JavaScript.
2//!
3//! # Bootstrap sequence
4//!
5//! 1. Page loads wasm-bindgen glue and calls `await init()`.
6//! 2. [`init`] — sets up `console_log` and a panic hook with backtrace.
7//! 3. App entry — the app crate exports `main_run` (see [`web-example`](../../web-example/src/lib.rs))
8//! which runs the function marked with `#[cotis_start_async]`.
9
10use cotis::launch::cotis_launch_async;
11use log::error;
12use std::backtrace::Backtrace;
13use std::panic;
14use wasm_bindgen::prelude::wasm_bindgen;
15
16/// Initializes logging and a WASM panic hook. Call once after wasm-bindgen `init()`.
17pub fn init() {
18 console_log::init_with_level(log::Level::Debug).unwrap();
19 set_panic_hook();
20}
21
22/// Dispatches to the application entry hook registered by `#[cotis_start_async]`.
23pub async fn main_run() {
24 cotis_launch_async().await;
25}
26
27/// Installs a panic hook that logs the message and a captured backtrace to the console.
28pub fn set_panic_hook() {
29 panic::set_hook(Box::new(hook_impl));
30}
31
32fn hook_impl(info: &panic::PanicHookInfo) {
33 let backtrace = Backtrace::force_capture();
34 error!("Panic occurred: {info}\nBacktrace:\n{backtrace}");
35}
36
37#[wasm_bindgen]
38pub fn init_wasm() {
39 crate::start_up::init();
40}
41
42/// Legacy export name kept for older `index.html` bootstraps that scan `wasmEntry_*`.
43#[wasm_bindgen]
44#[allow(non_snake_case)]
45pub async fn wasmEntry_main_run() {
46 cotis_launch_async().await;
47}