@symbiote-native/keep-awake 0.0.1

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.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +148 -0
  3. package/build/angular/index.d.ts +2 -0
  4. package/build/angular/index.js +4 -0
  5. package/build/angular/services/keep-awake.service/index.d.ts +5 -0
  6. package/build/angular/services/keep-awake.service/index.js +83 -0
  7. package/build/core/index.d.ts +3 -0
  8. package/build/core/index.js +1 -0
  9. package/build/core/keep-awake.d.ts +17 -0
  10. package/build/core/keep-awake.js +41 -0
  11. package/build/core/native-module.d.ts +9 -0
  12. package/build/core/native-module.js +3 -0
  13. package/build/core/types.d.ts +8 -0
  14. package/build/core/types.js +1 -0
  15. package/build/react/hooks/use-keep-awake/index.d.ts +2 -0
  16. package/build/react/hooks/use-keep-awake/index.js +35 -0
  17. package/build/react/index.d.ts +2 -0
  18. package/build/react/index.js +5 -0
  19. package/build/vue/composables/use-keep-awake/index.d.ts +2 -0
  20. package/build/vue/composables/use-keep-awake/index.js +28 -0
  21. package/build/vue/index.d.ts +2 -0
  22. package/build/vue/index.js +5 -0
  23. package/build-ngc/angular/index.d.ts +2 -0
  24. package/build-ngc/angular/index.js +5 -0
  25. package/build-ngc/angular/index.js.map +1 -0
  26. package/build-ngc/angular/services/keep-awake.service/index.d.ts +8 -0
  27. package/build-ngc/angular/services/keep-awake.service/index.js +41 -0
  28. package/build-ngc/angular/services/keep-awake.service/index.js.map +1 -0
  29. package/build-ngc/core/index.d.ts +3 -0
  30. package/build-ngc/core/index.js +2 -0
  31. package/build-ngc/core/index.js.map +1 -0
  32. package/build-ngc/core/keep-awake.d.ts +17 -0
  33. package/build-ngc/core/keep-awake.js +42 -0
  34. package/build-ngc/core/keep-awake.js.map +1 -0
  35. package/build-ngc/core/native-module.d.ts +9 -0
  36. package/build-ngc/core/native-module.js +4 -0
  37. package/build-ngc/core/native-module.js.map +1 -0
  38. package/build-ngc/core/types.d.ts +8 -0
  39. package/build-ngc/core/types.js +2 -0
  40. package/build-ngc/core/types.js.map +1 -0
  41. package/native-link.json +12 -0
  42. package/package.json +107 -0
  43. package/src/angular/index.ts +14 -0
  44. package/src/angular/services/keep-awake.service/index.ts +46 -0
  45. package/src/angular/services/keep-awake.service/keep-awake.service.test.ts +98 -0
  46. package/src/core/index.ts +9 -0
  47. package/src/core/keep-awake.test.ts +119 -0
  48. package/src/core/keep-awake.ts +50 -0
  49. package/src/core/native-module.ts +20 -0
  50. package/src/core/types.ts +14 -0
  51. package/src/react/hooks/use-keep-awake/index.ts +43 -0
  52. package/src/react/hooks/use-keep-awake/use-keep-awake.test.tsx +70 -0
  53. package/src/react/index.ts +16 -0
  54. package/src/vue/composables/use-keep-awake/index.ts +36 -0
  55. package/src/vue/composables/use-keep-awake/use-keep-awake.test.ts +76 -0
  56. package/src/vue/index.ts +16 -0
