webview_deno icon indicating copy to clipboard operation
webview_deno copied to clipboard

What’s wrong with webview_dispatcher ?

Open congzhangzh opened this issue 1 year ago • 11 comments

Hi Devs,

why disable "webview_dispatch"?

Btw, I am the author of some implementation of webview like julia & python.

https://github.com/congzhangzh/webview_python https://github.com/congzhangzh/Webview.jl

Tks, Cong

congzhangzh avatar Nov 25 '24 16:11 congzhangzh

The major issue, which I believe is mentioned elsewhere in some issues in this repo, is that the JavaScript event loop is simply stalled by the webview event loop on the main thread. Even when registering functions as callbacks and calling from another thread wrapped in webview_dispatch deno will simply hang. I have not tried this in a while, and I know the deno FFI has changed a lot since I first implemented it. If you could provide a working proof-of-concept simply using the direct ffi.ts bindings using webview_dispatch from a worker thread that would be great and something which could then further be developed into the actual feature itself.

eliassjogreen avatar Nov 25 '24 19:11 eliassjogreen

Yes, I know the internal trouble now! so the async binding will not work either if the deno event loop is hung by os event loop

So my Python binding & Julia binding is all ill-formed :), and all the async/await/future will not work!

Some related reference:

  1. [linux dispatcher]https://github.com/sunoru/Webviews.jl/blob/b28fb63300ac68a8f1a098e53237f1af46f99133/src/platforms/linux/Impl.jl#L127
  2. [win run]https://github.com/sunoru/Webviews.jl/blob/b28fb63300ac68a8f1a098e53237f1af46f99133/src/platforms/windows/Impl.jl#L72
  3. [win dispatcher]https://github.com/sunoru/Webviews.jl/blob/b28fb63300ac68a8f1a098e53237f1af46f99133/src/platforms/windows/Impl.jl#L99
  4. [mac os] https://github.com/sunoru/Webviews.jl/blob/b28fb63300ac68a8f1a098e53237f1af46f99133/src/platforms/apple/Impl.jl#L95
  5. [mac os] https://github.com/sunoru/Webviews.jl/blob/b28fb63300ac68a8f1a098e53237f1af46f99133/src/platforms/apple/Impl.jl#L16
  6. https://www.electronjs.org/blog/electron-internals-node-integration
  7. https://github.com/webview/webview_deno/issues/184
  8. https://github.com/congzhangzh/webview_python/issues/1

congzhangzh avatar Nov 26 '24 16:11 congzhangzh

https://github.com/webview/webview/issues/578

congzhangzh avatar Nov 26 '24 18:11 congzhangzh

More ref: https://ipython.readthedocs.io/en/stable/config/eventloops.html https://discuss.python.org/t/connecting-asyncio-and-tkinter-event-loops/14722 https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop

congzhangzh avatar Nov 30 '24 00:11 congzhangzh

more about how electron integration node event loop to native os part, hope this help others: https://docs.libuv.org/en/v1.x/loop.html#c.uv_backend_fd https://docs.libuv.org/en/v1.x/loop.html#c.uv_run_mode https://github.com/electron/electron/blob/8f00cc9c0e78b95e4de14b9b9e1a7df20935e948/shell/common/node_bindings.cc#L929 https://gist.github.com/bagder/ffb613e3c3c5f41813b31fc09ef1c261 https://docs.libuv.org/en/stable/guide/eventloops.html

congzhangzh avatar Nov 30 '24 02:11 congzhangzh

More ref 4 python side:

https://github.com/MagicStack/uvloop/blob/7bb12a174884b2ec8b3162a08564e5fb8a5c6b39/uvloop/loop.pyx#L1152 https://discuss.python.org/t/connecting-asyncio-and-tkinter-event-loops/14722

congzhangzh avatar Nov 30 '24 02:11 congzhangzh

I understand that Open Source means no expectations (thanks for your great work so far!) but does this mean that it's effectively impossible to currently use webview_deno in modern Deno? Unless I'm misunderstanding, It looks like webview.run() freezes deno up and makes it impossible to do anything.

DevRubicate avatar Dec 12 '24 14:12 DevRubicate

I understand that Open Source means no expectations (thanks for your great work so far!) but does this mean that it's effectively impossible to currently use webview_deno in modern Deno? Unless I'm misunderstanding, It looks like webview.run() freezes deno up and makes it impossible to do anything.

I think so, but I haven’t test it myself

congzhangzh avatar Mar 16 '25 00:03 congzhangzh

https://github.com/denoland/deno/discussions/25799

congzhangzh avatar Mar 19 '25 16:03 congzhangzh

I found that's maybe possilbe, some work like webview_python need to be done in deno。

and deno, tokio and v8's single v8 thread is very helpful:

  1. https://github.com/denoland/deno/pull/3844
  2. https://choubey.gitbook.io/internals-of-deno/threading-model/default-threads
  3. https://docs.rs/tokio/latest/tokio/runtime/index.html#current-thread-runtime-behavior-at-the-time-of-writing
  4. https://questions.deno.com/m/1247553728001609729
  5. https://www.reddit.com/r/rust/comments/tln9nu/what_is_the_difference_between_tokio_singlethread/

congzhangzh avatar Sep 07 '25 04:09 congzhangzh

maybe, this style works?

https://github.com/littledivy/deno_sdl2/blob/0e85bf1c5c7e591f908c152dc27de53c869b4b67/mod.ts#L1396

import { EventType, WindowBuilder } from "jsr:@divy/[email protected]";

const window = new WindowBuilder("Hello, Deno!", 640, 480).build(); const canvas = window.canvas();

for await (const event of window.events()) { if (event.type == EventType.Quit) { break; } else if (event.type == EventType.Draw) { // Rainbow effect const r = Math.sin(Date.now() / 1000) * 127 + 128; const g = Math.sin(Date.now() / 1000 + 2) * 127 + 128; const b = Math.sin(Date.now() / 1000 + 4) * 127 + 128; canvas.setDrawColor(Math.floor(r), Math.floor(g), Math.floor(b), 255); canvas.clear(); canvas.present(); } }

congzhangzh avatar Sep 12 '25 05:09 congzhangzh