@mapzone/nav-speed-alert 1.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/src/index.ts ADDED
@@ -0,0 +1,114 @@
1
+ import type { EventSubscription } from 'expo-modules-core';
2
+
3
+ import type {
4
+ ConfigureOptions,
5
+ LocationFix,
6
+ RerouteEvent,
7
+ RestrictionEvent,
8
+ ResultEvent,
9
+ SignsEvent,
10
+ VoiceAlertType,
11
+ VoiceEvent,
12
+ } from './NavSpeedAlert.types';
13
+ import NavSpeedAlertModule from './NavSpeedAlertModule';
14
+
15
+ export * from './NavSpeedAlert.types';
16
+ export { useNavSpeedAlertSigns } from './useNavSpeedAlertSigns';
17
+
18
+ /** Configure credentials and vehicle profile. Must precede `start()`. */
19
+ export function configure(options: ConfigureOptions): void {
20
+ NavSpeedAlertModule.configure(options);
21
+ }
22
+
23
+ /** Override the segment endpoint. Empty string restores the default. */
24
+ export function setSegmentUrl(url: string): void {
25
+ NavSpeedAlertModule.setSegmentUrl(url);
26
+ }
27
+
28
+ /**
29
+ * Add HTTP headers to segment requests. Call after `configure()` and before
30
+ * `start()`. Returns `null` on success, otherwise the native error message
31
+ * (reserved or malformed key/value); previous headers are kept on error.
32
+ * Pass `{}` to clear.
33
+ */
34
+ export function setExtraHeaders(headers: Record<string, string>): string | null {
35
+ return NavSpeedAlertModule.setExtraHeaders(headers);
36
+ }
37
+
38
+ /**
39
+ * Add body fields to segment requests. Same timing and return contract as
40
+ * `setExtraHeaders`. Pass `{}` to clear.
41
+ */
42
+ export function setExtraBodyFields(fields: Record<string, string>): string | null {
43
+ return NavSpeedAlertModule.setExtraBodyFields(fields);
44
+ }
45
+
46
+ /** Start (or restart after reroute) alerting along an encoded polyline (precision 1e6). */
47
+ export function start(routePolyline: string): void {
48
+ NavSpeedAlertModule.start(routePolyline);
49
+ }
50
+
51
+ /** Feed one GPS fix. Call at ~1 Hz. */
52
+ export function onLocation(fix: LocationFix): void {
53
+ NavSpeedAlertModule.onLocation(
54
+ fix.lat,
55
+ fix.lng,
56
+ fix.bearing,
57
+ fix.speedKmh,
58
+ fix.accuracy,
59
+ fix.fixTimeMillis
60
+ );
61
+ }
62
+
63
+ /** Stop alerting and drop the current route. Safe to call repeatedly. */
64
+ export function reset(): void {
65
+ NavSpeedAlertModule.reset();
66
+ }
67
+
68
+ /**
69
+ * Mute voice for the given categories. Camera/toll mutes also hide their signs;
70
+ * restriction mutes only silence the voice. Pass `[]` to unmute everything.
71
+ */
72
+ export function setMutedAlertTypes(types: VoiceAlertType[]): void {
73
+ NavSpeedAlertModule.setMutedAlertTypes(types.map((t) => t as number));
74
+ }
75
+
76
+ /**
77
+ * Whether the native build captures a diagnostic log that `exportLogs` can write.
78
+ * Async because it waits for the engine queue (a segment fetch may be in flight).
79
+ */
80
+ export function canExportLogs(): Promise<boolean> {
81
+ return NavSpeedAlertModule.canExportLogs();
82
+ }
83
+
84
+ /**
85
+ * Write the diagnostic log to `path`. Resolves to bytes written: `0` = nothing
86
+ * captured, `-1` = the engine was busy or the write failed.
87
+ */
88
+ export function exportLogs(path: string): Promise<number> {
89
+ return NavSpeedAlertModule.exportLogs(path);
90
+ }
91
+
92
+ export function addSignsListener(listener: (event: SignsEvent) => void): EventSubscription {
93
+ return NavSpeedAlertModule.addListener('onSigns', listener);
94
+ }
95
+
96
+ /** First listener registers the native restriction callback; last removal unregisters it. */
97
+ export function addRestrictionListener(
98
+ listener: (event: RestrictionEvent) => void
99
+ ): EventSubscription {
100
+ return NavSpeedAlertModule.addListener('onRestriction', listener);
101
+ }
102
+
103
+ /** While at least one listener exists the built-in voice player is silent. */
104
+ export function addVoiceListener(listener: (event: VoiceEvent) => void): EventSubscription {
105
+ return NavSpeedAlertModule.addListener('onVoice', listener);
106
+ }
107
+
108
+ export function addResultListener(listener: (event: ResultEvent) => void): EventSubscription {
109
+ return NavSpeedAlertModule.addListener('onResult', listener);
110
+ }
111
+
112
+ export function addRerouteListener(listener: (event: RerouteEvent) => void): EventSubscription {
113
+ return NavSpeedAlertModule.addListener('onReroute', listener);
114
+ }
@@ -0,0 +1,79 @@
1
+ import type { EventSubscription } from 'expo-modules-core';
2
+ import { useEffect, useRef, useState } from 'react';
3
+
4
+ import type { AlertSignRef, RestrictionEvent, SignsEvent } from './NavSpeedAlert.types';
5
+ import NavSpeedAlertModule from './NavSpeedAlertModule';
6
+
7
+ const subscribeSigns = (l: (e: SignsEvent) => void) =>
8
+ NavSpeedAlertModule.addListener('onSigns', l);
9
+ const subscribeRestriction = (l: (e: RestrictionEvent) => void) =>
10
+ NavSpeedAlertModule.addListener('onRestriction', l);
11
+
12
+ // Module-level constants keep the effect deps stable.
13
+ const SIGN_SLOTS: (keyof SignsEvent)[] = ['current', 'next', 'camera', 'toll'];
14
+ const RESTRICTION_SLOTS: (keyof RestrictionEvent)[] = ['stop', 'closed', 'vehicle', 'bua', 'turn'];
15
+
16
+ function releaseAll<T>(event: T, slots: (keyof T)[]) {
17
+ for (const slot of slots) {
18
+ (event[slot] as AlertSignRef | null)?.release();
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Keeps one event kind in state and releases the native image refs of every
24
+ * delivered event once React has committed a newer one. Events that arrive
25
+ * between two commits (batched) never become state, so they are tracked in a
26
+ * ref and released together with the committed predecessor.
27
+ */
28
+ function useLatestTick<T>(
29
+ subscribe: (listener: (event: T) => void) => EventSubscription,
30
+ slots: (keyof T)[]
31
+ ): T | null {
32
+ const [current, setCurrent] = useState<T | null>(null);
33
+ const pending = useRef<T[]>([]);
34
+
35
+ useEffect(() => {
36
+ const sub = subscribe((event) => {
37
+ pending.current.push(event);
38
+ setCurrent(event);
39
+ });
40
+ return () => sub.remove();
41
+ }, [subscribe]);
42
+
43
+ // Runs after each commit: everything delivered *before* the committed value
44
+ // is stale and is released exactly once here. Anything after it (an event
45
+ // that landed between the commit and this effect) is left alone — it is
46
+ // about to become `current` itself.
47
+ useEffect(() => {
48
+ if (!current) return;
49
+ const idx = pending.current.indexOf(current);
50
+ if (idx <= 0) return;
51
+ pending.current.splice(0, idx).forEach((e) => releaseAll(e, slots));
52
+ }, [current, slots]);
53
+
54
+ // Unmount only (the subscription above is already removed by then): release
55
+ // the committed value plus anything that arrived after the last commit.
56
+ useEffect(
57
+ () => () => {
58
+ pending.current.forEach((e) => releaseAll(e, slots));
59
+ pending.current = [];
60
+ },
61
+ [slots]
62
+ );
63
+
64
+ return current;
65
+ }
66
+
67
+ /**
68
+ * Latest `onSigns` / `onRestriction` tick. Subscribing to `onRestriction`
69
+ * here is what makes native start producing restriction signs; unmounting
70
+ * stops it again.
71
+ */
72
+ export function useNavSpeedAlertSigns(): {
73
+ signs: SignsEvent | null;
74
+ restriction: RestrictionEvent | null;
75
+ } {
76
+ const signs = useLatestTick<SignsEvent>(subscribeSigns, SIGN_SLOTS);
77
+ const restriction = useLatestTick<RestrictionEvent>(subscribeRestriction, RESTRICTION_SLOTS);
78
+ return { signs, restriction };
79
+ }