Skip to content
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

don't generate unused functions in bindings #138

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ rustdoc-args = ["--cfg", "docsrs"]
default = []
derive = ["dep:tauri-specta-macros"]
javascript = ["dep:specta-jsdoc"]
typescript = ["dep:specta-typescript"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the changes in this file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the js.rs file has this use use specta_typescript::{js_doc, ExportError, Typescript};
So compiling without the specta_typescript dependency fails if only the javascript feature is set.
This is why the dependency is now not optional anymore and the 'dep:..' is not needed.

typescript = []

[lints]
workspace = true

[dependencies]
# Public
specta = { workspace = true, features = ["function"] }
specta-typescript = { workspace = true, optional = true }
specta-typescript = { workspace = true }
specta-jsdoc = { workspace = true, optional = true }
tauri-specta-macros = { version = "=2.0.0-rc.16", optional = true, path = "./macros" }
serde = "1"
Expand Down
20 changes: 11 additions & 9 deletions examples/custom-plugin/plugin/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ export type RandomNumber = number

import {
invoke as TAURI_INVOKE,
Channel as TAURI_CHANNEL,
} from "@tauri-apps/api/core";


export type Result<T, E> =
| { status: "ok"; data: T }
| { status: "error"; error: E };


import * as TAURI_API_EVENT from "@tauri-apps/api/event";
import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow";

Expand All @@ -46,15 +52,11 @@ type __EventObj__<T> = {
once: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
emit: T extends null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change?

It also differs from the JS Doc alternative so they will need to match for this to be merged.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be honest i don't know when/how this changed. i will undo the change.

? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
emit: null extends T
? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
};

export type Result<T, E> =
| { status: "ok"; data: T }
| { status: "error"; error: E };

function __makeEvents__<T extends Record<string, any>>(
mappings: Record<keyof T, string>,
) {
Expand All @@ -68,7 +70,7 @@ function __makeEvents__<T extends Record<string, any>>(
get: (_, event) => {
const name = mappings[event as keyof T];

return new Proxy((() => {}) as any, {
return new Proxy((() => { }) as any, {
apply: (_, __, [window]: [__WebviewWindow__]) => ({
listen: (arg: any) => window.listen(name, arg),
once: (arg: any) => window.once(name, arg),
Expand Down
56 changes: 0 additions & 56 deletions src/lang/globals.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,9 @@
import {
invoke as TAURI_INVOKE,
Channel as TAURI_CHANNEL,
} from "@tauri-apps/api/core";
import * as TAURI_API_EVENT from "@tauri-apps/api/event";

/** @typedef {typeof import("@tauri-apps/api/window").WebviewWindowHandle} __WebviewWindowHandle__ */

/**
* @template T
* @typedef {{
* listen: (
* cb: TAURI_API_EVENT.EventCallback<T>
* ) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
* once: (
* cb: TAURI_API_EVENT.EventCallback<T>
* ) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
* emit: T extends null
* ? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
* : (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
* }} __EventObj__<T>
*/

/**
* @template T,E
* @typedef { { status: "ok", data: T } | { status: "error", error: E } } Result
*/

/**
* @template {Record<string, any>} T
* @param {Record<keyof T, string>} mappings
* @returns {{
* [K in keyof T]: __EventObj__<T[K]> & {
* (handle: __WebviewWindowHandle__): __EventObj__<T[K]>;
* };
* }}
*/
function __makeEvents__(mappings) {
return new Proxy(
{},
{
get: (_, event) => {
const name = mappings[event];

new Proxy(() => {}, {
apply: (_, __, [window]) => ({
listen: (arg) => window.listen(name, arg),
once: (arg) => window.once(name, arg),
emit: (arg) => window.emit(name, arg),
}),
get: (_, command) => {
switch (command) {
case "listen":
return (arg) => TAURI_API_EVENT.listen(name, arg);
case "once":
return (arg) => TAURI_API_EVENT.once(name, arg);
case "emit":
return (arg) => TAURI_API_EVENT.emit(name, arg);
}
},
});
},
},
);
}
48 changes: 0 additions & 48 deletions src/lang/globals.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
import {
invoke as TAURI_INVOKE,
Channel as TAURI_CHANNEL,
} from "@tauri-apps/api/core";
import * as TAURI_API_EVENT from "@tauri-apps/api/event";
import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow";

type __EventObj__<T> = {
listen: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
once: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
emit: null extends T
? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
};

export type Result<T, E> =
| { status: "ok"; data: T }
| { status: "error"; error: E };

function __makeEvents__<T extends Record<string, any>>(
mappings: Record<keyof T, string>,
) {
return new Proxy(
{} as unknown as {
[K in keyof T]: __EventObj__<T[K]> & {
(handle: __WebviewWindow__): __EventObj__<T[K]>;
};
},
{
get: (_, event) => {
const name = mappings[event as keyof T];

return new Proxy((() => {}) as any, {
apply: (_, __, [window]: [__WebviewWindow__]) => ({
listen: (arg: any) => window.listen(name, arg),
once: (arg: any) => window.once(name, arg),
emit: (arg: any) => window.emit(name, arg),
}),
get: (_, command: keyof __EventObj__<any>) => {
switch (command) {
case "listen":
return (arg: any) => TAURI_API_EVENT.listen(name, arg);
case "once":
return (arg: any) => TAURI_API_EVENT.once(name, arg);
case "emit":
return (arg: any) => TAURI_API_EVENT.emit(name, arg);
}
},
});
},
},
);
}
56 changes: 56 additions & 0 deletions src/lang/globals_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as TAURI_API_EVENT from "@tauri-apps/api/event";

/** @typedef {typeof import("@tauri-apps/api/window").WebviewWindowHandle} __WebviewWindowHandle__ */

/**
* @template T
* @typedef {{
* listen: (
* cb: TAURI_API_EVENT.EventCallback<T>
* ) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
* once: (
* cb: TAURI_API_EVENT.EventCallback<T>
* ) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
* emit: T extends null
* ? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
* : (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
* }} __EventObj__<T>
*/

/**
* @template {Record<string, any>} T
* @param {Record<keyof T, string>} mappings
* @returns {{
* [K in keyof T]: __EventObj__<T[K]> & {
* (handle: __WebviewWindowHandle__): __EventObj__<T[K]>;
* };
* }}
*/
function __makeEvents__(mappings) {
return new Proxy(
{},
{
get: (_, event) => {
const name = mappings[event];

new Proxy(() => { }, {
apply: (_, __, [window]) => ({
listen: (arg) => window.listen(name, arg),
once: (arg) => window.once(name, arg),
emit: (arg) => window.emit(name, arg),
}),
get: (_, command) => {
switch (command) {
case "listen":
return (arg) => TAURI_API_EVENT.listen(name, arg);
case "once":
return (arg) => TAURI_API_EVENT.once(name, arg);
case "emit":
return (arg) => TAURI_API_EVENT.emit(name, arg);
}
},
});
},
},
);
}
49 changes: 49 additions & 0 deletions src/lang/globals_events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as TAURI_API_EVENT from "@tauri-apps/api/event";
import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow";

type __EventObj__<T> = {
listen: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
once: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
emit: null extends T
? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
};

function __makeEvents__<T extends Record<string, any>>(
mappings: Record<keyof T, string>,
) {
return new Proxy(
{} as unknown as {
[K in keyof T]: __EventObj__<T[K]> & {
(handle: __WebviewWindow__): __EventObj__<T[K]>;
};
},
{
get: (_, event) => {
const name = mappings[event as keyof T];

return new Proxy((() => { }) as any, {
apply: (_, __, [window]: [__WebviewWindow__]) => ({
listen: (arg: any) => window.listen(name, arg),
once: (arg: any) => window.once(name, arg),
emit: (arg: any) => window.emit(name, arg),
}),
get: (_, command: keyof __EventObj__<any>) => {
switch (command) {
case "listen":
return (arg: any) => TAURI_API_EVENT.listen(name, arg);
case "once":
return (arg: any) => TAURI_API_EVENT.once(name, arg);
case "emit":
return (arg: any) => TAURI_API_EVENT.emit(name, arg);
}
},
});
},
},
);
}
2 changes: 2 additions & 0 deletions src/lang/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{ExportContext, LanguageExt};
use super::js_ts;

const GLOBALS: &str = include_str!("./globals.js");
const GLOBALS_EVENTS: &str = include_str!("./globals_events.js");

impl LanguageExt for specta_jsdoc::JSDoc {
fn render(&self, cfg: &ExportContext) -> Result<String, Self::Error> {
Expand All @@ -21,6 +22,7 @@ impl LanguageExt for specta_jsdoc::JSDoc {
cfg,
&dependant_types,
GLOBALS,
GLOBALS_EVENTS,
&self.0.header,
render_commands(&self.0, cfg)?,
render_events(&self.0, cfg)?,
Expand Down
5 changes: 4 additions & 1 deletion src/lang/js_ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn render_all_parts<L: LanguageExt>(
cfg: &ExportContext,
dependant_types: &str,
globals: &str,
globals_events: &str,
header: &str,
commands: String,
events: String,
Expand Down Expand Up @@ -73,7 +74,9 @@ pub fn render_all_parts<L: LanguageExt>(

/** tauri-specta globals **/

{globals}"#
{globals}
{}"#,
if events.is_empty(){""} else {globals_events}
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/lang/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use specta_typescript::{self as ts, Typescript};
use specta_typescript::{js_doc, ExportError};

const GLOBALS: &str = include_str!("./globals.ts");
const GLOBALS_EVENTS: &str = include_str!("./globals_events.ts");

impl LanguageExt for specta_typescript::Typescript {
fn render(&self, cfg: &ExportContext) -> Result<String, ExportError> {
Expand All @@ -19,6 +20,7 @@ impl LanguageExt for specta_typescript::Typescript {
cfg,
&dependant_types,
GLOBALS,
GLOBALS_EVENTS,
&self.header,
render_commands(self, cfg)?,
render_events(self, cfg)?,
Expand Down
Loading