UNPKG

expo-realtime-maps-navigation

Version:

JavaScript-pure React Native navigation package with Google Places + HERE Routing APIs, dual maps support, and complete customization - No native modules required!

151 lines (150 loc) 6.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useNavigationEventStats = exports.useNavigationEventLogger = exports.useReroutingEvents = exports.useArrivalEvents = exports.useNavigationErrors = exports.useVoiceEvents = exports.useManeuverEvents = exports.useNavigationProgress = exports.useNavigationEventType = exports.useNavigationEvents = void 0; const react_1 = require("react"); const navigation_1 = require("../api/navigation"); const useNavigationEvents = (callback, options = {}) => { const { eventTypes, throttleMs = 0, debug = false } = options; const callbackRef = (0, react_1.useRef)(callback); callbackRef.current = callback; const lastCallTimeRef = (0, react_1.useRef)({}); const throttledCallback = (0, react_1.useCallback)((event) => { const now = Date.now(); const eventType = event.type; const lastCallTime = lastCallTimeRef.current[eventType] || 0; if (throttleMs > 0 && (eventType === 'progress' || eventType === 'voice')) { if (now - lastCallTime < throttleMs) { return; } } lastCallTimeRef.current[eventType] = now; if (eventTypes && !eventTypes.includes(eventType)) { return; } if (debug) { console.warn(`[NavigationEvent] ${eventType}:`, event.data); } callbackRef.current(event); }, [eventTypes, throttleMs, debug]); (0, react_1.useEffect)(() => { const unsubscribeFunctions = [ navigation_1.events.subscribeToProgress(data => { throttledCallback({ type: 'progress', data }); }), navigation_1.events.subscribeToManeuvers(data => { throttledCallback({ type: 'maneuver', data }); }), navigation_1.events.subscribeToVoice(data => { throttledCallback({ type: 'voice', data }); }), navigation_1.events.subscribe(event => { if (event.type === 'rerouted') { throttledCallback({ type: 'rerouted', data: event.data }); } else if (event.type === 'arrived') { throttledCallback({ type: 'arrived', data: event.data }); } else if (event.type === 'error') { throttledCallback({ type: 'error', data: event.data }); } else if (event.type === 'deviation') { throttledCallback({ type: 'deviation', data: event.data }); } else if (event.type === 'speed_warning') { throttledCallback({ type: 'speed_warning', data: event.data }); } else if (event.type === 'tunnel_entered') { throttledCallback({ type: 'tunnel_entered', data: event.data }); } else if (event.type === 'tunnel_exited') { throttledCallback({ type: 'tunnel_exited', data: event.data }); } else if (event.type === 'gps_lost') { throttledCallback({ type: 'gps_lost', data: event.data }); } }), ]; return () => { unsubscribeFunctions.forEach(unsubscribe => { if (typeof unsubscribe === 'function') { unsubscribe(); } }); }; }, [throttledCallback]); }; exports.useNavigationEvents = useNavigationEvents; const useNavigationEventType = (eventType, callback, options = {}) => { const wrappedCallback = (0, react_1.useCallback)((event) => { if (event.type === eventType) { callback(event.data); } }, [eventType, callback]); (0, exports.useNavigationEvents)(wrappedCallback, { ...options, eventTypes: [eventType], }); }; exports.useNavigationEventType = useNavigationEventType; const useNavigationProgress = (callback, throttleMs = 1000) => { (0, exports.useNavigationEventType)('progress', callback, { throttleMs }); }; exports.useNavigationProgress = useNavigationProgress; const useManeuverEvents = (callback) => { (0, exports.useNavigationEventType)('maneuver', callback); }; exports.useManeuverEvents = useManeuverEvents; const useVoiceEvents = (callback) => { (0, exports.useNavigationEventType)('voice', callback); }; exports.useVoiceEvents = useVoiceEvents; const useNavigationErrors = (callback) => { (0, exports.useNavigationEventType)('error', callback); }; exports.useNavigationErrors = useNavigationErrors; const useArrivalEvents = (callback) => { (0, exports.useNavigationEventType)('arrived', callback); }; exports.useArrivalEvents = useArrivalEvents; const useReroutingEvents = (callback) => { (0, exports.useNavigationEventType)('rerouted', callback); }; exports.useReroutingEvents = useReroutingEvents; const useNavigationEventLogger = (enabled = __DEV__) => { (0, exports.useNavigationEvents)((0, react_1.useCallback)(event => { if (enabled) { const timestamp = new Date().toISOString(); console.warn(`[${timestamp}] Navigation Event:`, { type: event.type, data: event.data, }); } }, [enabled]), { debug: enabled }); }; exports.useNavigationEventLogger = useNavigationEventLogger; const useNavigationEventStats = () => { const eventCountsRef = (0, react_1.useRef)({}); const lastEventTimeRef = (0, react_1.useRef)({}); (0, exports.useNavigationEvents)((0, react_1.useCallback)(event => { const eventType = event.type; const now = Date.now(); eventCountsRef.current[eventType] = (eventCountsRef.current[eventType] || 0) + 1; lastEventTimeRef.current[eventType] = now; }, [])); const getEventStats = (0, react_1.useCallback)(() => { return { eventCounts: { ...eventCountsRef.current }, lastEventTimes: { ...lastEventTimeRef.current }, totalEvents: Object.values(eventCountsRef.current).reduce((sum, count) => sum + count, 0), }; }, []); const resetStats = (0, react_1.useCallback)(() => { eventCountsRef.current = {}; lastEventTimeRef.current = {}; }, []); return { getEventStats, resetStats, }; }; exports.useNavigationEventStats = useNavigationEventStats;