Skip to content

Commit

Permalink
Improve readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Nov 25, 2024
1 parent bbe2be7 commit 2e3b9b5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ impl ModuleDefExt<MyModuleOptions> for MyModule {

fn globals(globals: &Object<'_>, options: &MyModuleOptions) -> Result<()> {
// Set your globals here
globals.set("user", options.user.clone())?;
globals.set("global_user", options.user.clone())?;
Ok(())
}
}
```

At runtime, this module results in:

- A global `global_user` variable
- An importable module `import { user } from "my-module"`

### Globals only

If you only need to set globals and do **NOT** want an actual module that the Javascript can import, use `GlobalsOnly` for the implementation.
Expand Down Expand Up @@ -129,7 +134,10 @@ use rquickjs_module::globals_only_module;

struct MyModule;
globals_only_module!(MyModule, |globals| {
// Custom globals initialization code here
// Set your globals here
globals.set("hello", "world".to_string())?;
Ok(())
});
```

Both result in a global variable named `hello` begin available at runtime.

0 comments on commit 2e3b9b5

Please sign in to comment.