| 1 | // Bus de actividad de las herramientas: cada llamada que hace la elfa se anuncia |
| 2 | // al empezar y al acabar. Lo escuchan la vista de ciudad (para encender los |
| 3 | // ficheros que toca) y el registro de su pila, sin que ninguno conozca al otro. |
| 4 | // |
| 5 | // Un listener que lanza NO tumba la herramienta: si la vista está rota, el |
| 6 | // agente sigue trabajando. Mirar no debe cambiar lo que se hace. |
| 7 | |
| 8 | const oyentes = new Set(); |
| 9 | |
| 10 | export function escuchar(fn) { |
| 11 | oyentes.add(fn); |
| 12 | return () => oyentes.delete(fn); |
| 13 | } |
| 14 | |
| 15 | export function anunciar(evento) { |
| 16 | for (const fn of [...oyentes]) { |
| 17 | try { fn(evento); } catch { /* un oyente roto no para a la elfa */ } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | // Qué fichero toca una llamada y de qué manera. Solo lectura o escritura: la |
| 22 | // ciudad pinta «lee» y «escribe» distinto, y `terminal.run` no toca rutas. |
| 23 | const ESCRIBE = new Set(['code.write', 'code.edit']); |
| 24 | const LEE = new Set(['code.read', 'code.tree', 'code.search', 'code.run']); |
| 25 | |
| 26 | export function tocaFichero(herramienta, args) { |
| 27 | if (!args || typeof args.path !== 'string') return null; |
| 28 | if (ESCRIBE.has(herramienta)) return { ruta: args.path, modo: 'escribe' }; |
| 29 | if (LEE.has(herramienta)) return { ruta: args.path, modo: 'lee' }; |
| 30 | return null; |
| 31 | } |