specta-rs / tauri-specta

Completely typesafe Tauri commands
MIT License
368 stars 40 forks source link

Can v2 split exported type into multiple file #127

Open mslxl opened 2 months ago

mslxl commented 2 months ago

In v1, I can use two export statement to export these commands to 2 file:

 ts::export(collect_types![greet, greet2, greet3], "../src/greet.ts").unwrap();
 ts::export(collect_types![hello, hello2, hello3], "../src/hello.ts").unwrap();

and then register them in tauri.

But in V2, I noticed the commands function of Builder will override old value. Is there any new way to split these exports to multiple file now?

oscartbeaumont commented 2 months ago

It is not official supported but it should continue the function the same as in v1 like you showed above. With the downside being that what you showed only handle types.

The bigger problem with supporting this is that Tauri's generate_handler returns a single function which internally dispatches to each of the registered commands. Tauri Specta can't "see inside" that function so we can easily merge mutliple set of commands.

I suspect it might be possible to create a wrapper function to dispatch to multiple inner functions but i've gotta check exactly how Tauri works internlly cause doing so is not really intended and possible not even semver safe.

We should keep this issue though and I will revisit it when I get some time cause we could surely do better.

Also you could possbile look into using Tauri Specta in a plugin as a solution. The plugin and the app can generate different bindings files and Tauri lets them each register their own commands (although the plugin ones will be scoped I think).