@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,771 @@
|
|
|
1
|
+
import { DEFAULT_ROUTE_PLAYBACK_OPTIONS, ROUTE_PLAYBACK_MODES, ROUTE_PLAYBACK_SPEEDS, ViewerCommandError, toViewerCommandError, } from '@wovvmap/contracts';
|
|
2
|
+
function fail(code) {
|
|
3
|
+
throw new ViewerCommandError('Playback operation could not be completed.', { code });
|
|
4
|
+
}
|
|
5
|
+
const validDelay = (value) => value === undefined || (Number.isInteger(value) && value >= 0 && value <= 60000);
|
|
6
|
+
/** Display-only cursor. Replay never grants reverse travel through one-way/airport checkpoints.
|
|
7
|
+
* Hinglish: Clock/cursor yahan; allowed travel, graph aur security rules private backend mein hi hain. */
|
|
8
|
+
export class RoutePlayback {
|
|
9
|
+
changed;
|
|
10
|
+
now;
|
|
11
|
+
current;
|
|
12
|
+
progressed;
|
|
13
|
+
state = Object.freeze({
|
|
14
|
+
status: 'idle',
|
|
15
|
+
...DEFAULT_ROUTE_PLAYBACK_OPTIONS,
|
|
16
|
+
});
|
|
17
|
+
binding;
|
|
18
|
+
scope;
|
|
19
|
+
elapsed = 0;
|
|
20
|
+
playbackId = 0;
|
|
21
|
+
sequence = 0;
|
|
22
|
+
lastEmit = -Infinity;
|
|
23
|
+
progress = null;
|
|
24
|
+
distance = 0;
|
|
25
|
+
lastTime;
|
|
26
|
+
epoch = 0;
|
|
27
|
+
handoff;
|
|
28
|
+
presentationTimer;
|
|
29
|
+
countdownRemaining = 0;
|
|
30
|
+
countdownStartedAt;
|
|
31
|
+
finishDelay = 0;
|
|
32
|
+
completion;
|
|
33
|
+
pending;
|
|
34
|
+
constructor(changed, now = () => performance.now(), current = () => true, progressed = () => { }) {
|
|
35
|
+
this.changed = changed;
|
|
36
|
+
this.now = now;
|
|
37
|
+
this.current = current;
|
|
38
|
+
this.progressed = progressed;
|
|
39
|
+
}
|
|
40
|
+
get snapshot() {
|
|
41
|
+
return this.state;
|
|
42
|
+
}
|
|
43
|
+
get progressSnapshot() {
|
|
44
|
+
return this.progress;
|
|
45
|
+
}
|
|
46
|
+
get preferences() {
|
|
47
|
+
const { mode, speed, cameraFollow, stepSync, startDelayMs, finishView, finishDelayMs } = this.state;
|
|
48
|
+
return {
|
|
49
|
+
mode,
|
|
50
|
+
speed,
|
|
51
|
+
...(cameraFollow !== undefined ? { cameraFollow } : {}),
|
|
52
|
+
...(stepSync !== undefined ? { stepSync } : {}),
|
|
53
|
+
...(startDelayMs !== undefined ? { startDelayMs } : {}),
|
|
54
|
+
...(finishView !== undefined ? { finishView } : {}),
|
|
55
|
+
...(finishDelayMs !== undefined ? { finishDelayMs } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
emitProgress(force = false, time = this.now()) {
|
|
59
|
+
if (!this.scope ||
|
|
60
|
+
!this.playbackId ||
|
|
61
|
+
(!force && (!Number.isFinite(time) || time - this.lastEmit < 250)))
|
|
62
|
+
return;
|
|
63
|
+
if (!force && !Number.isFinite(this.lastEmit) && this.progress) {
|
|
64
|
+
this.lastEmit = time;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
this.lastEmit = Number.isFinite(time) ? time : -Infinity;
|
|
68
|
+
const step = this.state.stepSync === 'highlight' && this.state.status !== 'stopped'
|
|
69
|
+
? (this.binding?.activeStep?.(this.scope, this.distance) ?? null)
|
|
70
|
+
: null;
|
|
71
|
+
const total = this.state.mode === 'full-route'
|
|
72
|
+
? (this.scope.totalLength ?? this.scope.length)
|
|
73
|
+
: this.scope.length;
|
|
74
|
+
const offset = this.state.mode === 'full-route' ? (this.scope.offset ?? 0) : 0;
|
|
75
|
+
this.progress = Object.freeze({
|
|
76
|
+
playbackId: String(this.playbackId),
|
|
77
|
+
sequence: ++this.sequence,
|
|
78
|
+
progress: this.state.status === 'stopped'
|
|
79
|
+
? 0
|
|
80
|
+
: total
|
|
81
|
+
? Math.min(1, (offset + this.distance) / total)
|
|
82
|
+
: this.state.status === 'completed'
|
|
83
|
+
? 1
|
|
84
|
+
: 0,
|
|
85
|
+
elapsedSeconds: this.elapsed,
|
|
86
|
+
mode: this.state.mode,
|
|
87
|
+
floorIndex: this.scope.floorIndex,
|
|
88
|
+
activeStep: step ? Object.freeze({ stepIndex: step.stepIndex, stepId: step.stepId }) : null,
|
|
89
|
+
});
|
|
90
|
+
this.progressed(this.progress);
|
|
91
|
+
}
|
|
92
|
+
get running() {
|
|
93
|
+
return this.state.status === 'running';
|
|
94
|
+
}
|
|
95
|
+
resetClock() {
|
|
96
|
+
this.lastTime = undefined;
|
|
97
|
+
// The existing engine visibility reset is sufficient; a hidden countdown needs no new lifecycle watcher.
|
|
98
|
+
// Hinglish: Timer throttle hone se pehle pause ho; cancel/release ke reset se recursion na ho.
|
|
99
|
+
if (this.state.status === 'countdown' &&
|
|
100
|
+
this.countdownStartedAt !== undefined &&
|
|
101
|
+
!Number.isFinite(this.now()))
|
|
102
|
+
this.pause();
|
|
103
|
+
}
|
|
104
|
+
interrupt() {
|
|
105
|
+
const epoch = this.epoch;
|
|
106
|
+
this.cancelCompletion();
|
|
107
|
+
if (this.epoch !== epoch)
|
|
108
|
+
return;
|
|
109
|
+
if (['starting', 'countdown', 'running', 'transitioning'].includes(this.state.status))
|
|
110
|
+
this.pause();
|
|
111
|
+
else
|
|
112
|
+
this.resetClock();
|
|
113
|
+
}
|
|
114
|
+
/** Manual input always cancels automatic presentation, even when simulation follow is off.
|
|
115
|
+
* Hinglish: Countdown/finish ka old camera action user ke pan/zoom ko overwrite na kare. */
|
|
116
|
+
beforeCameraChange() {
|
|
117
|
+
const epoch = this.epoch;
|
|
118
|
+
this.cancelCompletion();
|
|
119
|
+
if (this.epoch !== epoch)
|
|
120
|
+
return;
|
|
121
|
+
if (['starting', 'countdown', 'transitioning', 'completed'].includes(this.state.status) ||
|
|
122
|
+
this.state.cameraFollow === 'follow')
|
|
123
|
+
this.pause();
|
|
124
|
+
}
|
|
125
|
+
unavailable() {
|
|
126
|
+
const unavailableReason = 'Playback is unavailable for this route, mode or floor.';
|
|
127
|
+
if (this.state.unavailableReason !== unavailableReason)
|
|
128
|
+
this.publish({ ...this.state, unavailableReason });
|
|
129
|
+
}
|
|
130
|
+
publish(state) {
|
|
131
|
+
const next = Object.freeze({ ...this.preferences, ...state }), epoch = this.epoch;
|
|
132
|
+
this.binding?.options?.(next, next.status === 'running');
|
|
133
|
+
if (this.epoch !== epoch)
|
|
134
|
+
return;
|
|
135
|
+
this.state = next;
|
|
136
|
+
this.changed();
|
|
137
|
+
if (this.epoch === epoch && this.state.status !== 'starting')
|
|
138
|
+
this.emitProgress(true);
|
|
139
|
+
}
|
|
140
|
+
cancel() {
|
|
141
|
+
const epoch = ++this.epoch;
|
|
142
|
+
this.countdownRemaining = this.remainingCountdown();
|
|
143
|
+
this.countdownStartedAt = undefined;
|
|
144
|
+
clearTimeout(this.presentationTimer);
|
|
145
|
+
this.presentationTimer = undefined;
|
|
146
|
+
const pending = this.pending, handoff = this.handoff;
|
|
147
|
+
this.pending = undefined;
|
|
148
|
+
this.handoff = undefined;
|
|
149
|
+
// Detach old slots before abort callbacks can synchronously issue a newer command.
|
|
150
|
+
// Hinglish: Cleanup ke event se naya Replay aaye to old operation uska pending slot na mitaaye.
|
|
151
|
+
pending?.cancel();
|
|
152
|
+
handoff?.abort();
|
|
153
|
+
this.cancelCompletion();
|
|
154
|
+
return epoch;
|
|
155
|
+
}
|
|
156
|
+
release() {
|
|
157
|
+
this.binding?.release();
|
|
158
|
+
this.binding?.bind(null);
|
|
159
|
+
this.resetClock();
|
|
160
|
+
}
|
|
161
|
+
/** Retire resources/callbacks, retaining only viewer preferences, never a stale route scope.
|
|
162
|
+
* Hinglish: Clear/new route par old cursor khatam; mode/speed isi viewer mein bachein. */
|
|
163
|
+
retire() {
|
|
164
|
+
const epoch = this.cancel();
|
|
165
|
+
if (this.epoch !== epoch)
|
|
166
|
+
return;
|
|
167
|
+
this.release();
|
|
168
|
+
if (this.epoch !== epoch)
|
|
169
|
+
return;
|
|
170
|
+
this.binding = undefined;
|
|
171
|
+
this.scope = undefined;
|
|
172
|
+
this.distance = 0;
|
|
173
|
+
this.state = Object.freeze({ ...this.preferences, status: 'idle' });
|
|
174
|
+
this.elapsed = 0;
|
|
175
|
+
this.countdownRemaining = 0;
|
|
176
|
+
const hadProgress = this.progress !== null;
|
|
177
|
+
this.progress = null;
|
|
178
|
+
if (hadProgress)
|
|
179
|
+
this.progressed(null);
|
|
180
|
+
}
|
|
181
|
+
update(options) {
|
|
182
|
+
if (!options ||
|
|
183
|
+
typeof options !== 'object' ||
|
|
184
|
+
Array.isArray(options) ||
|
|
185
|
+
Object.keys(options).some((key) => ![
|
|
186
|
+
'mode',
|
|
187
|
+
'speed',
|
|
188
|
+
'cameraFollow',
|
|
189
|
+
'stepSync',
|
|
190
|
+
'startDelayMs',
|
|
191
|
+
'finishView',
|
|
192
|
+
'finishDelayMs',
|
|
193
|
+
].includes(key)) ||
|
|
194
|
+
!validDelay(options.startDelayMs) ||
|
|
195
|
+
!validDelay(options.finishDelayMs) ||
|
|
196
|
+
(options.finishView !== undefined &&
|
|
197
|
+
!['preserve', 'route-overview'].includes(options.finishView)) ||
|
|
198
|
+
(options.cameraFollow !== undefined && !['off', 'follow'].includes(options.cameraFollow)) ||
|
|
199
|
+
(options.stepSync !== undefined && !['off', 'highlight'].includes(options.stepSync)) ||
|
|
200
|
+
(options.mode !== undefined && !ROUTE_PLAYBACK_MODES.includes(options.mode)) ||
|
|
201
|
+
(options.speed !== undefined && !ROUTE_PLAYBACK_SPEEDS.includes(options.speed)))
|
|
202
|
+
fail('INVALID_PLAYBACK_OPTIONS');
|
|
203
|
+
const mode = options.mode ?? this.state.mode, speed = options.speed ?? this.state.speed;
|
|
204
|
+
const cameraFollow = options.cameraFollow ?? this.state.cameraFollow ?? 'off';
|
|
205
|
+
const stepSync = options.stepSync ?? this.state.stepSync ?? 'off';
|
|
206
|
+
const settings = {
|
|
207
|
+
mode,
|
|
208
|
+
speed,
|
|
209
|
+
...(options.cameraFollow !== undefined ? { cameraFollow } : {}),
|
|
210
|
+
...(options.stepSync !== undefined ? { stepSync } : {}),
|
|
211
|
+
...(options.startDelayMs !== undefined ? { startDelayMs: options.startDelayMs } : {}),
|
|
212
|
+
...(options.finishView !== undefined ? { finishView: options.finishView } : {}),
|
|
213
|
+
...(options.finishDelayMs !== undefined ? { finishDelayMs: options.finishDelayMs } : {}),
|
|
214
|
+
};
|
|
215
|
+
if (mode === this.state.mode &&
|
|
216
|
+
speed === this.state.speed &&
|
|
217
|
+
cameraFollow === (this.state.cameraFollow ?? 'off') &&
|
|
218
|
+
stepSync === (this.state.stepSync ?? 'off') &&
|
|
219
|
+
(options.startDelayMs ?? this.state.startDelayMs ?? 0) === (this.state.startDelayMs ?? 0) &&
|
|
220
|
+
(options.finishView ?? this.state.finishView ?? 'preserve') ===
|
|
221
|
+
(this.state.finishView ?? 'preserve') &&
|
|
222
|
+
(options.finishDelayMs ?? this.state.finishDelayMs ?? 0) === (this.state.finishDelayMs ?? 0))
|
|
223
|
+
return false;
|
|
224
|
+
if (stepSync !== (this.state.stepSync ?? 'off') &&
|
|
225
|
+
['starting', 'countdown', 'running', 'transitioning'].includes(this.state.status))
|
|
226
|
+
fail('INVALID_PLAYBACK_STATE');
|
|
227
|
+
const changeMode = mode !== this.state.mode;
|
|
228
|
+
if (changeMode &&
|
|
229
|
+
['starting', 'countdown', 'running', 'transitioning', 'paused'].includes(this.state.status))
|
|
230
|
+
fail('INVALID_PLAYBACK_STATE');
|
|
231
|
+
// Settle at the old speed, then publish options and completion together before notifying consumers.
|
|
232
|
+
// Hinglish: Completion callback ka newer intent old speed-patch ke baad aaye, overwrite na ho.
|
|
233
|
+
const previous = this.state;
|
|
234
|
+
if (options.finishView === 'preserve')
|
|
235
|
+
this.cancelCompletion();
|
|
236
|
+
if (this.running)
|
|
237
|
+
this.advance(this.now());
|
|
238
|
+
if (this.state !== previous)
|
|
239
|
+
return true;
|
|
240
|
+
if (changeMode) {
|
|
241
|
+
const epoch = this.cancel();
|
|
242
|
+
if (this.epoch !== epoch)
|
|
243
|
+
return true;
|
|
244
|
+
this.release();
|
|
245
|
+
if (this.epoch !== epoch)
|
|
246
|
+
return true;
|
|
247
|
+
this.scope = undefined;
|
|
248
|
+
this.binding = undefined;
|
|
249
|
+
this.distance = 0;
|
|
250
|
+
const hadProgress = this.progress !== null;
|
|
251
|
+
this.progress = null;
|
|
252
|
+
this.elapsed = 0;
|
|
253
|
+
this.publish({ status: 'idle', ...settings });
|
|
254
|
+
if (hadProgress)
|
|
255
|
+
this.progressed(null);
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
const completed = this.running && !!this.scope && this.distance >= this.scope.length;
|
|
259
|
+
if (completed && this.state.mode === 'full-route' && this.binding?.next?.(this.scope)) {
|
|
260
|
+
this.publish({ ...this.state, ...settings });
|
|
261
|
+
this.settle();
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
if (completed)
|
|
265
|
+
this.finish(settings);
|
|
266
|
+
else
|
|
267
|
+
this.publish({ ...this.state, ...settings });
|
|
268
|
+
}
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
start(binding, kind, before) {
|
|
272
|
+
if (this.pending && kind === 'play')
|
|
273
|
+
return this.pending.promise;
|
|
274
|
+
if (this.running && kind !== 'replay')
|
|
275
|
+
return Promise.resolve();
|
|
276
|
+
if (kind === 'play' && !['idle', 'stopped'].includes(this.state.status))
|
|
277
|
+
fail('INVALID_PLAYBACK_STATE');
|
|
278
|
+
if (kind === 'resume' && this.state.status !== 'paused')
|
|
279
|
+
fail('INVALID_PLAYBACK_STATE');
|
|
280
|
+
if (kind === 'replay' && this.state.status === 'idle')
|
|
281
|
+
fail('INVALID_PLAYBACK_STATE');
|
|
282
|
+
// Validate before cancellation/mutation: a wrong-floor Resume must leave the paused marker intact.
|
|
283
|
+
// Hinglish: Invalid intent old accepted cursor/clock ko na badle.
|
|
284
|
+
let scope;
|
|
285
|
+
try {
|
|
286
|
+
scope = binding.scope(this.state.mode, kind === 'resume' || (kind === 'replay' && this.state.mode === 'current-floor')
|
|
287
|
+
? this.scope
|
|
288
|
+
: undefined);
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
if (this.state.status === 'idle' || this.state.status === 'stopped') {
|
|
292
|
+
this.unavailable();
|
|
293
|
+
}
|
|
294
|
+
throw error;
|
|
295
|
+
}
|
|
296
|
+
const preferences = this.state, previous = this.state.status === 'starting' ? (this.pending?.previous ?? this.state) : this.state, previousCursor = {
|
|
297
|
+
binding: this.binding,
|
|
298
|
+
scope: this.scope,
|
|
299
|
+
distance: this.distance,
|
|
300
|
+
elapsed: this.elapsed,
|
|
301
|
+
playbackId: this.playbackId,
|
|
302
|
+
progress: this.progress,
|
|
303
|
+
remaining: this.remainingCountdown(),
|
|
304
|
+
finishDelay: this.finishDelay,
|
|
305
|
+
};
|
|
306
|
+
const epoch = this.cancel();
|
|
307
|
+
if (this.epoch !== epoch)
|
|
308
|
+
fail('CANCELLED');
|
|
309
|
+
const controller = new AbortController();
|
|
310
|
+
let rejectCancel;
|
|
311
|
+
const cancelled = new Promise((_, reject) => {
|
|
312
|
+
rejectCancel = reject;
|
|
313
|
+
});
|
|
314
|
+
const entry = {
|
|
315
|
+
previous,
|
|
316
|
+
retain: kind === 'resume',
|
|
317
|
+
promise: Promise.resolve(),
|
|
318
|
+
cancel: () => {
|
|
319
|
+
controller.abort();
|
|
320
|
+
rejectCancel(new ViewerCommandError('Playback cancelled.', { code: 'CANCELLED' }));
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
this.pending = entry;
|
|
324
|
+
let adopted = false;
|
|
325
|
+
let committed = false;
|
|
326
|
+
entry.promise = Promise.race([
|
|
327
|
+
Promise.resolve().then(() => binding.ready?.(this.state, controller.signal)),
|
|
328
|
+
cancelled,
|
|
329
|
+
])
|
|
330
|
+
.then(async () => {
|
|
331
|
+
if (this.epoch !== epoch || this.pending !== entry)
|
|
332
|
+
fail('CANCELLED');
|
|
333
|
+
binding.scope(this.state.mode, kind === 'resume' || this.state.mode === 'current-floor' ? scope : undefined);
|
|
334
|
+
before();
|
|
335
|
+
if (this.epoch !== epoch || this.pending !== entry)
|
|
336
|
+
fail('CANCELLED');
|
|
337
|
+
const adopt = () => {
|
|
338
|
+
if (this.epoch !== epoch || this.pending !== entry)
|
|
339
|
+
fail('CANCELLED');
|
|
340
|
+
adopted = true;
|
|
341
|
+
this.binding = binding;
|
|
342
|
+
this.scope = scope;
|
|
343
|
+
this.distance = kind === 'resume' ? previousCursor.distance : 0;
|
|
344
|
+
this.elapsed = kind === 'resume' ? previousCursor.elapsed : 0;
|
|
345
|
+
if (kind !== 'resume')
|
|
346
|
+
this.playbackId++;
|
|
347
|
+
if (kind !== 'resume') {
|
|
348
|
+
this.countdownRemaining = preferences.startDelayMs ?? 0;
|
|
349
|
+
this.finishDelay = preferences.finishDelayMs ?? 0;
|
|
350
|
+
}
|
|
351
|
+
this.lastEmit = -Infinity;
|
|
352
|
+
binding.options?.(this.state, false);
|
|
353
|
+
this.resetClock();
|
|
354
|
+
if (scope.length)
|
|
355
|
+
binding.position(scope, this.distance);
|
|
356
|
+
committed = true;
|
|
357
|
+
if (binding.enter)
|
|
358
|
+
this.emitProgress(true);
|
|
359
|
+
};
|
|
360
|
+
if (binding.enter)
|
|
361
|
+
await Promise.race([
|
|
362
|
+
Promise.resolve(binding.enter(scope, controller.signal, adopt)),
|
|
363
|
+
cancelled,
|
|
364
|
+
]);
|
|
365
|
+
else
|
|
366
|
+
adopt();
|
|
367
|
+
if (!adopted || this.epoch !== epoch || this.pending !== entry)
|
|
368
|
+
fail('CANCELLED');
|
|
369
|
+
if (this.countdownRemaining > 0)
|
|
370
|
+
this.beginCountdown(epoch);
|
|
371
|
+
else
|
|
372
|
+
this.activate(epoch);
|
|
373
|
+
if (this.epoch !== epoch)
|
|
374
|
+
fail('CANCELLED');
|
|
375
|
+
})
|
|
376
|
+
.catch((error) => {
|
|
377
|
+
if (this.epoch === epoch) {
|
|
378
|
+
// An already published floor is never rolled back behind an observer's newer intent.
|
|
379
|
+
// Hinglish: Floor mount ho gaya par frame fail hua to adopted entry par paused retry rahe.
|
|
380
|
+
if (adopted && committed && binding.enter) {
|
|
381
|
+
this.release();
|
|
382
|
+
if (this.epoch !== epoch)
|
|
383
|
+
throw error;
|
|
384
|
+
this.publish({
|
|
385
|
+
...this.state,
|
|
386
|
+
status: 'paused',
|
|
387
|
+
unavailableReason: 'Playback renderer is unavailable.',
|
|
388
|
+
});
|
|
389
|
+
throw error;
|
|
390
|
+
}
|
|
391
|
+
// Roll back only a failed adoption; a readiness failure must retain an already accepted cursor.
|
|
392
|
+
// Hinglish: Partial driver hatao; Replay fail ho to old run/distance restore ho, zero jump nahi.
|
|
393
|
+
if (adopted) {
|
|
394
|
+
this.release();
|
|
395
|
+
if (this.epoch !== epoch)
|
|
396
|
+
throw error;
|
|
397
|
+
}
|
|
398
|
+
this.binding = previousCursor.binding;
|
|
399
|
+
this.scope = previousCursor.scope;
|
|
400
|
+
this.distance = previousCursor.distance;
|
|
401
|
+
this.elapsed = previousCursor.elapsed;
|
|
402
|
+
this.playbackId = previousCursor.playbackId;
|
|
403
|
+
this.progress = previousCursor.progress;
|
|
404
|
+
this.countdownRemaining = previousCursor.remaining;
|
|
405
|
+
this.finishDelay = previousCursor.finishDelay;
|
|
406
|
+
const failure = toViewerCommandError(error);
|
|
407
|
+
this.state = Object.freeze({
|
|
408
|
+
...previous,
|
|
409
|
+
...(['transitioning', 'countdown'].includes(previous.status)
|
|
410
|
+
? { status: 'paused' }
|
|
411
|
+
: {}),
|
|
412
|
+
...this.preferences,
|
|
413
|
+
...(previous.status !== 'running' && failure && failure.code !== 'CANCELLED'
|
|
414
|
+
? { unavailableReason: 'Playback renderer is unavailable.' }
|
|
415
|
+
: {}),
|
|
416
|
+
});
|
|
417
|
+
this.resetClock();
|
|
418
|
+
if (this.binding &&
|
|
419
|
+
this.scope &&
|
|
420
|
+
(previous.status === 'running' ||
|
|
421
|
+
previous.status === 'paused' ||
|
|
422
|
+
previous.status === 'transitioning' ||
|
|
423
|
+
previous.status === 'countdown')) {
|
|
424
|
+
try {
|
|
425
|
+
this.binding.options?.(this.state, previous.status === 'running');
|
|
426
|
+
if (adopted)
|
|
427
|
+
this.binding.position(this.scope, this.distance);
|
|
428
|
+
if (this.epoch === epoch)
|
|
429
|
+
this.binding?.bind(this);
|
|
430
|
+
}
|
|
431
|
+
catch {
|
|
432
|
+
if (this.epoch !== epoch)
|
|
433
|
+
throw error;
|
|
434
|
+
this.release();
|
|
435
|
+
if (this.epoch !== epoch)
|
|
436
|
+
throw error;
|
|
437
|
+
this.binding = undefined;
|
|
438
|
+
this.distance = 0;
|
|
439
|
+
this.state = Object.freeze({
|
|
440
|
+
...this.state,
|
|
441
|
+
status: 'stopped',
|
|
442
|
+
unavailableReason: 'Playback renderer is unavailable.',
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (this.epoch === epoch)
|
|
447
|
+
this.changed();
|
|
448
|
+
}
|
|
449
|
+
throw error;
|
|
450
|
+
})
|
|
451
|
+
.finally(() => {
|
|
452
|
+
if (this.pending === entry)
|
|
453
|
+
this.pending = undefined;
|
|
454
|
+
});
|
|
455
|
+
this.publish({ status: 'starting', mode: preferences.mode, speed: preferences.speed });
|
|
456
|
+
return entry.promise;
|
|
457
|
+
}
|
|
458
|
+
pause() {
|
|
459
|
+
const epoch = this.epoch;
|
|
460
|
+
this.cancelCompletion();
|
|
461
|
+
if (this.epoch !== epoch)
|
|
462
|
+
return;
|
|
463
|
+
if (this.state.status === 'completed') {
|
|
464
|
+
this.cancel();
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (this.state.status === 'countdown') {
|
|
468
|
+
const cancelled = this.cancel();
|
|
469
|
+
if (this.epoch !== cancelled)
|
|
470
|
+
return;
|
|
471
|
+
this.publish({
|
|
472
|
+
...this.state,
|
|
473
|
+
status: 'paused',
|
|
474
|
+
countdownSeconds: Math.ceil(this.countdownRemaining / 1000),
|
|
475
|
+
});
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
if (this.state.status === 'transitioning') {
|
|
479
|
+
const cancelled = this.cancel();
|
|
480
|
+
if (this.epoch !== cancelled)
|
|
481
|
+
return;
|
|
482
|
+
this.resetClock();
|
|
483
|
+
this.publish({ ...this.state, status: 'paused' });
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (this.state.status === 'starting') {
|
|
487
|
+
const retain = this.pending?.retain && this.pending.previous.status === 'paused' && !!this.scope;
|
|
488
|
+
const cancelled = this.cancel();
|
|
489
|
+
if (this.epoch !== cancelled)
|
|
490
|
+
return;
|
|
491
|
+
this.release();
|
|
492
|
+
if (this.epoch !== cancelled)
|
|
493
|
+
return;
|
|
494
|
+
if (retain) {
|
|
495
|
+
this.publish({ ...this.state, status: 'paused' });
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
this.binding = undefined;
|
|
499
|
+
this.scope = undefined;
|
|
500
|
+
this.distance = 0;
|
|
501
|
+
this.elapsed = 0;
|
|
502
|
+
this.countdownRemaining = 0;
|
|
503
|
+
const hadProgress = this.progress !== null;
|
|
504
|
+
this.progress = null;
|
|
505
|
+
this.publish({ status: 'idle', mode: this.state.mode, speed: this.state.speed });
|
|
506
|
+
if (hadProgress)
|
|
507
|
+
this.progressed(null);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (!this.running)
|
|
511
|
+
return;
|
|
512
|
+
this.tick(this.now());
|
|
513
|
+
if (this.snapshot.status === 'transitioning') {
|
|
514
|
+
this.pause();
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if (!this.running)
|
|
518
|
+
return;
|
|
519
|
+
this.resetClock();
|
|
520
|
+
this.publish({ ...this.state, status: 'paused' });
|
|
521
|
+
if (this.state.status === 'paused')
|
|
522
|
+
this.binding?.bind(this);
|
|
523
|
+
}
|
|
524
|
+
stop() {
|
|
525
|
+
if (this.state.status === 'idle' || this.state.status === 'stopped')
|
|
526
|
+
return;
|
|
527
|
+
const epoch = this.cancel();
|
|
528
|
+
if (this.epoch !== epoch)
|
|
529
|
+
return;
|
|
530
|
+
this.release();
|
|
531
|
+
if (this.epoch !== epoch)
|
|
532
|
+
return;
|
|
533
|
+
this.distance = 0;
|
|
534
|
+
this.elapsed = 0;
|
|
535
|
+
this.countdownRemaining = 0;
|
|
536
|
+
this.publish({ status: 'stopped', mode: this.state.mode, speed: this.state.speed });
|
|
537
|
+
}
|
|
538
|
+
/** Engine RAF only; no per-frame publication. Invalid/first deltas are zero, stalls capped at100ms.
|
|
539
|
+
* Hinglish: Har frame UI/Native event nahi; completion par sirf ek compact state update ho.
|
|
540
|
+
*/
|
|
541
|
+
tick(time) {
|
|
542
|
+
const changed = this.advance(time);
|
|
543
|
+
this.settle();
|
|
544
|
+
if (changed && this.running)
|
|
545
|
+
this.emitProgress(false, time);
|
|
546
|
+
return changed;
|
|
547
|
+
}
|
|
548
|
+
/** Consume only the next server-approved visit; no graph lookup, floor watcher or second clock.
|
|
549
|
+
* Hinglish: Boundary par ek cancellable handoff; zero visits bhi finite ordered profile se aage badhein. */
|
|
550
|
+
settle() {
|
|
551
|
+
if (this.running && this.scope && this.distance >= this.scope.length) {
|
|
552
|
+
const next = this.state.mode === 'full-route' ? this.binding?.next?.(this.scope) : undefined;
|
|
553
|
+
if (next) {
|
|
554
|
+
this.transition(next);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
this.finish();
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
remainingCountdown(time = performance.now()) {
|
|
561
|
+
// Readiness can return NaN during context loss; the same monotonic deadline still measures explicit interruption.
|
|
562
|
+
// Hinglish: Render unavailable ho tab bhi Pause ka exact remaining delay bache; naya clock/RAF nahi.
|
|
563
|
+
return this.countdownStartedAt === undefined
|
|
564
|
+
? this.countdownRemaining
|
|
565
|
+
: Math.max(0, this.countdownRemaining - Math.max(0, time - this.countdownStartedAt));
|
|
566
|
+
}
|
|
567
|
+
/** One deadline, no RAF/interval; only changed whole seconds reach host UI.
|
|
568
|
+
* Hinglish: Paused/hidden wall time simulation ya countdown mein catch-up na bane. */
|
|
569
|
+
beginCountdown(epoch) {
|
|
570
|
+
this.countdownStartedAt = performance.now();
|
|
571
|
+
this.binding?.bind(this);
|
|
572
|
+
if (this.epoch !== epoch)
|
|
573
|
+
return;
|
|
574
|
+
this.publish({
|
|
575
|
+
...this.preferences,
|
|
576
|
+
status: 'countdown',
|
|
577
|
+
countdownSeconds: Math.ceil(this.countdownRemaining / 1000),
|
|
578
|
+
});
|
|
579
|
+
const update = () => {
|
|
580
|
+
this.presentationTimer = undefined;
|
|
581
|
+
if (this.epoch !== epoch || this.state.status !== 'countdown' || !this.current())
|
|
582
|
+
return;
|
|
583
|
+
if (!Number.isFinite(this.now())) {
|
|
584
|
+
// No interruption event arrived: retain the last ready checkpoint, not throttled hidden wall time.
|
|
585
|
+
// Hinglish: Hidden timer late fire ho to wait skip na ho; last accepted remaining value freeze karo.
|
|
586
|
+
this.countdownStartedAt = undefined;
|
|
587
|
+
this.pause();
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
const time = performance.now(), remaining = this.remainingCountdown(time);
|
|
591
|
+
if (remaining <= 0) {
|
|
592
|
+
this.countdownRemaining = 0;
|
|
593
|
+
this.countdownStartedAt = undefined;
|
|
594
|
+
try {
|
|
595
|
+
this.activate(epoch);
|
|
596
|
+
}
|
|
597
|
+
catch {
|
|
598
|
+
if (this.epoch !== epoch)
|
|
599
|
+
return;
|
|
600
|
+
this.release();
|
|
601
|
+
if (this.epoch === epoch)
|
|
602
|
+
this.publish({
|
|
603
|
+
...this.preferences,
|
|
604
|
+
status: 'paused',
|
|
605
|
+
unavailableReason: 'Playback renderer is unavailable.',
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
this.countdownRemaining = remaining;
|
|
611
|
+
this.countdownStartedAt = time;
|
|
612
|
+
const seconds = Math.ceil(remaining / 1000);
|
|
613
|
+
if (this.state.countdownSeconds !== seconds)
|
|
614
|
+
this.publish({ ...this.state, countdownSeconds: seconds });
|
|
615
|
+
if (this.epoch === epoch && this.state.status === 'countdown')
|
|
616
|
+
this.presentationTimer = setTimeout(update, Math.max(1, Math.ceil(remaining - (seconds - 1) * 1000)));
|
|
617
|
+
};
|
|
618
|
+
if (this.epoch === epoch && this.state.status === 'countdown')
|
|
619
|
+
update();
|
|
620
|
+
}
|
|
621
|
+
activate(epoch) {
|
|
622
|
+
if (this.scope?.length) {
|
|
623
|
+
this.binding?.options?.(this.state, true);
|
|
624
|
+
if (this.epoch !== epoch)
|
|
625
|
+
return;
|
|
626
|
+
this.binding?.bind(this);
|
|
627
|
+
}
|
|
628
|
+
if (this.epoch !== epoch)
|
|
629
|
+
return;
|
|
630
|
+
this.resetClock();
|
|
631
|
+
this.publish({ ...this.preferences, status: 'running' });
|
|
632
|
+
this.settle();
|
|
633
|
+
}
|
|
634
|
+
cancelCompletion() {
|
|
635
|
+
if (!this.completion)
|
|
636
|
+
return;
|
|
637
|
+
const controller = this.completion;
|
|
638
|
+
this.completion = undefined;
|
|
639
|
+
clearTimeout(this.presentationTimer);
|
|
640
|
+
this.presentationTimer = undefined;
|
|
641
|
+
controller.abort();
|
|
642
|
+
}
|
|
643
|
+
/** Completion is semantic immediately; optional framing is cancellable presentation only.
|
|
644
|
+
* Hinglish: Replay/newer input ko delay block na kare; camera failure completed ko undo nahi kare. */
|
|
645
|
+
finish(settings = {}) {
|
|
646
|
+
const epoch = this.epoch, binding = this.binding, scope = this.scope;
|
|
647
|
+
this.release();
|
|
648
|
+
if (this.epoch !== epoch)
|
|
649
|
+
return;
|
|
650
|
+
this.publish({ ...this.preferences, ...settings, status: 'completed' });
|
|
651
|
+
if (this.epoch !== epoch ||
|
|
652
|
+
this.state.status !== 'completed' ||
|
|
653
|
+
this.state.finishView !== 'route-overview' ||
|
|
654
|
+
!binding?.focusCompletion ||
|
|
655
|
+
!scope)
|
|
656
|
+
return;
|
|
657
|
+
const controller = new AbortController();
|
|
658
|
+
this.completion = controller;
|
|
659
|
+
const current = () => this.epoch === epoch && this.completion === controller && !controller.signal.aborted;
|
|
660
|
+
const focus = async () => {
|
|
661
|
+
this.presentationTimer = undefined;
|
|
662
|
+
if (!current() || !this.current())
|
|
663
|
+
return;
|
|
664
|
+
try {
|
|
665
|
+
await binding.focusCompletion(scope, this.state.mode, controller.signal);
|
|
666
|
+
}
|
|
667
|
+
catch (error) {
|
|
668
|
+
if (current() && toViewerCommandError(error)?.code !== 'CANCELLED')
|
|
669
|
+
this.publish({ ...this.state, unavailableReason: 'Route overview could not be shown.' });
|
|
670
|
+
}
|
|
671
|
+
finally {
|
|
672
|
+
if (this.completion === controller)
|
|
673
|
+
this.completion = undefined;
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
if (this.finishDelay > 0)
|
|
677
|
+
this.presentationTimer = setTimeout(() => {
|
|
678
|
+
void focus();
|
|
679
|
+
}, this.finishDelay);
|
|
680
|
+
else
|
|
681
|
+
void Promise.resolve().then(focus);
|
|
682
|
+
}
|
|
683
|
+
transition(next) {
|
|
684
|
+
const binding = this.binding, source = this.scope, epoch = this.epoch, controller = new AbortController();
|
|
685
|
+
this.handoff = controller;
|
|
686
|
+
// Keep the inactive driver bound while awaiting readiness; do not allocate decorative arrows or spin RAF.
|
|
687
|
+
// Hinglish: Pending floor par cursor/follow release ho, simulator ka naya clock/arrow mesh na bane.
|
|
688
|
+
binding.release();
|
|
689
|
+
this.resetClock();
|
|
690
|
+
if (this.epoch !== epoch || this.handoff !== controller)
|
|
691
|
+
return;
|
|
692
|
+
this.publish({ ...this.state, status: 'transitioning', transition: next.transition });
|
|
693
|
+
const current = () => this.epoch === epoch && this.handoff === controller && !controller.signal.aborted;
|
|
694
|
+
if (!current())
|
|
695
|
+
return;
|
|
696
|
+
const adopt = () => {
|
|
697
|
+
// A synchronous scene callback can Pause after mount but before adoption; retain that target paused.
|
|
698
|
+
// Hinglish: Sirf usi Pause ka target commit ho; Stop/new route/newer floor ko kabhi resurrect na kare.
|
|
699
|
+
const pausedDuringMount = this.epoch === epoch + 1 &&
|
|
700
|
+
this.state.status === 'paused' &&
|
|
701
|
+
this.scope === source &&
|
|
702
|
+
this.binding === binding;
|
|
703
|
+
if ((!current() && !pausedDuringMount) || !this.current())
|
|
704
|
+
fail('CANCELLED');
|
|
705
|
+
this.scope = next;
|
|
706
|
+
this.distance = 0;
|
|
707
|
+
this.resetClock();
|
|
708
|
+
if (next.length)
|
|
709
|
+
binding.position(next, 0);
|
|
710
|
+
this.emitProgress(true);
|
|
711
|
+
};
|
|
712
|
+
void Promise.resolve()
|
|
713
|
+
.then(async () => {
|
|
714
|
+
if (!current() || !this.current())
|
|
715
|
+
fail('CANCELLED');
|
|
716
|
+
if (!binding.enter)
|
|
717
|
+
fail('UNAVAILABLE');
|
|
718
|
+
await binding.enter(next, controller.signal, adopt);
|
|
719
|
+
if (!current() || !this.current())
|
|
720
|
+
fail('CANCELLED');
|
|
721
|
+
this.resetClock();
|
|
722
|
+
binding.options?.(this.state, true);
|
|
723
|
+
if (next.length)
|
|
724
|
+
binding.position(next, 0, 0);
|
|
725
|
+
if (this.epoch !== epoch)
|
|
726
|
+
return;
|
|
727
|
+
binding.bind(this);
|
|
728
|
+
if (this.epoch !== epoch)
|
|
729
|
+
return;
|
|
730
|
+
this.handoff = undefined;
|
|
731
|
+
this.publish({ ...this.state, status: 'running', transition: undefined });
|
|
732
|
+
this.settle();
|
|
733
|
+
})
|
|
734
|
+
.catch((error) => {
|
|
735
|
+
if (!current())
|
|
736
|
+
return;
|
|
737
|
+
this.release();
|
|
738
|
+
if (!current())
|
|
739
|
+
return;
|
|
740
|
+
this.handoff = undefined;
|
|
741
|
+
this.resetClock();
|
|
742
|
+
this.publish({
|
|
743
|
+
...this.state,
|
|
744
|
+
status: 'paused',
|
|
745
|
+
unavailableReason: toViewerCommandError(error)?.code === 'CANCELLED'
|
|
746
|
+
? undefined
|
|
747
|
+
: 'Next floor could not be prepared. Resume to retry.',
|
|
748
|
+
});
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
advance(time) {
|
|
752
|
+
if (!this.running || !this.scope || !this.binding)
|
|
753
|
+
return false;
|
|
754
|
+
if (!this.current())
|
|
755
|
+
return true;
|
|
756
|
+
if (!Number.isFinite(time)) {
|
|
757
|
+
this.resetClock();
|
|
758
|
+
return false;
|
|
759
|
+
}
|
|
760
|
+
const delta = this.lastTime !== undefined ? Math.max(0, Math.min(0.1, (time - this.lastTime) / 1000)) : 0;
|
|
761
|
+
this.lastTime = Math.max(time, this.lastTime ?? time);
|
|
762
|
+
const remaining = (this.scope.length - this.distance) / (12 * this.state.speed);
|
|
763
|
+
this.elapsed += Math.min(delta, remaining);
|
|
764
|
+
this.distance = Math.min(this.scope.length, this.distance + delta * 12 * this.state.speed);
|
|
765
|
+
if (this.state.cameraFollow === 'follow')
|
|
766
|
+
this.binding.position(this.scope, this.distance, delta);
|
|
767
|
+
else
|
|
768
|
+
this.binding.position(this.scope, this.distance);
|
|
769
|
+
return true;
|
|
770
|
+
}
|
|
771
|
+
}
|