hermes-agent/apps/desktop/src/lib/stable-array.ts
Brooklyn Nicholson b6df712f44 refactor(desktop): DRY the computed-dedup into stableArray + freeze
One shared `stableArray(prev, next)` helper replaces the duplicated
element-equal/keep-prev logic in both stores, and freezes the shared ref so a
future in-place mutation fails loud instead of silently corrupting the cache.
Computed return type is now `readonly string[]` (it always was, immutably).
2026-07-19 19:46:09 -05:00

7 lines
547 B
TypeScript

/** Keep `prev`'s reference when it's element-equal to `next`, so a nanostores
* `computed` (notifies on `!==`) skips the emit when its projected list didn't
* actually change — e.g. status-id sets recomputed on every stream delta.
* `next` is frozen: the ref is shared across ticks, so an in-place mutation
* would corrupt the cache — fail loud instead. */
export const stableArray = <T>(prev: readonly T[], next: T[]): readonly T[] =>
prev.length === next.length && prev.every((v, i) => v === next[i]) ? prev : Object.freeze(next)