1 | import { useEffect } from 'react';
|
2 |
|
3 | import ExpoKeepAwake from './ExpoKeepAwake';
|
4 |
|
5 | const ExpoKeepAwakeTag = 'ExpoKeepAwakeDefaultTag';
|
6 |
|
7 | // @needsAudit
|
8 | /**
|
9 | * A React hook to keep the screen awake for as long as the owner component is mounted.
|
10 | * 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`
|
12 | * below to learn more about the `tag` argument.
|
13 | * @param tag *Optional*
|
14 | */
|
15 | export function useKeepAwake(tag: string = ExpoKeepAwakeTag): void {
|
16 | useEffect(() => {
|
17 | activateKeepAwake(tag);
|
18 | return () => deactivateKeepAwake(tag);
|
19 | }, [tag]);
|
20 | }
|
21 |
|
22 | // @needsAudit
|
23 | /**
|
24 | * Prevents the screen from sleeping until `deactivateKeepAwake` is called with the same `tag` value.
|
25 | *
|
26 | * If the `tag` argument is specified, the screen will not sleep until you call `deactivateKeepAwake`
|
27 | * with the same `tag` argument. When using multiple `tags` for activation you'll have to deactivate
|
28 | * each one in order to re-enable screen sleep. If tag is unspecified, the default `tag` is used.
|
29 | * @param tag *Optional* - Tag to lock screen sleep prevention. If not provided, the default tag is used.
|
30 | */
|
31 |
|
32 | export function activateKeepAwake(tag: string = ExpoKeepAwakeTag): void {
|
33 | if (ExpoKeepAwake.activate) ExpoKeepAwake.activate(tag);
|
34 | }
|
35 |
|
36 | // @needsAudit
|
37 | /**
|
38 | * Releases the lock on screen-sleep prevention associated with the given `tag` value. If `tag`
|
39 | * is unspecified, it defaults to the same default tag that `activateKeepAwake` uses.
|
40 | * @param tag *Optional* - Tag to release the lock on screen sleep prevention. If not provided,
|
41 | * the default tag is used.
|
42 | */
|
43 | export function deactivateKeepAwake(tag: string = ExpoKeepAwakeTag): void {
|
44 | if (ExpoKeepAwake.deactivate) ExpoKeepAwake.deactivate(tag);
|
45 | }
|