@capawesome/capacitor-gyroscope 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 (38) hide show
  1. package/CapawesomeCapacitorGyroscope.podspec +17 -0
  2. package/LICENSE +21 -0
  3. package/Package.swift +28 -0
  4. package/README.md +333 -0
  5. package/android/build.gradle +58 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/Gyroscope.java +97 -0
  8. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/GyroscopePlugin.java +152 -0
  9. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/Measurement.java +28 -0
  10. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/interfaces/Callback.java +5 -0
  11. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/interfaces/EmptyCallback.java +5 -0
  12. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/interfaces/NonEmptyResultCallback.java +7 -0
  13. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/interfaces/Result.java +7 -0
  14. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/results/GetMeasurementResult.java +21 -0
  15. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/results/IsAvailableResult.java +20 -0
  16. package/android/src/main/res/.gitkeep +0 -0
  17. package/dist/docs.json +358 -0
  18. package/dist/esm/definitions.d.ts +112 -0
  19. package/dist/esm/definitions.js +2 -0
  20. package/dist/esm/definitions.js.map +1 -0
  21. package/dist/esm/index.d.ts +4 -0
  22. package/dist/esm/index.js +7 -0
  23. package/dist/esm/index.js.map +1 -0
  24. package/dist/esm/web.d.ts +18 -0
  25. package/dist/esm/web.js +125 -0
  26. package/dist/esm/web.js.map +1 -0
  27. package/dist/plugin.cjs.js +139 -0
  28. package/dist/plugin.cjs.js.map +1 -0
  29. package/dist/plugin.js +142 -0
  30. package/dist/plugin.js.map +1 -0
  31. package/ios/Plugin/Classes/Measurement.swift +24 -0
  32. package/ios/Plugin/Classes/Results/IsAvailableResult.swift +15 -0
  33. package/ios/Plugin/Enums/CustomError.swift +20 -0
  34. package/ios/Plugin/Gyroscope.swift +87 -0
  35. package/ios/Plugin/GyroscopePlugin.swift +116 -0
  36. package/ios/Plugin/Info.plist +24 -0
  37. package/ios/Plugin/Protocols/Result.swift +5 -0
  38. package/package.json +91 -0
