@inglorious/web 6.27.1 → 6.29.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/README.md +263 -203
- package/package.json +68 -56
- package/src/device/biometrics/index.js +86 -0
- package/src/device/compass/handlers.js +163 -0
- package/src/{sensors → device}/element-size/handlers.js +2 -2
- package/src/device/geolocation/index.js +120 -0
- package/src/{haptics → device/haptics}/index.js +1 -1
- package/src/device/local-notifications/index.js +133 -0
- package/src/{sensors → device}/media-query/handlers.js +2 -2
- package/src/{sensors → device}/network-status/handlers.js +1 -1
- package/src/{sensors → device}/page-visibility/handlers.js +1 -1
- package/src/{share → device/share}/index.js +1 -1
- package/src/{mobile → device/storage}/index.js +29 -17
- package/src/mount.js +1 -1
- package/src/{app-links → native/app-links}/index.js +1 -1
- package/src/{back-button → native/back-button}/index.js +1 -1
- package/src/native/index.js +17 -0
- package/src/{live-update → native/live-update}/index.js +1 -1
- package/src/{system-bars → native/system-bars}/index.js +1 -1
- package/src/{native-update → native/update}/index.js +1 -1
- package/types/device/biometrics.d.ts +51 -0
- package/types/{sensors → device}/compass.d.ts +0 -2
- package/types/{sensors → device}/geolocation.d.ts +25 -7
- package/types/device/local-notifications.d.ts +76 -0
- package/types/{mobile.d.ts → device/storage.d.ts} +12 -2
- package/types/{app-links.d.ts → native/app-links.d.ts} +1 -1
- package/types/native/index.d.ts +1 -0
- package/types/{live-update.d.ts → native/live-update.d.ts} +1 -1
- package/types/{native-update.d.ts → native/update.d.ts} +1 -1
- package/src/sensors/compass/handlers.js +0 -209
- package/src/sensors/geolocation/handlers.js +0 -168
- package/src/sensors/geolocation/index.js +0 -3
- /package/src/{sensors → device}/compass/index.js +0 -0
- /package/src/{sensors → device}/element-size/index.js +0 -0
- /package/src/{keyboard → device/keyboard}/index.js +0 -0
- /package/src/{sensors → device}/media-query/index.js +0 -0
- /package/src/{sensors → device}/network-status/index.js +0 -0
- /package/src/{sensors → device}/page-visibility/index.js +0 -0
- /package/types/{sensors → device}/element-size.d.ts +0 -0
- /package/types/{haptics.d.ts → device/haptics.d.ts} +0 -0
- /package/types/{keyboard.d.ts → device/keyboard.d.ts} +0 -0
- /package/types/{sensors → device}/media-query.d.ts +0 -0
- /package/types/{sensors → device}/network-status.d.ts +0 -0
- /package/types/{sensors → device}/page-visibility.d.ts +0 -0
- /package/types/{share.d.ts → device/share.d.ts} +0 -0
- /package/types/{back-button.d.ts → native/back-button.d.ts} +0 -0
- /package/types/{system-bars.d.ts → native/system-bars.d.ts} +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @typedef {import("../../../types/
|
|
3
|
-
* @typedef {import("../../../types/
|
|
2
|
+
* @typedef {import("../../../types/device/media-query.js").MediaQueryEntity} MediaQueryEntity
|
|
3
|
+
* @typedef {import("../../../types/device/media-query.js").MediaQueryMatchEvent} MediaQueryMatchEvent
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
const listeners = new Map()
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @typedef {import("../../../types/
|
|
2
|
+
* @typedef {import("../../../types/device/page-visibility.js").PageVisibilityEntity} PageVisibilityEntity
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
const VISIBILITY_EVENT = "visibilitychange"
|
|
@@ -1,20 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const capacitor = globalThis.Capacitor ?? globalThis.window?.Capacitor ?? null
|
|
3
|
-
|
|
4
|
-
if (!capacitor) {
|
|
5
|
-
return false
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
if (typeof capacitor.isNativePlatform === "function") {
|
|
9
|
-
return capacitor.isNativePlatform()
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (typeof capacitor.getPlatform === "function") {
|
|
13
|
-
return ["android", "ios"].includes(capacitor.getPlatform())
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return capacitor.isNative === true
|
|
17
|
-
}
|
|
1
|
+
import { isNative } from "../../native/index.js"
|
|
18
2
|
|
|
19
3
|
/**
|
|
20
4
|
* A storage for `withStorage` that survives the operating system clearing the
|
|
@@ -47,3 +31,31 @@ export function createNativeStorage({ plugin } = {}) {
|
|
|
47
31
|
removeItem: (key) => plugin.remove({ key }),
|
|
48
32
|
}
|
|
49
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A storage for secrets, such as a session's tokens: in a native app the
|
|
37
|
+
* Keychain (iOS) or Keystore (Android), through `NativeBiometric` from
|
|
38
|
+
* `@capgo/capacitor-native-biometric`, given as `plugin`; in a browser, which
|
|
39
|
+
* has nothing safer, `localStorage`. Use it as `withStorage`'s storage or
|
|
40
|
+
* Supabase's `auth.storage`.
|
|
41
|
+
*
|
|
42
|
+
* @param {{ plugin?: { getData(options: { key: string }): Promise<{ value: string }>, setData(options: { key: string, value: string }): Promise<void>, deleteData(options: { key: string }): Promise<void> } }} [config]
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* import { NativeBiometric } from "@capgo/capacitor-native-biometric"
|
|
46
|
+
* const secrets = createSecureStorage({ plugin: NativeBiometric })
|
|
47
|
+
* createClient(url, key, { auth: { storage: secrets } })
|
|
48
|
+
*/
|
|
49
|
+
export function createSecureStorage({ plugin } = {}) {
|
|
50
|
+
if (!plugin || !isNative()) return createNativeStorage()
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
getItem: (key) =>
|
|
54
|
+
plugin.getData({ key }).then(
|
|
55
|
+
({ value }) => value,
|
|
56
|
+
() => null,
|
|
57
|
+
),
|
|
58
|
+
setItem: (key, value) => plugin.setData({ key, value }),
|
|
59
|
+
removeItem: (key) => plugin.deleteData({ key }),
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/mount.js
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function isNative() {
|
|
2
|
+
const capacitor = globalThis.Capacitor ?? globalThis.window?.Capacitor ?? null
|
|
3
|
+
|
|
4
|
+
if (!capacitor) {
|
|
5
|
+
return false
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (typeof capacitor.isNativePlatform === "function") {
|
|
9
|
+
return capacitor.isNativePlatform()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (typeof capacitor.getPlatform === "function") {
|
|
13
|
+
return ["android", "ios"].includes(capacitor.getPlatform())
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return capacitor.isNative === true
|
|
17
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { AsyncEvents } from "@inglorious/store/async"
|
|
2
|
+
|
|
3
|
+
import { Api } from "../mount"
|
|
4
|
+
|
|
5
|
+
/** What `createBiometrics` needs of `NativeBiometric` from `@capgo/capacitor-native-biometric`. */
|
|
6
|
+
export interface BiometricsPlugin {
|
|
7
|
+
isAvailable(): Promise<{ isAvailable: boolean; biometryType: number }>
|
|
8
|
+
verifyIdentity(options?: BiometricsOptions): Promise<void>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** What the system's prompt says. */
|
|
12
|
+
export interface BiometricsOptions {
|
|
13
|
+
reason?: string
|
|
14
|
+
title?: string
|
|
15
|
+
subtitle?: string
|
|
16
|
+
description?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type BiometryType =
|
|
20
|
+
"face" | "fingerprint" | "iris" | "multiple" | "passcode"
|
|
21
|
+
|
|
22
|
+
export interface BiometricsEntity {
|
|
23
|
+
id?: string
|
|
24
|
+
type: string
|
|
25
|
+
isAvailable: boolean
|
|
26
|
+
biometryType: BiometryType | null
|
|
27
|
+
status: "idle" | "loading" | "success" | "error"
|
|
28
|
+
error: "unavailable" | "failed" | null
|
|
29
|
+
result: "verified" | "cancelled" | null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type BiometricsType = {
|
|
33
|
+
create(entity: BiometricsEntity, payload: unknown, api: Api): void
|
|
34
|
+
biometricsAvailable(
|
|
35
|
+
entity: BiometricsEntity,
|
|
36
|
+
answer: { isAvailable: boolean; biometryType: number },
|
|
37
|
+
): void
|
|
38
|
+
} & AsyncEvents<
|
|
39
|
+
"verify",
|
|
40
|
+
BiometricsEntity,
|
|
41
|
+
BiometricsOptions | undefined,
|
|
42
|
+
BiometricsEntity["result"]
|
|
43
|
+
>
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The type behind "unlock with Face ID": the system's biometric prompt in a
|
|
47
|
+
* native app; never available in a browser.
|
|
48
|
+
*/
|
|
49
|
+
export declare function createBiometrics(config?: {
|
|
50
|
+
plugin?: BiometricsPlugin
|
|
51
|
+
}): BiometricsType
|
|
@@ -10,8 +10,6 @@ export interface CompassEntity {
|
|
|
10
10
|
isLoading: boolean
|
|
11
11
|
isSupported: boolean
|
|
12
12
|
manualOffset: number | null
|
|
13
|
-
_absoluteListener: ((event: DeviceOrientationEvent) => void) | null
|
|
14
|
-
_orientationListener: ((event: DeviceOrientationEvent) => void) | null
|
|
15
13
|
_isUsingAbsolute: boolean
|
|
16
14
|
compassTimeout: number | null
|
|
17
15
|
}
|
|
@@ -18,7 +18,6 @@ export interface GeolocationEntity {
|
|
|
18
18
|
isWatching: boolean
|
|
19
19
|
position: GeolocationPosition | null
|
|
20
20
|
type: string
|
|
21
|
-
watchId: number | null
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
export interface GeolocationError {
|
|
@@ -38,21 +37,40 @@ export interface GeolocationPosition {
|
|
|
38
37
|
}
|
|
39
38
|
|
|
40
39
|
export type GeolocationType = {
|
|
41
|
-
unwatch(entity: GeolocationEntity): void
|
|
42
40
|
create(entity: GeolocationEntity): void
|
|
43
41
|
destroy(entity: GeolocationEntity): void
|
|
44
|
-
|
|
45
|
-
request(
|
|
42
|
+
geolocationRequest(
|
|
46
43
|
entity: GeolocationEntity,
|
|
47
44
|
options: GeolocationOptions | undefined,
|
|
48
45
|
api: Api,
|
|
49
46
|
): void
|
|
50
|
-
|
|
51
|
-
watch(
|
|
47
|
+
geolocationWatch(
|
|
52
48
|
entity: GeolocationEntity,
|
|
53
49
|
options: GeolocationOptions | undefined,
|
|
54
50
|
api: Api,
|
|
55
51
|
): void
|
|
52
|
+
geolocationUnwatch(entity: GeolocationEntity): void
|
|
53
|
+
geolocationSuccess(
|
|
54
|
+
entity: GeolocationEntity,
|
|
55
|
+
position: GeolocationPosition,
|
|
56
|
+
): void
|
|
57
|
+
geolocationError(entity: GeolocationEntity, error: GeolocationError): void
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** What `createGeolocation` needs of `Geolocation` from `@capacitor/geolocation`. */
|
|
61
|
+
export interface GeolocationPlugin {
|
|
62
|
+
getCurrentPosition(options?: GeolocationOptions): Promise<GeolocationPosition>
|
|
63
|
+
watchPosition(
|
|
64
|
+
options: GeolocationOptions,
|
|
65
|
+
callback: (position: GeolocationPosition | null, error?: unknown) => void,
|
|
66
|
+
): Promise<string>
|
|
67
|
+
clearWatch(options: { id: string }): Promise<void>
|
|
56
68
|
}
|
|
57
69
|
|
|
58
|
-
|
|
70
|
+
/**
|
|
71
|
+
* The type behind a position: `navigator.geolocation`, or the Capacitor
|
|
72
|
+
* plugin in a native app.
|
|
73
|
+
*/
|
|
74
|
+
export declare function createGeolocation(config?: {
|
|
75
|
+
plugin?: GeolocationPlugin
|
|
76
|
+
}): GeolocationType
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Api } from "../mount"
|
|
2
|
+
|
|
3
|
+
/** What `createLocalNotifications` needs of `LocalNotifications` from `@capacitor/local-notifications`. */
|
|
4
|
+
export interface LocalNotificationsPlugin {
|
|
5
|
+
checkPermissions(): Promise<{ display: string }>
|
|
6
|
+
requestPermissions(): Promise<{ display: string }>
|
|
7
|
+
schedule(options: {
|
|
8
|
+
notifications: {
|
|
9
|
+
id: number
|
|
10
|
+
title: string
|
|
11
|
+
body?: string
|
|
12
|
+
schedule: { at: Date }
|
|
13
|
+
extra?: { url?: string }
|
|
14
|
+
}[]
|
|
15
|
+
}): Promise<unknown>
|
|
16
|
+
cancel(options: { notifications: { id: number }[] }): Promise<void>
|
|
17
|
+
addListener(
|
|
18
|
+
event: "localNotificationActionPerformed",
|
|
19
|
+
listener: (action: { notification: { extra?: { url?: string } } }) => void,
|
|
20
|
+
): unknown
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A reminder to show at a given time. */
|
|
24
|
+
export interface LocalNotification {
|
|
25
|
+
/** A whole number, to cancel it by. */
|
|
26
|
+
id: number
|
|
27
|
+
title: string
|
|
28
|
+
body?: string
|
|
29
|
+
at: Date
|
|
30
|
+
/** Where a tap goes. */
|
|
31
|
+
url?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type LocalNotificationsPermission =
|
|
35
|
+
"granted" | "denied" | "prompt" | "unsupported"
|
|
36
|
+
|
|
37
|
+
export interface LocalNotificationsEntity {
|
|
38
|
+
id?: string
|
|
39
|
+
type: string
|
|
40
|
+
/** `null` until the device says. */
|
|
41
|
+
permission: LocalNotificationsPermission | null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type LocalNotificationsType = {
|
|
45
|
+
create(entity: LocalNotificationsEntity, payload: unknown, api: Api): void
|
|
46
|
+
localNotificationsAsk(
|
|
47
|
+
entity: LocalNotificationsEntity,
|
|
48
|
+
payload: unknown,
|
|
49
|
+
api: Api,
|
|
50
|
+
): void
|
|
51
|
+
localNotificationsPermission(
|
|
52
|
+
entity: LocalNotificationsEntity,
|
|
53
|
+
permission: LocalNotificationsPermission,
|
|
54
|
+
): void
|
|
55
|
+
localNotificationSchedule(
|
|
56
|
+
entity: LocalNotificationsEntity,
|
|
57
|
+
notification: LocalNotification,
|
|
58
|
+
): void
|
|
59
|
+
localNotificationCancel(entity: LocalNotificationsEntity, id: number): void
|
|
60
|
+
localNotificationTap(
|
|
61
|
+
entity: LocalNotificationsEntity,
|
|
62
|
+
url: string | undefined,
|
|
63
|
+
api: Api,
|
|
64
|
+
): void
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Reminders the device shows at a given time: through the Capacitor plugin in
|
|
69
|
+
* a native app, even with the app closed; through the Notification API in a
|
|
70
|
+
* browser, while the page is open.
|
|
71
|
+
*/
|
|
72
|
+
export declare function createLocalNotifications(config?: {
|
|
73
|
+
plugin?: LocalNotificationsPlugin
|
|
74
|
+
/** Where a tap goes. Defaults to `#router:navigate`. */
|
|
75
|
+
onTap?: (url: string, api: Api) => void
|
|
76
|
+
}): LocalNotificationsType
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
export declare function isNative(): boolean
|
|
2
|
-
|
|
3
1
|
/** What `createNativeStorage` needs of `Preferences` from `@capacitor/preferences`. */
|
|
4
2
|
export interface PreferencesPlugin {
|
|
5
3
|
get(options: { key: string }): Promise<{ value: string | null }>
|
|
@@ -18,3 +16,15 @@ export interface NativeStorage {
|
|
|
18
16
|
export declare function createNativeStorage(config?: {
|
|
19
17
|
plugin?: PreferencesPlugin
|
|
20
18
|
}): NativeStorage
|
|
19
|
+
|
|
20
|
+
/** What `createSecureStorage` needs of `NativeBiometric` from `@capgo/capacitor-native-biometric`. */
|
|
21
|
+
export interface SecureStoragePlugin {
|
|
22
|
+
getData(options: { key: string }): Promise<{ value: string }>
|
|
23
|
+
setData(options: { key: string; value: string }): Promise<void>
|
|
24
|
+
deleteData(options: { key: string }): Promise<void>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The Keychain or Keystore in a native app, `localStorage` in a browser. */
|
|
28
|
+
export declare function createSecureStorage(config?: {
|
|
29
|
+
plugin?: SecureStoragePlugin
|
|
30
|
+
}): NativeStorage
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isNative(): boolean
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {import("../../../types/sensors/compass.js").CompassEntity} CompassEntity
|
|
3
|
-
* @typedef {import("../../../types/sensors/compass.js").CompassError} CompassError
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const COMPASS_TIMEOUT = 3000
|
|
7
|
-
const NO_TIMEOUT = null
|
|
8
|
-
|
|
9
|
-
const FULL_CIRCLE = 360
|
|
10
|
-
const DEFAULT_ORIENTATION = 0
|
|
11
|
-
const DEFAULT_ERROR_CODE = 0
|
|
12
|
-
|
|
13
|
-
export function create(entity) {
|
|
14
|
-
entity.error ??= null
|
|
15
|
-
entity.heading ??= null
|
|
16
|
-
entity.isActive ??= false
|
|
17
|
-
entity.isPermissionGranted ??= false
|
|
18
|
-
entity.isLoading ??= false
|
|
19
|
-
entity.isSupported = isCompassSupported()
|
|
20
|
-
entity.manualOffset ??= 0
|
|
21
|
-
entity._absoluteListener ??= null
|
|
22
|
-
entity._cancelTimeout ??= null
|
|
23
|
-
entity._orientationListener ??= null
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function destroy(entity) {
|
|
27
|
-
compassUnwatch(entity)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function compassUnwatch(entity) {
|
|
31
|
-
const win = getWindow()
|
|
32
|
-
|
|
33
|
-
if (entity._orientationListener && win) {
|
|
34
|
-
win.removeEventListener("deviceorientation", entity._orientationListener)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (entity._absoluteListener && win) {
|
|
38
|
-
win.removeEventListener(
|
|
39
|
-
"deviceorientationabsolute",
|
|
40
|
-
entity._absoluteListener,
|
|
41
|
-
)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (entity._cancelTimeout) {
|
|
45
|
-
entity._cancelTimeout()
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
entity.isLoading = false
|
|
49
|
-
entity.isActive = false
|
|
50
|
-
entity._orientationListener = null
|
|
51
|
-
entity._absoluteListener = null
|
|
52
|
-
entity._cancelTimeout = null
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function compassError(entity, error) {
|
|
56
|
-
entity.error = normalizeError(error)
|
|
57
|
-
entity.isLoading = false
|
|
58
|
-
entity.isPermissionGranted = false
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export async function compassPermissionsRequest(entity, _, api) {
|
|
62
|
-
entity.isSupported = isCompassSupported()
|
|
63
|
-
entity.error = null
|
|
64
|
-
|
|
65
|
-
if (!entity.isSupported) {
|
|
66
|
-
entity.isLoading = false
|
|
67
|
-
api.notify("compassError", createUnsupportedError())
|
|
68
|
-
return
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
entity.isLoading = true
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
typeof DeviceOrientationEvent !== "undefined" &&
|
|
75
|
-
typeof DeviceOrientationEvent.requestPermission === "function"
|
|
76
|
-
) {
|
|
77
|
-
try {
|
|
78
|
-
const permission = await DeviceOrientationEvent.requestPermission()
|
|
79
|
-
if (permission === "granted") {
|
|
80
|
-
api.notify("compassPermissionsGrant")
|
|
81
|
-
} else {
|
|
82
|
-
entity.isLoading = false
|
|
83
|
-
api.notify("compassError", createPermissionDeniedError())
|
|
84
|
-
}
|
|
85
|
-
} catch (error) {
|
|
86
|
-
entity.isLoading = false
|
|
87
|
-
api.notify("compassError", normalizeError(error))
|
|
88
|
-
}
|
|
89
|
-
} else {
|
|
90
|
-
api.notify("compassPermissionsGrant")
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function compassPermissionsGrant(entity) {
|
|
95
|
-
entity.isLoading = false
|
|
96
|
-
entity.isPermissionGranted = true
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function compassWatch(entity, _, api) {
|
|
100
|
-
// Snapshot values from the entity while the proxy is still live.
|
|
101
|
-
// Closures must not access `entity` after this handler returns, as
|
|
102
|
-
// Mutative revokes the draft proxy at that point.
|
|
103
|
-
const manualOffset = entity.manualOffset
|
|
104
|
-
|
|
105
|
-
let isUsingAbsolute = false
|
|
106
|
-
let timeoutId = NO_TIMEOUT
|
|
107
|
-
|
|
108
|
-
const cancelTimeout = () => {
|
|
109
|
-
if (timeoutId !== NO_TIMEOUT) {
|
|
110
|
-
clearTimeout(timeoutId)
|
|
111
|
-
timeoutId = NO_TIMEOUT
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const orientationListener = createOrientationChangeListener(
|
|
116
|
-
cancelTimeout,
|
|
117
|
-
manualOffset,
|
|
118
|
-
api,
|
|
119
|
-
)
|
|
120
|
-
|
|
121
|
-
const absoluteListener = (event) => {
|
|
122
|
-
if (!isUsingAbsolute) {
|
|
123
|
-
isUsingAbsolute = true
|
|
124
|
-
getWindow()?.removeEventListener("deviceorientation", orientationListener)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
orientationListener(event)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
entity._orientationListener = orientationListener
|
|
131
|
-
entity._absoluteListener = absoluteListener
|
|
132
|
-
entity._cancelTimeout = cancelTimeout
|
|
133
|
-
|
|
134
|
-
const win = getWindow()
|
|
135
|
-
win?.addEventListener("deviceorientationabsolute", absoluteListener)
|
|
136
|
-
win?.addEventListener("deviceorientation", orientationListener)
|
|
137
|
-
|
|
138
|
-
timeoutId = globalThis.setTimeout(() => {
|
|
139
|
-
timeoutId = NO_TIMEOUT
|
|
140
|
-
api.notify("compassActiveChange", false)
|
|
141
|
-
}, COMPASS_TIMEOUT)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export function compassActiveChange(entity, value) {
|
|
145
|
-
entity.isActive = value
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export function compassHeadingChange(entity, value) {
|
|
149
|
-
entity.isLoading = false
|
|
150
|
-
entity.heading = value
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function createOrientationChangeListener(cancelTimeout, manualOffset, api) {
|
|
154
|
-
return (event) => {
|
|
155
|
-
const heading = event?.alpha
|
|
156
|
-
if (typeof heading !== "number") {
|
|
157
|
-
return
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
cancelTimeout()
|
|
161
|
-
|
|
162
|
-
const screenAngle = getScreenAngle()
|
|
163
|
-
const adjustedHeading =
|
|
164
|
-
(((heading - screenAngle + manualOffset) % FULL_CIRCLE) + FULL_CIRCLE) %
|
|
165
|
-
FULL_CIRCLE
|
|
166
|
-
|
|
167
|
-
api.notify("compassActiveChange", true)
|
|
168
|
-
api.notify("compassHeadingChange", Math.round(adjustedHeading))
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function getWindow() {
|
|
173
|
-
return typeof window !== "undefined" ? window : null
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function getScreenAngle() {
|
|
177
|
-
const win = getWindow()
|
|
178
|
-
return (
|
|
179
|
-
win?.screen?.orientation?.angle ?? win?.orientation ?? DEFAULT_ORIENTATION
|
|
180
|
-
)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function isCompassSupported() {
|
|
184
|
-
return (
|
|
185
|
-
typeof DeviceOrientationEvent !== "undefined" &&
|
|
186
|
-
Boolean(getWindow()?.addEventListener)
|
|
187
|
-
)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function createUnsupportedError() {
|
|
191
|
-
return {
|
|
192
|
-
code: 0,
|
|
193
|
-
message: "Compass is not supported by this environment.",
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function createPermissionDeniedError() {
|
|
198
|
-
return {
|
|
199
|
-
code: 1,
|
|
200
|
-
message: "Compass permission denied.",
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function normalizeError(error) {
|
|
205
|
-
return {
|
|
206
|
-
code: error?.code ?? DEFAULT_ERROR_CODE,
|
|
207
|
-
message: error?.message ?? String(error),
|
|
208
|
-
}
|
|
209
|
-
}
|