@wovvmap/navigation 0.0.0-stage → 1.2.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 +45 -2
- package/dist/endpoints/EndpointLookupPort.d.ts +16 -0
- package/dist/endpoints/EndpointLookupPort.d.ts.map +1 -0
- package/dist/endpoints/EndpointLookupPort.js +1 -0
- package/dist/endpoints/EndpointService.d.ts +49 -0
- package/dist/endpoints/EndpointService.d.ts.map +1 -0
- package/dist/endpoints/EndpointService.js +278 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/routes/RoutePlayback.d.ts +68 -0
- package/dist/routes/RoutePlayback.d.ts.map +1 -0
- package/dist/routes/RoutePlayback.js +771 -0
- package/dist/routes/RouteRestore.d.ts +24 -0
- package/dist/routes/RouteRestore.d.ts.map +1 -0
- package/dist/routes/RouteRestore.js +1 -0
- package/dist/routes/RouteService.d.ts +121 -0
- package/dist/routes/RouteService.d.ts.map +1 -0
- package/dist/routes/RouteService.js +1123 -0
- package/dist/routes/navigationConfig.d.ts +2 -0
- package/dist/routes/navigationConfig.d.ts.map +1 -0
- package/dist/routes/navigationConfig.js +3 -0
- package/package.json +33 -3
|
@@ -0,0 +1,1123 @@
|
|
|
1
|
+
import { DEFAULT_NAVIGATION_CONFIG, freezeRouteState, ViewerCommandError, samePreparedMapIdentity, } from '@wovvmap/contracts';
|
|
2
|
+
import { resolveNavigationConfig } from './navigationConfig.js';
|
|
3
|
+
import { RoutePlayback } from './RoutePlayback.js';
|
|
4
|
+
function fail(code) {
|
|
5
|
+
throw new ViewerCommandError('Route operation could not be completed.', { code });
|
|
6
|
+
}
|
|
7
|
+
// Cancellation settles callers even when an adapter ignores AbortSignal. Late resources are disposed.
|
|
8
|
+
// Hinglish: Slow/bad adapter ke bawajood Clear ka caller atke nahi; late GPU resources leak na hon.
|
|
9
|
+
function waiting(operation, signal, late) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
let ended = false;
|
|
12
|
+
const abort = () => {
|
|
13
|
+
if (!ended) {
|
|
14
|
+
ended = true;
|
|
15
|
+
reject(new ViewerCommandError('Route cancelled.', { code: 'CANCELLED' }));
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
19
|
+
if (signal.aborted)
|
|
20
|
+
abort();
|
|
21
|
+
operation.then((value) => {
|
|
22
|
+
signal.removeEventListener('abort', abort);
|
|
23
|
+
if (ended) {
|
|
24
|
+
late?.(value);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
ended = true;
|
|
28
|
+
resolve(value);
|
|
29
|
+
}, (error) => {
|
|
30
|
+
signal.removeEventListener('abort', abort);
|
|
31
|
+
if (!ended) {
|
|
32
|
+
ended = true;
|
|
33
|
+
reject(error);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** One writable owner per rendering viewer. React and WebView invoke these same transitions.
|
|
39
|
+
* Hinglish: Host/store sirf projection hain; pending work, selection aur activation ka owner yahi hai. */
|
|
40
|
+
export class RouteService {
|
|
41
|
+
ports;
|
|
42
|
+
state = Object.freeze({
|
|
43
|
+
stage: 'idle',
|
|
44
|
+
config: DEFAULT_NAVIGATION_CONFIG,
|
|
45
|
+
map: null,
|
|
46
|
+
routeSet: null,
|
|
47
|
+
selectedRouteId: null,
|
|
48
|
+
activeRouteId: null,
|
|
49
|
+
});
|
|
50
|
+
work;
|
|
51
|
+
restoreWork;
|
|
52
|
+
provider;
|
|
53
|
+
savedRoute;
|
|
54
|
+
render;
|
|
55
|
+
acceptedIntent;
|
|
56
|
+
expiresAt = 0;
|
|
57
|
+
expiry;
|
|
58
|
+
configurationRequest = 0;
|
|
59
|
+
destroyed = false;
|
|
60
|
+
stepWork;
|
|
61
|
+
// At most two accepted route counts; lazy steps fill this same metadata cache on the first read.
|
|
62
|
+
// Hinglish: Sirf chhote counts hain, doosra cursor/path store nahi; steps-off geometry ka zero unknown hai.
|
|
63
|
+
stepTotals = new Map();
|
|
64
|
+
stepReads = new Map();
|
|
65
|
+
// Runtime-only render artifacts are bounded and never projected into host/UI state.
|
|
66
|
+
// Hinglish: Loaded step ka camera data reuse ho; Native/public store ko full instruction nahi mile.
|
|
67
|
+
stepArtifacts = new Map();
|
|
68
|
+
playback;
|
|
69
|
+
playbackWork;
|
|
70
|
+
constructor(ports) {
|
|
71
|
+
this.ports = ports;
|
|
72
|
+
this.provider = ports;
|
|
73
|
+
this.playback = new RoutePlayback(() => this.publish(this.state), ports.now, () => {
|
|
74
|
+
// Background timer throttling cannot make an expired accepted route animate on wake.
|
|
75
|
+
// Hinglish: Frame par sirf expiry check; source/NodePoint scan ya full summary rebuild nahi.
|
|
76
|
+
if (Date.now() >= this.expiresAt) {
|
|
77
|
+
this.reconcile();
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
}, () => this.ports.progressed?.(this.getPlaybackProgress()));
|
|
82
|
+
}
|
|
83
|
+
getPlaybackProgress() {
|
|
84
|
+
const frame = this.playback.progressSnapshot;
|
|
85
|
+
return frame && this.state.routeSet && this.state.selectedRouteId
|
|
86
|
+
? Object.freeze({
|
|
87
|
+
...frame,
|
|
88
|
+
routeSetId: this.state.routeSet.routeSetId,
|
|
89
|
+
routeId: this.state.selectedRouteId,
|
|
90
|
+
})
|
|
91
|
+
: null;
|
|
92
|
+
}
|
|
93
|
+
beforeCameraChange() {
|
|
94
|
+
this.playback.beforeCameraChange();
|
|
95
|
+
}
|
|
96
|
+
updateRoutePlaybackOptions(options) {
|
|
97
|
+
if (this.destroyed)
|
|
98
|
+
fail('VIEWER_DESTROYED');
|
|
99
|
+
this.playback.update(options);
|
|
100
|
+
return this.state;
|
|
101
|
+
}
|
|
102
|
+
playbackSelection(selection, context) {
|
|
103
|
+
const accepted = this.selection(selection, context);
|
|
104
|
+
if (accepted.state.selectedRouteId !== accepted.routeId)
|
|
105
|
+
fail('ROUTE_NOT_SELECTED');
|
|
106
|
+
if (accepted.state.stage !== 'guiding')
|
|
107
|
+
fail('ROUTE_NOT_STARTED');
|
|
108
|
+
if (accepted.state.config.simulation === 'off')
|
|
109
|
+
fail('PLAYBACK_DISABLED');
|
|
110
|
+
return accepted;
|
|
111
|
+
}
|
|
112
|
+
play(selection, kind, context) {
|
|
113
|
+
const { routeId } = this.playbackSelection(selection, context);
|
|
114
|
+
const owned = { routeSetId: this.state.routeSet.routeSetId, routeId };
|
|
115
|
+
const ownedContext = context
|
|
116
|
+
? { mallId: context.mallId, expectedRevision: context.expectedRevision }
|
|
117
|
+
: undefined;
|
|
118
|
+
const binding = this.render?.playback?.(routeId);
|
|
119
|
+
if (!binding) {
|
|
120
|
+
this.playback.unavailable();
|
|
121
|
+
fail('UNAVAILABLE');
|
|
122
|
+
}
|
|
123
|
+
const render = this.render;
|
|
124
|
+
const readyBinding = {
|
|
125
|
+
...binding,
|
|
126
|
+
options: (options, running) => binding.options?.({ ...options, stepSync: this.state.config.steps === 'off' ? 'off' : options.stepSync }, running),
|
|
127
|
+
activeStep: (scope, distance) => this.state.config.steps === 'off' ? null : (binding.activeStep?.(scope, distance) ?? null),
|
|
128
|
+
ready: async (options, signal) => {
|
|
129
|
+
await binding.ready?.(options, signal);
|
|
130
|
+
if (signal?.aborted || render !== this.render)
|
|
131
|
+
fail('CANCELLED');
|
|
132
|
+
if (options?.stepSync !== 'highlight' || this.state.config.steps === 'off')
|
|
133
|
+
return;
|
|
134
|
+
if (render?.hasPlaybackSteps?.(routeId))
|
|
135
|
+
return;
|
|
136
|
+
if (!this.provider.playbackSteps || !render?.setPlaybackSteps || !signal)
|
|
137
|
+
fail('UNAVAILABLE');
|
|
138
|
+
const value = await this.provider.playbackSteps(owned.routeSetId, routeId, this.state.routeSet.source, signal);
|
|
139
|
+
if (signal.aborted || render !== this.render)
|
|
140
|
+
fail('CANCELLED');
|
|
141
|
+
if (value.routeSetId !== owned.routeSetId ||
|
|
142
|
+
value.routeId !== routeId ||
|
|
143
|
+
value.expiresAt <= Date.now())
|
|
144
|
+
fail('INVALID_ROUTE_RESPONSE');
|
|
145
|
+
render.setPlaybackSteps(routeId, value);
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
const work = this.playback.start(readyBinding, kind, () => {
|
|
149
|
+
this.playbackSelection(owned, ownedContext);
|
|
150
|
+
this.cancelStep();
|
|
151
|
+
this.render?.highlightStep?.(routeId, null);
|
|
152
|
+
if (this.state.selectedStep)
|
|
153
|
+
this.state = freezeRouteState({ ...this.state, selectedStep: null });
|
|
154
|
+
});
|
|
155
|
+
if (this.playbackWork?.work === work)
|
|
156
|
+
return this.playbackWork.result;
|
|
157
|
+
const entry = { work, result: Promise.resolve(this.state) };
|
|
158
|
+
entry.result = work
|
|
159
|
+
.then(() => this.state)
|
|
160
|
+
.finally(() => {
|
|
161
|
+
if (this.playbackWork === entry)
|
|
162
|
+
this.playbackWork = undefined;
|
|
163
|
+
});
|
|
164
|
+
this.playbackWork = entry;
|
|
165
|
+
return entry.result;
|
|
166
|
+
}
|
|
167
|
+
startRoutePlayback(selection, context) {
|
|
168
|
+
return this.play(selection, 'play', context);
|
|
169
|
+
}
|
|
170
|
+
resumeRoutePlayback(selection, context) {
|
|
171
|
+
return this.play(selection, 'resume', context);
|
|
172
|
+
}
|
|
173
|
+
replayRoutePlayback(selection, context) {
|
|
174
|
+
return this.play(selection, 'replay', context);
|
|
175
|
+
}
|
|
176
|
+
pauseRoutePlayback(selection, context) {
|
|
177
|
+
this.playbackSelection(selection, context);
|
|
178
|
+
this.playback.pause();
|
|
179
|
+
return this.state;
|
|
180
|
+
}
|
|
181
|
+
stopRoutePlayback(selection, context) {
|
|
182
|
+
this.playbackSelection(selection, context);
|
|
183
|
+
this.playback.stop();
|
|
184
|
+
return this.state;
|
|
185
|
+
}
|
|
186
|
+
/** Called before an explicit floor replacement, never by a React floor watcher.
|
|
187
|
+
* Hinglish: User floor badle to cursor wahi pause ho; Resume hidden floor jump na kare. */
|
|
188
|
+
beforeFloorChange() {
|
|
189
|
+
this.playback.pause();
|
|
190
|
+
}
|
|
191
|
+
getRoute() {
|
|
192
|
+
if (this.acceptedIntent !== undefined)
|
|
193
|
+
this.reconcile();
|
|
194
|
+
return this.state;
|
|
195
|
+
}
|
|
196
|
+
/** Compact accepted provenance for saved-route orchestration; no display buffers leave the owner.
|
|
197
|
+
* Hinglish: Preview adoption ke event se pehle saved ID ready ho; late ACK ko lease ID na samjhein. */
|
|
198
|
+
getSavedRouteSummary() {
|
|
199
|
+
return this.savedRoute ?? null;
|
|
200
|
+
}
|
|
201
|
+
/** Storage invalidation retires this approval; a different pending restore keeps its own lease guards.
|
|
202
|
+
* Hinglish: Deleted saved ID ka path mite; naya compatible Restore bina wajah cancel na ho. */
|
|
203
|
+
invalidateSavedRoute(id) {
|
|
204
|
+
if (this.savedRoute?.savedRouteId === id)
|
|
205
|
+
this.resetRoute(true);
|
|
206
|
+
}
|
|
207
|
+
/** Explicit manual intent; a superseding selection cancels only the older step/camera work.
|
|
208
|
+
* Hinglish: Start ke baad interactive row se own floor/focus chale; late ACK nayi selection na badle. */
|
|
209
|
+
selectRouteStep(selection, context) {
|
|
210
|
+
const { state, routeId } = this.manualSelection(selection, context);
|
|
211
|
+
const stepIndex = selection.stepIndex;
|
|
212
|
+
if (!Number.isSafeInteger(stepIndex) || stepIndex < 0)
|
|
213
|
+
fail('INVALID_REQUEST');
|
|
214
|
+
const total = this.stepTotals.get(routeId);
|
|
215
|
+
if (total !== undefined && stepIndex >= total)
|
|
216
|
+
fail('INVALID_REQUEST');
|
|
217
|
+
if (!this.provider.steps || !this.render?.focusStep || !this.render.highlightStep)
|
|
218
|
+
fail('UNAVAILABLE');
|
|
219
|
+
const routeSetId = state.routeSet.routeSetId;
|
|
220
|
+
const key = JSON.stringify([routeSetId, routeId, stepIndex]);
|
|
221
|
+
if (this.stepWork?.key === key)
|
|
222
|
+
return this.stepWork.promise;
|
|
223
|
+
const render = this.render, intent = this.acceptedIntent, previousStep = this.stepWork;
|
|
224
|
+
// Capture the presentation intent before Pause can notify an observer with a newer command.
|
|
225
|
+
// Hinglish: Pause callback Clear/step/zoom kare to old request use cancel ya refocus na kare.
|
|
226
|
+
const presentationCurrent = render.beginStep?.();
|
|
227
|
+
this.playback.pause();
|
|
228
|
+
if (render !== this.render ||
|
|
229
|
+
intent !== this.acceptedIntent ||
|
|
230
|
+
previousStep !== this.stepWork ||
|
|
231
|
+
this.state.stage !== 'guiding' ||
|
|
232
|
+
this.state.config.steps !== 'interactive' ||
|
|
233
|
+
(presentationCurrent && !presentationCurrent()))
|
|
234
|
+
fail('CANCELLED');
|
|
235
|
+
this.cancelStep();
|
|
236
|
+
// Aborting the previous read may itself accept another step or clear the route.
|
|
237
|
+
// Hinglish: Abort callback ka naya step slot khaali samajh kar overwrite mat karo.
|
|
238
|
+
if (this.stepWork ||
|
|
239
|
+
render !== this.render ||
|
|
240
|
+
intent !== this.acceptedIntent ||
|
|
241
|
+
this.state.stage !== 'guiding' ||
|
|
242
|
+
this.state.config.steps !== 'interactive' ||
|
|
243
|
+
(presentationCurrent && !presentationCurrent()))
|
|
244
|
+
fail('CANCELLED');
|
|
245
|
+
const controller = new AbortController();
|
|
246
|
+
const entry = { key, controller, promise: Promise.resolve(state) };
|
|
247
|
+
this.stepWork = entry;
|
|
248
|
+
const check = () => {
|
|
249
|
+
this.reconcile();
|
|
250
|
+
if (controller.signal.aborted ||
|
|
251
|
+
this.stepWork !== entry ||
|
|
252
|
+
this.render !== render ||
|
|
253
|
+
this.acceptedIntent !== intent ||
|
|
254
|
+
this.state.stage !== 'guiding' ||
|
|
255
|
+
this.state.selectedRouteId !== routeId ||
|
|
256
|
+
this.state.config.steps !== 'interactive')
|
|
257
|
+
fail('CANCELLED');
|
|
258
|
+
};
|
|
259
|
+
entry.promise = Promise.resolve()
|
|
260
|
+
.then(async () => {
|
|
261
|
+
check();
|
|
262
|
+
// One bounded lookup reuses the authenticated steps delivery, never a new endpoint or route solve.
|
|
263
|
+
// Hinglish: Exact index ka ek step padhein; poori list/NodePoint scan ya route Generate nahi.
|
|
264
|
+
const artifactKey = JSON.stringify([routeSetId, routeId, stepIndex]);
|
|
265
|
+
let cached = this.stepArtifacts.get(artifactKey);
|
|
266
|
+
if (!cached) {
|
|
267
|
+
// At most eight current-route display pages: join a covering read without aborting its other consumers.
|
|
268
|
+
// Hinglish: Pehla page pending ho to wahi await karein; NodePoint scan/duplicate HTTP nahi.
|
|
269
|
+
const pending = [...this.stepReads.values()].find((read) => stepIndex >= read.offset && stepIndex < read.offset + read.limit);
|
|
270
|
+
if (pending) {
|
|
271
|
+
await waiting(pending.promise, controller.signal);
|
|
272
|
+
check();
|
|
273
|
+
cached = this.stepArtifacts.get(artifactKey);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const page = cached && cached.expiresAt > Date.now()
|
|
277
|
+
? {
|
|
278
|
+
routeSetId,
|
|
279
|
+
routeId,
|
|
280
|
+
offset: stepIndex,
|
|
281
|
+
limit: 1,
|
|
282
|
+
total: this.stepTotals.get(routeId),
|
|
283
|
+
expiresAt: cached.expiresAt,
|
|
284
|
+
nextOffset: null,
|
|
285
|
+
items: [cached.step],
|
|
286
|
+
}
|
|
287
|
+
: await waiting(this.provider.steps({ routeSetId, routeId, offset: stepIndex, limit: 1 }, state.routeSet.source, controller.signal), controller.signal);
|
|
288
|
+
check();
|
|
289
|
+
const step = page.items[0];
|
|
290
|
+
if (page.total <= stepIndex)
|
|
291
|
+
fail('INVALID_REQUEST');
|
|
292
|
+
if (presentationCurrent && !presentationCurrent())
|
|
293
|
+
fail('CANCELLED');
|
|
294
|
+
if (page.routeSetId !== routeSetId ||
|
|
295
|
+
page.routeId !== routeId ||
|
|
296
|
+
page.offset !== stepIndex ||
|
|
297
|
+
page.limit !== 1 ||
|
|
298
|
+
page.expiresAt <= Date.now() ||
|
|
299
|
+
!step ||
|
|
300
|
+
!step.focusBounds ||
|
|
301
|
+
!state.routeSet.options
|
|
302
|
+
.find((option) => option.routeId === routeId)
|
|
303
|
+
.floorIndices.includes(step.floorIndex))
|
|
304
|
+
fail('INVALID_ROUTE_RESPONSE');
|
|
305
|
+
this.stepTotals.set(routeId, page.total);
|
|
306
|
+
if (!cached || cached.expiresAt <= Date.now())
|
|
307
|
+
this.rememberStep(page, stepIndex, step);
|
|
308
|
+
await waiting(render.focusStep(routeId, step, controller.signal), controller.signal);
|
|
309
|
+
check();
|
|
310
|
+
render.highlightStep(routeId, step);
|
|
311
|
+
return this.publish({ ...this.state, selectedStep: { stepIndex, stepId: step.id } });
|
|
312
|
+
})
|
|
313
|
+
.finally(() => {
|
|
314
|
+
if (this.stepWork === entry)
|
|
315
|
+
this.stepWork = undefined;
|
|
316
|
+
});
|
|
317
|
+
return entry.promise;
|
|
318
|
+
}
|
|
319
|
+
manualSelection(selection, context) {
|
|
320
|
+
const { state, routeId } = this.selection(selection, context);
|
|
321
|
+
if (state.selectedRouteId !== routeId)
|
|
322
|
+
fail('ROUTE_NOT_SELECTED');
|
|
323
|
+
if (state.stage !== 'guiding')
|
|
324
|
+
fail('ROUTE_NOT_STARTED');
|
|
325
|
+
if (state.config.steps !== 'interactive')
|
|
326
|
+
fail('STEPS_DISABLED');
|
|
327
|
+
return { state, routeId };
|
|
328
|
+
}
|
|
329
|
+
/** Relative navigation reads the authoritative cursor, never a delayed host projection.
|
|
330
|
+
* Hinglish: Previous sirf instruction inspect karta hai; reverse walking permission/backend policy nahi badalti. */
|
|
331
|
+
nextRouteStep(selection, context) {
|
|
332
|
+
return this.moveStep(selection, 1, context);
|
|
333
|
+
}
|
|
334
|
+
previousRouteStep(selection, context) {
|
|
335
|
+
return this.moveStep(selection, -1, context);
|
|
336
|
+
}
|
|
337
|
+
moveStep(selection, direction, context) {
|
|
338
|
+
const { state, routeId } = this.manualSelection(selection, context);
|
|
339
|
+
const index = (state.selectedStep?.stepIndex ?? -1) + direction;
|
|
340
|
+
const total = this.stepTotals.get(routeId);
|
|
341
|
+
if (index < 0 || (total !== undefined && index >= total))
|
|
342
|
+
return Promise.resolve(state);
|
|
343
|
+
return this.selectRouteStep({ ...selection, stepIndex: index }, context);
|
|
344
|
+
}
|
|
345
|
+
/** Finish inspects the full selected route again, without Stop/Clear, a solve or an endpoint write.
|
|
346
|
+
* Hinglish: Same cancellation slot use ho; late Finish nayi row/floor/camera ko overwrite na kare. */
|
|
347
|
+
finishRouteSteps(selection, context) {
|
|
348
|
+
const { state, routeId } = this.manualSelection(selection, context);
|
|
349
|
+
const key = JSON.stringify([state.routeSet.routeSetId, routeId, 'finish']);
|
|
350
|
+
if (this.stepWork?.key === key)
|
|
351
|
+
return this.stepWork.promise;
|
|
352
|
+
if (!this.stepWork && !state.selectedStep)
|
|
353
|
+
return Promise.resolve(state);
|
|
354
|
+
const render = this.render, intent = this.acceptedIntent, previousStep = this.stepWork;
|
|
355
|
+
if (!render.highlightStep)
|
|
356
|
+
fail('UNAVAILABLE');
|
|
357
|
+
const presentationCurrent = render.beginStep?.();
|
|
358
|
+
const current = () => render === this.render &&
|
|
359
|
+
intent === this.acceptedIntent &&
|
|
360
|
+
this.state.stage === 'guiding' &&
|
|
361
|
+
this.state.config.steps === 'interactive';
|
|
362
|
+
// Capture before Pause/abort observers can replace the accepted target or step slot.
|
|
363
|
+
// Hinglish: Finish cleanup ka naya Clear/step hi jeete; disposed renderer se overview na chale.
|
|
364
|
+
this.playback.pause();
|
|
365
|
+
if (!current() || previousStep !== this.stepWork)
|
|
366
|
+
fail('CANCELLED');
|
|
367
|
+
this.cancelStep();
|
|
368
|
+
if (!current() || this.stepWork)
|
|
369
|
+
fail('CANCELLED');
|
|
370
|
+
const controller = new AbortController();
|
|
371
|
+
const entry = { key, controller, promise: Promise.resolve(state) };
|
|
372
|
+
this.stepWork = entry;
|
|
373
|
+
entry.promise = Promise.resolve()
|
|
374
|
+
.then(async () => {
|
|
375
|
+
this.reconcile();
|
|
376
|
+
if (controller.signal.aborted ||
|
|
377
|
+
this.stepWork !== entry ||
|
|
378
|
+
(presentationCurrent && !presentationCurrent()))
|
|
379
|
+
fail('CANCELLED');
|
|
380
|
+
await waiting(render.focus(routeId, controller.signal), controller.signal);
|
|
381
|
+
this.reconcile();
|
|
382
|
+
if (controller.signal.aborted ||
|
|
383
|
+
this.stepWork !== entry ||
|
|
384
|
+
this.render !== render ||
|
|
385
|
+
this.acceptedIntent !== intent ||
|
|
386
|
+
this.state.stage !== 'guiding' ||
|
|
387
|
+
this.state.selectedRouteId !== routeId ||
|
|
388
|
+
this.state.config.steps !== 'interactive')
|
|
389
|
+
fail('CANCELLED');
|
|
390
|
+
render.highlightStep(routeId, null);
|
|
391
|
+
return this.publish({ ...this.state, selectedStep: null });
|
|
392
|
+
})
|
|
393
|
+
.finally(() => {
|
|
394
|
+
if (this.stepWork === entry)
|
|
395
|
+
this.stepWork = undefined;
|
|
396
|
+
});
|
|
397
|
+
return entry.promise;
|
|
398
|
+
}
|
|
399
|
+
cancelStep() {
|
|
400
|
+
const step = this.stepWork;
|
|
401
|
+
this.stepWork = undefined;
|
|
402
|
+
step?.controller.abort();
|
|
403
|
+
}
|
|
404
|
+
/** Read bounded display data for the current selection; no graph, buffers or UI store are returned.
|
|
405
|
+
* Hinglish: Direct aur Native isi query ko use karein; same page dedupe ho aur late response reject ho. */
|
|
406
|
+
getRouteSteps(request, context) {
|
|
407
|
+
this.context(context);
|
|
408
|
+
this.reconcile();
|
|
409
|
+
const state = this.state, set = state.routeSet;
|
|
410
|
+
if (!set || set.routeSetId !== request.routeSetId || state.selectedRouteId !== request.routeId)
|
|
411
|
+
fail('STALE_ROUTE');
|
|
412
|
+
if (state.config.steps === 'off')
|
|
413
|
+
fail('STEPS_DISABLED');
|
|
414
|
+
const offset = request.offset ?? 0, limit = request.limit ?? 20;
|
|
415
|
+
if (!Number.isSafeInteger(offset) ||
|
|
416
|
+
offset < 0 ||
|
|
417
|
+
!Number.isSafeInteger(limit) ||
|
|
418
|
+
limit < 1 ||
|
|
419
|
+
limit > 50)
|
|
420
|
+
fail('INVALID_REQUEST');
|
|
421
|
+
const key = JSON.stringify([request.routeSetId, request.routeId, offset, limit]);
|
|
422
|
+
const cached = this.stepReads.get(key);
|
|
423
|
+
if (cached)
|
|
424
|
+
return cached.promise;
|
|
425
|
+
const read = this.provider.steps;
|
|
426
|
+
if (!read)
|
|
427
|
+
fail('UNAVAILABLE');
|
|
428
|
+
const controller = new AbortController();
|
|
429
|
+
const intent = this.acceptedIntent;
|
|
430
|
+
const owned = { routeSetId: request.routeSetId, routeId: request.routeId, offset, limit };
|
|
431
|
+
const promise = Promise.resolve()
|
|
432
|
+
.then(async () => {
|
|
433
|
+
if (controller.signal.aborted)
|
|
434
|
+
fail('CANCELLED');
|
|
435
|
+
const page = await waiting(read(owned, set.source, controller.signal), controller.signal);
|
|
436
|
+
this.reconcile();
|
|
437
|
+
if (controller.signal.aborted ||
|
|
438
|
+
intent !== this.acceptedIntent ||
|
|
439
|
+
this.state.selectedRouteId !== owned.routeId ||
|
|
440
|
+
this.state.config.steps === 'off')
|
|
441
|
+
fail('CANCELLED');
|
|
442
|
+
if (page.routeSetId !== owned.routeSetId ||
|
|
443
|
+
page.routeId !== owned.routeId ||
|
|
444
|
+
page.offset !== offset ||
|
|
445
|
+
page.limit !== limit ||
|
|
446
|
+
page.expiresAt <= Date.now())
|
|
447
|
+
fail('INVALID_ROUTE_RESPONSE');
|
|
448
|
+
this.stepTotals.set(owned.routeId, page.total);
|
|
449
|
+
return Object.freeze({
|
|
450
|
+
routeSetId: page.routeSetId,
|
|
451
|
+
routeId: page.routeId,
|
|
452
|
+
offset: page.offset,
|
|
453
|
+
limit: page.limit,
|
|
454
|
+
total: page.total,
|
|
455
|
+
nextOffset: page.nextOffset,
|
|
456
|
+
expiresAt: page.expiresAt,
|
|
457
|
+
items: Object.freeze(page.items.map((step, index) => {
|
|
458
|
+
this.rememberStep(page, offset + index, step);
|
|
459
|
+
return Object.freeze({
|
|
460
|
+
id: step.id,
|
|
461
|
+
action: step.action,
|
|
462
|
+
icon: step.icon,
|
|
463
|
+
instruction: step.instruction,
|
|
464
|
+
floorIndex: step.floorIndex,
|
|
465
|
+
floorName: step.floorName ?? `Floor ${step.floorIndex}`,
|
|
466
|
+
distanceMeters: step.distanceMeters,
|
|
467
|
+
estimatedDurationSeconds: step.estimatedDurationSeconds,
|
|
468
|
+
distanceText: step.distanceText,
|
|
469
|
+
timeText: step.timeText,
|
|
470
|
+
});
|
|
471
|
+
})),
|
|
472
|
+
});
|
|
473
|
+
})
|
|
474
|
+
.catch((error) => {
|
|
475
|
+
if (this.stepReads.get(key)?.controller === controller)
|
|
476
|
+
this.stepReads.delete(key);
|
|
477
|
+
throw error;
|
|
478
|
+
});
|
|
479
|
+
// Keep at most eight compact pages; the displayed UI owns only its loaded presentation rows.
|
|
480
|
+
// Hinglish: Shared read cache bounded hai; poori instructions har route event mein nahi jaati.
|
|
481
|
+
if (this.stepReads.size >= 8) {
|
|
482
|
+
const oldest = this.stepReads.keys().next().value;
|
|
483
|
+
this.stepReads.get(oldest).controller.abort();
|
|
484
|
+
this.stepReads.delete(oldest);
|
|
485
|
+
}
|
|
486
|
+
this.stepReads.set(key, { controller, offset, limit, promise });
|
|
487
|
+
return promise;
|
|
488
|
+
}
|
|
489
|
+
rememberStep(page, index, step) {
|
|
490
|
+
const key = JSON.stringify([page.routeSetId, page.routeId, index]);
|
|
491
|
+
if (this.stepArtifacts.has(key))
|
|
492
|
+
this.stepArtifacts.delete(key);
|
|
493
|
+
else if (this.stepArtifacts.size >= 400)
|
|
494
|
+
this.stepArtifacts.delete(this.stepArtifacts.keys().next().value);
|
|
495
|
+
this.stepArtifacts.set(key, { step: structuredClone(step), expiresAt: page.expiresAt });
|
|
496
|
+
}
|
|
497
|
+
clearStepReads() {
|
|
498
|
+
this.stepArtifacts.clear();
|
|
499
|
+
const reads = [...this.stepReads.values()];
|
|
500
|
+
this.stepReads.clear();
|
|
501
|
+
for (const read of reads)
|
|
502
|
+
read.controller.abort();
|
|
503
|
+
}
|
|
504
|
+
publish(state) {
|
|
505
|
+
const playback = this.playback.snapshot;
|
|
506
|
+
this.state = freezeRouteState({
|
|
507
|
+
...state,
|
|
508
|
+
playback: state.config.simulation !== 'off' ||
|
|
509
|
+
playback.mode !== 'full-route' ||
|
|
510
|
+
playback.speed !== 1 ||
|
|
511
|
+
playback.startDelayMs !== undefined ||
|
|
512
|
+
playback.finishView !== undefined ||
|
|
513
|
+
playback.finishDelayMs !== undefined ||
|
|
514
|
+
state.playback
|
|
515
|
+
? playback
|
|
516
|
+
: undefined,
|
|
517
|
+
});
|
|
518
|
+
this.ports.changed(this.state);
|
|
519
|
+
return this.state;
|
|
520
|
+
}
|
|
521
|
+
context(context) {
|
|
522
|
+
if (this.destroyed)
|
|
523
|
+
fail('VIEWER_DESTROYED');
|
|
524
|
+
const current = this.ports.context();
|
|
525
|
+
if (!current)
|
|
526
|
+
fail('MAP_NOT_LOADED');
|
|
527
|
+
if (context?.mallId !== undefined && context.mallId !== current.endpoints.mallId)
|
|
528
|
+
fail('MALL_MISMATCH');
|
|
529
|
+
if (context?.expectedRevision !== undefined &&
|
|
530
|
+
context.expectedRevision !== current.endpoints.revision)
|
|
531
|
+
fail('STALE_REVISION');
|
|
532
|
+
return current;
|
|
533
|
+
}
|
|
534
|
+
intent() {
|
|
535
|
+
const current = this.ports.context();
|
|
536
|
+
return current
|
|
537
|
+
? JSON.stringify([
|
|
538
|
+
current.source,
|
|
539
|
+
current.endpoints.mallId,
|
|
540
|
+
current.endpoints.revision,
|
|
541
|
+
current.endpoints.origin,
|
|
542
|
+
current.endpoints.destination,
|
|
543
|
+
])
|
|
544
|
+
: undefined;
|
|
545
|
+
}
|
|
546
|
+
cancel() {
|
|
547
|
+
const work = this.work;
|
|
548
|
+
this.work = undefined;
|
|
549
|
+
this.cancelStep();
|
|
550
|
+
work?.controller.abort();
|
|
551
|
+
}
|
|
552
|
+
cancelRestore() {
|
|
553
|
+
const work = this.restoreWork;
|
|
554
|
+
this.restoreWork = undefined;
|
|
555
|
+
work?.controller.abort();
|
|
556
|
+
}
|
|
557
|
+
retire(preserveRestore = false) {
|
|
558
|
+
const request = ++this.configurationRequest;
|
|
559
|
+
const restore = preserveRestore ? undefined : this.restoreWork;
|
|
560
|
+
if (!preserveRestore)
|
|
561
|
+
this.restoreWork = undefined;
|
|
562
|
+
this.provider = this.ports;
|
|
563
|
+
this.savedRoute = undefined;
|
|
564
|
+
const render = this.render, work = this.work, step = this.stepWork;
|
|
565
|
+
const reads = [...this.stepReads.values()];
|
|
566
|
+
// Detach old ownership before cleanup can accept a newer command through an observer.
|
|
567
|
+
// Hinglish: Purane resources pehle alag hon; cleanup callback ka naya route/config na mite.
|
|
568
|
+
this.render = undefined;
|
|
569
|
+
this.work = undefined;
|
|
570
|
+
this.stepWork = undefined;
|
|
571
|
+
this.stepTotals.clear();
|
|
572
|
+
this.stepArtifacts.clear();
|
|
573
|
+
this.stepReads.clear();
|
|
574
|
+
clearTimeout(this.expiry);
|
|
575
|
+
this.expiry = undefined;
|
|
576
|
+
this.acceptedIntent = undefined;
|
|
577
|
+
this.expiresAt = 0;
|
|
578
|
+
if (this.state.stage !== 'idle')
|
|
579
|
+
this.state = freezeRouteState({
|
|
580
|
+
stage: 'idle',
|
|
581
|
+
config: this.state.config,
|
|
582
|
+
map: this.state.map,
|
|
583
|
+
routeSet: null,
|
|
584
|
+
selectedRouteId: null,
|
|
585
|
+
activeRouteId: null,
|
|
586
|
+
});
|
|
587
|
+
this.playback.retire();
|
|
588
|
+
restore?.controller.abort();
|
|
589
|
+
step?.controller.abort();
|
|
590
|
+
for (const read of reads)
|
|
591
|
+
read.controller.abort();
|
|
592
|
+
work?.controller.abort();
|
|
593
|
+
render?.dispose();
|
|
594
|
+
return request;
|
|
595
|
+
}
|
|
596
|
+
/** Long approved replay lifetimes may exceed the browser's maximum single timeout.
|
|
597
|
+
* Hinglish: Timer cap ke baad baaki wait re-arm ho; retired timer newer route ko touch na kare. */
|
|
598
|
+
scheduleExpiry() {
|
|
599
|
+
clearTimeout(this.expiry);
|
|
600
|
+
const deadline = this.expiresAt;
|
|
601
|
+
const timer = setTimeout(() => {
|
|
602
|
+
if (this.expiry !== timer)
|
|
603
|
+
return;
|
|
604
|
+
this.expiry = undefined;
|
|
605
|
+
this.reconcile();
|
|
606
|
+
if (this.expiry === undefined &&
|
|
607
|
+
this.acceptedIntent !== undefined &&
|
|
608
|
+
this.expiresAt === deadline &&
|
|
609
|
+
Date.now() < deadline)
|
|
610
|
+
this.scheduleExpiry();
|
|
611
|
+
}, Math.min(Math.max(deadline - Date.now(), 0), 2_147_483_647));
|
|
612
|
+
this.expiry = timer;
|
|
613
|
+
}
|
|
614
|
+
/** Called by existing explicit endpoint/map actions, not React dependency watchers.
|
|
615
|
+
* Hinglish: Changed scope/source purana set retire kare; manual floor/theme route ko na badlein. */
|
|
616
|
+
reconcile() {
|
|
617
|
+
if (this.destroyed)
|
|
618
|
+
return;
|
|
619
|
+
if (this.restoreWork && this.restoreWork.intent !== this.intent())
|
|
620
|
+
this.cancelRestore();
|
|
621
|
+
const map = this.ports.context()?.endpoints;
|
|
622
|
+
const expired = this.acceptedIntent !== undefined && Date.now() >= this.expiresAt;
|
|
623
|
+
const sameIntent = this.acceptedIntent === this.intent();
|
|
624
|
+
const currentProvider = this.provider.isCurrent?.() !== false;
|
|
625
|
+
if ((this.state.stage === 'idle' &&
|
|
626
|
+
(this.state.map?.mallId !== map?.mallId || this.state.map?.revision !== map?.revision)) ||
|
|
627
|
+
(this.acceptedIntent !== undefined && (!sameIntent || expired || !currentProvider)))
|
|
628
|
+
// Expiry belongs to the displayed approval, not to a separate valid replacement candidate.
|
|
629
|
+
// Hinglish: Sirf old approval expire ho to compatible pending Restore bache; explicit Clear sab cancel kare.
|
|
630
|
+
this.resetRoute(expired && sameIntent && currentProvider);
|
|
631
|
+
}
|
|
632
|
+
configureNavigation(input) {
|
|
633
|
+
if (this.destroyed)
|
|
634
|
+
fail('VIEWER_DESTROYED');
|
|
635
|
+
const request = ++this.configurationRequest;
|
|
636
|
+
const config = resolveNavigationConfig(input, this.state.config);
|
|
637
|
+
if (config === this.state.config)
|
|
638
|
+
return this.state;
|
|
639
|
+
this.cancelRestore();
|
|
640
|
+
const retire = config.routes !== this.state.config.routes ||
|
|
641
|
+
config.activation !== this.state.config.activation;
|
|
642
|
+
if (!retire && this.render && config.pathAnimation !== this.state.config.pathAnimation)
|
|
643
|
+
this.applyAnimation(this.render, config.pathAnimation);
|
|
644
|
+
if (config.steps !== 'interactive') {
|
|
645
|
+
this.cancelStep();
|
|
646
|
+
if (this.state.selectedStep && this.state.selectedRouteId)
|
|
647
|
+
this.render?.highlightStep?.(this.state.selectedRouteId, null);
|
|
648
|
+
}
|
|
649
|
+
if (config.steps !== this.state.config.steps && this.playback.snapshot.stepSync === 'highlight')
|
|
650
|
+
this.playback.pause();
|
|
651
|
+
if (request !== this.configurationRequest)
|
|
652
|
+
return this.state;
|
|
653
|
+
if (config.steps === 'off') {
|
|
654
|
+
this.clearStepReads();
|
|
655
|
+
if (this.state.selectedRouteId)
|
|
656
|
+
this.render?.highlightStep?.(this.state.selectedRouteId, null);
|
|
657
|
+
}
|
|
658
|
+
if (config.simulation === 'off')
|
|
659
|
+
this.playback.retire();
|
|
660
|
+
// Renderer/playback cleanup can synchronously accept a newer capability choice.
|
|
661
|
+
// Hinglish: Cleanup callback ki latest config jeete; old off request baad mein commit na ho.
|
|
662
|
+
if (request !== this.configurationRequest)
|
|
663
|
+
return this.state;
|
|
664
|
+
if (retire) {
|
|
665
|
+
const retired = this.retire();
|
|
666
|
+
if (retired !== this.configurationRequest)
|
|
667
|
+
return this.state;
|
|
668
|
+
return this.publish({
|
|
669
|
+
stage: 'idle',
|
|
670
|
+
config,
|
|
671
|
+
map: this.state.map,
|
|
672
|
+
routeSet: null,
|
|
673
|
+
selectedRouteId: null,
|
|
674
|
+
activeRouteId: null,
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
// Presentation capabilities never reroute or move the camera.
|
|
678
|
+
// Hinglish: Steps/arrows/playback settings se private path dobara nahi banta.
|
|
679
|
+
return this.publish({
|
|
680
|
+
...this.state,
|
|
681
|
+
config,
|
|
682
|
+
...(config.steps !== 'interactive' ? { selectedStep: null } : {}),
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
/** Optional renderer capability, using the same accepted configuration in both transports.
|
|
686
|
+
* Hinglish: Static adapter off mode support kare; moving request silently ignore nahi ho. */
|
|
687
|
+
applyAnimation(render, mode) {
|
|
688
|
+
if (mode === 'moving-arrows' && !render.setPathAnimation)
|
|
689
|
+
fail('UNAVAILABLE');
|
|
690
|
+
render.setPathAnimation?.(mode);
|
|
691
|
+
}
|
|
692
|
+
/** Internal provider entry; public command registration belongs to the host integration checkpoint.
|
|
693
|
+
* Hinglish: Verified saved data wahi camera/steps/playback use kare; restore kabhi auto Start/Play nahi. */
|
|
694
|
+
async restoreSavedRoute(id, context, abort) {
|
|
695
|
+
if (abort?.aborted)
|
|
696
|
+
fail('CANCELLED');
|
|
697
|
+
const { source } = this.context(context);
|
|
698
|
+
if (!this.ports.restore)
|
|
699
|
+
fail('OFFLINE_ROUTE_UNSUPPORTED');
|
|
700
|
+
if (typeof id !== 'string' || !id || id.length > 512)
|
|
701
|
+
fail('INVALID_REQUEST');
|
|
702
|
+
const intent = this.intent(), config = this.state.config;
|
|
703
|
+
const configFields = Object.keys(config);
|
|
704
|
+
const configurationChanged = () => configFields.some((field) => this.state.config[field] !== config[field]);
|
|
705
|
+
this.cancelRestore();
|
|
706
|
+
if (this.restoreWork)
|
|
707
|
+
fail('CANCELLED');
|
|
708
|
+
if (this.state.stage === 'generating')
|
|
709
|
+
this.clearRoute();
|
|
710
|
+
else if (this.state.stage === 'starting')
|
|
711
|
+
this.stopRoute();
|
|
712
|
+
if (this.restoreWork || this.work || this.intent() !== intent || configurationChanged())
|
|
713
|
+
fail('CANCELLED');
|
|
714
|
+
const entry = { controller: new AbortController(), intent };
|
|
715
|
+
this.restoreWork = entry;
|
|
716
|
+
const cancelled = () => {
|
|
717
|
+
if (this.restoreWork === entry)
|
|
718
|
+
this.cancelRestore();
|
|
719
|
+
};
|
|
720
|
+
abort?.addEventListener('abort', cancelled, { once: true });
|
|
721
|
+
const signal = entry.controller.signal;
|
|
722
|
+
const check = () => {
|
|
723
|
+
if (signal.aborted ||
|
|
724
|
+
this.restoreWork !== entry ||
|
|
725
|
+
this.destroyed ||
|
|
726
|
+
configurationChanged() ||
|
|
727
|
+
this.intent() !== entry.intent)
|
|
728
|
+
fail('CANCELLED');
|
|
729
|
+
};
|
|
730
|
+
let candidate;
|
|
731
|
+
try {
|
|
732
|
+
const saved = await waiting(this.ports.restore(id, signal), signal);
|
|
733
|
+
check();
|
|
734
|
+
const selected = saved.routeSet.options.find((option) => option.routeId === saved.selectedRouteId);
|
|
735
|
+
if (!selected || !samePreparedMapIdentity(saved.routeSet.source, source))
|
|
736
|
+
fail('OFFLINE_ROUTE_STALE');
|
|
737
|
+
const eligible = () => {
|
|
738
|
+
if (!Number.isSafeInteger(saved.validUntil) || saved.validUntil <= Date.now())
|
|
739
|
+
fail('OFFLINE_ROUTE_EXPIRED');
|
|
740
|
+
};
|
|
741
|
+
eligible();
|
|
742
|
+
// Current host capabilities win; a stored alternative never enables a disabled presentation feature.
|
|
743
|
+
// Hinglish: Signed set unchanged rahe; single UI ko sirf captured selected option dikhayein.
|
|
744
|
+
const set = config.routes === 'single'
|
|
745
|
+
? {
|
|
746
|
+
...saved.routeSet,
|
|
747
|
+
recommendedRouteId: selected.routeId,
|
|
748
|
+
options: [selected],
|
|
749
|
+
}
|
|
750
|
+
: saved.routeSet;
|
|
751
|
+
const items = saved.geometry.filter((item) => set.options.some((option) => option.routeId === item.routeId));
|
|
752
|
+
if (set.options.length < 1 ||
|
|
753
|
+
set.options.length > 2 ||
|
|
754
|
+
items.length !== set.options.length ||
|
|
755
|
+
new Set(items.map((item) => item.routeId)).size !== items.length ||
|
|
756
|
+
items.some((item) => item.routeSetId !== set.routeSetId ||
|
|
757
|
+
item.expiresAt !== saved.validUntil ||
|
|
758
|
+
!samePreparedMapIdentity(item.artifact.source, source)))
|
|
759
|
+
fail('OFFLINE_ROUTE_INVALID');
|
|
760
|
+
await waiting(saved.check(signal), signal);
|
|
761
|
+
check();
|
|
762
|
+
candidate = await waiting(this.ports.prepare(items, signal), signal, (resource) => resource.dispose());
|
|
763
|
+
check();
|
|
764
|
+
eligible();
|
|
765
|
+
this.applyAnimation(candidate, config.pathAnimation);
|
|
766
|
+
await waiting(candidate.focus(selected.routeId, signal, true), signal);
|
|
767
|
+
check();
|
|
768
|
+
await waiting(saved.check(signal), signal);
|
|
769
|
+
check();
|
|
770
|
+
eligible();
|
|
771
|
+
candidate.show(selected.routeId, false);
|
|
772
|
+
check();
|
|
773
|
+
const ready = candidate;
|
|
774
|
+
// Retire before changing endpoints: cleanup callbacks may accept a newer Restore/Generate.
|
|
775
|
+
// Hinglish: Old cleanup naya intent de to pair abhi unchanged rahe; old restore use undo na kare.
|
|
776
|
+
this.restoreWork = undefined;
|
|
777
|
+
const request = this.retire();
|
|
778
|
+
if (request !== this.configurationRequest ||
|
|
779
|
+
signal.aborted ||
|
|
780
|
+
this.destroyed ||
|
|
781
|
+
this.restoreWork ||
|
|
782
|
+
this.intent() !== entry.intent)
|
|
783
|
+
fail('CANCELLED');
|
|
784
|
+
let committed;
|
|
785
|
+
saved.bind((endpoints) => {
|
|
786
|
+
if (signal.aborted ||
|
|
787
|
+
this.restoreWork ||
|
|
788
|
+
request !== this.configurationRequest ||
|
|
789
|
+
this.destroyed ||
|
|
790
|
+
this.ports.context()?.endpoints !== endpoints)
|
|
791
|
+
fail('CANCELLED');
|
|
792
|
+
eligible();
|
|
793
|
+
// Endpoint owner has installed the complete pair, but has not notified subscribers yet.
|
|
794
|
+
// Hinglish: New pair ke notification se pehle route adopt ho; reconcile purana intent na dekhe.
|
|
795
|
+
this.provider = saved.provider;
|
|
796
|
+
this.savedRoute = saved.savedRoute;
|
|
797
|
+
this.render = ready;
|
|
798
|
+
candidate = undefined;
|
|
799
|
+
this.acceptedIntent = this.intent();
|
|
800
|
+
this.expiresAt = saved.validUntil;
|
|
801
|
+
for (const item of items)
|
|
802
|
+
this.stepTotals.set(item.routeId, item.artifact.stepCount);
|
|
803
|
+
this.scheduleExpiry();
|
|
804
|
+
committed = this.publish({
|
|
805
|
+
stage: 'preview',
|
|
806
|
+
config,
|
|
807
|
+
map: { mallId: endpoints.mallId, revision: endpoints.revision },
|
|
808
|
+
routeSet: set,
|
|
809
|
+
selectedRouteId: selected.routeId,
|
|
810
|
+
activeRouteId: null,
|
|
811
|
+
selectedStep: null,
|
|
812
|
+
});
|
|
813
|
+
});
|
|
814
|
+
if (!committed ||
|
|
815
|
+
this.state !== committed ||
|
|
816
|
+
committed.stage !== 'preview' ||
|
|
817
|
+
this.acceptedIntent !== this.intent())
|
|
818
|
+
fail('CANCELLED');
|
|
819
|
+
return committed;
|
|
820
|
+
}
|
|
821
|
+
finally {
|
|
822
|
+
abort?.removeEventListener('abort', cancelled);
|
|
823
|
+
candidate?.dispose();
|
|
824
|
+
if (this.restoreWork === entry)
|
|
825
|
+
this.restoreWork = undefined;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
generateRoute(preferences, context) {
|
|
829
|
+
const { source, endpoints } = this.context(context);
|
|
830
|
+
if (!endpoints.origin || !endpoints.destination)
|
|
831
|
+
fail('INVALID_ENDPOINT');
|
|
832
|
+
if (endpoints.conflict)
|
|
833
|
+
fail(endpoints.conflict);
|
|
834
|
+
const intent = this.intent();
|
|
835
|
+
const config = this.state.config;
|
|
836
|
+
// Own only compact request data before yielding; caller mutation cannot alter an in-flight solve.
|
|
837
|
+
// Hinglish: Chhote request ko own karo; geometry/node arrays ka clone ya scan nahi.
|
|
838
|
+
const request = JSON.parse(JSON.stringify({
|
|
839
|
+
source,
|
|
840
|
+
viewerRevision: endpoints.revision,
|
|
841
|
+
origin: endpoints.origin.reference,
|
|
842
|
+
destination: endpoints.destination.reference,
|
|
843
|
+
preferences,
|
|
844
|
+
config,
|
|
845
|
+
}));
|
|
846
|
+
const key = JSON.stringify(request);
|
|
847
|
+
if (this.state.stage === 'generating' && this.work?.key === key)
|
|
848
|
+
return this.work.promise;
|
|
849
|
+
const retired = this.retire();
|
|
850
|
+
if (retired !== this.configurationRequest)
|
|
851
|
+
fail('CANCELLED');
|
|
852
|
+
const controller = new AbortController();
|
|
853
|
+
const entry = { controller, key, promise: Promise.resolve(this.state) };
|
|
854
|
+
this.work = entry;
|
|
855
|
+
this.acceptedIntent = intent;
|
|
856
|
+
this.expiresAt = Number.POSITIVE_INFINITY;
|
|
857
|
+
const map = Object.freeze({ mallId: endpoints.mallId, revision: endpoints.revision });
|
|
858
|
+
const check = () => {
|
|
859
|
+
if (controller.signal.aborted ||
|
|
860
|
+
this.work !== entry ||
|
|
861
|
+
this.intent() !== intent ||
|
|
862
|
+
this.destroyed)
|
|
863
|
+
fail('CANCELLED');
|
|
864
|
+
};
|
|
865
|
+
const operation = async () => {
|
|
866
|
+
let candidate;
|
|
867
|
+
try {
|
|
868
|
+
check();
|
|
869
|
+
const response = await waiting(this.ports.generate(request, controller.signal), controller.signal);
|
|
870
|
+
check();
|
|
871
|
+
const set = response.routeSet;
|
|
872
|
+
const returnedConfig = resolveNavigationConfig(response.config);
|
|
873
|
+
if (Object.keys(config).some((key) => config[key] !== returnedConfig[key]))
|
|
874
|
+
fail('INVALID_ROUTE_RESPONSE');
|
|
875
|
+
if (!samePreparedMapIdentity(set.source, source) ||
|
|
876
|
+
response.viewerRevision !== endpoints.revision ||
|
|
877
|
+
response.expiresAt <= Date.now() ||
|
|
878
|
+
set.options.length < 1 ||
|
|
879
|
+
set.options.length > (config.routes === 'single' ? 1 : 2) ||
|
|
880
|
+
!set.options.some((option) => option.routeId === set.recommendedRouteId) ||
|
|
881
|
+
new Set(set.options.map((option) => option.routeId)).size !== set.options.length)
|
|
882
|
+
fail('INVALID_ROUTE_RESPONSE');
|
|
883
|
+
const items = await waiting(Promise.all(set.options.map((option) => this.ports.geometry(set.routeSetId, option.routeId, source, controller.signal))), controller.signal);
|
|
884
|
+
check();
|
|
885
|
+
const expiresAt = Math.min(response.expiresAt, ...items.map((item) => item.expiresAt));
|
|
886
|
+
if (expiresAt <= Date.now() ||
|
|
887
|
+
items.some((item, index) => item.routeSetId !== set.routeSetId ||
|
|
888
|
+
item.routeId !== set.options[index].routeId ||
|
|
889
|
+
!samePreparedMapIdentity(item.artifact.source, source)))
|
|
890
|
+
fail('INVALID_ROUTE_RESPONSE');
|
|
891
|
+
candidate = await waiting(this.ports.prepare(items, controller.signal), controller.signal, (resource) => resource.dispose());
|
|
892
|
+
check();
|
|
893
|
+
this.applyAnimation(candidate, this.state.config.pathAnimation);
|
|
894
|
+
await waiting(candidate.focus(set.recommendedRouteId, controller.signal, true), controller.signal);
|
|
895
|
+
check();
|
|
896
|
+
if (expiresAt <= Date.now())
|
|
897
|
+
fail('ROUTE_EXPIRED');
|
|
898
|
+
this.applyAnimation(candidate, this.state.config.pathAnimation);
|
|
899
|
+
candidate.show(set.recommendedRouteId, false);
|
|
900
|
+
this.render = candidate;
|
|
901
|
+
for (const item of items) {
|
|
902
|
+
if (item.artifact.stepsPrepared)
|
|
903
|
+
this.stepTotals.set(item.routeId, item.artifact.stepCount);
|
|
904
|
+
}
|
|
905
|
+
candidate = undefined;
|
|
906
|
+
this.expiresAt = expiresAt;
|
|
907
|
+
this.scheduleExpiry();
|
|
908
|
+
this.work = undefined;
|
|
909
|
+
this.publish({
|
|
910
|
+
stage: 'preview',
|
|
911
|
+
config: this.state.config,
|
|
912
|
+
map,
|
|
913
|
+
routeSet: set,
|
|
914
|
+
selectedRouteId: set.recommendedRouteId,
|
|
915
|
+
activeRouteId: null,
|
|
916
|
+
});
|
|
917
|
+
// Automatic single mode uses the exact same Start checks; alternatives always remain manual.
|
|
918
|
+
// Hinglish: Compatibility auto mode bhi wahi Start owner use kare; bypass nahi.
|
|
919
|
+
if (config.activation === 'automatic') {
|
|
920
|
+
if (this.acceptedIntent !== intent || this.state.routeSet?.routeSetId !== set.routeSetId)
|
|
921
|
+
fail('CANCELLED');
|
|
922
|
+
return await this.startRoute({ routeSetId: set.routeSetId, routeId: set.recommendedRouteId }, { mallId: map.mallId, expectedRevision: map.revision });
|
|
923
|
+
}
|
|
924
|
+
return this.state;
|
|
925
|
+
}
|
|
926
|
+
catch (error) {
|
|
927
|
+
candidate?.dispose();
|
|
928
|
+
if (this.work === entry) {
|
|
929
|
+
const retired = this.retire();
|
|
930
|
+
const code = error instanceof ViewerCommandError ? (error.code ?? 'UNAVAILABLE') : 'UNAVAILABLE';
|
|
931
|
+
if (retired === this.configurationRequest)
|
|
932
|
+
this.publish({
|
|
933
|
+
stage: 'failed',
|
|
934
|
+
config: this.state.config,
|
|
935
|
+
map,
|
|
936
|
+
routeSet: null,
|
|
937
|
+
selectedRouteId: null,
|
|
938
|
+
activeRouteId: null,
|
|
939
|
+
error: Object.freeze({ code, message: 'Route could not be prepared.' }),
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
throw error;
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
entry.promise = Promise.resolve().then(operation);
|
|
946
|
+
this.publish({
|
|
947
|
+
stage: 'generating',
|
|
948
|
+
config,
|
|
949
|
+
map,
|
|
950
|
+
routeSet: null,
|
|
951
|
+
selectedRouteId: null,
|
|
952
|
+
activeRouteId: null,
|
|
953
|
+
});
|
|
954
|
+
return entry.promise;
|
|
955
|
+
}
|
|
956
|
+
selection(selection, context) {
|
|
957
|
+
// Capture primitive IDs before callbacks/awaits; the caller may reuse a mutable object.
|
|
958
|
+
// Hinglish: Caller choice baad mein badle tab bhi validated selected/active pair same rahe.
|
|
959
|
+
const routeSetId = selection?.routeSetId, routeId = selection?.routeId;
|
|
960
|
+
this.context(context);
|
|
961
|
+
this.reconcile();
|
|
962
|
+
const state = this.state;
|
|
963
|
+
if (!state.routeSet ||
|
|
964
|
+
routeSetId !== state.routeSet.routeSetId ||
|
|
965
|
+
!state.routeSet.options.some((option) => option.routeId === routeId))
|
|
966
|
+
fail('STALE_ROUTE');
|
|
967
|
+
this.cancelRestore();
|
|
968
|
+
return { state: state, routeId };
|
|
969
|
+
}
|
|
970
|
+
selectRoute(selection, context) {
|
|
971
|
+
const { state, routeId } = this.selection(selection, context);
|
|
972
|
+
if (state.selectedRouteId === routeId)
|
|
973
|
+
return state;
|
|
974
|
+
if (state.stage !== 'preview')
|
|
975
|
+
fail('ROUTE_BUSY');
|
|
976
|
+
this.clearStepReads();
|
|
977
|
+
this.render.show(routeId, false);
|
|
978
|
+
return this.publish({ ...state, selectedRouteId: routeId });
|
|
979
|
+
}
|
|
980
|
+
startRoute(selection, context) {
|
|
981
|
+
const { state, routeId } = this.selection(selection, context);
|
|
982
|
+
if (state.selectedRouteId !== routeId)
|
|
983
|
+
fail('ROUTE_NOT_SELECTED');
|
|
984
|
+
if (state.stage === 'guiding')
|
|
985
|
+
return Promise.resolve(state);
|
|
986
|
+
if (state.stage === 'starting')
|
|
987
|
+
return this.work.promise;
|
|
988
|
+
if (state.stage !== 'preview')
|
|
989
|
+
fail('ROUTE_BUSY');
|
|
990
|
+
const controller = new AbortController();
|
|
991
|
+
const entry = { controller, key: 'start', promise: Promise.resolve(this.state) };
|
|
992
|
+
this.work = entry;
|
|
993
|
+
entry.promise = Promise.resolve().then(async () => {
|
|
994
|
+
try {
|
|
995
|
+
if (controller.signal.aborted || this.work !== entry)
|
|
996
|
+
fail('CANCELLED');
|
|
997
|
+
await waiting(this.render.focus(routeId, controller.signal), controller.signal);
|
|
998
|
+
if (controller.signal.aborted || this.work !== entry)
|
|
999
|
+
fail('CANCELLED');
|
|
1000
|
+
this.reconcile();
|
|
1001
|
+
if (this.work !== entry)
|
|
1002
|
+
fail('CANCELLED');
|
|
1003
|
+
this.render.show(routeId, true);
|
|
1004
|
+
this.work = undefined;
|
|
1005
|
+
const guiding = this.publish({
|
|
1006
|
+
...state,
|
|
1007
|
+
config: this.state.config,
|
|
1008
|
+
stage: 'guiding',
|
|
1009
|
+
activeRouteId: routeId,
|
|
1010
|
+
});
|
|
1011
|
+
if (guiding.config.simulation === 'on-start') {
|
|
1012
|
+
try {
|
|
1013
|
+
await this.startRoutePlayback({ routeSetId: state.routeSet.routeSetId, routeId }, { mallId: state.map.mallId, expectedRevision: state.map.revision });
|
|
1014
|
+
}
|
|
1015
|
+
catch (error) {
|
|
1016
|
+
if (!(error instanceof ViewerCommandError) || error.code !== 'UNAVAILABLE')
|
|
1017
|
+
throw error;
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
return this.state;
|
|
1021
|
+
}
|
|
1022
|
+
catch (error) {
|
|
1023
|
+
if (this.work === entry) {
|
|
1024
|
+
this.cancel();
|
|
1025
|
+
this.publish({
|
|
1026
|
+
...state,
|
|
1027
|
+
config: this.state.config,
|
|
1028
|
+
stage: 'preview',
|
|
1029
|
+
activeRouteId: null,
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
throw error;
|
|
1033
|
+
}
|
|
1034
|
+
});
|
|
1035
|
+
this.publish({ ...state, stage: 'starting' });
|
|
1036
|
+
return entry.promise;
|
|
1037
|
+
}
|
|
1038
|
+
stopRoute(context) {
|
|
1039
|
+
if (this.destroyed)
|
|
1040
|
+
fail('VIEWER_DESTROYED');
|
|
1041
|
+
if (context)
|
|
1042
|
+
this.context(context);
|
|
1043
|
+
this.cancelRestore();
|
|
1044
|
+
this.reconcile();
|
|
1045
|
+
const state = this.state;
|
|
1046
|
+
if (state.stage !== 'starting' && state.stage !== 'guiding')
|
|
1047
|
+
return state;
|
|
1048
|
+
const request = this.configurationRequest, render = this.render;
|
|
1049
|
+
// Every cleanup callback may accept a newer command; never touch its renderer or publish old state.
|
|
1050
|
+
// Hinglish: Stop ka abort/release/highlight callback naya intent de to purana Stop wahi ruk jaye.
|
|
1051
|
+
this.cancel();
|
|
1052
|
+
if (request !== this.configurationRequest)
|
|
1053
|
+
return this.state;
|
|
1054
|
+
this.playback.retire();
|
|
1055
|
+
if (request !== this.configurationRequest)
|
|
1056
|
+
return this.state;
|
|
1057
|
+
render.highlightStep?.(state.selectedRouteId, null);
|
|
1058
|
+
if (request !== this.configurationRequest)
|
|
1059
|
+
return this.state;
|
|
1060
|
+
render.show(state.selectedRouteId, false);
|
|
1061
|
+
if (request !== this.configurationRequest)
|
|
1062
|
+
return this.state;
|
|
1063
|
+
return this.publish({
|
|
1064
|
+
...state,
|
|
1065
|
+
stage: 'preview',
|
|
1066
|
+
activeRouteId: null,
|
|
1067
|
+
selectedStep: null,
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
clearRoute(context) {
|
|
1071
|
+
if (this.destroyed)
|
|
1072
|
+
fail('VIEWER_DESTROYED');
|
|
1073
|
+
if (context)
|
|
1074
|
+
this.context(context);
|
|
1075
|
+
return this.resetRoute();
|
|
1076
|
+
}
|
|
1077
|
+
resetRoute(preserveRestore = false) {
|
|
1078
|
+
const map = this.ports.context()?.endpoints;
|
|
1079
|
+
const previous = this.state;
|
|
1080
|
+
const retired = this.retire(preserveRestore);
|
|
1081
|
+
if (retired !== this.configurationRequest)
|
|
1082
|
+
return this.state;
|
|
1083
|
+
const currentMap = map ? Object.freeze({ mallId: map.mallId, revision: map.revision }) : null;
|
|
1084
|
+
if (previous.stage === 'idle' &&
|
|
1085
|
+
previous.map?.mallId === currentMap?.mallId &&
|
|
1086
|
+
previous.map?.revision === currentMap?.revision)
|
|
1087
|
+
return this.state;
|
|
1088
|
+
return this.publish({
|
|
1089
|
+
stage: 'idle',
|
|
1090
|
+
config: this.state.config,
|
|
1091
|
+
map: currentMap,
|
|
1092
|
+
routeSet: null,
|
|
1093
|
+
selectedRouteId: null,
|
|
1094
|
+
activeRouteId: null,
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
/** A temporary rendering outage retires pending work but never replays Generate on restore.
|
|
1098
|
+
* Hinglish: Accepted static path reh sakta hai; pending Generate/Start orphan operation na bane. */
|
|
1099
|
+
interrupt() {
|
|
1100
|
+
this.cancelRestore();
|
|
1101
|
+
this.playback.interrupt();
|
|
1102
|
+
this.cancelStep();
|
|
1103
|
+
this.clearStepReads();
|
|
1104
|
+
if (this.state.stage === 'generating')
|
|
1105
|
+
this.clearRoute();
|
|
1106
|
+
else if (this.state.stage === 'starting')
|
|
1107
|
+
this.stopRoute();
|
|
1108
|
+
}
|
|
1109
|
+
destroy() {
|
|
1110
|
+
if (this.destroyed)
|
|
1111
|
+
return;
|
|
1112
|
+
this.destroyed = true;
|
|
1113
|
+
this.retire();
|
|
1114
|
+
this.state = freezeRouteState({
|
|
1115
|
+
stage: 'idle',
|
|
1116
|
+
config: this.state.config,
|
|
1117
|
+
map: null,
|
|
1118
|
+
routeSet: null,
|
|
1119
|
+
selectedRouteId: null,
|
|
1120
|
+
activeRouteId: null,
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
}
|