brrd / electron-tabs

Tab component for Electron
MIT License
697 stars 130 forks source link

Is it possible to disable background throttling on tabs? #195

Closed hiagodotme closed 11 months ago

hiagodotme commented 11 months ago

My use of tabs is to keep more than one centralized panel connected to sockets.

Each tab will connect to a socket and there will still be intervals running non-stop every 1s. In my current electron application I basically needed to do this:

const win = new BrowserWindow({
     // ...
     webPreferences: {
       // ...
       backgroundThrottling: false
       // ...
     },
     // ...
});

This solves my problem, but when running my application within the tab when it is not active, backgroundThrottling limits the tab, impacting my application.

My question is, how do I disable backgroundThrottling within a tab/webview?

I tried the following, but without success:

const tab = tabGroup.addTab({
           title: data.title,
           src: data.url,
           iconURL: data.iconURL || undefined,
           active: true,
           webviewAttributes: {
             preload: rootDir + '/preload-withsdk.js',
             backgroundThrottling: false, // <----------
           }
});
brrd commented 11 months ago

I think you need to get the tab webContents and use contents.setBackgroundThrottling(false) as described here: https://www.electronjs.org/docs/latest/api/web-contents#contentssetbackgroundthrottlingallowed

The webContents you need is the one related to the webview (which you can access in tab.webview), not the window's one. Electron used to provide a method webview.getWebContents() but it's now deprecated, so you will have to write your own function to get it: https://www.electronjs.org/docs/latest/breaking-changes#deprecated-webviewgetwebcontents