powerSaveBlocker - Unhelpful docs?


I’m using powerSaveBlocker in my Electron app to prevent sleep, but the docs are quite presumptuous, and I’m not entirely sure what they expect. https://github.com/atom/electron/blob/master/docs/api/power-save-blocker.md

So I’m using .start('prevent-display-sleep') on a page (in my SPA) and getting an ID back that I’m storing. On navigating away from the page, I’m using .stop(id).

But it’s possible to navigate between pages that start the powerSaveBlocker, so I need to know if I’ve already got one running, so I don’t have multiple powerSaveBlockers running at once. I realise I can use .isStarted(id) to check if one has been started, but what does this return when it has been stopped? Does it error? Does it fail silently? Or does it just return true (because technically it has been started)?

What if I try and stop one that has already been stopped? Are any stopped ones deleted, or do they linger indefinitely?

I don’t quite understand the structure of Electron to work it out for myself, unfortunately.



These questions could be easily answered by using the Dev Tools Console, I believe. I don’t recall if there is an easy way to activate the Dev Tools in Electron apps by default … but it shouldn’t be too hard to add a key binding or something so that you can activate it and test some sample code snippets out.


I had been doing that, and I was getting some quite mysterious error messages back.

Naturally, after getting back to work after the weekend, the problem seems to have disappeared. I’m going to do a little digging anyway, find out exactly what’s going in.

Just to give this thread some value, to open dev tools by default, the BrowserWindow object has a openDevTools function. Or ⌘++i (ctrl+alt+i, I guess?) opens dev tools, if a menu isn’t set.


Sussed it, my errors was because I was passing isStarted and undefined value, which is reads as -1, which it rejects (rather than returning false as it does with any other value without a corresponding blocker).