import { ActiveSymbol } from './symbols.ts'; import { isState } from './guards.ts'; import type { Active } from './types.ts'; /** * Wraps a reactive container in a readonly view, preventing external mutation. * If the container is already readonly, it is returned as-is with no extra wrapping. * * @example * const count = state(0); // State — mutable * const roCount = readonly(count); // Reactive — readonly * * roCount.current = 1; // TS error: cannot assign to readonly property * count.current = 1; // still works — original is unaffected */ export function readonly(source: Active): Active { // Already readonly — return as-is, no unnecessary wrapping. if (!isState(source)) return source; return { [ActiveSymbol]: true, get current() { return source.current; } }; }