All files / lib/evolution event.bus.ts

0% Statements 0/24
0% Branches 0/1
0% Functions 0/1
0% Lines 0/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                     
// lib/evolution/event.bus.ts
import { appendEvolutionSnapshot } from './history.collector';
import { alignEvolutionTimeline } from './timeline.align';
 
/**
 * Event Bus بسيط للأحداث الزمنية داخل النظام.
 * عند كل حدث جديد، بنسجّل snapshot ونحدّث الخط الزمني aligned.
 */
 
type EventHandler = (payload?: any) => void;
const listeners: Record<string, EventHandler[]> = {};
 
export function on(event: string, handler: EventHandler) {
  listeners[event] = listeners[event] || [];
  listeners[event].push(handler);
}
 
export function emit(event: string, payload?: any) {
  if (event === 'evolution:update') {
    // كل تحديث للنظام ⇒ snapshot + alignment
    appendEvolutionSnapshot();
    const aligned = alignEvolutionTimeline('daily');
    localStorage.setItem('ALIGNED_TIMELINE', JSON.stringify(aligned));
  }
  (listeners[event] || []).forEach((fn) => fn(payload));
}
 
export function getAlignedTimeline() {
  try {
    return JSON.parse(localStorage.getItem('ALIGNED_TIMELINE') || '[]');
  } catch {
    return [];
  }
}