Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 1 deletion example/src/lib/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// My header
// This file has been generated by TauRPC. Do not edit this file manually.

import { Channel } from '@tauri-apps/api/core';

export type Error = string;

export type PhaseSpecificRename = PhaseSpecificRename_Serialize | PhaseSpecificRename_Deserialize;
Expand Down Expand Up @@ -73,7 +75,7 @@ export const RESULT_MAP = {
}
};

export const TRANSFORM_MAP = { "": { "semantic_types_event": { args: [(v) => ({...v,bytes:[...v.bytes]})], eventArgs: [(v) => ({...v,date:new Date(v.date),bytes:new Uint8Array(v.bytes),url:new URL(v.url)})], result: null }, "semantic_types": { args: [(v) => ({...v,bytes:[...v.bytes]}), (v) => { const transform = (response) => ({...response,date:new Date(response.date),bytes:new Uint8Array(response.bytes),url:new URL(response.url)}); if (typeof v === "function") return (response) => v(transform(response)); const onmessage = v.onmessage; v.onmessage = (response) => onmessage(transform(response)); return v }], eventArgs: [(v) => ({...v,date:new Date(v.date),bytes:new Uint8Array(v.bytes),url:new URL(v.url)}), null], result: (v) => ({...v,date:new Date(v.date),bytes:new Uint8Array(v.bytes),url:new URL(v.url)}) } } };
export const TRANSFORM_MAP = { "": { "semantic_types_event": { args: [(v) => ({...v,bytes:[...v.bytes]})], eventArgs: [(v) => ({...v,date:new Date(v.date),bytes:new Uint8Array(v.bytes),url:new URL(v.url)})], result: null }, "semantic_types": { args: [(v) => ({...v,bytes:[...v.bytes]}), (v) => { const transform = (response) => ({...response,date:new Date(response.date),bytes:new Uint8Array(response.bytes),url:new URL(response.url)}); if (typeof v === "function") return (response) => v(transform(response)); return new Channel((response) => v.onmessage(transform(response))) }], eventArgs: [(v) => ({...v,date:new Date(v.date),bytes:new Uint8Array(v.bytes),url:new URL(v.url)}), null], result: (v) => ({...v,date:new Date(v.date),bytes:new Uint8Array(v.bytes),url:new URL(v.url)}) } } };

export type Router = {
"": {
Expand Down
24 changes: 23 additions & 1 deletion taurpc/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub(super) fn export_types(
.framework_runtime(move |mut exporter| {
let mut out = String::new();

if requires_channel_transform(&functions, &exporter, &export_runtime_for_runtime) {
out.push_str("import { Channel } from '@tauri-apps/api/core';\n\n");
}

out.push_str(&exporter.render_types()?);

out.push_str(
Expand Down Expand Up @@ -379,6 +383,24 @@ fn apply_semantic_type_for_phase(
}
}

fn requires_channel_transform(
functions: &BTreeMap<String, Vec<Function>>,
exporter: &FrameworkExporter,
export_runtime: &ExportRuntimeConfig,
) -> bool {
if export_runtime.semantic_types.is_none() {
return false;
}

functions.values().flatten().any(|function| {
function.args().iter().any(|(_, arg_dt)| {
tauri_channel_reference(arg_dt, exporter.types)
.and_then(|channel| named_reference_generics(channel).first())
.is_some()
})
})
}

fn generate_transform_map(
functions: &BTreeMap<String, Vec<Function>>,
exporter: &FrameworkExporter,
Expand Down Expand Up @@ -418,7 +440,7 @@ fn generate_transform_map(
.filter(|runtime| runtime != "response")
.map(|runtime| {
format!(
"{{ const transform = (response) => {runtime}; if (typeof v === \"function\") return (response) => v(transform(response)); const onmessage = v.onmessage; v.onmessage = (response) => onmessage(transform(response)); return v }}"
"{{ const transform = (response) => {runtime}; if (typeof v === \"function\") return (response) => v(transform(response)); return new Channel((response) => v.onmessage(transform(response))) }}"
)
})
} else {
Expand Down
Loading