@capgo/capacitor-accelerometer 7.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/dist/docs.json ADDED
@@ -0,0 +1,367 @@
1
+ {
2
+ "api": {
3
+ "name": "CapacitorAccelerometerPlugin",
4
+ "slug": "capacitoraccelerometerplugin",
5
+ "docs": "Capacitor plugin contract for working with the device accelerometer.",
6
+ "tags": [
7
+ {
8
+ "text": "1.0.0",
9
+ "name": "since"
10
+ }
11
+ ],
12
+ "methods": [
13
+ {
14
+ "name": "getMeasurement",
15
+ "signature": "() => Promise<GetMeasurementResult>",
16
+ "parameters": [],
17
+ "returns": "Promise<Measurement>",
18
+ "tags": [
19
+ {
20
+ "name": "returns",
21
+ "text": "The latest x, y and z axis values."
22
+ },
23
+ {
24
+ "name": "since",
25
+ "text": "1.0.0"
26
+ }
27
+ ],
28
+ "docs": "Get the most recent accelerometer sample that was recorded by the native layer.",
29
+ "complexTypes": [
30
+ "Measurement",
31
+ "GetMeasurementResult"
32
+ ],
33
+ "slug": "getmeasurement"
34
+ },
35
+ {
36
+ "name": "isAvailable",
37
+ "signature": "() => Promise<IsAvailableResult>",
38
+ "parameters": [],
39
+ "returns": "Promise<IsAvailableResult>",
40
+ "tags": [
41
+ {
42
+ "name": "returns",
43
+ "text": "Whether an accelerometer is available."
44
+ },
45
+ {
46
+ "name": "since",
47
+ "text": "1.0.0"
48
+ }
49
+ ],
50
+ "docs": "Check if the current device includes an accelerometer sensor.",
51
+ "complexTypes": [
52
+ "IsAvailableResult"
53
+ ],
54
+ "slug": "isavailable"
55
+ },
56
+ {
57
+ "name": "startMeasurementUpdates",
58
+ "signature": "() => Promise<void>",
59
+ "parameters": [],
60
+ "returns": "Promise<void>",
61
+ "tags": [
62
+ {
63
+ "name": "since",
64
+ "text": "1.0.0"
65
+ }
66
+ ],
67
+ "docs": "Begin streaming accelerometer updates to the JavaScript layer.\n\nCall {@link addListener} with the `measurement` event to receive the updates.",
68
+ "complexTypes": [],
69
+ "slug": "startmeasurementupdates"
70
+ },
71
+ {
72
+ "name": "stopMeasurementUpdates",
73
+ "signature": "() => Promise<void>",
74
+ "parameters": [],
75
+ "returns": "Promise<void>",
76
+ "tags": [
77
+ {
78
+ "name": "since",
79
+ "text": "1.0.0"
80
+ }
81
+ ],
82
+ "docs": "Stop streaming accelerometer updates started via {@link startMeasurementUpdates}.",
83
+ "complexTypes": [],
84
+ "slug": "stopmeasurementupdates"
85
+ },
86
+ {
87
+ "name": "checkPermissions",
88
+ "signature": "() => Promise<PermissionStatus>",
89
+ "parameters": [],
90
+ "returns": "Promise<PermissionStatus>",
91
+ "tags": [
92
+ {
93
+ "name": "since",
94
+ "text": "1.0.0"
95
+ }
96
+ ],
97
+ "docs": "Return the current permission state for accessing motion data.\n\nOn platforms without explicit permissions this resolves to `granted`.",
98
+ "complexTypes": [
99
+ "PermissionStatus"
100
+ ],
101
+ "slug": "checkpermissions"
102
+ },
103
+ {
104
+ "name": "requestPermissions",
105
+ "signature": "() => Promise<PermissionStatus>",
106
+ "parameters": [],
107
+ "returns": "Promise<PermissionStatus>",
108
+ "tags": [
109
+ {
110
+ "name": "since",
111
+ "text": "1.0.0"
112
+ }
113
+ ],
114
+ "docs": "Request permission to access motion data if supported by the platform.",
115
+ "complexTypes": [
116
+ "PermissionStatus"
117
+ ],
118
+ "slug": "requestpermissions"
119
+ },
120
+ {
121
+ "name": "addListener",
122
+ "signature": "(eventName: 'measurement', listenerFunc: (event: MeasurementEvent) => void) => Promise<PluginListenerHandle>",
123
+ "parameters": [
124
+ {
125
+ "name": "eventName",
126
+ "docs": "Only the `measurement` event is supported.",
127
+ "type": "'measurement'"
128
+ },
129
+ {
130
+ "name": "listenerFunc",
131
+ "docs": "Callback invoked with each measurement.",
132
+ "type": "(event: Measurement) => void"
133
+ }
134
+ ],
135
+ "returns": "Promise<PluginListenerHandle>",
136
+ "tags": [
137
+ {
138
+ "name": "param",
139
+ "text": "eventName Only the `measurement` event is supported."
140
+ },
141
+ {
142
+ "name": "param",
143
+ "text": "listenerFunc Callback invoked with each measurement."
144
+ },
145
+ {
146
+ "name": "since",
147
+ "text": "1.0.0"
148
+ }
149
+ ],
150
+ "docs": "Listen for measurement updates.",
151
+ "complexTypes": [
152
+ "PluginListenerHandle",
153
+ "MeasurementEvent"
154
+ ],
155
+ "slug": "addlistenermeasurement-"
156
+ },
157
+ {
158
+ "name": "removeAllListeners",
159
+ "signature": "() => Promise<void>",
160
+ "parameters": [],
161
+ "returns": "Promise<void>",
162
+ "tags": [
163
+ {
164
+ "name": "since",
165
+ "text": "1.0.0"
166
+ }
167
+ ],
168
+ "docs": "Remove all listeners that have been registered on the plugin.",
169
+ "complexTypes": [],
170
+ "slug": "removealllisteners"
171
+ }
172
+ ],
173
+ "properties": []
174
+ },
175
+ "interfaces": [
176
+ {
177
+ "name": "Measurement",
178
+ "slug": "measurement",
179
+ "docs": "The x, y and z axis acceleration values reported by the device motion sensors.",
180
+ "tags": [
181
+ {
182
+ "text": "1.0.0",
183
+ "name": "since"
184
+ }
185
+ ],
186
+ "methods": [],
187
+ "properties": [
188
+ {
189
+ "name": "x",
190
+ "tags": [
191
+ {
192
+ "text": "1.0.0",
193
+ "name": "since"
194
+ }
195
+ ],
196
+ "docs": "The acceleration on the x-axis in G's.",
197
+ "complexTypes": [],
198
+ "type": "number"
199
+ },
200
+ {
201
+ "name": "y",
202
+ "tags": [
203
+ {
204
+ "text": "1.0.0",
205
+ "name": "since"
206
+ }
207
+ ],
208
+ "docs": "The acceleration on the y-axis in G's.",
209
+ "complexTypes": [],
210
+ "type": "number"
211
+ },
212
+ {
213
+ "name": "z",
214
+ "tags": [
215
+ {
216
+ "text": "1.0.0",
217
+ "name": "since"
218
+ }
219
+ ],
220
+ "docs": "The acceleration on the z-axis in G's.",
221
+ "complexTypes": [],
222
+ "type": "number"
223
+ }
224
+ ]
225
+ },
226
+ {
227
+ "name": "IsAvailableResult",
228
+ "slug": "isavailableresult",
229
+ "docs": "Result returned by {@link CapacitorAccelerometerPlugin.isAvailable}.",
230
+ "tags": [
231
+ {
232
+ "text": "1.0.0",
233
+ "name": "since"
234
+ }
235
+ ],
236
+ "methods": [],
237
+ "properties": [
238
+ {
239
+ "name": "isAvailable",
240
+ "tags": [
241
+ {
242
+ "text": "1.0.0",
243
+ "name": "since"
244
+ }
245
+ ],
246
+ "docs": "Whether an accelerometer sensor is available on the device.",
247
+ "complexTypes": [],
248
+ "type": "boolean"
249
+ }
250
+ ]
251
+ },
252
+ {
253
+ "name": "PermissionStatus",
254
+ "slug": "permissionstatus",
255
+ "docs": "Permission information returned by {@link CapacitorAccelerometerPlugin.checkPermissions}\nand {@link CapacitorAccelerometerPlugin.requestPermissions}.",
256
+ "tags": [
257
+ {
258
+ "text": "1.0.0",
259
+ "name": "since"
260
+ }
261
+ ],
262
+ "methods": [],
263
+ "properties": [
264
+ {
265
+ "name": "accelerometer",
266
+ "tags": [
267
+ {
268
+ "text": "1.0.0",
269
+ "name": "since"
270
+ }
271
+ ],
272
+ "docs": "The permission state for accessing motion data on the current platform.",
273
+ "complexTypes": [
274
+ "AccelerometerPermissionState"
275
+ ],
276
+ "type": "AccelerometerPermissionState"
277
+ }
278
+ ]
279
+ },
280
+ {
281
+ "name": "PluginListenerHandle",
282
+ "slug": "pluginlistenerhandle",
283
+ "docs": "",
284
+ "tags": [],
285
+ "methods": [],
286
+ "properties": [
287
+ {
288
+ "name": "remove",
289
+ "tags": [],
290
+ "docs": "",
291
+ "complexTypes": [],
292
+ "type": "() => Promise<void>"
293
+ }
294
+ ]
295
+ }
296
+ ],
297
+ "enums": [],
298
+ "typeAliases": [
299
+ {
300
+ "name": "GetMeasurementResult",
301
+ "slug": "getmeasurementresult",
302
+ "docs": "Alias for the most recent measurement.",
303
+ "types": [
304
+ {
305
+ "text": "Measurement",
306
+ "complexTypes": [
307
+ "Measurement"
308
+ ]
309
+ }
310
+ ]
311
+ },
312
+ {
313
+ "name": "AccelerometerPermissionState",
314
+ "slug": "accelerometerpermissionstate",
315
+ "docs": "Permission state union including `limited` for platforms that can throttle motion access.",
316
+ "types": [
317
+ {
318
+ "text": "PermissionState",
319
+ "complexTypes": [
320
+ "PermissionState"
321
+ ]
322
+ },
323
+ {
324
+ "text": "'limited'",
325
+ "complexTypes": []
326
+ }
327
+ ]
328
+ },
329
+ {
330
+ "name": "PermissionState",
331
+ "slug": "permissionstate",
332
+ "docs": "",
333
+ "types": [
334
+ {
335
+ "text": "'prompt'",
336
+ "complexTypes": []
337
+ },
338
+ {
339
+ "text": "'prompt-with-rationale'",
340
+ "complexTypes": []
341
+ },
342
+ {
343
+ "text": "'granted'",
344
+ "complexTypes": []
345
+ },
346
+ {
347
+ "text": "'denied'",
348
+ "complexTypes": []
349
+ }
350
+ ]
351
+ },
352
+ {
353
+ "name": "MeasurementEvent",
354
+ "slug": "measurementevent",
355
+ "docs": "Event payload emitted when {@link CapacitorAccelerometerPlugin.startMeasurementUpdates}\nis active.",
356
+ "types": [
357
+ {
358
+ "text": "Measurement",
359
+ "complexTypes": [
360
+ "Measurement"
361
+ ]
362
+ }
363
+ ]
364
+ }
365
+ ],
366
+ "pluginConfigs": []
367
+ }
@@ -0,0 +1,141 @@
1
+ import type { PluginListenerHandle } from '@capacitor/core';
2
+ /**
3
+ * The x, y and z axis acceleration values reported by the device motion sensors.
4
+ *
5
+ * @since 1.0.0
6
+ */
7
+ export interface Measurement {
8
+ /**
9
+ * The acceleration on the x-axis in G's.
10
+ *
11
+ * @since 1.0.0
12
+ */
13
+ x: number;
14
+ /**
15
+ * The acceleration on the y-axis in G's.
16
+ *
17
+ * @since 1.0.0
18
+ */
19
+ y: number;
20
+ /**
21
+ * The acceleration on the z-axis in G's.
22
+ *
23
+ * @since 1.0.0
24
+ */
25
+ z: number;
26
+ }
27
+ /**
28
+ * Result returned by {@link CapacitorAccelerometerPlugin.isAvailable}.
29
+ *
30
+ * @since 1.0.0
31
+ */
32
+ export interface IsAvailableResult {
33
+ /**
34
+ * Whether an accelerometer sensor is available on the device.
35
+ *
36
+ * @since 1.0.0
37
+ */
38
+ isAvailable: boolean;
39
+ }
40
+ /**
41
+ * Permission information returned by {@link CapacitorAccelerometerPlugin.checkPermissions}
42
+ * and {@link CapacitorAccelerometerPlugin.requestPermissions}.
43
+ *
44
+ * @since 1.0.0
45
+ */
46
+ export interface PermissionStatus {
47
+ /**
48
+ * The permission state for accessing motion data on the current platform.
49
+ *
50
+ * @since 1.0.0
51
+ */
52
+ accelerometer: AccelerometerPermissionState;
53
+ }
54
+ /**
55
+ * Alias for the most recent measurement.
56
+ *
57
+ * @since 1.0.0
58
+ */
59
+ export type GetMeasurementResult = Measurement;
60
+ /**
61
+ * Permission state union including `limited` for platforms that can throttle motion access.
62
+ *
63
+ * @since 1.0.0
64
+ */
65
+ export type AccelerometerPermissionState = PermissionState | 'limited';
66
+ /**
67
+ * Platform permission states supported by Capacitor.
68
+ *
69
+ * @since 1.0.0
70
+ */
71
+ export type PermissionState = 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';
72
+ /**
73
+ * Event payload emitted when {@link CapacitorAccelerometerPlugin.startMeasurementUpdates}
74
+ * is active.
75
+ *
76
+ * @since 1.0.0
77
+ */
78
+ export type MeasurementEvent = Measurement;
79
+ /**
80
+ * Capacitor plugin contract for working with the device accelerometer.
81
+ *
82
+ * @since 1.0.0
83
+ */
84
+ export interface CapacitorAccelerometerPlugin {
85
+ /**
86
+ * Get the most recent accelerometer sample that was recorded by the native layer.
87
+ *
88
+ * @returns The latest x, y and z axis values.
89
+ * @since 1.0.0
90
+ */
91
+ getMeasurement(): Promise<GetMeasurementResult>;
92
+ /**
93
+ * Check if the current device includes an accelerometer sensor.
94
+ *
95
+ * @returns Whether an accelerometer is available.
96
+ * @since 1.0.0
97
+ */
98
+ isAvailable(): Promise<IsAvailableResult>;
99
+ /**
100
+ * Begin streaming accelerometer updates to the JavaScript layer.
101
+ *
102
+ * Call {@link addListener} with the `measurement` event to receive the updates.
103
+ *
104
+ * @since 1.0.0
105
+ */
106
+ startMeasurementUpdates(): Promise<void>;
107
+ /**
108
+ * Stop streaming accelerometer updates started via {@link startMeasurementUpdates}.
109
+ *
110
+ * @since 1.0.0
111
+ */
112
+ stopMeasurementUpdates(): Promise<void>;
113
+ /**
114
+ * Return the current permission state for accessing motion data.
115
+ *
116
+ * On platforms without explicit permissions this resolves to `granted`.
117
+ *
118
+ * @since 1.0.0
119
+ */
120
+ checkPermissions(): Promise<PermissionStatus>;
121
+ /**
122
+ * Request permission to access motion data if supported by the platform.
123
+ *
124
+ * @since 1.0.0
125
+ */
126
+ requestPermissions(): Promise<PermissionStatus>;
127
+ /**
128
+ * Listen for measurement updates.
129
+ *
130
+ * @param eventName Only the `measurement` event is supported.
131
+ * @param listenerFunc Callback invoked with each measurement.
132
+ * @since 1.0.0
133
+ */
134
+ addListener(eventName: 'measurement', listenerFunc: (event: MeasurementEvent) => void): Promise<PluginListenerHandle>;
135
+ /**
136
+ * Remove all listeners that have been registered on the plugin.
137
+ *
138
+ * @since 1.0.0
139
+ */
140
+ removeAllListeners(): Promise<void>;
141
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=definitions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PluginListenerHandle } from '@capacitor/core';\n\n/**\n * The x, y and z axis acceleration values reported by the device motion sensors.\n *\n * @since 1.0.0\n */\nexport interface Measurement {\n /**\n * The acceleration on the x-axis in G's.\n *\n * @since 1.0.0\n */\n x: number;\n\n /**\n * The acceleration on the y-axis in G's.\n *\n * @since 1.0.0\n */\n y: number;\n\n /**\n * The acceleration on the z-axis in G's.\n *\n * @since 1.0.0\n */\n z: number;\n}\n\n/**\n * Result returned by {@link CapacitorAccelerometerPlugin.isAvailable}.\n *\n * @since 1.0.0\n */\nexport interface IsAvailableResult {\n /**\n * Whether an accelerometer sensor is available on the device.\n *\n * @since 1.0.0\n */\n isAvailable: boolean;\n}\n\n/**\n * Permission information returned by {@link CapacitorAccelerometerPlugin.checkPermissions}\n * and {@link CapacitorAccelerometerPlugin.requestPermissions}.\n *\n * @since 1.0.0\n */\nexport interface PermissionStatus {\n /**\n * The permission state for accessing motion data on the current platform.\n *\n * @since 1.0.0\n */\n accelerometer: AccelerometerPermissionState;\n}\n\n/**\n * Alias for the most recent measurement.\n *\n * @since 1.0.0\n */\nexport type GetMeasurementResult = Measurement;\n\n/**\n * Permission state union including `limited` for platforms that can throttle motion access.\n *\n * @since 1.0.0\n */\nexport type AccelerometerPermissionState = PermissionState | 'limited';\n\n/**\n * Platform permission states supported by Capacitor.\n *\n * @since 1.0.0\n */\nexport type PermissionState = 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';\n\n/**\n * Event payload emitted when {@link CapacitorAccelerometerPlugin.startMeasurementUpdates}\n * is active.\n *\n * @since 1.0.0\n */\nexport type MeasurementEvent = Measurement;\n\n/**\n * Capacitor plugin contract for working with the device accelerometer.\n *\n * @since 1.0.0\n */\nexport interface CapacitorAccelerometerPlugin {\n /**\n * Get the most recent accelerometer sample that was recorded by the native layer.\n *\n * @returns The latest x, y and z axis values.\n * @since 1.0.0\n */\n getMeasurement(): Promise<GetMeasurementResult>;\n\n /**\n * Check if the current device includes an accelerometer sensor.\n *\n * @returns Whether an accelerometer is available.\n * @since 1.0.0\n */\n isAvailable(): Promise<IsAvailableResult>;\n\n /**\n * Begin streaming accelerometer updates to the JavaScript layer.\n *\n * Call {@link addListener} with the `measurement` event to receive the updates.\n *\n * @since 1.0.0\n */\n startMeasurementUpdates(): Promise<void>;\n\n /**\n * Stop streaming accelerometer updates started via {@link startMeasurementUpdates}.\n *\n * @since 1.0.0\n */\n stopMeasurementUpdates(): Promise<void>;\n\n /**\n * Return the current permission state for accessing motion data.\n *\n * On platforms without explicit permissions this resolves to `granted`.\n *\n * @since 1.0.0\n */\n checkPermissions(): Promise<PermissionStatus>;\n\n /**\n * Request permission to access motion data if supported by the platform.\n *\n * @since 1.0.0\n */\n requestPermissions(): Promise<PermissionStatus>;\n\n /**\n * Listen for measurement updates.\n *\n * @param eventName Only the `measurement` event is supported.\n * @param listenerFunc Callback invoked with each measurement.\n * @since 1.0.0\n */\n addListener(eventName: 'measurement', listenerFunc: (event: MeasurementEvent) => void): Promise<PluginListenerHandle>;\n\n /**\n * Remove all listeners that have been registered on the plugin.\n *\n * @since 1.0.0\n */\n removeAllListeners(): Promise<void>;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { CapacitorAccelerometerPlugin } from './definitions';
2
+ declare const CapacitorAccelerometer: CapacitorAccelerometerPlugin;
3
+ export * from './definitions';
4
+ export { CapacitorAccelerometer };
@@ -0,0 +1,7 @@
1
+ import { registerPlugin } from '@capacitor/core';
2
+ const CapacitorAccelerometer = registerPlugin('CapacitorAccelerometer', {
3
+ web: () => import('./web').then((m) => new m.CapacitorAccelerometerWeb()),
4
+ });
5
+ export * from './definitions';
6
+ export { CapacitorAccelerometer };
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,sBAAsB,GAAG,cAAc,CAA+B,wBAAwB,EAAE;IACpG,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,yBAAyB,EAAE,CAAC;CAC1E,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,sBAAsB,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { CapacitorAccelerometerPlugin } from './definitions';\n\nconst CapacitorAccelerometer = registerPlugin<CapacitorAccelerometerPlugin>('CapacitorAccelerometer', {\n web: () => import('./web').then((m) => new m.CapacitorAccelerometerWeb()),\n});\n\nexport * from './definitions';\nexport { CapacitorAccelerometer };\n"]}
@@ -0,0 +1,19 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import type { CapacitorAccelerometerPlugin, GetMeasurementResult, IsAvailableResult, Measurement, PermissionStatus } from './definitions';
3
+ export declare class CapacitorAccelerometerWeb extends WebPlugin implements CapacitorAccelerometerPlugin {
4
+ private lastMeasurement;
5
+ private motionHandler;
6
+ private permissionState;
7
+ private handleMotion;
8
+ private supportsDeviceMotion;
9
+ private get motionEventConstructor();
10
+ getMeasurement(): Promise<GetMeasurementResult>;
11
+ isAvailable(): Promise<IsAvailableResult>;
12
+ startMeasurementUpdates(): Promise<void>;
13
+ stopMeasurementUpdates(): Promise<void>;
14
+ checkPermissions(): Promise<PermissionStatus>;
15
+ requestPermissions(): Promise<PermissionStatus>;
16
+ addListener(eventName: 'measurement', listenerFunc: (event: Measurement) => void): Promise<import("@capacitor/core").PluginListenerHandle>;
17
+ removeAllListeners(): Promise<void>;
18
+ private ensurePermission;
19
+ }
@@ -0,0 +1,95 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ const DEFAULT_MEASUREMENT = { x: 0, y: 0, z: 0 };
3
+ export class CapacitorAccelerometerWeb extends WebPlugin {
4
+ constructor() {
5
+ super(...arguments);
6
+ this.lastMeasurement = Object.assign({}, DEFAULT_MEASUREMENT);
7
+ this.motionHandler = null;
8
+ this.permissionState = 'prompt';
9
+ this.handleMotion = (event) => {
10
+ var _a, _b, _c, _d;
11
+ const accel = (_a = event.accelerationIncludingGravity) !== null && _a !== void 0 ? _a : event.acceleration;
12
+ if (!accel) {
13
+ return;
14
+ }
15
+ this.lastMeasurement = {
16
+ x: (_b = accel.x) !== null && _b !== void 0 ? _b : 0,
17
+ y: (_c = accel.y) !== null && _c !== void 0 ? _c : 0,
18
+ z: (_d = accel.z) !== null && _d !== void 0 ? _d : 0,
19
+ };
20
+ this.notifyListeners('measurement', Object.assign({}, this.lastMeasurement));
21
+ };
22
+ }
23
+ supportsDeviceMotion() {
24
+ return typeof window !== 'undefined' && typeof window.DeviceMotionEvent !== 'undefined';
25
+ }
26
+ get motionEventConstructor() {
27
+ return this.supportsDeviceMotion() ? window.DeviceMotionEvent : null;
28
+ }
29
+ async getMeasurement() {
30
+ return Object.assign({}, this.lastMeasurement);
31
+ }
32
+ async isAvailable() {
33
+ return { isAvailable: this.supportsDeviceMotion() };
34
+ }
35
+ async startMeasurementUpdates() {
36
+ await this.ensurePermission(true);
37
+ if (!this.supportsDeviceMotion()) {
38
+ throw this.unavailable('DeviceMotionEvent is not available in this browser.');
39
+ }
40
+ if (this.motionHandler) {
41
+ return;
42
+ }
43
+ this.motionHandler = this.handleMotion;
44
+ window.addEventListener('devicemotion', this.motionHandler);
45
+ }
46
+ async stopMeasurementUpdates() {
47
+ if (this.motionHandler && this.supportsDeviceMotion()) {
48
+ window.removeEventListener('devicemotion', this.motionHandler);
49
+ }
50
+ this.motionHandler = null;
51
+ }
52
+ async checkPermissions() {
53
+ await this.ensurePermission(false);
54
+ return { accelerometer: this.permissionState };
55
+ }
56
+ async requestPermissions() {
57
+ await this.ensurePermission(true);
58
+ return { accelerometer: this.permissionState };
59
+ }
60
+ async addListener(eventName, listenerFunc) {
61
+ const handle = await super.addListener(eventName, listenerFunc);
62
+ return handle;
63
+ }
64
+ async removeAllListeners() {
65
+ await super.removeAllListeners();
66
+ }
67
+ async ensurePermission(request) {
68
+ if (!this.supportsDeviceMotion()) {
69
+ this.permissionState = 'denied';
70
+ return;
71
+ }
72
+ const constructor = this.motionEventConstructor;
73
+ if (!(constructor === null || constructor === void 0 ? void 0 : constructor.requestPermission)) {
74
+ // Platforms without explicit permission prompts expose motion data if feature is available.
75
+ this.permissionState = 'granted';
76
+ return;
77
+ }
78
+ if (!request) {
79
+ // We cannot probe state without prompting, assume prompt until user interacts.
80
+ if (this.permissionState === 'prompt') {
81
+ return;
82
+ }
83
+ return;
84
+ }
85
+ try {
86
+ const status = await constructor.requestPermission();
87
+ this.permissionState = status === 'granted' ? 'granted' : 'denied';
88
+ }
89
+ catch (error) {
90
+ console.warn('CapacitorAccelerometer: permission request failed', error);
91
+ this.permissionState = 'denied';
92
+ }
93
+ }
94
+ }
95
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAe5C,MAAM,mBAAmB,GAAgB,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;AAE9D,MAAM,OAAO,yBAA0B,SAAQ,SAAS;IAAxD;;QACU,oBAAe,qBAAqB,mBAAmB,EAAG;QAC1D,kBAAa,GAAgD,IAAI,CAAC;QAClE,oBAAe,GAAgC,QAAQ,CAAC;QAExD,iBAAY,GAAG,CAAC,KAAwB,EAAE,EAAE;;YAClD,MAAM,KAAK,GAAG,MAAA,KAAK,CAAC,4BAA4B,mCAAI,KAAK,CAAC,YAAY,CAAC;YACvE,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO;YACT,CAAC;YAED,IAAI,CAAC,eAAe,GAAG;gBACrB,CAAC,EAAE,MAAA,KAAK,CAAC,CAAC,mCAAI,CAAC;gBACf,CAAC,EAAE,MAAA,KAAK,CAAC,CAAC,mCAAI,CAAC;gBACf,CAAC,EAAE,MAAA,KAAK,CAAC,CAAC,mCAAI,CAAC;aAChB,CAAC;YAEF,IAAI,CAAC,eAAe,CAAC,aAAa,oBAAO,IAAI,CAAC,eAAe,EAAG,CAAC;QACnE,CAAC,CAAC;IAqFJ,CAAC;IAnFS,oBAAoB;QAC1B,OAAO,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,iBAAiB,KAAK,WAAW,CAAC;IAC1F,CAAC;IAED,IAAY,sBAAsB;QAChC,OAAO,IAAI,CAAC,oBAAoB,EAAE,CAAC,CAAC,CAAE,MAAM,CAAC,iBAAgD,CAAC,CAAC,CAAC,IAAI,CAAC;IACvG,CAAC;IAED,KAAK,CAAC,cAAc;QAClB,yBAAY,IAAI,CAAC,eAAe,EAAG;IACrC,CAAC;IAED,KAAK,CAAC,WAAW;QACf,OAAO,EAAE,WAAW,EAAE,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC;IACtD,CAAC;IAED,KAAK,CAAC,uBAAuB;QAC3B,MAAM,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAClC,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC;YACjC,MAAM,IAAI,CAAC,WAAW,CAAC,qDAAqD,CAAC,CAAC;QAChF,CAAC;QACD,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACvB,OAAO;QACT,CAAC;QACD,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,YAAY,CAAC;QACvC,MAAM,CAAC,gBAAgB,CAAC,cAAc,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;IAC9D,CAAC;IAED,KAAK,CAAC,sBAAsB;QAC1B,IAAI,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC;YACtD,MAAM,CAAC,mBAAmB,CAAC,cAAc,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QACjE,CAAC;QACD,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,MAAM,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;QACnC,OAAO,EAAE,aAAa,EAAE,IAAI,CAAC,eAAe,EAAE,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,kBAAkB;QACtB,MAAM,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAClC,OAAO,EAAE,aAAa,EAAE,IAAI,CAAC,eAAe,EAAE,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,SAAwB,EAAE,YAA0C;QACpF,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,WAAW,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;QAChE,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,KAAK,CAAC,kBAAkB;QACtB,MAAM,KAAK,CAAC,kBAAkB,EAAE,CAAC;IACnC,CAAC;IAEO,KAAK,CAAC,gBAAgB,CAAC,OAAgB;QAC7C,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC;YACjC,IAAI,CAAC,eAAe,GAAG,QAAQ,CAAC;YAChC,OAAO;QACT,CAAC;QAED,MAAM,WAAW,GAAG,IAAI,CAAC,sBAAsB,CAAC;QAChD,IAAI,CAAC,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,iBAAiB,CAAA,EAAE,CAAC;YACpC,4FAA4F;YAC5F,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;YACjC,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,+EAA+E;YAC/E,IAAI,IAAI,CAAC,eAAe,KAAK,QAAQ,EAAE,CAAC;gBACtC,OAAO;YACT,CAAC;YACD,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,iBAAiB,EAAE,CAAC;YACrD,IAAI,CAAC,eAAe,GAAG,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC;QACrE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,IAAI,CAAC,mDAAmD,EAAE,KAAK,CAAC,CAAC;YACzE,IAAI,CAAC,eAAe,GAAG,QAAQ,CAAC;QAClC,CAAC;IACH,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type {\n CapacitorAccelerometerPlugin,\n GetMeasurementResult,\n IsAvailableResult,\n Measurement,\n PermissionState,\n PermissionStatus,\n} from './definitions';\n\ntype DeviceMotionWithPermission = typeof DeviceMotionEvent & {\n requestPermission?: () => Promise<'granted' | 'denied'>;\n};\n\nconst DEFAULT_MEASUREMENT: Measurement = { x: 0, y: 0, z: 0 };\n\nexport class CapacitorAccelerometerWeb extends WebPlugin implements CapacitorAccelerometerPlugin {\n private lastMeasurement: Measurement = { ...DEFAULT_MEASUREMENT };\n private motionHandler: ((event: DeviceMotionEvent) => void) | null = null;\n private permissionState: PermissionState | 'limited' = 'prompt';\n\n private handleMotion = (event: DeviceMotionEvent) => {\n const accel = event.accelerationIncludingGravity ?? event.acceleration;\n if (!accel) {\n return;\n }\n\n this.lastMeasurement = {\n x: accel.x ?? 0,\n y: accel.y ?? 0,\n z: accel.z ?? 0,\n };\n\n this.notifyListeners('measurement', { ...this.lastMeasurement });\n };\n\n private supportsDeviceMotion(): boolean {\n return typeof window !== 'undefined' && typeof window.DeviceMotionEvent !== 'undefined';\n }\n\n private get motionEventConstructor(): DeviceMotionWithPermission | null {\n return this.supportsDeviceMotion() ? (window.DeviceMotionEvent as DeviceMotionWithPermission) : null;\n }\n\n async getMeasurement(): Promise<GetMeasurementResult> {\n return { ...this.lastMeasurement };\n }\n\n async isAvailable(): Promise<IsAvailableResult> {\n return { isAvailable: this.supportsDeviceMotion() };\n }\n\n async startMeasurementUpdates(): Promise<void> {\n await this.ensurePermission(true);\n if (!this.supportsDeviceMotion()) {\n throw this.unavailable('DeviceMotionEvent is not available in this browser.');\n }\n if (this.motionHandler) {\n return;\n }\n this.motionHandler = this.handleMotion;\n window.addEventListener('devicemotion', this.motionHandler);\n }\n\n async stopMeasurementUpdates(): Promise<void> {\n if (this.motionHandler && this.supportsDeviceMotion()) {\n window.removeEventListener('devicemotion', this.motionHandler);\n }\n this.motionHandler = null;\n }\n\n async checkPermissions(): Promise<PermissionStatus> {\n await this.ensurePermission(false);\n return { accelerometer: this.permissionState };\n }\n\n async requestPermissions(): Promise<PermissionStatus> {\n await this.ensurePermission(true);\n return { accelerometer: this.permissionState };\n }\n\n async addListener(eventName: 'measurement', listenerFunc: (event: Measurement) => void) {\n const handle = await super.addListener(eventName, listenerFunc);\n return handle;\n }\n\n async removeAllListeners(): Promise<void> {\n await super.removeAllListeners();\n }\n\n private async ensurePermission(request: boolean): Promise<void> {\n if (!this.supportsDeviceMotion()) {\n this.permissionState = 'denied';\n return;\n }\n\n const constructor = this.motionEventConstructor;\n if (!constructor?.requestPermission) {\n // Platforms without explicit permission prompts expose motion data if feature is available.\n this.permissionState = 'granted';\n return;\n }\n\n if (!request) {\n // We cannot probe state without prompting, assume prompt until user interacts.\n if (this.permissionState === 'prompt') {\n return;\n }\n return;\n }\n\n try {\n const status = await constructor.requestPermission();\n this.permissionState = status === 'granted' ? 'granted' : 'denied';\n } catch (error) {\n console.warn('CapacitorAccelerometer: permission request failed', error);\n this.permissionState = 'denied';\n }\n }\n}\n"]}