Files
MBN_STOCK_WEBVIEW/Web/playout-safety.js

58 lines
2.0 KiB
JavaScript

(function (root, factory) {
"use strict";
const api = Object.freeze(factory());
if (typeof module === "object" && module.exports) module.exports = api;
if (root) root.MbnPlayoutSafety = api;
})(typeof globalThis === "object" ? globalThis : this, function () {
"use strict";
const connectionStates = new Set([
"disabled", "dry-run-ready", "disconnected", "connecting", "connected",
"reconnecting", "disconnecting", "faulted", "outcome-unknown", "disposed"
]);
function normalizeConnectionState(value) {
const normalized = String(value || "unavailable")
.trim()
.toLocaleLowerCase("en-US")
.replace(/[\s_]+/g, "-");
return connectionStates.has(normalized) ? normalized : "unavailable";
}
function responseTimeoutFromNative(value) {
const operationTimeout = Number(value);
if (!Number.isFinite(operationTimeout) || operationTimeout < 100 || operationTimeout > 300000) {
return 15000;
}
return Math.min(305000, Math.max(5100, Math.trunc(operationTimeout) + 5000));
}
function commandBlockReason(snapshot, command) {
if (!snapshot || snapshot.pending) return "busy";
if (snapshot.outcomeUnknown) return "outcome-unknown";
if (!snapshot.commandAvailable) return "unavailable";
const onAir = snapshot.engineState === "PROGRAM" || Boolean(snapshot.onAirCode);
const prepared = snapshot.engineState === "PREPARED" || Boolean(snapshot.preparedCode);
if (command === "next" && !onAir) return "next-requires-on-air";
if (command === "take-in" && !prepared) return "take-in-requires-prepare";
if (command === "take-out" && !onAir && !prepared) return "nothing-to-take-out";
return null;
}
function matchesPendingCommand(pending, payload) {
return Boolean(
pending && payload &&
payload.requestId === pending.requestId &&
payload.command === pending.command);
}
return {
normalizeConnectionState,
responseTimeoutFromNative,
commandBlockReason,
matchesPendingCommand
};
});