Macro zng_env::init

source ·
macro_rules! init {
    () => { ... };
}
Expand description

Inits process metadata, calls process start handlers and defines the process lifetime in main.

This must be called in main.

Init about an About for the process metadata. See on_process_start! for process start handlers. See on_process_exit for exit handlers called at the end of the main function.

§Process Start

A single Zng executable can be built with multiple components that spawn different instances of the executable that must run as different processes. If the current instance is requested by component init! runs it and exits the process, never returning flow to the normal main function.

fn main() {
    println!("print in all processes");
    zng::env::init!();
    println!("print only in the app-process");

    // directories are available after `init!`.
    let _res = zng::env::res("");
     
    // APP.defaults().run(...);

    // on_exit handlers are called here
}

§Web Start

WebAssembly builds (target_arch = "wasm32") must share the app wasm module reference by setting the custom attribute __zng_env_init_module on the Javascript window object.

The init! call will panic if the attribute is not found.

<script type="module">
import init, * as my_app_wasm from './my_app.js';
window.__zng_env_init_module = my_app_wasm;
async function main() {
  await init();
}
main();
</script>

The example above imports and runs an app built using wasm-pack with --target web options.

§Android Start

Android builds (target_os = "android") receive an AndroidApp instance from the android_main. This type is tightly coupled with the view-process implementation and so it is defined by the zng-view crate. In builds feature "view" you must call zng::view_process::default::android::init_android_app just after init!.

#[no_mangle]
fn android_main(app: zng::view_process::default::android::AndroidApp) {
    zng::env::init!();
    zng::view_process::default::android::init_android_app(app);
    // zng::view_process::default::run_same_process(..);
}

See the multi example for more details on how to support Android and other platforms.