|
| 1 | +import { useLogger } from '@libs/composables'; |
| 2 | +import { |
| 3 | + clearPrewarmedResources, |
| 4 | + prewarm, |
| 5 | + setMaxParallelImageRequests, |
| 6 | + setWorkerCount, |
| 7 | +} from 'maplibre-gl'; |
| 8 | +import { onUnmounted } from 'vue'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Configuration options for MapLibre GL performance optimization |
| 12 | + */ |
| 13 | +export interface MaplibreConfigOptions { |
| 14 | + /** Number of web workers for tile loading (default: 4) */ |
| 15 | + workerCount?: number; |
| 16 | + /** Maximum parallel image requests (default: 16) */ |
| 17 | + maxParallelImageRequests?: number; |
| 18 | + /** Whether to prewarm MapLibre resources on initialization (default: true) */ |
| 19 | + prewarmResources?: boolean; |
| 20 | + /** Enable debug logging */ |
| 21 | + debug?: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Actions returned by useMaplibreConfig |
| 26 | + */ |
| 27 | +export interface MaplibreConfigActions { |
| 28 | + /** Clears prewarmed resources - call on app unmount to release memory */ |
| 29 | + clearPrewarmedResources: () => void; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Composable for configuring MapLibre GL global performance settings. |
| 34 | + * |
| 35 | + * This should be called once at the application level (e.g., in App.vue or main.ts) |
| 36 | + * to optimize MapLibre performance across all map instances. |
| 37 | + * |
| 38 | + * @example |
| 39 | + * ```vue |
| 40 | + * <script setup> |
| 41 | + * import { useMaplibreConfig } from 'vue3-maplibre-gl'; |
| 42 | + * |
| 43 | + * // Initialize MapLibre with optimized settings |
| 44 | + * useMaplibreConfig({ |
| 45 | + * workerCount: 4, |
| 46 | + * maxParallelImageRequests: 16, |
| 47 | + * prewarmResources: true, |
| 48 | + * }); |
| 49 | + * </script> |
| 50 | + * ``` |
| 51 | + * |
| 52 | + * @param options - Configuration options for MapLibre performance |
| 53 | + * @returns Actions for managing MapLibre configuration |
| 54 | + */ |
| 55 | +export function useMaplibreConfig( |
| 56 | + options: MaplibreConfigOptions = {}, |
| 57 | +): MaplibreConfigActions { |
| 58 | + const { |
| 59 | + workerCount = 4, |
| 60 | + maxParallelImageRequests = 16, |
| 61 | + prewarmResources = true, |
| 62 | + debug = false, |
| 63 | + } = options; |
| 64 | + |
| 65 | + const { log, logError } = useLogger(debug); |
| 66 | + |
| 67 | + try { |
| 68 | + // Configure web worker count for parallel tile loading |
| 69 | + setWorkerCount(workerCount); |
| 70 | + log('MapLibre worker count set to:', workerCount); |
| 71 | + |
| 72 | + // Configure maximum parallel image requests |
| 73 | + setMaxParallelImageRequests(maxParallelImageRequests); |
| 74 | + log( |
| 75 | + 'MapLibre max parallel image requests set to:', |
| 76 | + maxParallelImageRequests, |
| 77 | + ); |
| 78 | + |
| 79 | + // Prewarm MapLibre resources for faster initial map render |
| 80 | + if (prewarmResources) { |
| 81 | + prewarm(); |
| 82 | + log('MapLibre resources prewarmed'); |
| 83 | + } |
| 84 | + } catch (error) { |
| 85 | + logError('Error configuring MapLibre:', error); |
| 86 | + } |
| 87 | + |
| 88 | + // Cleanup prewarmed resources on unmount |
| 89 | + onUnmounted(() => { |
| 90 | + try { |
| 91 | + clearPrewarmedResources(); |
| 92 | + log('MapLibre prewarmed resources cleared'); |
| 93 | + } catch (error) { |
| 94 | + logError('Error clearing prewarmed resources:', error); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + return { |
| 99 | + clearPrewarmedResources, |
| 100 | + }; |
| 101 | +} |
0 commit comments