Avoid throttling applications when the electron is in the background

Consider the following example:

setInterval(function() { console.log(new Date()); }); 

If I run it using electron example.js under OS X, it will open the icon in my dock bar and start printing time on the console. However, if the application is not focused, after a while it starts to throttle.

I looked around and found that this is related to the OS X energy-saving strategy. Now, what if I need it to work in the background? My application will be daemons that do things from time to time, and I cannot endlessly look at my application at my application.

I found out here what I can do

 electron.powerSaveBlocker.start('prevent-app-suspension'); 

Which actually fixes my problem. This, however, is quite invasive, because, as I understand it, this prevents the system from sleeping at all! I don’t need this, I just need my application to do something when the computer is active and online, without making it awake forever.

Isn’t there a middle ground between my users constantly keeping the application in the foreground and making their computer sleepless forever?

+7
throttling electron power-saving
source share
1 answer

In accordance with current documents:

Note: prevent-display-sleep has a higher priority on prevent-app-suspension . Only the highest priority takes effect. In other words, prevent-display-sleep always takes precedence over prevent-app-suspension .

For example, the API calling A requests for prevent-app-suspension and another B request requests prevent-display-sleep . prevent-display-sleep will be used until B stops its request. After that, prevent-app-suspension .

This means that the prevent-app-suspension installation is enabled, it will still allow the system to sleep and just does what you want. However, you can run this function twice by passing both flags, including both options. However, as indicated in the docs above, if both options are enabled, the system will not sleep until this flag is removed.

+3
source share

All Articles