expo-keep-awake 11.0.0 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -10,6 +10,27 @@
10
10
 
11
11
  ### 💡 Others
12
12
 
13
+ ## 12.0.0 — 2023-02-03
14
+
15
+ ### 🛠 Breaking changes
16
+
17
+ - `KeepAwake.activateKeepAwake` has been deprecated in favor of `KeepAwake.activateKeepAwakeAsync`. ([#15826](https://github.com/expo/expo/pull/15826) by [@EvanBacon](https://github.com/EvanBacon))
18
+
19
+ ### 🎉 New features
20
+
21
+ - Added web support. ([#15826](https://github.com/expo/expo/pull/15826) by [@EvanBacon](https://github.com/EvanBacon))
22
+ - Added `KeepAwake.isAvailableAsync` which returns false on certain web browsers. ([#15826](https://github.com/expo/expo/pull/15826) by [@EvanBacon](https://github.com/EvanBacon))
23
+ - Added `KeepAwake.addListener` to observe state changes on web. ([#15826](https://github.com/expo/expo/pull/15826) by [@EvanBacon](https://github.com/EvanBacon))
24
+
25
+ ### 💡 Others
26
+
27
+ - Define `KeepAwakeOptions` type, update the doc comments. ([#20489](https://github.com/expo/expo/pull/20489) by [@Simek](https://github.com/Simek))
28
+ - On Android bump `compileSdkVersion` and `targetSdkVersion` to `33`. ([#20721](https://github.com/expo/expo/pull/20721) by [@lukmccall](https://github.com/lukmccall))
29
+
30
+ ## 11.0.1 — 2022-10-28
31
+
32
+ _This version does not introduce any user-facing changes._
33
+
13
34
  ## 11.0.0 — 2022-10-06
14
35
 
15
36
  ### 🛠 Breaking changes
package/README.md CHANGED
@@ -4,7 +4,7 @@ Provides a React component that prevents the screen sleeping when rendered. It a
4
4
 
5
5
  # API documentation
6
6
 
7
- - [Documentation for the main branch](https://github.com/expo/expo/blob/main/docs/pages/versions/unversioned/sdk/keep-awake.md)
7
+ - [Documentation for the main branch](https://github.com/expo/expo/blob/main/docs/pages/versions/unversioned/sdk/keep-awake.mdx)
8
8
  - [Documentation for the latest stable release](https://docs.expo.dev/versions/latest/sdk/keep-awake/)
9
9
 
10
10
  # Installation in managed Expo projects
@@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'
3
3
  apply plugin: 'maven-publish'
4
4
 
5
5
  group = 'host.exp.exponent'
6
- version = '11.0.0'
6
+ version = '12.0.0'
7
7
 
8
8
  buildscript {
9
9
  def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
@@ -59,7 +59,7 @@ afterEvaluate {
59
59
  }
60
60
 
61
61
  android {
62
- compileSdkVersion safeExtGet("compileSdkVersion", 31)
62
+ compileSdkVersion safeExtGet("compileSdkVersion", 33)
63
63
 
64
64
  compileOptions {
65
65
  sourceCompatibility JavaVersion.VERSION_11
@@ -72,9 +72,9 @@ android {
72
72
 
73
73
  defaultConfig {
74
74
  minSdkVersion safeExtGet("minSdkVersion", 21)
75
- targetSdkVersion safeExtGet("targetSdkVersion", 31)
75
+ targetSdkVersion safeExtGet("targetSdkVersion", 33)
76
76
  versionCode 16
77
- versionName "11.0.0"
77
+ versionName "12.0.0"
78
78
  }
79
79
  lintOptions {
80
80
  abortOnError false
@@ -1,3 +1,11 @@
1
- declare const _default: {};
1
+ import { Subscription } from 'expo-modules-core';
2
+ import { KeepAwakeListener } from './KeepAwake.types';
3
+ /** Wraps the webWakeLock API https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API */
4
+ declare const _default: {
5
+ isAvailableAsync(): Promise<boolean>;
6
+ activate(tag: string): Promise<void>;
7
+ deactivate(tag: string): Promise<void>;
8
+ addListenerForTag(tag: string, listener: KeepAwakeListener): Subscription;
9
+ };
2
10
  export default _default;
3
11
  //# sourceMappingURL=ExpoKeepAwake.web.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoKeepAwake.web.d.ts","sourceRoot":"","sources":["../src/ExpoKeepAwake.web.ts"],"names":[],"mappings":";AAAA,wBAAkB"}
1
+ {"version":3,"file":"ExpoKeepAwake.web.d.ts","sourceRoot":"","sources":["../src/ExpoKeepAwake.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEvE,OAAO,EAAuB,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAoB3E,sGAAsG;;;kBAQhF,MAAM;oBAOJ,MAAM;2BAcL,MAAM,YAAY,iBAAiB,GAAG,YAAY;;AA5B3E,wBAqDE"}
@@ -1,2 +1,59 @@
1
- export default {};
1
+ import { CodedError, Platform } from 'expo-modules-core';
2
+ import { KeepAwakeEventState } from './KeepAwake.types';
3
+ const wakeLockMap = {};
4
+ /** Wraps the webWakeLock API https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API */
5
+ export default {
6
+ async isAvailableAsync() {
7
+ if (Platform.isDOMAvailable) {
8
+ return 'wakeLock' in navigator;
9
+ }
10
+ return false;
11
+ },
12
+ async activate(tag) {
13
+ if (!Platform.isDOMAvailable) {
14
+ return;
15
+ }
16
+ const wakeLock = await navigator.wakeLock.request('screen');
17
+ wakeLockMap[tag] = wakeLock;
18
+ },
19
+ async deactivate(tag) {
20
+ if (!Platform.isDOMAvailable) {
21
+ return;
22
+ }
23
+ if (wakeLockMap[tag]) {
24
+ wakeLockMap[tag].release?.();
25
+ delete wakeLockMap[tag];
26
+ }
27
+ else {
28
+ throw new CodedError('ERR_KEEP_AWAKE_TAG_INVALID', `The wake lock with tag ${tag} has not activated yet`);
29
+ }
30
+ },
31
+ addListenerForTag(tag, listener) {
32
+ const eventListener = () => {
33
+ listener({ state: KeepAwakeEventState.RELEASE });
34
+ };
35
+ const sentinel = wakeLockMap[tag];
36
+ if (sentinel) {
37
+ if ('addEventListener' in sentinel) {
38
+ sentinel.addEventListener?.('release', eventListener);
39
+ }
40
+ else {
41
+ sentinel.onrelease = eventListener;
42
+ }
43
+ }
44
+ return {
45
+ remove: () => {
46
+ const sentinel = wakeLockMap[tag];
47
+ if (sentinel) {
48
+ if (sentinel.removeEventListener) {
49
+ sentinel.removeEventListener('release', eventListener);
50
+ }
51
+ else {
52
+ sentinel.onrelease = null;
53
+ }
54
+ }
55
+ },
56
+ };
57
+ },
58
+ };
2
59
  //# sourceMappingURL=ExpoKeepAwake.web.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoKeepAwake.web.js","sourceRoot":"","sources":["../src/ExpoKeepAwake.web.ts"],"names":[],"mappings":"AAAA,eAAe,EAAE,CAAC","sourcesContent":["export default {};\n"]}
1
+ {"version":3,"file":"ExpoKeepAwake.web.js","sourceRoot":"","sources":["../src/ExpoKeepAwake.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAgB,MAAM,mBAAmB,CAAC;AAEvE,OAAO,EAAE,mBAAmB,EAAqB,MAAM,mBAAmB,CAAC;AAE3E,MAAM,WAAW,GAAqC,EAAE,CAAC;AAkBzD,sGAAsG;AACtG,eAAe;IACb,KAAK,CAAC,gBAAgB;QACpB,IAAI,QAAQ,CAAC,cAAc,EAAE;YAC3B,OAAO,UAAU,IAAI,SAAS,CAAC;SAChC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,KAAK,CAAC,QAAQ,CAAC,GAAW;QACxB,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE;YAC5B,OAAO;SACR;QACD,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC5D,WAAW,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC;IAC9B,CAAC;IACD,KAAK,CAAC,UAAU,CAAC,GAAW;QAC1B,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE;YAC5B,OAAO;SACR;QACD,IAAI,WAAW,CAAC,GAAG,CAAC,EAAE;YACpB,WAAW,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;YAC7B,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;SACzB;aAAM;YACL,MAAM,IAAI,UAAU,CAClB,4BAA4B,EAC5B,0BAA0B,GAAG,wBAAwB,CACtD,CAAC;SACH;IACH,CAAC;IACD,iBAAiB,CAAC,GAAW,EAAE,QAA2B;QACxD,MAAM,aAAa,GAAG,GAAG,EAAE;YACzB,QAAQ,CAAC,EAAE,KAAK,EAAE,mBAAmB,CAAC,OAAO,EAAE,CAAC,CAAC;QACnD,CAAC,CAAC;QACF,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,QAAQ,EAAE;YACZ,IAAI,kBAAkB,IAAI,QAAQ,EAAE;gBAClC,QAAQ,CAAC,gBAAgB,EAAE,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;aACvD;iBAAM;gBACL,QAAQ,CAAC,SAAS,GAAG,aAAa,CAAC;aACpC;SACF;QACD,OAAO;YACL,MAAM,EAAE,GAAG,EAAE;gBACX,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;gBAClC,IAAI,QAAQ,EAAE;oBACZ,IAAI,QAAQ,CAAC,mBAAmB,EAAE;wBAChC,QAAQ,CAAC,mBAAmB,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;qBACxD;yBAAM;wBACL,QAAQ,CAAC,SAAS,GAAG,IAAI,CAAC;qBAC3B;iBACF;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC","sourcesContent":["import { CodedError, Platform, Subscription } from 'expo-modules-core';\n\nimport { KeepAwakeEventState, KeepAwakeListener } from './KeepAwake.types';\n\nconst wakeLockMap: Record<string, WakeLockSentinel> = {};\n\ntype WakeLockSentinel = {\n onrelease: null | ((event: any) => void);\n released: boolean;\n type: 'screen';\n release?: Function;\n\n addEventListener?: (event: string, listener: (event: any) => void) => void;\n removeEventListener?: (event: string, listener: (event: any) => void) => void;\n};\n\ndeclare const navigator: {\n wakeLock: {\n request(type: 'screen'): Promise<WakeLockSentinel>;\n };\n};\n\n/** Wraps the webWakeLock API https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API */\nexport default {\n async isAvailableAsync() {\n if (Platform.isDOMAvailable) {\n return 'wakeLock' in navigator;\n }\n return false;\n },\n async activate(tag: string) {\n if (!Platform.isDOMAvailable) {\n return;\n }\n const wakeLock = await navigator.wakeLock.request('screen');\n wakeLockMap[tag] = wakeLock;\n },\n async deactivate(tag: string) {\n if (!Platform.isDOMAvailable) {\n return;\n }\n if (wakeLockMap[tag]) {\n wakeLockMap[tag].release?.();\n delete wakeLockMap[tag];\n } else {\n throw new CodedError(\n 'ERR_KEEP_AWAKE_TAG_INVALID',\n `The wake lock with tag ${tag} has not activated yet`\n );\n }\n },\n addListenerForTag(tag: string, listener: KeepAwakeListener): Subscription {\n const eventListener = () => {\n listener({ state: KeepAwakeEventState.RELEASE });\n };\n const sentinel = wakeLockMap[tag];\n if (sentinel) {\n if ('addEventListener' in sentinel) {\n sentinel.addEventListener?.('release', eventListener);\n } else {\n sentinel.onrelease = eventListener;\n }\n }\n return {\n remove: () => {\n const sentinel = wakeLockMap[tag];\n if (sentinel) {\n if (sentinel.removeEventListener) {\n sentinel.removeEventListener('release', eventListener);\n } else {\n sentinel.onrelease = null;\n }\n }\n },\n };\n },\n};\n"]}
@@ -0,0 +1,21 @@
1
+ export type KeepAwakeEvent = {
2
+ /** Keep awake state. */
3
+ state: KeepAwakeEventState;
4
+ };
5
+ export declare enum KeepAwakeEventState {
6
+ RELEASE = "release"
7
+ }
8
+ export type KeepAwakeListener = (event: KeepAwakeEvent) => void;
9
+ export type KeepAwakeOptions = {
10
+ /**
11
+ * The call will throw an unhandled promise rejection on Android when the original Activity is dead or deactivated.
12
+ * Set the value to `true` for suppressing the uncaught exception.
13
+ */
14
+ suppressDeactivateWarnings?: boolean;
15
+ /**
16
+ * A callback that is invoked when the keep-awake state changes.
17
+ * @platform web
18
+ */
19
+ listener?: KeepAwakeListener;
20
+ };
21
+ //# sourceMappingURL=KeepAwake.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeepAwake.types.d.ts","sourceRoot":"","sources":["../src/KeepAwake.types.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,cAAc,GAAG;IAC3B,wBAAwB;IACxB,KAAK,EAAE,mBAAmB,CAAC;CAC5B,CAAC;AAGF,oBAAY,mBAAmB;IAC7B,OAAO,YAAY;CACpB;AAGD,MAAM,MAAM,iBAAiB,GAAG,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,CAAC;AAEhE,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;OAGG;IACH,0BAA0B,CAAC,EAAE,OAAO,CAAC;IAErC;;;OAGG;IACH,QAAQ,CAAC,EAAE,iBAAiB,CAAC;CAC9B,CAAC"}
@@ -0,0 +1,6 @@
1
+ // @needsAudit
2
+ export var KeepAwakeEventState;
3
+ (function (KeepAwakeEventState) {
4
+ KeepAwakeEventState["RELEASE"] = "release";
5
+ })(KeepAwakeEventState || (KeepAwakeEventState = {}));
6
+ //# sourceMappingURL=KeepAwake.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeepAwake.types.js","sourceRoot":"","sources":["../src/KeepAwake.types.ts"],"names":[],"mappings":"AAMA,cAAc;AACd,MAAM,CAAN,IAAY,mBAEX;AAFD,WAAY,mBAAmB;IAC7B,0CAAmB,CAAA;AACrB,CAAC,EAFW,mBAAmB,KAAnB,mBAAmB,QAE9B","sourcesContent":["// @needsAudit\nexport type KeepAwakeEvent = {\n /** Keep awake state. */\n state: KeepAwakeEventState;\n};\n\n// @needsAudit\nexport enum KeepAwakeEventState {\n RELEASE = 'release',\n}\n\n// @needsAudit\nexport type KeepAwakeListener = (event: KeepAwakeEvent) => void;\n\nexport type KeepAwakeOptions = {\n /**\n * The call will throw an unhandled promise rejection on Android when the original Activity is dead or deactivated.\n * Set the value to `true` for suppressing the uncaught exception.\n */\n suppressDeactivateWarnings?: boolean;\n\n /**\n * A callback that is invoked when the keep-awake state changes.\n * @platform web\n */\n listener?: KeepAwakeListener;\n};\n"]}
package/build/index.d.ts CHANGED
@@ -1,34 +1,64 @@
1
+ import { Subscription } from 'expo-modules-core';
2
+ import { KeepAwakeListener, KeepAwakeOptions } from './KeepAwake.types';
3
+ /** Default tag, used when no tag has been specified in keep awake method calls. */
1
4
  export declare const ExpoKeepAwakeTag = "ExpoKeepAwakeDefaultTag";
5
+ /** @returns `true` on all platforms except [unsupported web browsers](https://caniuse.com/wake-lock). */
6
+ export declare function isAvailableAsync(): Promise<boolean>;
2
7
  /**
3
8
  * A React hook to keep the screen awake for as long as the owner component is mounted.
4
9
  * The optionally provided `tag` argument is used when activating and deactivating the keep-awake
5
- * feature. If unspecified, the default `tag` is used. See the documentation for `activateKeepAwake`
10
+ * feature. If unspecified, the default `tag` is used. See the documentation for `activateKeepAwakeAsync`
6
11
  * below to learn more about the `tag` argument.
7
12
  *
8
- * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.
9
- * @param options *Optional*
10
- * - `suppressDeactivateWarnings` *Optional* -
11
- * The call will throw an unhandled promise rejection on Android
12
- * when the original Activity is dead or deactivated.
13
- * Set the value to true for suppressing the uncaught exception.
13
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
14
+ * @param options Additional options for the keep awake hook.
14
15
  */
15
- export declare function useKeepAwake(tag?: string, options?: {
16
- suppressDeactivateWarnings: boolean;
17
- }): void;
16
+ export declare function useKeepAwake(tag?: string, options?: KeepAwakeOptions): void;
18
17
  /**
19
18
  * Prevents the screen from sleeping until `deactivateKeepAwake` is called with the same `tag` value.
20
19
  *
21
20
  * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`
22
21
  * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate
23
22
  * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.
24
- * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.
23
+ *
24
+ * Web support [is limited](https://caniuse.com/wake-lock).
25
+ *
26
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
27
+ * @deprecated use `activateKeepAwakeAsync` instead.
25
28
  */
26
29
  export declare function activateKeepAwake(tag?: string): Promise<void>;
30
+ /**
31
+ * Prevents the screen from sleeping until `deactivateKeepAwake` is called with the same `tag` value.
32
+ *
33
+ * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`
34
+ * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate
35
+ * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.
36
+ *
37
+ * Web support [is limited](https://caniuse.com/wake-lock).
38
+ *
39
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
40
+ */
41
+ export declare function activateKeepAwakeAsync(tag?: string): Promise<void>;
27
42
  /**
28
43
  * Releases the lock on screen-sleep prevention associated with the given `tag` value. If `tag`
29
44
  * is unspecified, it defaults to the same default tag that `activateKeepAwake` uses.
30
- * @param tag *Optional* - Tag to release the lock on screen sleep prevention. If not provided,
45
+ *
46
+ * @param tag Tag to release the lock on screen sleep prevention. If not provided,
31
47
  * the default tag is used.
32
48
  */
33
49
  export declare function deactivateKeepAwake(tag?: string): Promise<void>;
50
+ /**
51
+ * Observe changes to the keep awake timer.
52
+ * On web, this changes when navigating away from the active window/tab. No-op on native.
53
+ * @platform web
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * KeepAwake.addListener(({ state }) => {
58
+ * // ...
59
+ * });
60
+ * ```
61
+ */
62
+ export declare function addListener(tagOrListener: string | KeepAwakeListener, listener?: KeepAwakeListener): Subscription;
63
+ export * from './KeepAwake.types';
34
64
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,gBAAgB,4BAA4B,CAAC;AAG1D;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAC1B,GAAG,GAAE,MAAyB,EAC9B,OAAO,CAAC,EAAE;IACR,0BAA0B,EAAE,OAAO,CAAC;CACrC,GACA,IAAI,CAWN;AAGD;;;;;;;GAOG;AAEH,wBAAsB,iBAAiB,CAAC,GAAG,GAAE,MAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAErF;AAGD;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,GAAE,MAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAEvF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAuB,MAAM,mBAAmB,CAAC;AAItE,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAExE,mFAAmF;AACnF,eAAO,MAAM,gBAAgB,4BAA4B,CAAC;AAE1D,yGAAyG;AACzG,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,OAAO,CAAC,CAKzD;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,GAAG,GAAE,MAAyB,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,IAAI,CAiB7F;AAGD;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,GAAE,MAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAG/E;AAGD;;;;;;;;;;GAUG;AACH,wBAAsB,sBAAsB,CAAC,GAAG,GAAE,MAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAE1F;AAGD;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,GAAE,MAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAEvF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CACzB,aAAa,EAAE,MAAM,GAAG,iBAAiB,EACzC,QAAQ,CAAC,EAAE,iBAAiB,GAC3B,YAAY,CAUd;AAED,cAAc,mBAAmB,CAAC"}
package/build/index.js CHANGED
@@ -1,24 +1,34 @@
1
+ import { UnavailabilityError } from 'expo-modules-core';
1
2
  import { useEffect } from 'react';
2
3
  import ExpoKeepAwake from './ExpoKeepAwake';
4
+ /** Default tag, used when no tag has been specified in keep awake method calls. */
3
5
  export const ExpoKeepAwakeTag = 'ExpoKeepAwakeDefaultTag';
4
- // @needsAudit
6
+ /** @returns `true` on all platforms except [unsupported web browsers](https://caniuse.com/wake-lock). */
7
+ export async function isAvailableAsync() {
8
+ if (ExpoKeepAwake.isAvailableAsync) {
9
+ return await ExpoKeepAwake.isAvailableAsync();
10
+ }
11
+ return true;
12
+ }
5
13
  /**
6
14
  * A React hook to keep the screen awake for as long as the owner component is mounted.
7
15
  * The optionally provided `tag` argument is used when activating and deactivating the keep-awake
8
- * feature. If unspecified, the default `tag` is used. See the documentation for `activateKeepAwake`
16
+ * feature. If unspecified, the default `tag` is used. See the documentation for `activateKeepAwakeAsync`
9
17
  * below to learn more about the `tag` argument.
10
18
  *
11
- * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.
12
- * @param options *Optional*
13
- * - `suppressDeactivateWarnings` *Optional* -
14
- * The call will throw an unhandled promise rejection on Android
15
- * when the original Activity is dead or deactivated.
16
- * Set the value to true for suppressing the uncaught exception.
19
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
20
+ * @param options Additional options for the keep awake hook.
17
21
  */
18
22
  export function useKeepAwake(tag = ExpoKeepAwakeTag, options) {
19
23
  useEffect(() => {
20
- activateKeepAwake(tag);
24
+ let isMounted = true;
25
+ activateKeepAwakeAsync(tag).then(() => {
26
+ if (isMounted && ExpoKeepAwake.addListenerForTag && options?.listener) {
27
+ addListener(tag, options.listener);
28
+ }
29
+ });
21
30
  return () => {
31
+ isMounted = false;
22
32
  if (options?.suppressDeactivateWarnings) {
23
33
  deactivateKeepAwake(tag).catch(() => { });
24
34
  }
@@ -35,19 +45,62 @@ export function useKeepAwake(tag = ExpoKeepAwakeTag, options) {
35
45
  * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`
36
46
  * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate
37
47
  * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.
38
- * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.
48
+ *
49
+ * Web support [is limited](https://caniuse.com/wake-lock).
50
+ *
51
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
52
+ * @deprecated use `activateKeepAwakeAsync` instead.
39
53
  */
40
- export async function activateKeepAwake(tag = ExpoKeepAwakeTag) {
54
+ export function activateKeepAwake(tag = ExpoKeepAwakeTag) {
55
+ console.warn('`activateKeepAwake` is deprecated. Use `activateKeepAwakeAsync` instead.');
56
+ return activateKeepAwakeAsync(tag);
57
+ }
58
+ // @needsAudit
59
+ /**
60
+ * Prevents the screen from sleeping until `deactivateKeepAwake` is called with the same `tag` value.
61
+ *
62
+ * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`
63
+ * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate
64
+ * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.
65
+ *
66
+ * Web support [is limited](https://caniuse.com/wake-lock).
67
+ *
68
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
69
+ */
70
+ export async function activateKeepAwakeAsync(tag = ExpoKeepAwakeTag) {
41
71
  await ExpoKeepAwake.activate?.(tag);
42
72
  }
43
73
  // @needsAudit
44
74
  /**
45
75
  * Releases the lock on screen-sleep prevention associated with the given `tag` value. If `tag`
46
76
  * is unspecified, it defaults to the same default tag that `activateKeepAwake` uses.
47
- * @param tag *Optional* - Tag to release the lock on screen sleep prevention. If not provided,
77
+ *
78
+ * @param tag Tag to release the lock on screen sleep prevention. If not provided,
48
79
  * the default tag is used.
49
80
  */
50
81
  export async function deactivateKeepAwake(tag = ExpoKeepAwakeTag) {
51
82
  await ExpoKeepAwake.deactivate?.(tag);
52
83
  }
84
+ /**
85
+ * Observe changes to the keep awake timer.
86
+ * On web, this changes when navigating away from the active window/tab. No-op on native.
87
+ * @platform web
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * KeepAwake.addListener(({ state }) => {
92
+ * // ...
93
+ * });
94
+ * ```
95
+ */
96
+ export function addListener(tagOrListener, listener) {
97
+ // Assert so the type is non-nullable.
98
+ if (!ExpoKeepAwake.addListenerForTag) {
99
+ throw new UnavailabilityError('ExpoKeepAwake', 'addListenerForTag');
100
+ }
101
+ const tag = typeof tagOrListener === 'string' ? tagOrListener : ExpoKeepAwakeTag;
102
+ const _listener = typeof tagOrListener === 'function' ? tagOrListener : listener;
103
+ return ExpoKeepAwake.addListenerForTag(tag, _listener);
104
+ }
105
+ export * from './KeepAwake.types';
53
106
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAElC,OAAO,aAAa,MAAM,iBAAiB,CAAC;AAE5C,MAAM,CAAC,MAAM,gBAAgB,GAAG,yBAAyB,CAAC;AAE1D,cAAc;AACd;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAC1B,MAAc,gBAAgB,EAC9B,OAEC;IAED,SAAS,CAAC,GAAG,EAAE;QACb,iBAAiB,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,GAAG,EAAE;YACV,IAAI,OAAO,EAAE,0BAA0B,EAAE;gBACvC,mBAAmB,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;aAC1C;iBAAM;gBACL,mBAAmB,CAAC,GAAG,CAAC,CAAC;aAC1B;QACH,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC;AAED,cAAc;AACd;;;;;;;GAOG;AAEH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,MAAc,gBAAgB;IACpE,MAAM,aAAa,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;AACtC,CAAC;AAED,cAAc;AACd;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,MAAc,gBAAgB;IACtE,MAAM,aAAa,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC;AACxC,CAAC","sourcesContent":["import { useEffect } from 'react';\n\nimport ExpoKeepAwake from './ExpoKeepAwake';\n\nexport const ExpoKeepAwakeTag = 'ExpoKeepAwakeDefaultTag';\n\n// @needsAudit\n/**\n * A React hook to keep the screen awake for as long as the owner component is mounted.\n * The optionally provided `tag` argument is used when activating and deactivating the keep-awake\n * feature. If unspecified, the default `tag` is used. See the documentation for `activateKeepAwake`\n * below to learn more about the `tag` argument.\n *\n * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.\n * @param options *Optional*\n * - `suppressDeactivateWarnings` *Optional* -\n * The call will throw an unhandled promise rejection on Android\n * when the original Activity is dead or deactivated.\n * Set the value to true for suppressing the uncaught exception.\n */\nexport function useKeepAwake(\n tag: string = ExpoKeepAwakeTag,\n options?: {\n suppressDeactivateWarnings: boolean;\n }\n): void {\n useEffect(() => {\n activateKeepAwake(tag);\n return () => {\n if (options?.suppressDeactivateWarnings) {\n deactivateKeepAwake(tag).catch(() => {});\n } else {\n deactivateKeepAwake(tag);\n }\n };\n }, [tag]);\n}\n\n// @needsAudit\n/**\n * Prevents the screen from sleeping until `deactivateKeepAwake` is called with the same `tag` value.\n *\n * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`\n * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate\n * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.\n * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.\n */\n\nexport async function activateKeepAwake(tag: string = ExpoKeepAwakeTag): Promise<void> {\n await ExpoKeepAwake.activate?.(tag);\n}\n\n// @needsAudit\n/**\n * Releases the lock on screen-sleep prevention associated with the given `tag` value. If `tag`\n * is unspecified, it defaults to the same default tag that `activateKeepAwake` uses.\n * @param tag *Optional* - Tag to release the lock on screen sleep prevention. If not provided,\n * the default tag is used.\n */\nexport async function deactivateKeepAwake(tag: string = ExpoKeepAwakeTag): Promise<void> {\n await ExpoKeepAwake.deactivate?.(tag);\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAElC,OAAO,aAAa,MAAM,iBAAiB,CAAC;AAG5C,mFAAmF;AACnF,MAAM,CAAC,MAAM,gBAAgB,GAAG,yBAAyB,CAAC;AAE1D,yGAAyG;AACzG,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,IAAI,aAAa,CAAC,gBAAgB,EAAE;QAClC,OAAO,MAAM,aAAa,CAAC,gBAAgB,EAAE,CAAC;KAC/C;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAAC,MAAc,gBAAgB,EAAE,OAA0B;IACrF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,SAAS,GAAG,IAAI,CAAC;QACrB,sBAAsB,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;YACpC,IAAI,SAAS,IAAI,aAAa,CAAC,iBAAiB,IAAI,OAAO,EAAE,QAAQ,EAAE;gBACrE,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;aACpC;QACH,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,EAAE;YACV,SAAS,GAAG,KAAK,CAAC;YAClB,IAAI,OAAO,EAAE,0BAA0B,EAAE;gBACvC,mBAAmB,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;aAC1C;iBAAM;gBACL,mBAAmB,CAAC,GAAG,CAAC,CAAC;aAC1B;QACH,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC;AAED,cAAc;AACd;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAc,gBAAgB;IAC9D,OAAO,CAAC,IAAI,CAAC,0EAA0E,CAAC,CAAC;IACzF,OAAO,sBAAsB,CAAC,GAAG,CAAC,CAAC;AACrC,CAAC;AAED,cAAc;AACd;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,MAAc,gBAAgB;IACzE,MAAM,aAAa,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;AACtC,CAAC;AAED,cAAc;AACd;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,MAAc,gBAAgB;IACtE,MAAM,aAAa,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CACzB,aAAyC,EACzC,QAA4B;IAE5B,sCAAsC;IACtC,IAAI,CAAC,aAAa,CAAC,iBAAiB,EAAE;QACpC,MAAM,IAAI,mBAAmB,CAAC,eAAe,EAAE,mBAAmB,CAAC,CAAC;KACrE;IAED,MAAM,GAAG,GAAG,OAAO,aAAa,KAAK,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,gBAAgB,CAAC;IACjF,MAAM,SAAS,GAAG,OAAO,aAAa,KAAK,UAAU,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC;IAEjF,OAAO,aAAa,CAAC,iBAAiB,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;AACzD,CAAC;AAED,cAAc,mBAAmB,CAAC","sourcesContent":["import { Subscription, UnavailabilityError } from 'expo-modules-core';\nimport { useEffect } from 'react';\n\nimport ExpoKeepAwake from './ExpoKeepAwake';\nimport { KeepAwakeListener, KeepAwakeOptions } from './KeepAwake.types';\n\n/** Default tag, used when no tag has been specified in keep awake method calls. */\nexport const ExpoKeepAwakeTag = 'ExpoKeepAwakeDefaultTag';\n\n/** @returns `true` on all platforms except [unsupported web browsers](https://caniuse.com/wake-lock). */\nexport async function isAvailableAsync(): Promise<boolean> {\n if (ExpoKeepAwake.isAvailableAsync) {\n return await ExpoKeepAwake.isAvailableAsync();\n }\n return true;\n}\n\n/**\n * A React hook to keep the screen awake for as long as the owner component is mounted.\n * The optionally provided `tag` argument is used when activating and deactivating the keep-awake\n * feature. If unspecified, the default `tag` is used. See the documentation for `activateKeepAwakeAsync`\n * below to learn more about the `tag` argument.\n *\n * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.\n * @param options Additional options for the keep awake hook.\n */\nexport function useKeepAwake(tag: string = ExpoKeepAwakeTag, options?: KeepAwakeOptions): void {\n useEffect(() => {\n let isMounted = true;\n activateKeepAwakeAsync(tag).then(() => {\n if (isMounted && ExpoKeepAwake.addListenerForTag && options?.listener) {\n addListener(tag, options.listener);\n }\n });\n return () => {\n isMounted = false;\n if (options?.suppressDeactivateWarnings) {\n deactivateKeepAwake(tag).catch(() => {});\n } else {\n deactivateKeepAwake(tag);\n }\n };\n }, [tag]);\n}\n\n// @needsAudit\n/**\n * Prevents the screen from sleeping until `deactivateKeepAwake` is called with the same `tag` value.\n *\n * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`\n * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate\n * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.\n *\n * Web support [is limited](https://caniuse.com/wake-lock).\n *\n * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.\n * @deprecated use `activateKeepAwakeAsync` instead.\n */\nexport function activateKeepAwake(tag: string = ExpoKeepAwakeTag): Promise<void> {\n console.warn('`activateKeepAwake` is deprecated. Use `activateKeepAwakeAsync` instead.');\n return activateKeepAwakeAsync(tag);\n}\n\n// @needsAudit\n/**\n * Prevents the screen from sleeping until `deactivateKeepAwake` is called with the same `tag` value.\n *\n * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`\n * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate\n * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.\n *\n * Web support [is limited](https://caniuse.com/wake-lock).\n *\n * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.\n */\nexport async function activateKeepAwakeAsync(tag: string = ExpoKeepAwakeTag): Promise<void> {\n await ExpoKeepAwake.activate?.(tag);\n}\n\n// @needsAudit\n/**\n * Releases the lock on screen-sleep prevention associated with the given `tag` value. If `tag`\n * is unspecified, it defaults to the same default tag that `activateKeepAwake` uses.\n *\n * @param tag Tag to release the lock on screen sleep prevention. If not provided,\n * the default tag is used.\n */\nexport async function deactivateKeepAwake(tag: string = ExpoKeepAwakeTag): Promise<void> {\n await ExpoKeepAwake.deactivate?.(tag);\n}\n\n/**\n * Observe changes to the keep awake timer.\n * On web, this changes when navigating away from the active window/tab. No-op on native.\n * @platform web\n *\n * @example\n * ```ts\n * KeepAwake.addListener(({ state }) => {\n * // ...\n * });\n * ```\n */\nexport function addListener(\n tagOrListener: string | KeepAwakeListener,\n listener?: KeepAwakeListener\n): Subscription {\n // Assert so the type is non-nullable.\n if (!ExpoKeepAwake.addListenerForTag) {\n throw new UnavailabilityError('ExpoKeepAwake', 'addListenerForTag');\n }\n\n const tag = typeof tagOrListener === 'string' ? tagOrListener : ExpoKeepAwakeTag;\n const _listener = typeof tagOrListener === 'function' ? tagOrListener : listener;\n\n return ExpoKeepAwake.addListenerForTag(tag, _listener);\n}\n\nexport * from './KeepAwake.types';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-keep-awake",
3
- "version": "11.0.0",
3
+ "version": "12.0.0",
4
4
  "description": "Provides a React component that prevents the screen sleeping when rendered. It also exposes static methods to control the behavior imperatively.",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -34,7 +34,7 @@
34
34
  "dependencies": {},
35
35
  "devDependencies": {
36
36
  "@testing-library/react-hooks": "^7.0.1",
37
- "expo-module-scripts": "^2.0.0"
37
+ "expo-module-scripts": "^3.0.0"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "expo": "*"
@@ -42,5 +42,5 @@
42
42
  "jest": {
43
43
  "preset": "expo-module-scripts"
44
44
  },
45
- "gitHead": "1327fe2bc92f71f7213cd5ddd40fa4f8a5336bc6"
45
+ "gitHead": "1815e2eaad8c753588c7b1eb74420174a28e01f4"
46
46
  }
@@ -1 +1,77 @@
1
- export default {};
1
+ import { CodedError, Platform, Subscription } from 'expo-modules-core';
2
+
3
+ import { KeepAwakeEventState, KeepAwakeListener } from './KeepAwake.types';
4
+
5
+ const wakeLockMap: Record<string, WakeLockSentinel> = {};
6
+
7
+ type WakeLockSentinel = {
8
+ onrelease: null | ((event: any) => void);
9
+ released: boolean;
10
+ type: 'screen';
11
+ release?: Function;
12
+
13
+ addEventListener?: (event: string, listener: (event: any) => void) => void;
14
+ removeEventListener?: (event: string, listener: (event: any) => void) => void;
15
+ };
16
+
17
+ declare const navigator: {
18
+ wakeLock: {
19
+ request(type: 'screen'): Promise<WakeLockSentinel>;
20
+ };
21
+ };
22
+
23
+ /** Wraps the webWakeLock API https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API */
24
+ export default {
25
+ async isAvailableAsync() {
26
+ if (Platform.isDOMAvailable) {
27
+ return 'wakeLock' in navigator;
28
+ }
29
+ return false;
30
+ },
31
+ async activate(tag: string) {
32
+ if (!Platform.isDOMAvailable) {
33
+ return;
34
+ }
35
+ const wakeLock = await navigator.wakeLock.request('screen');
36
+ wakeLockMap[tag] = wakeLock;
37
+ },
38
+ async deactivate(tag: string) {
39
+ if (!Platform.isDOMAvailable) {
40
+ return;
41
+ }
42
+ if (wakeLockMap[tag]) {
43
+ wakeLockMap[tag].release?.();
44
+ delete wakeLockMap[tag];
45
+ } else {
46
+ throw new CodedError(
47
+ 'ERR_KEEP_AWAKE_TAG_INVALID',
48
+ `The wake lock with tag ${tag} has not activated yet`
49
+ );
50
+ }
51
+ },
52
+ addListenerForTag(tag: string, listener: KeepAwakeListener): Subscription {
53
+ const eventListener = () => {
54
+ listener({ state: KeepAwakeEventState.RELEASE });
55
+ };
56
+ const sentinel = wakeLockMap[tag];
57
+ if (sentinel) {
58
+ if ('addEventListener' in sentinel) {
59
+ sentinel.addEventListener?.('release', eventListener);
60
+ } else {
61
+ sentinel.onrelease = eventListener;
62
+ }
63
+ }
64
+ return {
65
+ remove: () => {
66
+ const sentinel = wakeLockMap[tag];
67
+ if (sentinel) {
68
+ if (sentinel.removeEventListener) {
69
+ sentinel.removeEventListener('release', eventListener);
70
+ } else {
71
+ sentinel.onrelease = null;
72
+ }
73
+ }
74
+ },
75
+ };
76
+ },
77
+ };
@@ -0,0 +1,27 @@
1
+ // @needsAudit
2
+ export type KeepAwakeEvent = {
3
+ /** Keep awake state. */
4
+ state: KeepAwakeEventState;
5
+ };
6
+
7
+ // @needsAudit
8
+ export enum KeepAwakeEventState {
9
+ RELEASE = 'release',
10
+ }
11
+
12
+ // @needsAudit
13
+ export type KeepAwakeListener = (event: KeepAwakeEvent) => void;
14
+
15
+ export type KeepAwakeOptions = {
16
+ /**
17
+ * The call will throw an unhandled promise rejection on Android when the original Activity is dead or deactivated.
18
+ * Set the value to `true` for suppressing the uncaught exception.
19
+ */
20
+ suppressDeactivateWarnings?: boolean;
21
+
22
+ /**
23
+ * A callback that is invoked when the keep-awake state changes.
24
+ * @platform web
25
+ */
26
+ listener?: KeepAwakeListener;
27
+ };
package/src/index.ts CHANGED
@@ -1,32 +1,39 @@
1
+ import { Subscription, UnavailabilityError } from 'expo-modules-core';
1
2
  import { useEffect } from 'react';
2
3
 
3
4
  import ExpoKeepAwake from './ExpoKeepAwake';
5
+ import { KeepAwakeListener, KeepAwakeOptions } from './KeepAwake.types';
4
6
 
7
+ /** Default tag, used when no tag has been specified in keep awake method calls. */
5
8
  export const ExpoKeepAwakeTag = 'ExpoKeepAwakeDefaultTag';
6
9
 
7
- // @needsAudit
10
+ /** @returns `true` on all platforms except [unsupported web browsers](https://caniuse.com/wake-lock). */
11
+ export async function isAvailableAsync(): Promise<boolean> {
12
+ if (ExpoKeepAwake.isAvailableAsync) {
13
+ return await ExpoKeepAwake.isAvailableAsync();
14
+ }
15
+ return true;
16
+ }
17
+
8
18
  /**
9
19
  * A React hook to keep the screen awake for as long as the owner component is mounted.
10
20
  * The optionally provided `tag` argument is used when activating and deactivating the keep-awake
11
- * feature. If unspecified, the default `tag` is used. See the documentation for `activateKeepAwake`
21
+ * feature. If unspecified, the default `tag` is used. See the documentation for `activateKeepAwakeAsync`
12
22
  * below to learn more about the `tag` argument.
13
23
  *
14
- * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.
15
- * @param options *Optional*
16
- * - `suppressDeactivateWarnings` *Optional* -
17
- * The call will throw an unhandled promise rejection on Android
18
- * when the original Activity is dead or deactivated.
19
- * Set the value to true for suppressing the uncaught exception.
24
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
25
+ * @param options Additional options for the keep awake hook.
20
26
  */
21
- export function useKeepAwake(
22
- tag: string = ExpoKeepAwakeTag,
23
- options?: {
24
- suppressDeactivateWarnings: boolean;
25
- }
26
- ): void {
27
+ export function useKeepAwake(tag: string = ExpoKeepAwakeTag, options?: KeepAwakeOptions): void {
27
28
  useEffect(() => {
28
- activateKeepAwake(tag);
29
+ let isMounted = true;
30
+ activateKeepAwakeAsync(tag).then(() => {
31
+ if (isMounted && ExpoKeepAwake.addListenerForTag && options?.listener) {
32
+ addListener(tag, options.listener);
33
+ }
34
+ });
29
35
  return () => {
36
+ isMounted = false;
30
37
  if (options?.suppressDeactivateWarnings) {
31
38
  deactivateKeepAwake(tag).catch(() => {});
32
39
  } else {
@@ -43,10 +50,30 @@ export function useKeepAwake(
43
50
  * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`
44
51
  * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate
45
52
  * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.
46
- * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.
53
+ *
54
+ * Web support [is limited](https://caniuse.com/wake-lock).
55
+ *
56
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
57
+ * @deprecated use `activateKeepAwakeAsync` instead.
47
58
  */
59
+ export function activateKeepAwake(tag: string = ExpoKeepAwakeTag): Promise<void> {
60
+ console.warn('`activateKeepAwake` is deprecated. Use `activateKeepAwakeAsync` instead.');
61
+ return activateKeepAwakeAsync(tag);
62
+ }
48
63
 
49
- export async function activateKeepAwake(tag: string = ExpoKeepAwakeTag): Promise<void> {
64
+ // @needsAudit
65
+ /**
66
+ * Prevents the screen from sleeping until `deactivateKeepAwake` is called with the same `tag` value.
67
+ *
68
+ * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`
69
+ * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate
70
+ * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.
71
+ *
72
+ * Web support [is limited](https://caniuse.com/wake-lock).
73
+ *
74
+ * @param tag Tag to lock screen sleep prevention. If not provided, the default tag is used.
75
+ */
76
+ export async function activateKeepAwakeAsync(tag: string = ExpoKeepAwakeTag): Promise<void> {
50
77
  await ExpoKeepAwake.activate?.(tag);
51
78
  }
52
79
 
@@ -54,9 +81,39 @@ export async function activateKeepAwake(tag: string = ExpoKeepAwakeTag): Promise
54
81
  /**
55
82
  * Releases the lock on screen-sleep prevention associated with the given `tag` value. If `tag`
56
83
  * is unspecified, it defaults to the same default tag that `activateKeepAwake` uses.
57
- * @param tag *Optional* - Tag to release the lock on screen sleep prevention. If not provided,
84
+ *
85
+ * @param tag Tag to release the lock on screen sleep prevention. If not provided,
58
86
  * the default tag is used.
59
87
  */
60
88
  export async function deactivateKeepAwake(tag: string = ExpoKeepAwakeTag): Promise<void> {
61
89
  await ExpoKeepAwake.deactivate?.(tag);
62
90
  }
91
+
92
+ /**
93
+ * Observe changes to the keep awake timer.
94
+ * On web, this changes when navigating away from the active window/tab. No-op on native.
95
+ * @platform web
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * KeepAwake.addListener(({ state }) => {
100
+ * // ...
101
+ * });
102
+ * ```
103
+ */
104
+ export function addListener(
105
+ tagOrListener: string | KeepAwakeListener,
106
+ listener?: KeepAwakeListener
107
+ ): Subscription {
108
+ // Assert so the type is non-nullable.
109
+ if (!ExpoKeepAwake.addListenerForTag) {
110
+ throw new UnavailabilityError('ExpoKeepAwake', 'addListenerForTag');
111
+ }
112
+
113
+ const tag = typeof tagOrListener === 'string' ? tagOrListener : ExpoKeepAwakeTag;
114
+ const _listener = typeof tagOrListener === 'function' ? tagOrListener : listener;
115
+
116
+ return ExpoKeepAwake.addListenerForTag(tag, _listener);
117
+ }
118
+
119
+ export * from './KeepAwake.types';