tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[feat] Allow disable background throttling

Open mantou132 opened this issue 2 years ago • 10 comments

Describe the problem

When the APP minimizes, the webview timer will be throttling.

Describe the solution you'd like

Electorn has webPreferences.backgroundThrottling options

https://www.electronjs.org/docs/latest/api/browser-window#new-browserwindowoptions

Alternatives considered

No response

Additional context

Related: https://github.com/electron/electron/issues/1786

mantou132 avatar Sep 21 '22 05:09 mantou132

Sooo, i researched this topic 2 times already. Once for #5147 and a few weeks before that in context of a discord thread. And it really doesn't look good. The webviews don't have APIs for this at all and of course we don't have as much control over the engines as electron does -> their solution is irrelevant for us :( Here's the most relevant upstream issue i could find: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1172

The only thing left that i couldn't figure out (though it didn't look good either) is to try to make it think it's visible and focused by some weird window manipulation, but that's beyond me - cc @amrbashir

tldr: Don't wait for this feature :/

FabianLars avatar Sep 21 '22 09:09 FabianLars

This only affects Windows or other platforms as well?

try to make it think it's visible and focused by some weird window manipulation

On Windows, we don't hide the webview when the window is minimized (although we probably should) and yet the timers are being throttled. So I don't think we can do much here without explicit support in the webviews.

amrbashir avatar Sep 21 '22 11:09 amrbashir

This question was always asked in the context of Windows only. No idea how the other webiews actually behave tho

FabianLars avatar Sep 21 '22 11:09 FabianLars

Sooo, i researched this topic 2 times already. Once for #5147 and a few weeks before that in context of a discord thread. And it really doesn't look good. The webviews don't have APIs for this at all and of course we don't have as much control over the engines as electron does -> their solution is irrelevant for us :( Here's the most relevant upstream issue i could find: MicrosoftEdge/WebView2Feedback#1172

The only thing left that i couldn't figure out (though it didn't look good either) is to try to make it think it's visible and focused by some weird window manipulation, but that's beyond me - cc @amrbashir

tldr: Don't wait for this feature :/ 查了几天资料, 原因就是浏览器将不在前台的页面休眠了, Chromium最长会一分钟才唤醒一次, 导致setInterval不管设置多小的值也要等1分钟执行一次. 现在有几种方案:

  1. 最简单的方案, 使用Web Worker 中调用setInterval, 然后发送事件通知app. 但是偶尔也会等几秒才唤醒一次Web Worker.
  2. 保持一个WebSocket连接, 当有网络连接存在时Chromium不会将页面休眠. 这个必须要tauri来实现, 现在无法使用原生的WebSocket! 希望这个问题能够得到完美的解决! 谢谢!

chen-gaoyuan avatar Oct 16 '22 12:10 chen-gaoyuan

Doesn't seem like @chen-gaoyuan's idea about WebSockets and Web Worker works that well.

Did anybody solve this issue on their end?

eugenehp avatar Apr 02 '24 05:04 eugenehp

It looks like macOS recently added support for picking between {throttle, suspend, none}: https://developer.apple.com/documentation/webkit/wkpreferences/4173317-inactiveschedulingpolicy

I'm not sure if this actually works, or is the thing we're looking for, but in theory, setting it to .none in wry somewhere around the other config logic (https://github.com/tauri-apps/wry/blob/3e3d59cd4f79c21571e503a5bf80d4d54a654a38/src/wkwebview/mod.rs#L457) should work (for macOS).

venkr avatar Apr 18 '24 01:04 venkr

Still no option to avoid it? It's blocker for me, maybe needs to migrate to electron, that I don't want :(

snatvb avatar Apr 24 '24 17:04 snatvb

Still no option to avoid it? It's blocker for me, maybe needs to migrate to electron, that I don't want :(

Minimized application should not rely on interval and timers precision.

If you really need that behavior, use timers that run in background worker : https://www.npmjs.com/package/worker-timers - this should solve all of your issues.

arialpew avatar May 20 '24 18:05 arialpew

FYI: We opted out to use a web socket server that does a heartbeat to a web worker in the browser instead. It's a hack, but it solved it for now.

eugenehp avatar May 20 '24 18:05 eugenehp

Minimized application should not rely on interval and timers precision.

If you really need that behavior, use timers that run in background worker : https://www.npmjs.com/package/worker-timers - this should solve all of your issues.

Just I make a game, that works online, and there is exists animations :)

snatvb avatar May 20 '24 19:05 snatvb