Commit a89553e1 authored by vertighel's avatar vertighel
Browse files

Fase 5 JS punto 4: applyTransform() condiviso



- ui-core.js: nuovo helper applyTransform(value, transformName,
  callerLabel), stessa logica prima duplicata quasi verbatim in
  status-stream.js e status-view.js (stesso lookup noctuaTransforms,
  stesso try/catch, differivano solo nel prefisso del warn).
- Entrambi i file ora import { noctuaTransforms } direttamente da
  ui-core.js invece di leggere window.noctuaTransforms, come gia'
  fa synoptic.js.
- Con questo, window.noctuaTransforms in ui-core.js era l'ultimo
  consumer rimasto -- shim morto, rimosso (stesso trattamento di
  window.showToast al punto 1).
- Verificato con node --check sui 3 file toccati.
- PLAN.md aggiornato.

Co-Authored-By: default avatarClaude Sonnet 5 <noreply@anthropic.com>
parent 077f4781
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -511,8 +511,18 @@ Working point by point per the user's request, checking in after each
      for all of those.

      Verified with `node --check` on every touched JS file.
- [ ] Extract shared `applyTransform()` helper (dedupe
      `status-stream.js`/`status-view.js`).
- [x] Extract shared `applyTransform()` helper (dedupe
      `status-stream.js`/`status-view.js`). Added to `ui-core.js`
      (same module as `applyMap`, which both files already used the
      same way). Folded in the "worth tidying" note from the same doc
      section: both files now `import { ... noctuaTransforms ... }`
      directly (matching `synoptic.js`'s existing style) instead of
      reading `window.noctuaTransforms`. That was the last consumer of
      the global, so `ui-core.js`'s `window.noctuaTransforms =
      noctuaTransforms` shim is now dead (confirmed — grepped every
      `.js` file and page) and was removed, same treatment as
      `window.showToast` in point 1. Verified with `node --check` on
      all 3 touched files.
- [ ] Unify the 4 error-toast-extraction idioms into one helper; fix
      `webcam.js` to actually call `showToast` on failure (currently
      only `console.error`s).
+2 −11
Original line number Diff line number Diff line
// status-stream.js
// Standard telemetry listener updating control panels with data-status attributes.

import { applyVarStatusStyles, resolveValue, applyMap } from './ui-core.js';
import { applyVarStatusStyles, resolveValue, applyMap, applyTransform } from './ui-core.js';

document.addEventListener('DOMContentLoaded', () => {
    const previousState = {};
@@ -61,16 +61,7 @@ document.addEventListener('DOMContentLoaded', () => {
            finalValue = applyMap(finalValue, el.dataset.map);

            // 2. Apply numeric transform (data-transform) — e.g. round_0, deg_to_arcsec
            const transformName = el.dataset.transform;
            if (transformName && window.noctuaTransforms?.[transformName]) {
                try {
                    if (finalValue !== null && finalValue !== undefined) {
                        finalValue = window.noctuaTransforms[transformName](finalValue);
                    }
                } catch (e) {
                    console.warn(`status-stream: transform '${transformName}' failed on`, finalValue, e);
                }
            }
            finalValue = applyTransform(finalValue, el.dataset.transform, 'status-stream');

            // 3. Format for display
            let displayValue = (finalValue === null || finalValue === undefined) ? 'N/A' : finalValue;
+2 −11
Original line number Diff line number Diff line
// status-view.js
// Dynamic telemetry update loop. Clones HTML blueprints and maps keys as labels.

import { applyVarStatusStyles, resolveValue, applyMap } from './ui-core.js';
import { applyVarStatusStyles, resolveValue, applyMap, applyTransform } from './ui-core.js';

document.addEventListener('DOMContentLoaded', () => {
    const tableBlueprint    = document.getElementById('table-blueprint');
@@ -144,16 +144,7 @@ document.addEventListener('DOMContentLoaded', () => {
            // Apply map (data-map) then transform (data-transform)
            finalValue = applyMap(finalValue, el.dataset.map);

            const transformName = el.dataset.transform;
            if (transformName && window.noctuaTransforms?.[transformName]) {
                try {
                    if (finalValue !== null && finalValue !== undefined) {
                        finalValue = window.noctuaTransforms[transformName](finalValue);
                    }
                } catch (e) {
                    console.warn(`status-view: transform '${transformName}' failed on`, finalValue, e);
                }
            }
            finalValue = applyTransform(finalValue, el.dataset.transform, 'status-view');

            // Transition from flat <data> to subtable when value becomes an object
            const isFlatVar = el.tagName.toLowerCase() === 'data' && !resolved.subProperty;
+21 −1
Original line number Diff line number Diff line
@@ -171,4 +171,24 @@ export const noctuaTransforms = {
    unix_to_isot:    vectorize((val) => { const n = parseFloat(val); if (isNaN(n)) return val; return new Date(n * 1000).toISOString().split('.')[0]; })
};

window.noctuaTransforms = noctuaTransforms;
/**
 * Applies a noctuaTransforms entry (data-transform attribute) to a value.
 * Shared by status-stream.js and status-view.js. Leaves the value
 * unchanged (and warns) if the named transform throws, or if value is
 * null/undefined/the transform doesn't exist.
 *
 * @param {any} value
 * @param {string} transformName
 * @param {string} callerLabel - prefix for the console.warn message (e.g. "status-view").
 * @returns {any}
 */
export function applyTransform(value, transformName, callerLabel) {
    if (!transformName || !noctuaTransforms[transformName]) return value;
    if (value === null || value === undefined) return value;
    try {
        return noctuaTransforms[transformName](value);
    } catch (e) {
        console.warn(`${callerLabel}: transform '${transformName}' failed on`, value, e);
        return value;
    }
}