From a93fe411fca3aa6e83274ac2c3a20f235c2e37cc Mon Sep 17 00:00:00 2001 From: MnlPhlp Date: Sat, 19 Oct 2024 12:05:23 +0200 Subject: [PATCH 1/4] do not generate unused definitions in bindings --- examples/custom-plugin/plugin/bindings.ts | 20 ++++---- src/lang/globals.js | 56 ----------------------- src/lang/globals.ts | 48 ------------------- src/lang/globals_events.js | 56 +++++++++++++++++++++++ src/lang/globals_events.ts | 49 ++++++++++++++++++++ src/lang/js.rs | 2 + src/lang/js_ts.rs | 5 +- src/lang/ts.rs | 2 + 8 files changed, 124 insertions(+), 114 deletions(-) create mode 100644 src/lang/globals_events.js create mode 100644 src/lang/globals_events.ts diff --git a/examples/custom-plugin/plugin/bindings.ts b/examples/custom-plugin/plugin/bindings.ts index 70d340c..cbc76f1 100644 --- a/examples/custom-plugin/plugin/bindings.ts +++ b/examples/custom-plugin/plugin/bindings.ts @@ -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 = + | { 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"; @@ -46,15 +52,11 @@ type __EventObj__ = { once: ( cb: TAURI_API_EVENT.EventCallback, ) => ReturnType>; - emit: T extends null - ? (payload?: T) => ReturnType - : (payload: T) => ReturnType; + emit: null extends T + ? (payload?: T) => ReturnType + : (payload: T) => ReturnType; }; -export type Result = - | { status: "ok"; data: T } - | { status: "error"; error: E }; - function __makeEvents__>( mappings: Record, ) { @@ -68,7 +70,7 @@ function __makeEvents__>( 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), diff --git a/src/lang/globals.js b/src/lang/globals.js index 2fb56bf..81ce46b 100644 --- a/src/lang/globals.js +++ b/src/lang/globals.js @@ -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 - * ) => ReturnType>; - * once: ( - * cb: TAURI_API_EVENT.EventCallback - * ) => ReturnType>; - * emit: T extends null - * ? (payload?: T) => ReturnType - * : (payload: T) => ReturnType; - * }} __EventObj__ - */ /** * @template T,E * @typedef { { status: "ok", data: T } | { status: "error", error: E } } Result */ -/** - * @template {Record} T - * @param {Record} mappings - * @returns {{ - * [K in keyof T]: __EventObj__ & { - * (handle: __WebviewWindowHandle__): __EventObj__; - * }; - * }} - */ -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); - } - }, - }); - }, - }, - ); -} diff --git a/src/lang/globals.ts b/src/lang/globals.ts index a100a27..dc9147b 100644 --- a/src/lang/globals.ts +++ b/src/lang/globals.ts @@ -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__ = { - listen: ( - cb: TAURI_API_EVENT.EventCallback, - ) => ReturnType>; - once: ( - cb: TAURI_API_EVENT.EventCallback, - ) => ReturnType>; - emit: null extends T - ? (payload?: T) => ReturnType - : (payload: T) => ReturnType; -}; export type Result = | { status: "ok"; data: T } | { status: "error"; error: E }; -function __makeEvents__>( - mappings: Record, -) { - return new Proxy( - {} as unknown as { - [K in keyof T]: __EventObj__ & { - (handle: __WebviewWindow__): __EventObj__; - }; - }, - { - 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__) => { - 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); - } - }, - }); - }, - }, - ); -} diff --git a/src/lang/globals_events.js b/src/lang/globals_events.js new file mode 100644 index 0000000..37fa41c --- /dev/null +++ b/src/lang/globals_events.js @@ -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 + * ) => ReturnType>; + * once: ( + * cb: TAURI_API_EVENT.EventCallback + * ) => ReturnType>; + * emit: T extends null + * ? (payload?: T) => ReturnType + * : (payload: T) => ReturnType; + * }} __EventObj__ + */ + +/** + * @template {Record} T + * @param {Record} mappings + * @returns {{ + * [K in keyof T]: __EventObj__ & { + * (handle: __WebviewWindowHandle__): __EventObj__; + * }; + * }} + */ +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); + } + }, + }); + }, + }, + ); +} diff --git a/src/lang/globals_events.ts b/src/lang/globals_events.ts new file mode 100644 index 0000000..ff67eef --- /dev/null +++ b/src/lang/globals_events.ts @@ -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__ = { + listen: ( + cb: TAURI_API_EVENT.EventCallback, + ) => ReturnType>; + once: ( + cb: TAURI_API_EVENT.EventCallback, + ) => ReturnType>; + emit: null extends T + ? (payload?: T) => ReturnType + : (payload: T) => ReturnType; +}; + +function __makeEvents__>( + mappings: Record, +) { + return new Proxy( + {} as unknown as { + [K in keyof T]: __EventObj__ & { + (handle: __WebviewWindow__): __EventObj__; + }; + }, + { + 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__) => { + 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); + } + }, + }); + }, + }, + ); +} diff --git a/src/lang/js.rs b/src/lang/js.rs index 7ffd7d3..4815bf8 100644 --- a/src/lang/js.rs +++ b/src/lang/js.rs @@ -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 { @@ -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)?, diff --git a/src/lang/js_ts.rs b/src/lang/js_ts.rs index e3512c2..11d7522 100644 --- a/src/lang/js_ts.rs +++ b/src/lang/js_ts.rs @@ -20,6 +20,7 @@ pub fn render_all_parts( cfg: &ExportContext, dependant_types: &str, globals: &str, + globals_events: &str, header: &str, commands: String, events: String, @@ -73,7 +74,9 @@ pub fn render_all_parts( /** tauri-specta globals **/ -{globals}"# +{globals} +{}"#, + if events.is_empty(){""} else {globals_events} }) } diff --git a/src/lang/ts.rs b/src/lang/ts.rs index 4f1a8a4..24543ef 100644 --- a/src/lang/ts.rs +++ b/src/lang/ts.rs @@ -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 { @@ -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)?, From d1d185ffb7ec5d07f2c81e30b2aea72d90bbd0a1 Mon Sep 17 00:00:00 2001 From: MnlPhlp Date: Sat, 19 Oct 2024 12:21:32 +0200 Subject: [PATCH 2/4] allow compiling without typescript feature --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3a4aa22..414c6ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ rustdoc-args = ["--cfg", "docsrs"] default = [] derive = ["dep:tauri-specta-macros"] javascript = ["dep:specta-jsdoc"] -typescript = ["dep:specta-typescript"] +typescript = [] [lints] workspace = true @@ -29,7 +29,7 @@ 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" From 96f6b25a83cb6327b599309a74e5ac0cc4d4040f Mon Sep 17 00:00:00 2001 From: MnlPhlp Date: Mon, 21 Oct 2024 10:58:06 +0200 Subject: [PATCH 3/4] undo accidental change --- src/lang/globals_events.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lang/globals_events.ts b/src/lang/globals_events.ts index ff67eef..7348704 100644 --- a/src/lang/globals_events.ts +++ b/src/lang/globals_events.ts @@ -8,7 +8,7 @@ type __EventObj__ = { once: ( cb: TAURI_API_EVENT.EventCallback, ) => ReturnType>; - emit: null extends T + emit: T extends null ? (payload?: T) => ReturnType : (payload: T) => ReturnType; }; From 0ffa694e9af2f63488464dccda01d2ebd9dbb8ef Mon Sep 17 00:00:00 2001 From: MnlPhlp Date: Mon, 21 Oct 2024 11:00:38 +0200 Subject: [PATCH 4/4] regenerate binding --- examples/custom-plugin/plugin/bindings.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/custom-plugin/plugin/bindings.ts b/examples/custom-plugin/plugin/bindings.ts index cbc76f1..3bb01e9 100644 --- a/examples/custom-plugin/plugin/bindings.ts +++ b/examples/custom-plugin/plugin/bindings.ts @@ -52,7 +52,7 @@ type __EventObj__ = { once: ( cb: TAURI_API_EVENT.EventCallback, ) => ReturnType>; - emit: null extends T + emit: T extends null ? (payload?: T) => ReturnType : (payload: T) => ReturnType; };