Spaces:
Sleeping
Sleeping
| /** | |
| * pythonVarsStore.ts — Store Zustand per le variabili Python persistenti (S71) | |
| * | |
| * Aggiornato in tempo reale dopo ogni esecuzione run_python: | |
| * - networkTools.ts chiama usePythonVarsStore.getState().updateVars() dal bridge | |
| * - PythonVarsPanel.tsx legge e visualizza con ricerca, filtri, expand, copia | |
| * | |
| * Le variabili sopravvivono tra messaggi della stessa sessione — il worker singleton | |
| * (S70) mantiene l'interprete Pyodide vivo, quindi anche il namespace Python persiste. | |
| */ | |
| import { create } from "zustand"; | |
| // ─── Tipi ──────────────────────────────────────────────────────────────────── | |
| export interface PythonVar { | |
| name: string; | |
| type: string; // "int", "float", "str", "bool", "list", "dict", "ndarray", "DataFrame", "Series", "set", "function", "module", "complex", "bytes", "NoneType", … | |
| preview: string; // anteprima breve ≤80 chars | |
| value: string; // rappresentazione completa (fino a 8000 chars) | |
| size?: string; // "3 el.", "shape (100,5)", "42 char", "12 chiavi" | |
| dtype?: string; // numpy/pandas dtype (es. "float64", "int32") | |
| runIndex: number; // numero del run che ha prodotto/aggiornato questa var | |
| updatedAt: number; // timestamp ms | |
| } | |
| interface PythonVarsState { | |
| vars: PythonVar[]; | |
| runIndex: number; // monotòno, incrementa ad ogni updateVars() | |
| totalRuns: number; // quanti run_python sono stati completati | |
| lastRunAt: number; // timestamp dell'ultimo run | |
| // Actions | |
| updateVars: (incoming: Omit<PythonVar, "runIndex" | "updatedAt">[]) => void; | |
| clearVars: () => void; | |
| removeVar: (name: string) => void; | |
| } | |
| // ─── Store ─────────────────────────────────────────────────────────────────── | |
| export const usePythonVarsStore = create<PythonVarsState>((set, get) => ({ | |
| vars: [], | |
| runIndex: 0, | |
| totalRuns: 0, | |
| lastRunAt: 0, | |
| updateVars: (incoming) => { | |
| const now = Date.now(); | |
| const nextRun = get().runIndex + 1; | |
| const existing = new Map(get().vars.map(v => [v.name, v])); | |
| for (const iv of incoming) { | |
| existing.set(iv.name, { | |
| ...iv, | |
| runIndex: nextRun, | |
| updatedAt: now, | |
| }); | |
| } | |
| set({ | |
| vars: Array.from(existing.values()), | |
| runIndex: nextRun, | |
| totalRuns: get().totalRuns + 1, | |
| lastRunAt: now, | |
| }); | |
| }, | |
| clearVars: () => set({ vars: [], runIndex: 0, totalRuns: 0, lastRunAt: 0 }), | |
| removeVar: (name) => | |
| set(s => ({ vars: s.vars.filter(v => v.name !== name) })), | |
| })); | |
| // ─── Selectors ──────────────────────────────────────────────────────────────── | |
| export const selectPythonVars = (s: PythonVarsState) => s.vars; | |
| export const selectPyRunIndex = (s: PythonVarsState) => s.runIndex; | |
| export const selectPyTotalRuns = (s: PythonVarsState) => s.totalRuns; | |
| export const selectPyLastRunAt = (s: PythonVarsState) => s.lastRunAt; | |