-
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional input argument #4
Comments
I don't think the solution you've suggested will work since you still need to pass an argument of type |
|
Are you sure |
Yeah that would work, but you'd still have to provide an argument. We could just make the argument section optional entirely (which is less typesafe) or use the system I've been working on which exports discrete functions that match their corresponding commands. |
That doesn't seem to be how it works. If you see the PR I linked, it made trailing I think having discrete functions sounds amazing though, would love to see that! |
This issue shouldn't apply with 9107481. |
That seems like it should work indeed, looks wonderful to use. Now that I think about it though, the functions present a bit of a challenge for me. At the moment, I have a wrapper for invoking commands, so that I don't need a async function runCmd(cmd, input) {
try {
return await invoke(cmd, input)
} catch (e) {
invoke('error_popup', { msg: String(e) })
throw e
}
} Not quite sure how I should deal with that one 🤔 |
@probablykasper I can seen a few ways of doing this: import { helloWorld, errorPopup } from "./bindings";
async function main() {
const result = await helloWorld("Test").catch(rethrowWithPopup);
}
function rethrowWithPopup(e: any) {
errorPopup(String(e));
throw e;
} Alternatively, let import { helloWorld, errorPopup } from "./bindings"
async function main() {
const result = await rethrowWithPopup(() => helloWorld("Test"));
}
async function rethrowWithPopup<T>(callback: () => Promise<T>) {
try {
return await callback();
} catch (e: any) {
errorPopup(String(e));
throw e;
}
} If this is all too clunky, you could get a bit crazy and use a Proxy to rewrite the underlying logic: // utils.ts
import * as commands from "./bindings";
export const commands = new Proxy({}, {
get: (_, property) => async (...args) => {
try {
return await commands[string](...args)
} catch (e) {
commands.errorPopup(String(e));
throw e
}
}
);
// main.ts
import { commands } from "./utils";
async function main() {
commands.helloWorld("Test");
} This approach isn't very TypeScript friendly though, you'd need a generic that can index the exports of the star import and idk how to do that. |
Yeah I think something like the the last option is what I'll need to look at - I don't want to risk forgetting to handle an error |
@probablykasper This seems to work great in TS: // utils.ts
import * as c from "./bindings";
const commands = new Proxy({} as typeof c, {
get:
(_, property: string) =>
async (...args: any[]) => {
try {
return await (c as any)[property](...args);
} catch (e) {
c.helloWorld(String(e));
throw e;
}
},
});
// main.ts
import { commands } from "./utils";
async function main() {
commands.helloWorld("Test");
} I reckon we can close this issue now. |
Oh cool, I'll try it out! Thank you so much, didn't expect you to just do it all for me haha |
No worries, didn't wanna leave you without a proper solution before marking this as done haha |
Not sure how it would be done, but would be great to have the
input
be optional when a function has no argument. Currently you have to supplynull
. Setting the type tovoid
might be an option.The text was updated successfully, but these errors were encountered: