import { Observable } from "@noya-app/observable"; export type ServerScriptLogLevel = "log" | "warn" | "error"; export type ServerScriptLogEntry = { id: string; scriptId: string; level: ServerScriptLogLevel; values: unknown[]; timestamp: number; origin?: string; target?: string; mode?: string; }; export type LogManagerOptions = { maxEntries?: number; }; const DEFAULT_MAX_ENTRIES = 50; /** * Stores server script log entries emitted over the multiplayer channel. */ export class LogManager { logs$ = new Observable([]); constructor(private options: LogManagerOptions = {}) {} append(entry: ServerScriptLogEntry) { this.logs$.set(this.truncate([...this.logs$.get(), entry])); } appendMany(entries: ServerScriptLogEntry[]) { if (entries.length === 0) return; this.logs$.set(this.truncate([...this.logs$.get(), ...entries])); } clear() { this.logs$.set([]); } private truncate(entries: ServerScriptLogEntry[]) { const limit = this.options.maxEntries ?? DEFAULT_MAX_ENTRIES; if (limit <= 0) return entries; return entries.slice(-limit); } }