webview-bun icon indicating copy to clipboard operation
webview-bun copied to clipboard

Async callbacks?

Open danielmahal opened this issue 7 months ago • 0 comments

Thanks for this library, neat for prototyping!

Is there a way to use async callbacks?

This is my case:

import { Webview } from "webview-bun";

const html = await Bun.file("index.html").text();

const webview = new Webview();

webview.bind("setName", async (name) => {
  console.log("Setting name to", name);
  await Bun.write("name.txt", name);
  console.log('File written "name.txt"', name);
});

const initialName = await Bun.file("name.txt").text();

webview.bind("initialName", () => {
  return initialName;
});

webview.title = "Bun App";

webview.setHTML(`
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" id="input" />
    <script type="module">
      input.value = await initialName();

      input.addEventListener("input", (e) => {
        setName(e.target.value).then((res) => {
          console.log(res);
        });
      });
    </script>
  </body>
</html>
`);

webview.run();

Steps to reproduce:

  1. Run the app above
  2. Type something in the input field

Expected behavior:

  • [x] Logs out "Setting name to…"
  • [x] Writes file
  • [x] Logs out "File written…"

Actual behavior:

  • [x] Logs out "Setting name to…"
  • [x] Writes file
  • [ ] ~~Logs out "File written…"~~

Seems like it stalls at/after the await in setName. The following code isn't executed.

This is a simplified example by the way, I discovered this when I wanted to perform a check if the file exists (await Bun.file('name').exists()), before performing other actions (writing, etc…)

danielmahal avatar Feb 28 '25 07:02 danielmahal