Skip to content

Power save blocker API - #2025

Merged
zcbenz merged 4 commits into
masterfrom
power-save-blocker-api
Jun 24, 2015
Merged

zcbenz merged 4 commits into
masterfrom
power-save-blocker-api

Conversation

@hokein

@hokein hokein commented Jun 21, 2015

Copy link
Copy Markdown
Contributor

The PR introduces a new type powerSaveBlocker API. Fixes #1936

API details:

power-save-blocker

The power-save-blocker module is used to block the system from entering
low-power(sleep) mode, allowing app to keep system and screen active.

An example is:

var powerSaveBlocker = require('power-save-blocker');

var id = powerSaveBlocker.start(powerSaveBlocker.PREVENT_DISPLAY_SLEEP);
console.log(powerSaveBlocker.isStarted(id));

powerSaveBlocker.stop(id);

powerSaveBlocker.start(type)

  • type - Power save blocker type
    • powerSaveBlocker.PREVENT_APP_SUSPENSION - Prevent the application from being
      suspended. Keeps system active, but allows screen to be turned off.
      Example use cases: downloading a file, playing audio.
    • powerSaveBlocker.PREVENT_DISPLAY_SLEEP - Prevent the display from going to sleep.
      Keeps system and screen active.
      Example use case: playing video.

Starts the power save blocker preventing the system entering lower-power mode.
Returns an integer identified the power save blocker.

Note:
PREVENT_DISPLAY_SLEEP has higher precedence level than PREVENT_APP_SUSPENSION.
Only the highest precedence type takes effect. In other words, PREVENT_DISPLAY_SLEEP
always take precedence over PREVENT_APP_SUSPENSION.

For example, an API calling A requests for PREVENT_APP_SUSPENSION, and
another calling B requests for PREVENT_DISPLAY_SLEEP. PREVENT_DISPLAY_SLEEP
will be used until B stops its request. After that, PREVENT_APP_SUSPENSION is used.

powerSaveBlocker.stop(id)

  • id Integer - The power save blocker id returned by powerSaveBlocker.start.

Stops the specified power save blocker.

powerSaveBlocker.isStarted(id)

  • id Integer - The power save blocker id returned by powerSaveBlocker.start.

Returns whether the corresponding powerSaveBlocker starts.

@steelbrain

Copy link
Copy Markdown
Contributor

While it is okay to put it in a separate module, It would've been ideal to have this inside the app one, as it's functions are quite simple and very much related to it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably should return some kind of token you then pass to Stop, or at least does a refcount thing so that two Starts require two Stops to stop. Otherwise it's very hard to use this in a library without worrying if you're stomping somebody else's Power Save Block - i.e:

  1. App calls Start for a long process
  2. Some library also calls Start, for something short
  3. That library calls Stop
  4. Library accidentally stomps the App's Start call and has no way to not do that

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on the idea

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulcbetts 👍 Thanks for pointing out this. Having updated the APIs.

@hokein
hokein force-pushed the power-save-blocker-api branch from f433c64 to 0dfd277 Compare June 22, 2015 03:05

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why log at ERROR level here ?

@hokein
hokein force-pushed the power-save-blocker-api branch from 0dfd277 to 532f75f Compare June 22, 2015 04:08

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the ATOM_PRODUCT_NAME macro.

@zcbenz

zcbenz commented Jun 23, 2015

Copy link
Copy Markdown
Contributor

API returning an ID usually means the API can be started for infinite times, like setTimeout and setInterval, since powerSaveBlocker can only be started once for each type, current APIs would be quite confusing.

We can just use APIs like this:

powerSaveBlocker.preventAppSuspension();
powerSaveBlocker.allowAppSuspension();
powerSaveBlocker.isAppSuspensionPrevented();

powerSaveBlocker.preventDisplaySleep();
powerSaveBlocker.allowDisplaySleep();
powerSaveBlocker.isDisplaySleepPrevented();

It is very simple to use and implement, and there is no complex logic involved.

@hokein

hokein commented Jun 24, 2015

Copy link
Copy Markdown
Contributor Author

powerSaveBlocker.preventAppSuspension();
powerSaveBlocker.allowAppSuspension();
powerSaveBlocker.isAppSuspensionPrevented();
powerSaveBlocker.preventDisplaySleep();
powerSaveBlocker.allowDisplaySleep();
powerSaveBlocker.isDisplaySleepPrevented();

But the stomping problem pointed by @paulcbetts still exites under this set of APIs.

@zcbenz

zcbenz commented Jun 24, 2015

Copy link
Copy Markdown
Contributor

But the stomping problem pointed by @paulcbetts still exites under this set of APIs.

Ah I missed that, the design looks good then.

Comment thread docs/api/power-save-blocker.md Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use String as parameter name, like the clipboard and dialog module:

powerSaveBlocker.start('prevent-display-sleep');

It is very rare to use constant integers as parameters in JavaScript.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@zcbenz

zcbenz commented Jun 24, 2015

Copy link
Copy Markdown
Contributor

👍

zcbenz added a commit that referenced this pull request Jun 24, 2015
@zcbenz
zcbenz merged commit f93d50c into master Jun 24, 2015
@zcbenz
zcbenz deleted the power-save-blocker-api branch June 24, 2015 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants