capacitor-device-profile 0.1.4 → 0.2.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 +8 -0
- package/README.md +4 -1
- package/dist/cjs/lifecycle.cjs +22 -2
- package/dist/cjs/lifecycle.d.cts +7 -0
- package/dist/cjs/lifecycle.d.cts.map +1 -1
- package/dist/esm/lifecycle.d.ts +7 -0
- package/dist/esm/lifecycle.d.ts.map +1 -1
- package/dist/esm/lifecycle.js +22 -2
- package/package.json +1 -1
- package/src/lifecycle.ts +29 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.2.0](https://github.com/jbcom/capacitor-device-profile/compare/v0.1.4...v0.2.0) (2026-10-10)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **lifecycle:** subscribe to pause and resume, many listeners each with its own unsubscribe ([a1371f5](https://github.com/jbcom/capacitor-device-profile/commit/a1371f5807bbd49bce3c49a84d2d80edae4a8ae3))
|
|
9
|
+
* **lifecycle:** subscribe to pause and resume, many listeners each with its own unsubscribe ([8fe78a9](https://github.com/jbcom/capacitor-device-profile/commit/8fe78a9e24e638358a96faa7f51edd38b3932fb1))
|
|
10
|
+
|
|
3
11
|
## [0.1.4](https://github.com/jbcom/capacitor-device-profile/compare/v0.1.3...v0.1.4) (2026-10-08)
|
|
4
12
|
|
|
5
13
|
|
package/README.md
CHANGED
|
@@ -45,6 +45,9 @@ const haptics = createHaptics({ plugin: Haptics, native })
|
|
|
45
45
|
// An open modal closes first on the Android back button; an unhandled press minimises the app.
|
|
46
46
|
const closeModal = lifecycle.pushBackHandler(() => dialog.close())
|
|
47
47
|
|
|
48
|
+
// Any part of the app can hear pause and resume for itself, and stop when it unmounts.
|
|
49
|
+
const stopSaving = lifecycle.onPause(() => void saveCheckpoint())
|
|
50
|
+
|
|
48
51
|
function Screen() {
|
|
49
52
|
const profile = useDeviceProfile({ loadPlatform: () => Device.getInfo() })
|
|
50
53
|
const insets = useSafeAreaInsets()
|
|
@@ -62,7 +65,7 @@ function Screen() {
|
|
|
62
65
|
| `readViewportFacts(window, platform?)` | Reads `innerWidth`/`innerHeight`/DPR/touch; `null` gives a desktop default |
|
|
63
66
|
| `decideOrientation(profile, preferred)` | Which orientations to accept and whether to show a "turn the device" hint. Never force-rotates; tablets and foldables accept both |
|
|
64
67
|
| `watchSafeArea(options)` / `readSafeAreaInsets()` | Measures `env(safe-area-inset-*)` through a probe and publishes `--safe-top/right/bottom/left` px variables, re-measured on resize, orientation change, visual-viewport resize and fold posture change |
|
|
65
|
-
| `createAppLifecycle({ app, native, onPause, onResume })` | Pause/resume from Capacitor `App` (native) or page visibility (web); `pushBackHandler` stack; an unconsumed back press minimises instead of exiting |
|
|
68
|
+
| `createAppLifecycle({ app, native, onPause, onResume })` | Pause/resume from Capacitor `App` (native) or page visibility (web); `onPause(listener)` / `onResume(listener)` subscriptions, each returning its unsubscribe; `pushBackHandler` stack; an unconsumed back press minimises instead of exiting |
|
|
66
69
|
| `createHaptics({ plugin, native })` | `impact`, `notify`, `selection`; no-ops on the web and while disabled; plugin failures never throw |
|
|
67
70
|
| `capacitor-device-profile/react`: `useDeviceProfile`, `useSafeAreaInsets`, `subscribeViewportGeometry` | Live bindings: a fold or unfold re-classifies without a reload |
|
|
68
71
|
|
package/dist/cjs/lifecycle.cjs
CHANGED
|
@@ -16,16 +16,32 @@ function createAppLifecycle(options) {
|
|
|
16
16
|
const handlers = [];
|
|
17
17
|
const handles = [];
|
|
18
18
|
const removers = [];
|
|
19
|
+
const pauseListeners = new Set();
|
|
20
|
+
const resumeListeners = new Set();
|
|
19
21
|
let state = 'active';
|
|
20
22
|
let disposed = false;
|
|
23
|
+
const subscribe = (listeners, listener) => {
|
|
24
|
+
// Wrapped, so one listener subscribed twice is two subscriptions, each removed on its own.
|
|
25
|
+
const entry = () => listener();
|
|
26
|
+
listeners.add(entry);
|
|
27
|
+
return () => {
|
|
28
|
+
listeners.delete(entry);
|
|
29
|
+
};
|
|
30
|
+
};
|
|
21
31
|
const transition = (next) => {
|
|
22
32
|
if (disposed || next === state)
|
|
23
33
|
return;
|
|
24
34
|
state = next;
|
|
25
|
-
if (next === 'active')
|
|
35
|
+
if (next === 'active') {
|
|
26
36
|
options.onResume?.();
|
|
27
|
-
|
|
37
|
+
for (const listener of [...resumeListeners])
|
|
38
|
+
listener();
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
28
41
|
options.onPause?.();
|
|
42
|
+
for (const listener of [...pauseListeners])
|
|
43
|
+
listener();
|
|
44
|
+
}
|
|
29
45
|
};
|
|
30
46
|
const handleBack = () => {
|
|
31
47
|
for (let index = handlers.length - 1; index >= 0; index -= 1) {
|
|
@@ -64,6 +80,8 @@ function createAppLifecycle(options) {
|
|
|
64
80
|
get state() {
|
|
65
81
|
return state;
|
|
66
82
|
},
|
|
83
|
+
onPause: (listener) => subscribe(pauseListeners, listener),
|
|
84
|
+
onResume: (listener) => subscribe(resumeListeners, listener),
|
|
67
85
|
pushBackHandler(handler) {
|
|
68
86
|
handlers.push(handler);
|
|
69
87
|
return () => {
|
|
@@ -76,6 +94,8 @@ function createAppLifecycle(options) {
|
|
|
76
94
|
async dispose() {
|
|
77
95
|
disposed = true;
|
|
78
96
|
handlers.length = 0;
|
|
97
|
+
pauseListeners.clear();
|
|
98
|
+
resumeListeners.clear();
|
|
79
99
|
for (const remove of removers.splice(0))
|
|
80
100
|
remove();
|
|
81
101
|
const settled = await Promise.allSettled(handles.splice(0));
|
package/dist/cjs/lifecycle.d.cts
CHANGED
|
@@ -38,6 +38,13 @@ export interface AppLifecycleOptions {
|
|
|
38
38
|
}
|
|
39
39
|
export interface AppLifecycle {
|
|
40
40
|
readonly state: LifecycleState;
|
|
41
|
+
/**
|
|
42
|
+
* Hears every pause from now on, after `options.onPause`; the returned function stops it. Many
|
|
43
|
+
* parts of an app (a save checkpoint, the audio, a timer) each subscribe for themselves.
|
|
44
|
+
*/
|
|
45
|
+
onPause(listener: () => void): () => void;
|
|
46
|
+
/** Hears every resume from now on, after `options.onResume`; the returned function stops it. */
|
|
47
|
+
onResume(listener: () => void): () => void;
|
|
41
48
|
/** Pushes a back handler; the returned function removes it. */
|
|
42
49
|
pushBackHandler(handler: BackHandler): () => void;
|
|
43
50
|
/** Runs the back stack as a hardware back press would. Returns true when a handler consumed it. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lifecycle.d.ts","sourceRoot":"","sources":["../../src/lifecycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,YAAY,CAAA;AAEpD,MAAM,WAAW,wBAAwB;IACvC,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACxB;AAED,+DAA+D;AAC/D,MAAM,WAAW,aAAa;IAC5B,WAAW,CACT,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,CAAC,KAAK,EAAE;QAAE,QAAQ,EAAE,OAAO,CAAA;KAAE,KAAK,IAAI,GAC/C,OAAO,CAAC,wBAAwB,CAAC,CAAA;IACpC,WAAW,CACT,KAAK,EAAE,YAAY,EACnB,QAAQ,EAAE,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,OAAO,CAAA;KAAE,KAAK,IAAI,GAChD,OAAO,CAAC,wBAAwB,CAAC,CAAA;IACpC,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CAC7B;AAED,8CAA8C;AAC9C,MAAM,MAAM,WAAW,GAAG,MAAM,OAAO,CAAA;AAEvC,MAAM,WAAW,mBAAmB;IAClC,kFAAkF;IAClF,GAAG,CAAC,EAAE,aAAa,CAAA;IACnB,mEAAmE;IACnE,MAAM,EAAE,OAAO,CAAA;IACf,OAAO,CAAC,EAAE,MAAM,IAAI,CAAA;IACpB,QAAQ,CAAC,EAAE,MAA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E,MAAM,EAAE,OAAO,CAAA;IACf,OAAO,CAAC,EAAE,MAAM,IAAI,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAA;IACrB,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,QAAQ,CAAC,EAAE,QAAQ,CAAA;CACpB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAA;IAC9B;;;OAGG;IACH,OAAO,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAA;IACzC,gGAAgG;IAChG,QAAQ,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAA;IAC1C,+DAA+D;IAC/D,eAAe,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,IAAI,CAAA;IACjD,mGAAmG;IACnG,UAAU,IAAI,OAAO,CAAA;IACrB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACzB;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CA8F7E"}
|
package/dist/esm/lifecycle.d.ts
CHANGED
|
@@ -38,6 +38,13 @@ export interface AppLifecycleOptions {
|
|
|
38
38
|
}
|
|
39
39
|
export interface AppLifecycle {
|
|
40
40
|
readonly state: LifecycleState;
|
|
41
|
+
/**
|
|
42
|
+
* Hears every pause from now on, after `options.onPause`; the returned function stops it. Many
|
|
43
|
+
* parts of an app (a save checkpoint, the audio, a timer) each subscribe for themselves.
|
|
44
|
+
*/
|
|
45
|
+
onPause(listener: () => void): () => void;
|
|
46
|
+
/** Hears every resume from now on, after `options.onResume`; the returned function stops it. */
|
|
47
|
+
onResume(listener: () => void): () => void;
|
|
41
48
|
/** Pushes a back handler; the returned function removes it. */
|
|
42
49
|
pushBackHandler(handler: BackHandler): () => void;
|
|
43
50
|
/** Runs the back stack as a hardware back press would. Returns true when a handler consumed it. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-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I,GAAG,MAAM,IAAI,CAAA;IACzC,gGAAgG;IAChG,QAAQ,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAA;IAC1C,+DAA+D;IAC/D,eAAe,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,IAAI,CAAA;IACjD,mGAAmG;IACnG,UAAU,IAAI,OAAO,CAAA;IACrB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACzB;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CA8F7E"}
|
package/dist/esm/lifecycle.js
CHANGED
|
@@ -13,16 +13,32 @@ export function createAppLifecycle(options) {
|
|
|
13
13
|
const handlers = [];
|
|
14
14
|
const handles = [];
|
|
15
15
|
const removers = [];
|
|
16
|
+
const pauseListeners = new Set();
|
|
17
|
+
const resumeListeners = new Set();
|
|
16
18
|
let state = 'active';
|
|
17
19
|
let disposed = false;
|
|
20
|
+
const subscribe = (listeners, listener) => {
|
|
21
|
+
// Wrapped, so one listener subscribed twice is two subscriptions, each removed on its own.
|
|
22
|
+
const entry = () => listener();
|
|
23
|
+
listeners.add(entry);
|
|
24
|
+
return () => {
|
|
25
|
+
listeners.delete(entry);
|
|
26
|
+
};
|
|
27
|
+
};
|
|
18
28
|
const transition = (next) => {
|
|
19
29
|
if (disposed || next === state)
|
|
20
30
|
return;
|
|
21
31
|
state = next;
|
|
22
|
-
if (next === 'active')
|
|
32
|
+
if (next === 'active') {
|
|
23
33
|
options.onResume?.();
|
|
24
|
-
|
|
34
|
+
for (const listener of [...resumeListeners])
|
|
35
|
+
listener();
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
25
38
|
options.onPause?.();
|
|
39
|
+
for (const listener of [...pauseListeners])
|
|
40
|
+
listener();
|
|
41
|
+
}
|
|
26
42
|
};
|
|
27
43
|
const handleBack = () => {
|
|
28
44
|
for (let index = handlers.length - 1; index >= 0; index -= 1) {
|
|
@@ -61,6 +77,8 @@ export function createAppLifecycle(options) {
|
|
|
61
77
|
get state() {
|
|
62
78
|
return state;
|
|
63
79
|
},
|
|
80
|
+
onPause: (listener) => subscribe(pauseListeners, listener),
|
|
81
|
+
onResume: (listener) => subscribe(resumeListeners, listener),
|
|
64
82
|
pushBackHandler(handler) {
|
|
65
83
|
handlers.push(handler);
|
|
66
84
|
return () => {
|
|
@@ -73,6 +91,8 @@ export function createAppLifecycle(options) {
|
|
|
73
91
|
async dispose() {
|
|
74
92
|
disposed = true;
|
|
75
93
|
handlers.length = 0;
|
|
94
|
+
pauseListeners.clear();
|
|
95
|
+
resumeListeners.clear();
|
|
76
96
|
for (const remove of removers.splice(0))
|
|
77
97
|
remove();
|
|
78
98
|
const settled = await Promise.allSettled(handles.splice(0));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "capacitor-device-profile",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Mobile and foldable runtime over Capacitor: device profile (phone, foldable-open, tablet, desktop) that re-classifies live on fold and unfold, orientation policy, live safe-area insets, app lifecycle with a back-button stack, and a haptics facade.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/lifecycle.ts
CHANGED
|
@@ -46,6 +46,13 @@ export interface AppLifecycleOptions {
|
|
|
46
46
|
|
|
47
47
|
export interface AppLifecycle {
|
|
48
48
|
readonly state: LifecycleState
|
|
49
|
+
/**
|
|
50
|
+
* Hears every pause from now on, after `options.onPause`; the returned function stops it. Many
|
|
51
|
+
* parts of an app (a save checkpoint, the audio, a timer) each subscribe for themselves.
|
|
52
|
+
*/
|
|
53
|
+
onPause(listener: () => void): () => void
|
|
54
|
+
/** Hears every resume from now on, after `options.onResume`; the returned function stops it. */
|
|
55
|
+
onResume(listener: () => void): () => void
|
|
49
56
|
/** Pushes a back handler; the returned function removes it. */
|
|
50
57
|
pushBackHandler(handler: BackHandler): () => void
|
|
51
58
|
/** Runs the back stack as a hardware back press would. Returns true when a handler consumed it. */
|
|
@@ -57,14 +64,30 @@ export function createAppLifecycle(options: AppLifecycleOptions): AppLifecycle {
|
|
|
57
64
|
const handlers: BackHandler[] = []
|
|
58
65
|
const handles: Promise<PluginListenerHandleLike>[] = []
|
|
59
66
|
const removers: Array<() => void> = []
|
|
67
|
+
const pauseListeners = new Set<() => void>()
|
|
68
|
+
const resumeListeners = new Set<() => void>()
|
|
60
69
|
let state: LifecycleState = 'active'
|
|
61
70
|
let disposed = false
|
|
62
71
|
|
|
72
|
+
const subscribe = (listeners: Set<() => void>, listener: () => void) => {
|
|
73
|
+
// Wrapped, so one listener subscribed twice is two subscriptions, each removed on its own.
|
|
74
|
+
const entry = () => listener()
|
|
75
|
+
listeners.add(entry)
|
|
76
|
+
return () => {
|
|
77
|
+
listeners.delete(entry)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
63
81
|
const transition = (next: LifecycleState) => {
|
|
64
82
|
if (disposed || next === state) return
|
|
65
83
|
state = next
|
|
66
|
-
if (next === 'active')
|
|
67
|
-
|
|
84
|
+
if (next === 'active') {
|
|
85
|
+
options.onResume?.()
|
|
86
|
+
for (const listener of [...resumeListeners]) listener()
|
|
87
|
+
} else {
|
|
88
|
+
options.onPause?.()
|
|
89
|
+
for (const listener of [...pauseListeners]) listener()
|
|
90
|
+
}
|
|
68
91
|
}
|
|
69
92
|
|
|
70
93
|
const handleBack = (): boolean => {
|
|
@@ -109,6 +132,8 @@ export function createAppLifecycle(options: AppLifecycleOptions): AppLifecycle {
|
|
|
109
132
|
get state() {
|
|
110
133
|
return state
|
|
111
134
|
},
|
|
135
|
+
onPause: (listener) => subscribe(pauseListeners, listener),
|
|
136
|
+
onResume: (listener) => subscribe(resumeListeners, listener),
|
|
112
137
|
pushBackHandler(handler) {
|
|
113
138
|
handlers.push(handler)
|
|
114
139
|
return () => {
|
|
@@ -120,6 +145,8 @@ export function createAppLifecycle(options: AppLifecycleOptions): AppLifecycle {
|
|
|
120
145
|
async dispose() {
|
|
121
146
|
disposed = true
|
|
122
147
|
handlers.length = 0
|
|
148
|
+
pauseListeners.clear()
|
|
149
|
+
resumeListeners.clear()
|
|
123
150
|
for (const remove of removers.splice(0)) remove()
|
|
124
151
|
const settled = await Promise.allSettled(handles.splice(0))
|
|
125
152
|
await Promise.allSettled(
|