Files
gods-eye-view/src/layers/awareness/dependencies.js
T
Sameh Khamis 63fd559496 Keep the ridden aircraft's layer when Contacts ends, and clear its rows from Live Signals
Riding an aircraft now adopts its layer the way a direct selection does, and
Contacts gives up a dependency the user adopts, so ending Contacts mid-session
(one of its layers off, or Contacts itself) no longer turns off the ridden
layer and ends the Cockpit. When Contacts goes off, Live Signals drops the
rows built from its snapshot instead of leaving stale distances marked
CURRENT, and the feed-off warning is no longer duplicated.
2026-11-05 14:01:17 -08:00

139 lines
4.8 KiB
JavaScript

import { DEPENDENCIES, AIRCRAFT_DEPENDENCIES } from './policy.js';
export function createDependencies({
state: layerState,
services,
parts,
source,
}) {
function refreshAfterDeferredDependency(activationId) {
if (
!layerState.enabled ||
layerState.passive ||
activationId !== layerState.activationId
)
return;
if (layerState.subject) {
parts.subject.refreshSelectedSubject(true);
return;
}
if (layerState.autoFocusRetryPending) {
layerState.autoFocusRetryPending = !parts.focus.focusAttentionTarget();
}
}
async function enableDependencies(activationId = layerState.activationId) {
const aircraftPending = [];
for (const layerId of DEPENDENCIES) {
if (!layerState.enabled) return;
if (!layerState.dataManager) continue;
const alreadyEnabled =
layerState.dataManager.isEffectivelyEnabled?.(layerId) ??
layerState.dataManager.isEnabled(layerId);
if (alreadyEnabled) continue;
layerState.ownedDependencies.add(layerId);
// Each layer serializes its own lifecycle internally. Start them together
// so a slow or unavailable live feed cannot prevent the remaining context
// sources from turning on.
let pending;
try {
// Register the ON intent synchronously. If Contacts is turned OFF in the
// same turn, DataLayerManager then sees OFF as the newest intent and a
// stale deferred ON cannot resurrect a released dependency.
pending = Promise.resolve(
layerState.dataManager.setEnabled(layerId, true),
);
} catch (error) {
pending = Promise.reject(error);
}
if (AIRCRAFT_DEPENDENCIES.includes(layerId)) {
aircraftPending.push(pending);
} else {
// Vessels and mapped installations enrich an already usable aircraft
// context. Their settlement must never hold the Contacts transaction or
// Cockpit gate open, and the activation token prevents a late result
// from repainting or refocusing a newer/closed session.
pending.then(
() => refreshAfterDeferredDependency(activationId),
() => refreshAfterDeferredDependency(activationId),
);
}
}
await Promise.allSettled(aircraftPending);
}
function releaseOwnedDependencies(releaseActivationId) {
const owned = [...layerState.ownedDependencies];
layerState.ownedDependencies.clear();
if (!layerState.dataManager || !owned.length) return Promise.resolve();
return Promise.allSettled(
owned.map((layerId) => layerState.dataManager.setEnabled(layerId, false)),
).then(() => {
if (
layerState.enabled &&
layerState.activationId !== releaseActivationId
) {
return enableDependencies(layerState.activationId);
}
return null;
});
}
/**
* Give up a dependency the user adopts. A direct selection, or the Cockpit
* riding one of its aircraft, promotes a layer Contacts turned on into the
* user's own (`DataLayerManager.adoptLayerVisibility`). Releasing it when
* Contacts goes off would tear down that track or Cockpit before the
* Context restore could put the layer back.
*/
function relinquishAdoptedDependency(change) {
if (change?.type === 'visibility' && change.adopted && change.enabled)
layerState.ownedDependencies.delete(change.layerId);
}
/** Follow one manager's adoptions; null stops following. */
function watchAdoptions(dataManager) {
layerState.unwatchAdoptions?.();
layerState.unwatchAdoptions =
dataManager?.subscribe?.(relinquishAdoptedDependency) || null;
}
function activateOperationalContext() {
const activationId = ++layerState.activationId;
layerState.autoFocusRetryPending = false;
const initialTrackedSubject = parts.subject.currentTrackedFlightSubject();
if (initialTrackedSubject)
parts.subject.selectSubject(initialTrackedSubject);
return enableDependencies(activationId)
.then(() => {
if (
!layerState.enabled ||
layerState.passive ||
activationId !== layerState.activationId
)
return;
const trackedSubject = parts.subject.currentTrackedFlightSubject();
if (trackedSubject) {
parts.subject.selectSubject(trackedSubject);
return;
}
if (layerState.subject) {
parts.subject.selectSubject(layerState.subject);
return;
}
if (layerState.autoFocusAttempted) return;
layerState.autoFocusRetryPending = !parts.focus.focusAttentionTarget();
})
.catch((error) =>
console.warn('[Global Context] dependency enable failed', error),
);
}
return {
refreshAfterDeferredDependency,
enableDependencies,
releaseOwnedDependencies,
watchAdoptions,
activateOperationalContext,
};
}