@@ -0,0 +1,36 @@
1
+ // Vue lifecycle wiring over the framework-agnostic core (core/keep-awake.ts) — activates on
2
+ // mount, deactivates on unmount, mirroring React's useKeepAwake hook. Vue has no useId
3
+ // equivalent, so the default tag comes from a small monotonically-incrementing module-local
4
+ // counter instead — simplest robust way to give each composable instance its own tag when the
5
+ // caller doesn't supply one.
6
+ import { onMounted, onUnmounted } from '@vue/runtime-core';
7
+ import {
8
+ activateKeepAwakeAsync,
9
+ addListener,
10
+ deactivateKeepAwake,
11
+ type KeepAwakeOptions,
12
+ } from '../../../core';
13
+
14
+ let tagCounter = 0;
15
+
16
+ export function useKeepAwake(tag?: string, options?: KeepAwakeOptions): void {
17
+ const tagOrDefault = tag ?? `keep-awake-tag-${++tagCounter}`;
18
+
19
+ onMounted(() => {
20
+ activateKeepAwakeAsync(tagOrDefault)
21
+ .then(() => {
22
+ if (options?.listener) {
23
+ addListener(tagOrDefault, options.listener);
24
+ }
25
+ })
26
+ .catch(() => {});
27
+ });
28
+
29
+ onUnmounted(() => {
30
+ if (options?.suppressDeactivateWarnings) {
31
+ deactivateKeepAwake(tagOrDefault).catch(() => {});
32
+ } else {
33
+ deactivateKeepAwake(tagOrDefault);
34
+ }
35
+ });
36
+ }
@@ -0,0 +1,76 @@
1
+ // Co-located Vue-driven test (ADR 0025) for useKeepAwake. See battery's
2
+ // use-low-power-mode.test.ts for the shared rationale.
3
+
4
+ import { defineComponent, h } from '@vue/runtime-core';
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
+ import { mount, unmount } from '@symbiote-native/vue';
7
+ import { installFabric } from '@symbiote-native/test-utils';
8
+ import { useKeepAwake } from './index';
9
+
10
+ const ROOT_TAG = 9954;
11
+
12
+ const activateKeepAwakeAsyncMock = vi.fn(async (_tag: string) => undefined);
13
+ const addListenerMock = vi.fn();
14
+ const deactivateKeepAwakeMock = vi.fn(async (_tag: string) => undefined);
15
+
16
+ vi.mock('../../../core', () => ({
17
+ activateKeepAwakeAsync: (tag: string) => activateKeepAwakeAsyncMock(tag),
18
+ addListener: (...args: unknown[]) => addListenerMock(...args),
19
+ deactivateKeepAwake: (tag: string) => deactivateKeepAwakeMock(tag),
20
+ }));
21
+
22
+ const fabric = installFabric();
23
+
24
+ beforeEach(() => {
25
+ fabric.reset();
26
+ activateKeepAwakeAsyncMock.mockClear();
27
+ addListenerMock.mockClear();
28
+ deactivateKeepAwakeMock.mockClear();
29
+ activateKeepAwakeAsyncMock.mockResolvedValue(undefined);
30
+ deactivateKeepAwakeMock.mockResolvedValue(undefined);
31
+ });
32
+
33
+ afterEach(() => unmount(ROOT_TAG));
34
+
35
+ function mountKeepAwake(tag?: string): void {
36
+ mount(
37
+ ROOT_TAG,
38
+ defineComponent({
39
+ setup: () => {
40
+ useKeepAwake(tag);
41
+ return () => h('symbiote-text', {}, 'keep-awake');
42
+ },
43
+ }),
44
+ );
45
+ }
46
+
47
+ describe('useKeepAwake (Vue)', () => {
48
+ it('activates a default tag on mount', async () => {
49
+ mountKeepAwake();
50
+
51
+ await vi.waitFor(() => expect(activateKeepAwakeAsyncMock).toHaveBeenCalledTimes(1));
52
+ expect(typeof activateKeepAwakeAsyncMock.mock.calls[0][0]).toBe('string');
53
+ });
54
+
55
+ it('activates the explicit tag when one is given', async () => {
56
+ mountKeepAwake('custom-tag');
57
+
58
+ await vi.waitFor(() => expect(activateKeepAwakeAsyncMock).toHaveBeenCalledWith('custom-tag'));
59
+ });
60
+
61
+ it('deactivates the same tag on unmount', async () => {
62
+ mountKeepAwake('custom-tag');
63
+ await vi.waitFor(() => expect(activateKeepAwakeAsyncMock).toHaveBeenCalledTimes(1));
64
+
65
+ unmount(ROOT_TAG);
66
+
67
+ expect(deactivateKeepAwakeMock).toHaveBeenCalledWith('custom-tag');
68
+ });
69
+
70
+ it('never touches addListener when no options are given', async () => {
71
+ mountKeepAwake('custom-tag');
72
+
73
+ await vi.waitFor(() => expect(activateKeepAwakeAsyncMock).toHaveBeenCalledTimes(1));
74
+ expect(addListenerMock).not.toHaveBeenCalled();
75
+ });
76
+ });
@@ -0,0 +1,16 @@
1
+ // @symbiote-native/keep-awake/vue: the Vue entry over the framework-agnostic core. The
2
+ // lifecycle bucket is `composables/` (never `hooks/`, that's React's term) — see
3
+ // adapters/vue/src/composables for the same convention.
4
+
5
+ export { useKeepAwake } from './composables/use-keep-awake';
6
+ export {
7
+ ExpoKeepAwakeTag,
8
+ isAvailableAsync,
9
+ activateKeepAwakeAsync,
10
+ deactivateKeepAwake,
11
+ addListener,
12
+ type KeepAwakeEvent,
13
+ type KeepAwakeListener,
14
+ type KeepAwakeOptions,
15
+ type EventSubscription,
16
+ } from '../core';