Skip to content

Commit 5f2526b

Browse files
committed
refactor(tauri): simplify script handling in execute function
- Updated the `execute` function to enhance handling of string and function scripts by passing them as-is, improving clarity and functionality. - Adjusted comments for better understanding of the script processing logic, ensuring that both strings and functions are correctly wrapped for execution.
1 parent f21d60a commit 5f2526b

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

packages/tauri-service/src/service.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,9 @@ export default class TauriWorkerService {
318318
script: string | ((...args: InnerArguments) => ReturnValue),
319319
...args: InnerArguments
320320
): Promise<ReturnValue> {
321-
// For functions, always use .toString() - produces valid JS function source
322-
// For strings:
323-
// - embedded path: pass as-is (WebDriver handles execution)
324-
// - tauri-driver path: use JSON.stringify (wrapped in template literal)
325-
const scriptString =
326-
typeof script === 'function' ? script.toString() : isEmbedded ? script : JSON.stringify(script);
321+
// For functions: use .toString() - produces valid JS function source
322+
// For strings: pass as-is (wrapper template wraps in parentheses to make callable)
323+
const scriptString = typeof script === 'function' ? script.toString() : script;
327324

328325
if (isEmbedded) {
329326
// For embedded WebDriver: skip console wrapper as console forwarding
@@ -332,10 +329,12 @@ export default class TauriWorkerService {
332329
}
333330

334331
// For tauri-driver: use sync execute with console wrapper
335-
// Note: scriptString is already properly escaped via JSON.stringify above
332+
// Note: scriptString is passed as-is - wrap in IIFE to make both strings and functions callable
333+
// For string scripts: "return x" -> "(() => return x)()" - wraps statement as expression
334+
// For function scripts: "(a,b) => a+b" -> "((a,b) => a+b)()" - works as IIFE
336335
const wrappedScript = `
337336
${CONSOLE_WRAPPER_SCRIPT}
338-
return (${scriptString}).apply(null, arguments);
337+
return ((${scriptString})()).apply(null, arguments);
339338
`;
340339

341340
return originalExecute(wrappedScript, ...args) as Promise<ReturnValue>;

0 commit comments

Comments
 (0)