mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 17:52:21 +00:00
Space Missions no longer runs as a Context mode. The launches layer owns its Satellites dependency, including the rule that a Satellites off asked for after Space Missions was switched on ends it (request order decides), and a small controller connects the layer to its own panel and store. Context no longer isolates, snapshots or restores Space Missions or Satellites, so Contacts and Space Missions run together in either order, and scene playback no longer exits an isolating mode. Settled by the owner: Contacts leaves Satellites as it is, and only a person's or voice Satellites off ends Space Missions; a scene, story, preset or other programmatic off does not. Turning the layer on by hand or by voice, choosing it at first run, or picking a mission on the globe opens the Space Missions panel, and on a phone its Missions sheet. Asking voice to open the panel while the layer is off is refused with a reason, and voice never opens it while the Cockpit owns the screen. With no Context mode on, a voice Context off switches Space Missions off. A failed Satellites restore no longer cuts the layer's teardown short. Mission labels on the globe select their mission. The layer names a failed first index load itself, so the Context toast no longer imports Space Missions code. Also carries the satellites request ordering and DataLayerManager.waitForLayerIdle, the share token for the panel, voice panel ids, Cockpit folding the panel away and the phone tab ellipsis. The panel shares the rail gutters and hover border of the other side panels. On top of the Omnibar: "open space missions panel" reaches the new panel, and Open Context no longer answers to "missions". Co-authored-by: Sameh Khamis <[email protected]>
322 lines
11 KiB
JavaScript
322 lines
11 KiB
JavaScript
import { MAX_POST_TLE_RETRIES, POST_TLE_RETRY_DELAY_MS } from './policy.js';
|
|
|
|
/**
|
|
* Let pending Satellites work finish, including requests that join it while
|
|
* waiting, so the mission dependency cannot overtake it. Bounded: a
|
|
* Satellites change that never settles cannot hold missions forever.
|
|
*/
|
|
async function waitForSatelliteVisibility(manager, { signal, timeoutMs }) {
|
|
if (typeof manager.waitForLayerIdle !== 'function') return true;
|
|
return manager.waitForLayerIdle('satellites', { signal, timeoutMs });
|
|
}
|
|
|
|
export function createIngestion({
|
|
state: layerState,
|
|
services,
|
|
parts,
|
|
source,
|
|
}) {
|
|
/**
|
|
* Decide whether one bounded rebuild is needed after the active TLE lookup.
|
|
* @param {object} input Retry state.
|
|
* @param {boolean} input.enabled Whether Space Missions is active.
|
|
* @param {number} input.retryCount Number of post-TLE retries already used.
|
|
* @param {string|null} input.activeTleText Resolved active TLE catalog.
|
|
* @param {string|null} input.renderedTleText TLE catalog used for the last build.
|
|
* @returns {boolean} Whether to schedule a refresh.
|
|
*/
|
|
|
|
function shouldRetryAfterActiveTle({
|
|
enabled,
|
|
retryCount,
|
|
activeTleText,
|
|
renderedTleText,
|
|
}) {
|
|
return Boolean(
|
|
enabled &&
|
|
activeTleText &&
|
|
activeTleText !== renderedTleText &&
|
|
retryCount < MAX_POST_TLE_RETRIES,
|
|
);
|
|
}
|
|
|
|
function clearPostTleRetry() {
|
|
if (layerState._retryTimer) clearTimeout(layerState._retryTimer);
|
|
layerState._retryTimer = null;
|
|
}
|
|
|
|
function schedulePostTleRetry(token) {
|
|
if (
|
|
layerState._retryTimer ||
|
|
token !== layerState._lifecycleToken ||
|
|
!shouldRetryAfterActiveTle({
|
|
enabled: layerState._enabled,
|
|
retryCount: layerState._postTleRetryCount,
|
|
activeTleText: layerState._activeTleText,
|
|
renderedTleText: layerState._renderedTleText,
|
|
})
|
|
)
|
|
return;
|
|
layerState._postTleRetryCount++;
|
|
layerState._retryTimer = setTimeout(() => {
|
|
layerState._retryTimer = null;
|
|
// A failed retry is already recorded in the layer stats.
|
|
if (layerState._enabled && token === layerState._lifecycleToken)
|
|
requestMissionUpdate().catch(() => {});
|
|
}, POST_TLE_RETRY_DELAY_MS);
|
|
}
|
|
|
|
function ensureActiveTleLookup(token) {
|
|
if (layerState._activeTleText)
|
|
return Promise.resolve(layerState._activeTleText);
|
|
if (
|
|
layerState._activeTlePromise &&
|
|
layerState._activeTlePromiseToken === token
|
|
)
|
|
return layerState._activeTlePromise;
|
|
const request = source
|
|
.getActiveTle({ signal: layerState._sourceController.signal })
|
|
.then((text) => {
|
|
if (!layerState._enabled || token !== layerState._lifecycleToken)
|
|
return null;
|
|
layerState._activeTleText = text;
|
|
layerState._focusAfterActiveLookup = Boolean(
|
|
layerState._selectedLaunchId,
|
|
);
|
|
schedulePostTleRetry(token);
|
|
return text;
|
|
})
|
|
.catch((error) => {
|
|
if (layerState._enabled && token === layerState._lifecycleToken) {
|
|
console.warn(
|
|
'[Data:RocketLaunches] Active satellite lookup unavailable:',
|
|
error.message,
|
|
);
|
|
}
|
|
return null;
|
|
})
|
|
.finally(() => {
|
|
if (layerState._activeTlePromise === request) {
|
|
layerState._activeTlePromise = null;
|
|
layerState._activeTlePromiseToken = 0;
|
|
}
|
|
});
|
|
layerState._activeTlePromise = request;
|
|
layerState._activeTlePromiseToken = token;
|
|
return request;
|
|
}
|
|
|
|
async function captureSatelliteDependency() {
|
|
const manager = layerState._dataManager;
|
|
if (!manager || layerState._satelliteStateBeforeMission) return;
|
|
const token = layerState._lifecycleToken;
|
|
// Let a pending Satellites action finish before reserving dependency ON.
|
|
// A successful explicit OFF can cancel this mission entry while we wait.
|
|
const idle = await waitForSatelliteVisibility(manager, {
|
|
signal: layerState._sourceController?.signal,
|
|
timeoutMs: layerState.satelliteSettleTimeoutMs,
|
|
});
|
|
if (token !== layerState._lifecycleToken) return;
|
|
if (!idle && layerState._enabled)
|
|
throw new Error(
|
|
'Space Missions could not start: the satellites layer did not settle',
|
|
);
|
|
if (
|
|
!layerState._enabled ||
|
|
(manager.layers?.has('rocket-launches') &&
|
|
!manager.isEffectivelyEnabled('rocket-launches'))
|
|
)
|
|
return;
|
|
layerState._satelliteStateBeforeMission = {
|
|
enabled: manager.isEnabled('satellites'),
|
|
params: parts.policyHelpers.satelliteParamsAfterSpaceMissions(
|
|
manager.getLayerParams('satellites'),
|
|
),
|
|
};
|
|
manager.setLayerParams(
|
|
'satellites',
|
|
parts.policyHelpers.satelliteParamsForSpaceMissions(
|
|
layerState._satelliteStateBeforeMission.params,
|
|
),
|
|
);
|
|
const activation = Promise.resolve(manager.setEnabled('satellites', true));
|
|
layerState._satelliteActivationPromise = activation;
|
|
try {
|
|
const activated = await activation;
|
|
// Space Missions without its satellite dependency is a broken replay
|
|
// surface; fail the mission enable so the manager's fail-closed path and
|
|
// the Context rollback see an honest failure instead of a silent success.
|
|
if (
|
|
token === layerState._lifecycleToken &&
|
|
layerState._enabled &&
|
|
(activated === false || !manager.isEnabled('satellites'))
|
|
) {
|
|
throw new Error(
|
|
'Space Missions requires the satellites layer, which failed to start',
|
|
);
|
|
}
|
|
} finally {
|
|
if (
|
|
token === layerState._lifecycleToken &&
|
|
layerState._satelliteActivationPromise === activation
|
|
) {
|
|
layerState._satelliteActivationPromise = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
async function restoreSatelliteDependency() {
|
|
const manager = layerState._dataManager;
|
|
const snapshot = layerState._satelliteStateBeforeMission;
|
|
if (!snapshot || !manager) return;
|
|
// Keep the snapshot available until any explicit Satellites action settles.
|
|
const token = layerState._lifecycleToken;
|
|
const idle = await waitForSatelliteVisibility(manager, {
|
|
timeoutMs: layerState.satelliteSettleTimeoutMs,
|
|
});
|
|
if (
|
|
token !== layerState._lifecycleToken ||
|
|
layerState._satelliteStateBeforeMission !== snapshot
|
|
)
|
|
return;
|
|
layerState._satelliteStateBeforeMission = null;
|
|
layerState._satelliteActivationPromise = null;
|
|
manager.setLayerParams(
|
|
'satellites',
|
|
parts.policyHelpers.satelliteParamsAfterSpaceMissions(snapshot.params),
|
|
);
|
|
// An accepted Satellites action owns visibility through mission teardown.
|
|
if (snapshot.restoreVisibility === false) return;
|
|
if (!idle)
|
|
throw new Error(
|
|
'Space Missions could not restore the satellites layer: it did not settle',
|
|
);
|
|
const restored = await manager.setEnabled('satellites', snapshot.enabled);
|
|
if (
|
|
restored === false ||
|
|
manager.isEnabled('satellites') !== snapshot.enabled
|
|
) {
|
|
throw new Error('Space Missions could not restore the satellites layer');
|
|
}
|
|
}
|
|
|
|
async function performMissionUpdate(token) {
|
|
try {
|
|
ensureActiveTleLookup(token);
|
|
const { payload, stale } = source.getLaunchSnapshot
|
|
? await source.getLaunchSnapshot({
|
|
signal: layerState._sourceController.signal,
|
|
})
|
|
: {
|
|
payload: await source.getLaunches({
|
|
signal: layerState._sourceController.signal,
|
|
}),
|
|
stale: false,
|
|
};
|
|
const launches = parts.model.normalizeRocketLaunches(payload);
|
|
if (
|
|
!layerState._enabled ||
|
|
token !== layerState._lifecycleToken ||
|
|
!layerState._missionSets
|
|
)
|
|
return;
|
|
const activeTleText = layerState._activeTleText;
|
|
if (layerState._replayCameraLaunchId) parts.replay.stopMissionReplay();
|
|
layerState._launches = launches;
|
|
parts.orbitRendering.removeMissionOrbitPrimitives();
|
|
parts.rendering.clearLaunchEntities();
|
|
layerState._missionOverlayRecords.clear();
|
|
layerState._satelliteTelemetry.clear();
|
|
layerState._replayTracks.clear();
|
|
layerState._orbitMatches = 0;
|
|
launches.forEach((launch) =>
|
|
parts.rendering.addLaunchEntity(launch, activeTleText, { defer: true }),
|
|
);
|
|
parts.rendering.flushLaunchEntities();
|
|
layerState._renderedTleText = activeTleText;
|
|
if (layerState._renderedTleText === layerState._activeTleText)
|
|
clearPostTleRetry();
|
|
layerState._count = launches.length;
|
|
// The roster reads these to tell a loaded empty index from loading.
|
|
layerState._lastUpdate = Date.now();
|
|
layerState._lastError = null;
|
|
layerState._stale = stale === true;
|
|
parts.panel.renderMissionRoster();
|
|
if (
|
|
!layerState._selectedLaunchId ||
|
|
!launches.some((launch) => launch.id === layerState._selectedLaunchId)
|
|
) {
|
|
parts.selection.setSelectedMission(null, false);
|
|
} else {
|
|
parts.selection.setSelectedMission(layerState._selectedLaunchId, true);
|
|
if (layerState._focusAfterActiveLookup) {
|
|
parts.selection.focusMission(
|
|
launches.find(
|
|
(launch) => launch.id === layerState._selectedLaunchId,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
layerState._focusAfterActiveLookup = false;
|
|
parts.panel.renderMissionPanel();
|
|
layerState.coverageChanged();
|
|
layerState._entryCameraView = null;
|
|
} catch (error) {
|
|
if (layerState._enabled && token === layerState._lifecycleToken) {
|
|
layerState._lastError = error.message;
|
|
console.warn('[Data:RocketLaunches] Fetch error:', error);
|
|
// No index yet: fail the enable so Context rolls back.
|
|
// After a loaded index, a failed refresh keeps its records.
|
|
if (layerState._lastUpdate === null) {
|
|
parts.panel.renderMissionRoster();
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function requestMissionUpdate() {
|
|
if (!layerState._enabled) return Promise.resolve();
|
|
const token = layerState._lifecycleToken;
|
|
layerState._updateDirty = true;
|
|
if (layerState._updatePromise && layerState._updatePromiseToken === token)
|
|
return layerState._updatePromise;
|
|
let request;
|
|
request = (async () => {
|
|
while (
|
|
layerState._enabled &&
|
|
token === layerState._lifecycleToken &&
|
|
layerState._updateDirty
|
|
) {
|
|
layerState._updateDirty = false;
|
|
await performMissionUpdate(token);
|
|
}
|
|
})().finally(() => {
|
|
if (layerState._updatePromise === request) {
|
|
layerState._updatePromise = null;
|
|
layerState._updatePromiseToken = 0;
|
|
}
|
|
});
|
|
layerState._updatePromise = request;
|
|
layerState._updatePromiseToken = token;
|
|
return request;
|
|
}
|
|
const methods = {
|
|
update() {
|
|
return requestMissionUpdate();
|
|
},
|
|
};
|
|
|
|
return {
|
|
shouldRetryAfterActiveTle,
|
|
clearPostTleRetry,
|
|
schedulePostTleRetry,
|
|
ensureActiveTleLookup,
|
|
captureSatelliteDependency,
|
|
restoreSatelliteDependency,
|
|
performMissionUpdate,
|
|
requestMissionUpdate,
|
|
methods,
|
|
};
|
|
}
|