@@ -0,0 +1,112 @@
1
+ import type { PermissionState, PluginListenerHandle } from '@capacitor/core';
2
+ export type GyroscopePermissionState = PermissionState | 'limited';
3
+ export interface GyroscopePlugin {
4
+ /**
5
+ * Called when a new measurement is available.
6
+ *
7
+ * Only available on Android and iOS.
8
+ *
9
+ * @since 0.1.0
10
+ */
11
+ addListener(eventName: 'measurement', listenerFunc: (event: MeasurementEvent) => void): Promise<PluginListenerHandle>;
12
+ /**
13
+ * Check if the app has permission to access the gyroscope sensor.
14
+ *
15
+ * @since 0.1.0
16
+ */
17
+ checkPermissions(): Promise<PermissionStatus>;
18
+ /**
19
+ * Get the latest measurement.
20
+ *
21
+ * This method returns the most recent measurement from the gyroscope sensor.
22
+ *
23
+ * @since 0.1.0
24
+ */
25
+ getMeasurement(): Promise<GetMeasurementResult>;
26
+ /**
27
+ * Check if the gyroscope sensor is available on the device.
28
+ *
29
+ * @since 0.1.0
30
+ */
31
+ isAvailable(): Promise<IsAvailableResult>;
32
+ /**
33
+ * Remove all listeners for this plugin.
34
+ *
35
+ * @since 0.1.0
36
+ */
37
+ removeAllListeners(): Promise<void>;
38
+ /**
39
+ * Request permission to access the gyroscope sensor.
40
+ *
41
+ * @since 0.1.0
42
+ */
43
+ requestPermissions(): Promise<PermissionStatus>;
44
+ /**
45
+ * Start emitting `measurement` events.
46
+ *
47
+ * @since 0.1.0
48
+ */
49
+ startMeasurementUpdates(): Promise<void>;
50
+ /**
51
+ * Stop emitting `measurement` events.
52
+ *
53
+ * @since 0.1.0
54
+ */
55
+ stopMeasurementUpdates(): Promise<void>;
56
+ }
57
+ /**
58
+ * @since 0.1.0
59
+ */
60
+ export type GetMeasurementResult = Measurement;
61
+ /**
62
+ * @since 0.1.0
63
+ */
64
+ export interface IsAvailableResult {
65
+ /**
66
+ * Whether the gyroscope sensor is available on the device.
67
+ *
68
+ * @since 0.1.0
69
+ */
70
+ isAvailable: boolean;
71
+ }
72
+ /**
73
+ * @since 0.1.0
74
+ */
75
+ export type MeasurementEvent = Measurement;
76
+ /**
77
+ * @since 0.1.0
78
+ */
79
+ export interface Measurement {
80
+ /**
81
+ * The rotation rate around the x-axis in radians per second (rad/s).
82
+ *
83
+ * @example 0.12
84
+ * @since 0.1.0
85
+ */
86
+ x: number;
87
+ /**
88
+ * The rotation rate around the y-axis in radians per second (rad/s).
89
+ *
90
+ * @example 0.12
91
+ * @since 0.1.0
92
+ */
93
+ y: number;
94
+ /**
95
+ * The rotation rate around the z-axis in radians per second (rad/s).
96
+ *
97
+ * @example 0.12
98
+ * @since 0.1.0
99
+ */
100
+ z: number;
101
+ }
102
+ /**
103
+ * @since 0.1.0
104
+ */
105
+ export interface PermissionStatus {
106
+ /**
107
+ * The permission status of the gyroscope sensor.
108
+ *
109
+ * @since 0.1.0
110
+ */
111
+ gyroscope: GyroscopePermissionState;
112
+ }
@@ -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 { PermissionState, PluginListenerHandle } from '@capacitor/core';\n\nexport type GyroscopePermissionState = PermissionState | 'limited';\n\nexport interface GyroscopePlugin {\n /**\n * Called when a new measurement is available.\n *\n * Only available on Android and iOS.\n *\n * @since 0.1.0\n */\n addListener(\n eventName: 'measurement',\n listenerFunc: (event: MeasurementEvent) => void,\n ): Promise<PluginListenerHandle>;\n /**\n * Check if the app has permission to access the gyroscope sensor.\n *\n * @since 0.1.0\n */\n checkPermissions(): Promise<PermissionStatus>;\n /**\n * Get the latest measurement.\n *\n * This method returns the most recent measurement from the gyroscope sensor.\n *\n * @since 0.1.0\n */\n getMeasurement(): Promise<GetMeasurementResult>;\n /**\n * Check if the gyroscope sensor is available on the device.\n *\n * @since 0.1.0\n */\n isAvailable(): Promise<IsAvailableResult>;\n /**\n * Remove all listeners for this plugin.\n *\n * @since 0.1.0\n */\n removeAllListeners(): Promise<void>;\n /**\n * Request permission to access the gyroscope sensor.\n *\n * @since 0.1.0\n */\n requestPermissions(): Promise<PermissionStatus>;\n /**\n * Start emitting `measurement` events.\n *\n * @since 0.1.0\n */\n startMeasurementUpdates(): Promise<void>;\n /**\n * Stop emitting `measurement` events.\n *\n * @since 0.1.0\n */\n stopMeasurementUpdates(): Promise<void>;\n}\n\n/**\n * @since 0.1.0\n */\nexport type GetMeasurementResult = Measurement;\n\n/**\n * @since 0.1.0\n */\nexport interface IsAvailableResult {\n /**\n * Whether the gyroscope sensor is available on the device.\n *\n * @since 0.1.0\n */\n isAvailable: boolean;\n}\n\n/**\n * @since 0.1.0\n */\nexport type MeasurementEvent = Measurement;\n\n/**\n * @since 0.1.0\n */\nexport interface Measurement {\n /**\n * The rotation rate around the x-axis in radians per second (rad/s).\n *\n * @example 0.12\n * @since 0.1.0\n */\n x: number;\n /**\n * The rotation rate around the y-axis in radians per second (rad/s).\n *\n * @example 0.12\n * @since 0.1.0\n */\n y: number;\n /**\n * The rotation rate around the z-axis in radians per second (rad/s).\n *\n * @example 0.12\n * @since 0.1.0\n */\n z: number;\n}\n\n/**\n * @since 0.1.0\n */\nexport interface PermissionStatus {\n /**\n * The permission status of the gyroscope sensor.\n *\n * @since 0.1.0\n */\n gyroscope: GyroscopePermissionState;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { GyroscopePlugin } from './definitions';
2
+ declare const Gyroscope: GyroscopePlugin;
3
+ export * from './definitions';
4
+ export { Gyroscope };
@@ -0,0 +1,7 @@
1
+ import { registerPlugin } from '@capacitor/core';
2
+ const Gyroscope = registerPlugin('Gyroscope', {
3
+ web: () => import('./web').then(m => new m.GyroscopeWeb()),
4
+ });
5
+ export * from './definitions';
6
+ export { Gyroscope };
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,SAAS,GAAG,cAAc,CAAkB,WAAW,EAAE;IAC7D,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,CAAC;CAC3D,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,SAAS,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { GyroscopePlugin } from './definitions';\n\nconst Gyroscope = registerPlugin<GyroscopePlugin>('Gyroscope', {\n web: () => import('./web').then(m => new m.GyroscopeWeb()),\n});\n\nexport * from './definitions';\nexport { Gyroscope };\n"]}
@@ -0,0 +1,18 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import type { GyroscopePlugin, IsAvailableResult, Measurement, PermissionStatus } from './definitions';
3
+ export declare class GyroscopeWeb extends WebPlugin implements GyroscopePlugin {
4
+ private _gyroscope;
5
+ private readonly _isAvailable;
6
+ private measurementEventStarted;
7
+ checkPermissions(): Promise<PermissionStatus>;
8
+ getMeasurement(): Promise<Measurement>;
9
+ isAvailable(): Promise<IsAvailableResult>;
10
+ removeAllListeners(): Promise<void>;
11
+ requestPermissions(): Promise<PermissionStatus>;
12
+ startMeasurementUpdates(): Promise<void>;
13
+ stopMeasurementUpdates(): Promise<void>;
14
+ private createOrGetGyroscope;
15
+ private createUnavailableException;
16
+ private handleMeasurementEvent;
17
+ private readMeasurementFromGyroscope;
18
+ }
@@ -0,0 +1,125 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2
+ import { CapacitorException, ExceptionCode, WebPlugin } from '@capacitor/core';
3
+ export class GyroscopeWeb extends WebPlugin {
4
+ constructor() {
5
+ super(...arguments);
6
+ this._isAvailable = 'Gyroscope' in window;
7
+ this.measurementEventStarted = false;
8
+ }
9
+ async checkPermissions() {
10
+ const { state } = await navigator.permissions.query({
11
+ name: 'gyroscope',
12
+ });
13
+ return { gyroscope: state };
14
+ }
15
+ async getMeasurement() {
16
+ if (!this._isAvailable) {
17
+ throw this.createUnavailableException();
18
+ }
19
+ const gyroscope = this.createOrGetGyroscope();
20
+ return new Promise((resolve, reject) => {
21
+ gyroscope.onerror = (event) => {
22
+ console.error(event);
23
+ reject(event);
24
+ };
25
+ gyroscope.onreading = () => {
26
+ const measurement = this.readMeasurementFromGyroscope();
27
+ gyroscope.stop();
28
+ resolve(measurement);
29
+ };
30
+ gyroscope.start();
31
+ });
32
+ }
33
+ async isAvailable() {
34
+ let isAvailable = false;
35
+ if (!this._isAvailable) {
36
+ return { isAvailable };
37
+ }
38
+ // According to an article on Chrome Developers (https://developer.chrome.com/docs/capabilities/web-apis/generic-sensor#feature-detection)
39
+ // we also need to connect to the sensor for an actual meaningful feature detection
40
+ const gyroscope = this.createOrGetGyroscope();
41
+ try {
42
+ await new Promise(resolve => {
43
+ gyroscope.onerror = (event) => {
44
+ console.error(event);
45
+ isAvailable = false;
46
+ gyroscope.stop();
47
+ resolve();
48
+ };
49
+ gyroscope.onreading = () => {
50
+ isAvailable = true;
51
+ gyroscope.stop();
52
+ resolve();
53
+ };
54
+ gyroscope.start();
55
+ });
56
+ }
57
+ catch (error) {
58
+ console.error(error);
59
+ isAvailable = false;
60
+ }
61
+ return { isAvailable };
62
+ }
63
+ async removeAllListeners() {
64
+ await super.removeAllListeners();
65
+ if (this.measurementEventStarted) {
66
+ await this.stopMeasurementUpdates();
67
+ }
68
+ }
69
+ async requestPermissions() {
70
+ const { state } = await navigator.permissions.query({
71
+ name: 'gyroscope',
72
+ });
73
+ return { gyroscope: state };
74
+ }
75
+ async startMeasurementUpdates() {
76
+ if (!this._isAvailable) {
77
+ throw this.createUnavailableException();
78
+ }
79
+ if (this.measurementEventStarted) {
80
+ return;
81
+ }
82
+ this.measurementEventStarted = true;
83
+ const gyroscope = this.createOrGetGyroscope();
84
+ gyroscope.onreading = () => this.handleMeasurementEvent();
85
+ gyroscope.start();
86
+ }
87
+ async stopMeasurementUpdates() {
88
+ if (!this._isAvailable) {
89
+ throw this.createUnavailableException();
90
+ }
91
+ if (!this.measurementEventStarted) {
92
+ return;
93
+ }
94
+ this.measurementEventStarted = false;
95
+ const gyroscope = this.createOrGetGyroscope();
96
+ gyroscope.stop();
97
+ gyroscope.onreading = null;
98
+ }
99
+ // @ts-ignore
100
+ createOrGetGyroscope() {
101
+ if (!this._gyroscope) {
102
+ // @ts-ignore
103
+ this._gyroscope = new Gyroscope({
104
+ frequency: 10,
105
+ });
106
+ }
107
+ return this._gyroscope;
108
+ }
109
+ createUnavailableException() {
110
+ return new CapacitorException('This plugin method is not available on this platform.', ExceptionCode.Unavailable);
111
+ }
112
+ handleMeasurementEvent() {
113
+ const measurement = this.readMeasurementFromGyroscope();
114
+ this.notifyListeners('measurement', measurement);
115
+ }
116
+ readMeasurementFromGyroscope() {
117
+ const gyroscope = this.createOrGetGyroscope();
118
+ return {
119
+ x: Math.round(gyroscope.x * 100) / 100,
120
+ y: Math.round(gyroscope.y * 100) / 100,
121
+ z: Math.round(gyroscope.z * 100) / 100,
122
+ };
123
+ }
124
+ }
125
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAS/E,MAAM,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C9C,OAAO;YACL,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;YACtC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;YACtC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;SACvC,CAAC;IACJ,CAAC;CACF","sourcesContent":["/* eslint-disable @typescript-eslint/ban-ts-comment */\nimport { CapacitorException, ExceptionCode, WebPlugin } from '@capacitor/core';\n\nimport type {\n GyroscopePlugin,\n IsAvailableResult,\n Measurement,\n PermissionStatus,\n} from './definitions';\n\nexport class GyroscopeWeb extends WebPlugin implements GyroscopePlugin {\n // @ts-ignore\n private _gyroscope: Gyroscope | undefined;\n private readonly _isAvailable = 'Gyroscope' in window;\n private measurementEventStarted = false;\n\n async checkPermissions(): Promise<PermissionStatus> {\n const { state } = await navigator.permissions.query({\n name: 'gyroscope' as PermissionName,\n });\n return { gyroscope: state };\n }\n\n async getMeasurement(): Promise<Measurement> {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n const gyroscope = this.createOrGetGyroscope();\n return new Promise<Measurement>((resolve, reject) => {\n gyroscope.onerror = (event: any) => {\n console.error(event);\n reject(event);\n };\n gyroscope.onreading = () => {\n const measurement = this.readMeasurementFromGyroscope();\n gyroscope.stop();\n resolve(measurement);\n };\n gyroscope.start();\n });\n }\n\n async isAvailable(): Promise<IsAvailableResult> {\n let isAvailable = false;\n if (!this._isAvailable) {\n return { isAvailable };\n }\n // According to an article on Chrome Developers (https://developer.chrome.com/docs/capabilities/web-apis/generic-sensor#feature-detection)\n // we also need to connect to the sensor for an actual meaningful feature detection\n const gyroscope = this.createOrGetGyroscope();\n try {\n await new Promise<void>(resolve => {\n gyroscope.onerror = (event: any) => {\n console.error(event);\n isAvailable = false;\n gyroscope.stop();\n resolve();\n };\n gyroscope.onreading = () => {\n isAvailable = true;\n gyroscope.stop();\n resolve();\n };\n gyroscope.start();\n });\n } catch (error: any) {\n console.error(error);\n isAvailable = false;\n }\n return { isAvailable };\n }\n\n async removeAllListeners(): Promise<void> {\n await super.removeAllListeners();\n if (this.measurementEventStarted) {\n await this.stopMeasurementUpdates();\n }\n }\n\n async requestPermissions(): Promise<PermissionStatus> {\n const { state } = await navigator.permissions.query({\n name: 'gyroscope' as PermissionName,\n });\n return { gyroscope: state };\n }\n\n async startMeasurementUpdates(): Promise<void> {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n if (this.measurementEventStarted) {\n return;\n }\n this.measurementEventStarted = true;\n const gyroscope = this.createOrGetGyroscope();\n gyroscope.onreading = () => this.handleMeasurementEvent();\n gyroscope.start();\n }\n\n async stopMeasurementUpdates(): Promise<void> {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n if (!this.measurementEventStarted) {\n return;\n }\n this.measurementEventStarted = false;\n const gyroscope = this.createOrGetGyroscope();\n gyroscope.stop();\n gyroscope.onreading = null;\n }\n\n // @ts-ignore\n private createOrGetGyroscope(): Gyroscope {\n if (!this._gyroscope) {\n // @ts-ignore\n this._gyroscope = new Gyroscope({\n frequency: 10,\n });\n }\n return this._gyroscope;\n }\n\n private createUnavailableException(): CapacitorException {\n return new CapacitorException(\n 'This plugin method is not available on this platform.',\n ExceptionCode.Unavailable,\n );\n }\n\n private handleMeasurementEvent(): void {\n const measurement = this.readMeasurementFromGyroscope();\n this.notifyListeners('measurement', measurement);\n }\n\n private readMeasurementFromGyroscope(): Measurement {\n const gyroscope = this.createOrGetGyroscope();\n return {\n x: Math.round(gyroscope.x * 100) / 100,\n y: Math.round(gyroscope.y * 100) / 100,\n z: Math.round(gyroscope.z * 100) / 100,\n };\n }\n}\n"]}
@@ -0,0 +1,139 @@
1
+ 'use strict';
2
+
3
+ var core = require('@capacitor/core');
4
+
5
+ const Gyroscope$1 = core.registerPlugin('Gyroscope', {
6
+ web: () => Promise.resolve().then(function () { return web; }).then(m => new m.GyroscopeWeb()),
7
+ });
8
+
9
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
10
+ class GyroscopeWeb extends core.WebPlugin {
11
+ constructor() {
12
+ super(...arguments);
13
+ this._isAvailable = 'Gyroscope' in window;
14
+ this.measurementEventStarted = false;
15
+ }
16
+ async checkPermissions() {
17
+ const { state } = await navigator.permissions.query({
18
+ name: 'gyroscope',
19
+ });
20
+ return { gyroscope: state };
21
+ }
22
+ async getMeasurement() {
23
+ if (!this._isAvailable) {
24
+ throw this.createUnavailableException();
25
+ }
26
+ const gyroscope = this.createOrGetGyroscope();
27
+ return new Promise((resolve, reject) => {
28
+ gyroscope.onerror = (event) => {
29
+ console.error(event);
30
+ reject(event);
31
+ };
32
+ gyroscope.onreading = () => {
33
+ const measurement = this.readMeasurementFromGyroscope();
34
+ gyroscope.stop();
35
+ resolve(measurement);
36
+ };
37
+ gyroscope.start();
38
+ });
39
+ }
40
+ async isAvailable() {
41
+ let isAvailable = false;
42
+ if (!this._isAvailable) {
43
+ return { isAvailable };
44
+ }
45
+ // According to an article on Chrome Developers (https://developer.chrome.com/docs/capabilities/web-apis/generic-sensor#feature-detection)
46
+ // we also need to connect to the sensor for an actual meaningful feature detection
47
+ const gyroscope = this.createOrGetGyroscope();
48
+ try {
49
+ await new Promise(resolve => {
50
+ gyroscope.onerror = (event) => {
51
+ console.error(event);
52
+ isAvailable = false;
53
+ gyroscope.stop();
54
+ resolve();
55
+ };
56
+ gyroscope.onreading = () => {
57
+ isAvailable = true;
58
+ gyroscope.stop();
59
+ resolve();
60
+ };
61
+ gyroscope.start();
62
+ });
63
+ }
64
+ catch (error) {
65
+ console.error(error);
66
+ isAvailable = false;
67
+ }
68
+ return { isAvailable };
69
+ }
70
+ async removeAllListeners() {
71
+ await super.removeAllListeners();
72
+ if (this.measurementEventStarted) {
73
+ await this.stopMeasurementUpdates();
74
+ }
75
+ }
76
+ async requestPermissions() {
77
+ const { state } = await navigator.permissions.query({
78
+ name: 'gyroscope',
79
+ });
80
+ return { gyroscope: state };
81
+ }
82
+ async startMeasurementUpdates() {
83
+ if (!this._isAvailable) {
84
+ throw this.createUnavailableException();
85
+ }
86
+ if (this.measurementEventStarted) {
87
+ return;
88
+ }
89
+ this.measurementEventStarted = true;
90
+ const gyroscope = this.createOrGetGyroscope();
91
+ gyroscope.onreading = () => this.handleMeasurementEvent();
92
+ gyroscope.start();
93
+ }
94
+ async stopMeasurementUpdates() {
95
+ if (!this._isAvailable) {
96
+ throw this.createUnavailableException();
97
+ }
98
+ if (!this.measurementEventStarted) {
99
+ return;
100
+ }
101
+ this.measurementEventStarted = false;
102
+ const gyroscope = this.createOrGetGyroscope();
103
+ gyroscope.stop();
104
+ gyroscope.onreading = null;
105
+ }
106
+ // @ts-ignore
107
+ createOrGetGyroscope() {
108
+ if (!this._gyroscope) {
109
+ // @ts-ignore
110
+ this._gyroscope = new Gyroscope({
111
+ frequency: 10,
112
+ });
113
+ }
114
+ return this._gyroscope;
115
+ }
116
+ createUnavailableException() {
117
+ return new core.CapacitorException('This plugin method is not available on this platform.', core.ExceptionCode.Unavailable);
118
+ }
119
+ handleMeasurementEvent() {
120
+ const measurement = this.readMeasurementFromGyroscope();
121
+ this.notifyListeners('measurement', measurement);
122
+ }
123
+ readMeasurementFromGyroscope() {
124
+ const gyroscope = this.createOrGetGyroscope();
125
+ return {
126
+ x: Math.round(gyroscope.x * 100) / 100,
127
+ y: Math.round(gyroscope.y * 100) / 100,
128
+ z: Math.round(gyroscope.z * 100) / 100,
129
+ };
130
+ }
131
+ }
132
+
133
+ var web = /*#__PURE__*/Object.freeze({
134
+ __proto__: null,
135
+ GyroscopeWeb: GyroscopeWeb
136
+ });
137
+
138
+ exports.Gyroscope = Gyroscope$1;
139
+ //# sourceMappingURL=plugin.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Gyroscope = registerPlugin('Gyroscope', {\n web: () => import('./web').then(m => new m.GyroscopeWeb()),\n});\nexport * from './definitions';\nexport { Gyroscope };\n//# sourceMappingURL=index.js.map","/* eslint-disable @typescript-eslint/ban-ts-comment */\nimport { CapacitorException, ExceptionCode, WebPlugin } from '@capacitor/core';\nexport class GyroscopeWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this._isAvailable = 'Gyroscope' in window;\n this.measurementEventStarted = false;\n }\n async checkPermissions() {\n const { state } = await navigator.permissions.query({\n name: 'gyroscope',\n });\n return { gyroscope: state };\n }\n async getMeasurement() {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n const gyroscope = this.createOrGetGyroscope();\n return new Promise((resolve, reject) => {\n gyroscope.onerror = (event) => {\n console.error(event);\n reject(event);\n };\n gyroscope.onreading = () => {\n const measurement = this.readMeasurementFromGyroscope();\n gyroscope.stop();\n resolve(measurement);\n };\n gyroscope.start();\n });\n }\n async isAvailable() {\n let isAvailable = false;\n if (!this._isAvailable) {\n return { isAvailable };\n }\n // According to an article on Chrome Developers (https://developer.chrome.com/docs/capabilities/web-apis/generic-sensor#feature-detection)\n // we also need to connect to the sensor for an actual meaningful feature detection\n const gyroscope = this.createOrGetGyroscope();\n try {\n await new Promise(resolve => {\n gyroscope.onerror = (event) => {\n console.error(event);\n isAvailable = false;\n gyroscope.stop();\n resolve();\n };\n gyroscope.onreading = () => {\n isAvailable = true;\n gyroscope.stop();\n resolve();\n };\n gyroscope.start();\n });\n }\n catch (error) {\n console.error(error);\n isAvailable = false;\n }\n return { isAvailable };\n }\n async removeAllListeners() {\n await super.removeAllListeners();\n if (this.measurementEventStarted) {\n await this.stopMeasurementUpdates();\n }\n }\n async requestPermissions() {\n const { state } = await navigator.permissions.query({\n name: 'gyroscope',\n });\n return { gyroscope: state };\n }\n async startMeasurementUpdates() {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n if (this.measurementEventStarted) {\n return;\n }\n this.measurementEventStarted = true;\n const gyroscope = this.createOrGetGyroscope();\n gyroscope.onreading = () => this.handleMeasurementEvent();\n gyroscope.start();\n }\n async stopMeasurementUpdates() {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n if (!this.measurementEventStarted) {\n return;\n }\n this.measurementEventStarted = false;\n const gyroscope = this.createOrGetGyroscope();\n gyroscope.stop();\n gyroscope.onreading = null;\n }\n // @ts-ignore\n createOrGetGyroscope() {\n if (!this._gyroscope) {\n // @ts-ignore\n this._gyroscope = new Gyroscope({\n frequency: 10,\n });\n }\n return this._gyroscope;\n }\n createUnavailableException() {\n return new CapacitorException('This plugin method is not available on this platform.', ExceptionCode.Unavailable);\n }\n handleMeasurementEvent() {\n const measurement = this.readMeasurementFromGyroscope();\n this.notifyListeners('measurement', measurement);\n }\n readMeasurementFromGyroscope() {\n const gyroscope = this.createOrGetGyroscope();\n return {\n x: Math.round(gyroscope.x * 100) / 100,\n y: Math.round(gyroscope.y * 100) / 100,\n z: Math.round(gyroscope.z * 100) / 100,\n };\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["Gyroscope","registerPlugin","WebPlugin","CapacitorException","ExceptionCode"],"mappings":";;;;AACK,MAACA,WAAS,GAAGC,mBAAc,CAAC,WAAW,EAAE;AAC9C,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,YAAY,EAAE,CAAC;AAC9D,CAAC;;ACHD;AAEO,MAAM,YAAY,SAASC,cAAS,CAAC;AAC5C,IAAI,WAAW,GAAG;AAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC;AAC3B,QAAQ,IAAI,CAAC,YAAY,GAAG,WAAW,IAAI,MAAM;AACjD,QAAQ,IAAI,CAAC,uBAAuB,GAAG,KAAK;AAC5C,IAAI;AACJ,IAAI,MAAM,gBAAgB,GAAG;AAC7B,QAAQ,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,SAAS,CAAC,WAA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E,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;AAClD,YAAY,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;AAClD,YAAY,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;AAClD,SAAS;AACT,IAAI;AACJ;;;;;;;;;"}
package/dist/plugin.js ADDED
@@ -0,0 +1,142 @@
1
+ var capacitorGyroscope = (function (exports, core) {
2
+ 'use strict';
3
+
4
+ const Gyroscope$1 = core.registerPlugin('Gyroscope', {
5
+ web: () => Promise.resolve().then(function () { return web; }).then(m => new m.GyroscopeWeb()),
6
+ });
7
+
8
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
9
+ class GyroscopeWeb extends core.WebPlugin {
10
+ constructor() {
11
+ super(...arguments);
12
+ this._isAvailable = 'Gyroscope' in window;
13
+ this.measurementEventStarted = false;
14
+ }
15
+ async checkPermissions() {
16
+ const { state } = await navigator.permissions.query({
17
+ name: 'gyroscope',
18
+ });
19
+ return { gyroscope: state };
20
+ }
21
+ async getMeasurement() {
22
+ if (!this._isAvailable) {
23
+ throw this.createUnavailableException();
24
+ }
25
+ const gyroscope = this.createOrGetGyroscope();
26
+ return new Promise((resolve, reject) => {
27
+ gyroscope.onerror = (event) => {
28
+ console.error(event);
29
+ reject(event);
30
+ };
31
+ gyroscope.onreading = () => {
32
+ const measurement = this.readMeasurementFromGyroscope();
33
+ gyroscope.stop();
34
+ resolve(measurement);
35
+ };
36
+ gyroscope.start();
37
+ });
38
+ }
39
+ async isAvailable() {
40
+ let isAvailable = false;
41
+ if (!this._isAvailable) {
42
+ return { isAvailable };
43
+ }
44
+ // According to an article on Chrome Developers (https://developer.chrome.com/docs/capabilities/web-apis/generic-sensor#feature-detection)
45
+ // we also need to connect to the sensor for an actual meaningful feature detection
46
+ const gyroscope = this.createOrGetGyroscope();
47
+ try {
48
+ await new Promise(resolve => {
49
+ gyroscope.onerror = (event) => {
50
+ console.error(event);
51
+ isAvailable = false;
52
+ gyroscope.stop();
53
+ resolve();
54
+ };
55
+ gyroscope.onreading = () => {
56
+ isAvailable = true;
57
+ gyroscope.stop();
58
+ resolve();
59
+ };
60
+ gyroscope.start();
61
+ });
62
+ }
63
+ catch (error) {
64
+ console.error(error);
65
+ isAvailable = false;
66
+ }
67
+ return { isAvailable };
68
+ }
69
+ async removeAllListeners() {
70
+ await super.removeAllListeners();
71
+ if (this.measurementEventStarted) {
72
+ await this.stopMeasurementUpdates();
73
+ }
74
+ }
75
+ async requestPermissions() {
76
+ const { state } = await navigator.permissions.query({
77
+ name: 'gyroscope',
78
+ });
79
+ return { gyroscope: state };
80
+ }
81
+ async startMeasurementUpdates() {
82
+ if (!this._isAvailable) {
83
+ throw this.createUnavailableException();
84
+ }
85
+ if (this.measurementEventStarted) {
86
+ return;
87
+ }
88
+ this.measurementEventStarted = true;
89
+ const gyroscope = this.createOrGetGyroscope();
90
+ gyroscope.onreading = () => this.handleMeasurementEvent();
91
+ gyroscope.start();
92
+ }
93
+ async stopMeasurementUpdates() {
94
+ if (!this._isAvailable) {
95
+ throw this.createUnavailableException();
96
+ }
97
+ if (!this.measurementEventStarted) {
98
+ return;
99
+ }
100
+ this.measurementEventStarted = false;
101
+ const gyroscope = this.createOrGetGyroscope();
102
+ gyroscope.stop();
103
+ gyroscope.onreading = null;
104
+ }
105
+ // @ts-ignore
106
+ createOrGetGyroscope() {
107
+ if (!this._gyroscope) {
108
+ // @ts-ignore
109
+ this._gyroscope = new Gyroscope({
110
+ frequency: 10,
111
+ });
112
+ }
113
+ return this._gyroscope;
114
+ }
115
+ createUnavailableException() {
116
+ return new core.CapacitorException('This plugin method is not available on this platform.', core.ExceptionCode.Unavailable);
117
+ }
118
+ handleMeasurementEvent() {
119
+ const measurement = this.readMeasurementFromGyroscope();
120
+ this.notifyListeners('measurement', measurement);
121
+ }
122
+ readMeasurementFromGyroscope() {
123
+ const gyroscope = this.createOrGetGyroscope();
124
+ return {
125
+ x: Math.round(gyroscope.x * 100) / 100,
126
+ y: Math.round(gyroscope.y * 100) / 100,
127
+ z: Math.round(gyroscope.z * 100) / 100,
128
+ };
129
+ }
130
+ }
131
+
132
+ var web = /*#__PURE__*/Object.freeze({
133
+ __proto__: null,
134
+ GyroscopeWeb: GyroscopeWeb
135
+ });
136
+
137
+ exports.Gyroscope = Gyroscope$1;
138
+
139
+ return exports;
140
+
141
+ })({}, capacitorExports);
142
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Gyroscope = registerPlugin('Gyroscope', {\n web: () => import('./web').then(m => new m.GyroscopeWeb()),\n});\nexport * from './definitions';\nexport { Gyroscope };\n//# sourceMappingURL=index.js.map","/* eslint-disable @typescript-eslint/ban-ts-comment */\nimport { CapacitorException, ExceptionCode, WebPlugin } from '@capacitor/core';\nexport class GyroscopeWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this._isAvailable = 'Gyroscope' in window;\n this.measurementEventStarted = false;\n }\n async checkPermissions() {\n const { state } = await navigator.permissions.query({\n name: 'gyroscope',\n });\n return { gyroscope: state };\n }\n async getMeasurement() {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n const gyroscope = this.createOrGetGyroscope();\n return new Promise((resolve, reject) => {\n gyroscope.onerror = (event) => {\n console.error(event);\n reject(event);\n };\n gyroscope.onreading = () => {\n const measurement = this.readMeasurementFromGyroscope();\n gyroscope.stop();\n resolve(measurement);\n };\n gyroscope.start();\n });\n }\n async isAvailable() {\n let isAvailable = false;\n if (!this._isAvailable) {\n return { isAvailable };\n }\n // According to an article on Chrome Developers (https://developer.chrome.com/docs/capabilities/web-apis/generic-sensor#feature-detection)\n // we also need to connect to the sensor for an actual meaningful feature detection\n const gyroscope = this.createOrGetGyroscope();\n try {\n await new Promise(resolve => {\n gyroscope.onerror = (event) => {\n console.error(event);\n isAvailable = false;\n gyroscope.stop();\n resolve();\n };\n gyroscope.onreading = () => {\n isAvailable = true;\n gyroscope.stop();\n resolve();\n };\n gyroscope.start();\n });\n }\n catch (error) {\n console.error(error);\n isAvailable = false;\n }\n return { isAvailable };\n }\n async removeAllListeners() {\n await super.removeAllListeners();\n if (this.measurementEventStarted) {\n await this.stopMeasurementUpdates();\n }\n }\n async requestPermissions() {\n const { state } = await navigator.permissions.query({\n name: 'gyroscope',\n });\n return { gyroscope: state };\n }\n async startMeasurementUpdates() {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n if (this.measurementEventStarted) {\n return;\n }\n this.measurementEventStarted = true;\n const gyroscope = this.createOrGetGyroscope();\n gyroscope.onreading = () => this.handleMeasurementEvent();\n gyroscope.start();\n }\n async stopMeasurementUpdates() {\n if (!this._isAvailable) {\n throw this.createUnavailableException();\n }\n if (!this.measurementEventStarted) {\n return;\n }\n this.measurementEventStarted = false;\n const gyroscope = this.createOrGetGyroscope();\n gyroscope.stop();\n gyroscope.onreading = null;\n }\n // @ts-ignore\n createOrGetGyroscope() {\n if (!this._gyroscope) {\n // @ts-ignore\n this._gyroscope = new Gyroscope({\n frequency: 10,\n });\n }\n return this._gyroscope;\n }\n createUnavailableException() {\n return new CapacitorException('This plugin method is not available on this platform.', ExceptionCode.Unavailable);\n }\n handleMeasurementEvent() {\n const measurement = this.readMeasurementFromGyroscope();\n this.notifyListeners('measurement', measurement);\n }\n readMeasurementFromGyroscope() {\n const gyroscope = this.createOrGetGyroscope();\n return {\n x: Math.round(gyroscope.x * 100) / 100,\n y: Math.round(gyroscope.y * 100) / 100,\n z: Math.round(gyroscope.z * 100) / 100,\n };\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["Gyroscope","registerPlugin","WebPlugin","CapacitorException","ExceptionCode"],"mappings":";;;AACK,UAACA,WAAS,GAAGC,mBAAc,C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QAAQ;IACR,QAAQ,OAAO,IAAI,CAAC,UAAU;IAC9B,IAAI;IACJ,IAAI,0BAA0B,GAAG;IACjC,QAAQ,OAAO,IAAIC,uBAAkB,CAAC,uDAAuD,EAAEC,kBAAa,CAAC,WAAW,CAAC;IACzH,IAAI;IACJ,IAAI,sBAAsB,GAAG;IAC7B,QAAQ,MAAM,WAAW,GAAG,IAAI,CAAC,4BAA4B,EAAE;IAC/D,QAAQ,IAAI,CAAC,eAAe,CAAC,aAAa,EAAE,WAAW,CAAC;IACxD,IAAI;IACJ,IAAI,4BAA4B,GAAG;IACnC,QAAQ,MAAM,SAAS,GAAG,IAAI,CAAC,oBAAoB,EAAE;IACrD,QAAQ,OAAO;IACf,YAAY,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;IAClD,YAAY,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;IAClD,YAAY,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;IAClD,SAAS;IACT,IAAI;IACJ;;;;;;;;;;;;;;;"}
@@ -0,0 +1,24 @@
1
+ import Capacitor
2
+ import CoreMotion
3
+
4
+ @objc public class Measurement: NSObject, Result {
5
+ // swiftlint:disable identifier_name
6
+ let x: Double
7
+ let y: Double
8
+ let z: Double
9
+ // swiftlint:enable identifier_name
10
+
11
+ init(_ data: CMGyroData) {
12
+ self.x = round(data.rotationRate.x * 100) / 100
13
+ self.y = round(data.rotationRate.y * 100) / 100
14
+ self.z = round(data.rotationRate.z * 100) / 100
15
+ }
16
+
17
+ public func toJSObject() -> AnyObject {
18
+ var result = JSObject()
19
+ result["x"] = x
20
+ result["y"] = y
21
+ result["z"] = z
22
+ return result as AnyObject
23
+ }
24
+ }
@@ -0,0 +1,15 @@
1
+ import Capacitor
2
+
3
+ @objc public class IsAvailableResult: NSObject, Result {
4
+ private let isAvailable: Bool
5
+
6
+ init(isAvailable: Bool) {
7
+ self.isAvailable = isAvailable
8
+ }
9
+
10
+ public func toJSObject() -> AnyObject {
11
+ var result = JSObject()
12
+ result["isAvailable"] = isAvailable
13
+ return result as AnyObject
14
+ }
15
+ }