import type { ComponentType } from 'react'; import type { DebugFeature, DebugFeatureListener, DebugFeatureRenderProps } from '../types'; import type { EventChannel } from './createEventChannel'; import { createObservableStore, type ObservableStore } from './createObservableStore'; import { createPersistedObservableStore, type PersistedObservableStore, } from './createPersistedObservableStore'; import { sanitizeDebugLogEntry } from './deviceReport'; import type { StorageAdapter } from './StorageAdapter'; const DEFAULT_MAX_LOGS = 200; export interface ChannelFeaturePersistConfig { storage: StorageAdapter; storageKey: string; maxPersist: number; debounceMs?: number; serialize?: (entry: TEntry) => unknown; isActive?: () => boolean; } /** * Generic factory for features that subscribe to an event channel. * Eliminates boilerplate across channel-based features (Track, Navigation, Zustand). * * @param getChannel - Function returning the current channel (supports test resets) * @param toEntry - Maps a payload + auto-generated id to a typed log entry * @param options - Feature name, label, render component, and optional maxLogs */ export function createChannelFeature( getChannel: () => EventChannel, toEntry: (payload: TPayload, id: string) => TEntry, options: { name: string; label: string; renderContent?: ComponentType>; maxLogs?: number; persist?: ChannelFeaturePersistConfig; /** Return null to skip, or the (possibly modified) payload to proceed. */ beforePush?: (payload: TPayload) => TPayload | null; /** Called after channel subscription in setup. Return a cleanup function. */ onSetup?: () => (() => void) | void; }, ): DebugFeature { const maxLogs = options.maxLogs ?? DEFAULT_MAX_LOGS; let nextId = 0; let logStore: ObservableStore; let persistedStore: PersistedObservableStore | null = null; let getId: () => string; if (options.persist) { const persisted = createPersistedObservableStore({ storage: options.persist.storage, storageKey: options.persist.storageKey, maxPersist: options.persist.maxPersist, debounceMs: options.persist.debounceMs, serialize: options.persist.serialize, isActive: options.persist.isActive, maxEntries: maxLogs, }); persistedStore = persisted; logStore = persisted; getId = () => persisted.nextId(); } else { logStore = createObservableStore(); getId = () => String(nextId++); } let initialized = false; let unsubscribe: (() => void) | null = null; let customCleanup: (() => void) | null = null; return { name: options.name, label: options.label, status: { phase: 'initializing', issues: [] }, renderContent: options.renderContent, setup: () => { if (initialized || options.persist?.isActive?.() === false) { return; } unsubscribe = getChannel().subscribe((payload) => { const snapshot = sanitizeDebugLogEntry(payload) as TPayload; const filtered = options.beforePush ? options.beforePush(snapshot) : snapshot; if (filtered == null) { return; } logStore.push(sanitizeDebugLogEntry(toEntry(filtered, getId())) as TEntry, maxLogs); }); const cleanup = options.onSetup?.(); if (cleanup) { customCleanup = cleanup; } initialized = true; return persistedStore?.ready; }, getSnapshot: () => logStore.getData(), clear: () => { if (persistedStore) { persistedStore.clearPersisted(); } else { logStore.clear(); } }, cleanup: () => { customCleanup?.(); customCleanup = null; unsubscribe?.(); unsubscribe = null; if (persistedStore) { persistedStore.dispose(); } else { logStore.clear(); } initialized = false; }, subscribe: (listener: DebugFeatureListener) => logStore.subscribe(listener), }; }