File size: 3,264 Bytes
641b62c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * 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;