Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
213 lines (192 loc) · 7.41 KB
/
Copy pathmain.js
File metadata and controls
213 lines (192 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { createApp, shallowRef } from "vue";
import App from "./App.vue";
import "./assets/sass/theme.scss";
import { emitter } from "./emitter.js";
import { initLocaleCache } from "./composables/useLocale.js";
import { getMapInstance } from "./composables/useMap.js";
import { usePersistedState as _usePersistedState } from "./resilience/persistedState.js";
import { useSettings as _useSettings } from "./composables/useSettings.js";
import { useLocale as _useLocale } from "./composables/useLocale.js";
import { setupServiceWorker } from "./utils/serviceWorker.js";
import {
installGlobalErrorHandlers,
installVueErrorHandler,
reportError,
} from "./resilience/errors.js";
import { installFeature } from "./resilience/boot.js";
import { RoutesFeature } from "./features/routes/index.js";
// Auth features — commented out: the app is currently in SPA front-end only mode
// (no backend). Re-enable when the API/auth backend is available.
// import { AccountFeature } from "./features/account/index.js";
// import { MapsFeature } from "./features/maps/index.js";
// import { CollectionsFeature } from "./features/collections/index.js";
const boot = () => {
// Global handlers go in first, so a failure anywhere in boot is contained.
installGlobalErrorHandlers();
// --- Instance config ---
// The instance ID scopes storage keys, enabling iframe isolation.
// Override via ?id= URL param (e.g. an embedded iframe with id="trip-planner").
const params = new URLSearchParams(window.location.search);
const instanceId =
(params.get("id") || "").replace(/[^a-zA-Z0-9_-]/g, "").slice(0, 64) ||
"app";
const locale = params.get("locale") || null;
// --- App setup ---
initLocaleCache(instanceId, locale, {});
const app = createApp(App);
installVueErrorHandler(app);
app.provide("ogisAppId", instanceId);
app.provide("navigatorLocale", locale);
app.provide("navigatorMessages", {});
// Reactive button and panel registries — features register into these.
const buttonsRef = shallowRef([]);
const panelsRef = shallowRef([]);
app.provide("navigatorButtons", buttonsRef);
app.provide("navigatorPanels", panelsRef);
// Reactive map ref — null until MapLibre emits map:ready.
const mapRef = shallowRef(null);
emitter.once("map:ready", ({ map }) => {
mapRef.value = map;
});
// Auto-cleanup tracking for feature-added map sources/layers and for
// feature-owned side effects registered through `featureCtx.onCleanup`.
const trackedSources = [];
const trackedLayers = [];
const cleanups = [];
emitter.on("destroy", () => {
mapRef.value = null;
// Feature cleanups run first, newest first, so a feature can release its
// own listeners or timers before the shared map teardown below. Each
// entry is deactivated before it runs, so a remover cannot re-run it.
for (const entry of [...cleanups].reverse()) {
if (!entry.active) continue;
entry.active = false;
try {
entry.fn();
} catch (error) {
reportError(error, { source: "feature-cleanup" });
}
}
cleanups.length = 0;
const map = getMapInstance(instanceId);
if (map) {
for (const layerId of [...trackedLayers].reverse()) {
if (map.getLayer(layerId)) map.removeLayer(layerId);
}
for (const sourceId of [...trackedSources].reverse()) {
if (map.getSource(sourceId)) map.removeSource(sourceId);
}
}
});
const removeConfig = (ref, config) => () => {
ref.value = ref.value.filter((item) => item !== config);
};
// --- Internal feature context ---
// Passed to each feature's install() method. Provides scoped access to
// app services without exposing the raw Vue app or emitter. Registration
// methods return a disposer so installFeature can roll a failure back.
// `onCleanup` likewise returns an idempotent remover, and the registered
// callbacks run in reverse order on app destroy.
const featureCtx = {
app,
instanceId,
map: mapRef,
emitter,
useStorage: (namespace, definition) =>
_usePersistedState(namespace, definition, instanceId),
useSettings: () => _useSettings(),
useLocale: () => _useLocale(instanceId),
getMap: () => getMapInstance(instanceId),
onMapReady: (callback) => {
const listener = ({ map }) => {
try {
callback({
map,
addSource: (sourceId, options) => {
map.addSource(sourceId, options);
trackedSources.push(sourceId);
},
addLayer: (layerConfig, before) => {
map.addLayer(layerConfig, before);
trackedLayers.push(layerConfig.id);
},
});
} catch (error) {
reportError(error, { source: "map-ready" });
}
};
emitter.once("map:ready", listener);
return () => emitter.off("map:ready", listener);
},
on: (e, f) => {
emitter.on(e, f);
return () => emitter.off(e, f);
},
once: (e, f) => {
emitter.once(e, f);
return () => emitter.off(e, f);
},
off: (e, f) => emitter.off(e, f),
emit: (e, ...a) => emitter.emit(e, ...a),
provide: (key, value) => app.provide(key, value),
addButton: (config) => {
buttonsRef.value = [...buttonsRef.value, config];
return removeConfig(buttonsRef, config);
},
addPanel: (config) => {
panelsRef.value = [...panelsRef.value, config];
return removeConfig(panelsRef, config);
},
/**
* Register a feature-owned cleanup for app destroy.
*
* Use it for watchers, listeners and timers the feature owns. Shared
* composables (`useGeoJSON`, `useLocate`, `useActions`) are app-wide and
* must not be disposed from a single feature.
*
* @param {Function} fn - Cleanup to run once, newest first, on destroy.
* @returns {Function} Idempotent remover that does not run the cleanup.
*/
onCleanup: (fn) => {
if (typeof fn !== "function") {
throw new TypeError("onCleanup() requires a function");
}
const entry = { fn, active: true };
cleanups.push(entry);
return () => {
if (!entry.active) return;
entry.active = false;
const index = cleanups.indexOf(entry);
if (index !== -1) cleanups.splice(index, 1);
};
},
};
// --- Install core features ---
// Each feature is isolated: one failing install is reported and rolled back
// without preventing the others from installing or the app from mounting.
installFeature("routes", RoutesFeature, featureCtx);
// Auth features — commented out (SPA front-end only mode).
// installFeature("account", AccountFeature, featureCtx);
// installFeature("maps", MapsFeature, featureCtx);
// installFeature("collections", CollectionsFeature, featureCtx);
// --- Mount ---
app.mount("#app");
// --- Service worker ---
// Registers the app-shell worker in production (or when opted in with
// VITE_SW=1). In dev any legacy registration is unregistered and all origin
// caches are purged so poisoned profiles self-heal. See src/utils/serviceWorker.js.
// A worker failure must never turn a successful mount into a boot fallback.
try {
setupServiceWorker()?.catch?.((error) =>
reportError(error, { source: "service-worker" }),
);
} catch (error) {
reportError(error, { source: "service-worker" });
}
};
try {
boot();
} catch (error) {
reportError(error, { source: "boot" });
window.__ogisBoot?.fail?.(error);
}