Skip to content

Commit 60c5021

Browse files
authored
🤖 Merge PR DefinitelyTyped#73565 [chrome] update idle namespace by @erwanjugand
1 parent 549619f commit 60c5021

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

types/chrome/index.d.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6121,38 +6121,43 @@ declare namespace chrome {
61216121
* Permissions: "idle"
61226122
*/
61236123
export namespace idle {
6124-
export type IdleState = "active" | "idle" | "locked";
6125-
export interface IdleStateChangedEvent extends chrome.events.Event<(newState: IdleState) => void> {}
6124+
/** @since Chrome 44 */
6125+
export enum IdleState {
6126+
ACTIVE = "active",
6127+
IDLE = "idle",
6128+
LOCKED = "locked",
6129+
}
61266130

61276131
/**
61286132
* Returns "locked" if the system is locked, "idle" if the user has not generated any input for a specified number of seconds, or "active" otherwise.
61296133
* @param detectionIntervalInSeconds The system is considered idle if detectionIntervalInSeconds seconds have elapsed since the last user input detected.
6130-
* @since Chrome 116
6131-
*/
6132-
export function queryState(detectionIntervalInSeconds: number): Promise<IdleState>;
6133-
/**
6134-
* Returns "locked" if the system is locked, "idle" if the user has not generated any input for a specified number of seconds, or "active" otherwise.
6135-
* @param detectionIntervalInSeconds The system is considered idle if detectionIntervalInSeconds seconds have elapsed since the last user input detected.
6136-
* @since Chrome 25
6134+
*
6135+
* Can return its result via Promise in Manifest V3 or later since Chrome 116.
61376136
*/
6138-
export function queryState(detectionIntervalInSeconds: number, callback: (newState: IdleState) => void): void;
6137+
export function queryState(detectionIntervalInSeconds: number): Promise<`${IdleState}`>;
6138+
export function queryState(
6139+
detectionIntervalInSeconds: number,
6140+
callback: (newState: `${IdleState}`) => void,
6141+
): void;
61396142

61406143
/**
61416144
* Sets the interval, in seconds, used to determine when the system is in an idle state for onStateChanged events. The default interval is 60 seconds.
6142-
* @since Chrome 25
61436145
* @param intervalInSeconds Threshold, in seconds, used to determine when the system is in an idle state.
61446146
*/
61456147
export function setDetectionInterval(intervalInSeconds: number): void;
61466148

61476149
/**
6148-
* Gets the time, in seconds, it takes until the screen is locked automatically while idle. Returns a zero duration if the screen is never locked automatically. Currently supported on Chrome OS only.
6149-
* Parameter delay: Time, in seconds, until the screen is locked automatically while idle. This is zero if the screen never locks automatically.
6150+
* Gets the time, in seconds, it takes until the screen is locked automatically while idle. Returns a zero duration if the screen is never locked automatically.
6151+
*
6152+
* Can return its result via Promise in Manifest V3 or later since Chrome 116.
6153+
* @since Chrome 73
6154+
* @platform ChromeOS only
61506155
*/
61516156
export function getAutoLockDelay(): Promise<number>;
61526157
export function getAutoLockDelay(callback: (delay: number) => void): void;
61536158

61546159
/** Fired when the system changes to an active, idle or locked state. The event fires with "locked" if the screen is locked or the screensaver activates, "idle" if the system is unlocked and the user has not generated any input for a specified number of seconds, and "active" when the user generates input on an idle system. */
6155-
export var onStateChanged: IdleStateChangedEvent;
6160+
export const onStateChanged: events.Event<(newState: `${IdleState}`) => void>;
61566161
}
61576162

61586163
////////////////////

types/chrome/test/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4476,6 +4476,34 @@ function testIdentity() {
44764476
chrome.identity.removeCachedAuthToken(invalidTokenDetails, () => void 0).then(() => void 0);
44774477
}
44784478

4479+
// https://developer.chrome.com/docs/extensions/reference/api/idle
4480+
function testIdle() {
4481+
chrome.idle.IdleState.ACTIVE === "active";
4482+
chrome.idle.IdleState.IDLE === "idle";
4483+
chrome.idle.IdleState.LOCKED === "locked";
4484+
4485+
chrome.idle.getAutoLockDelay(); // $ExpectType Promise<number>
4486+
chrome.idle.getAutoLockDelay(delay => { // $ExpectType void
4487+
delay; // $ExpectType number
4488+
});
4489+
// @ts-expect-error
4490+
chrome.idle.getAutoLockDelay(() => {}).then(() => {});
4491+
4492+
const intervalInSeconds = 2;
4493+
chrome.idle.queryState(intervalInSeconds); // $ExpectType Promise<"active" | "idle" | "locked">
4494+
chrome.idle.queryState(intervalInSeconds, (newState) => { // $ExpectType void
4495+
newState; // $ExpectType "active" | "idle" | "locked"
4496+
});
4497+
// @ts-expect-error
4498+
chrome.idle.queryState(intervalInSeconds, () => {}).then(() => {});
4499+
4500+
chrome.idle.setDetectionInterval(intervalInSeconds); // $ExpectType void
4501+
4502+
checkChromeEvent(chrome.idle.onStateChanged, (newState) => {
4503+
newState; // $ExpectType "active" | "idle" | "locked"
4504+
});
4505+
}
4506+
44794507
// https://developer.chrome.com/docs/extensions/reference/topSites/
44804508
function testTopSites() {
44814509
chrome.topSites.get(() => {});

0 commit comments

Comments
 (0)