UNPKG

compote-ui

Version:

An opinionated UI component library for Svelte, built on top of [Ark UI](https://ark-ui.com) with additional components and features not available in the core Ark UI library.

43 lines (42 loc) • 1.64 kB
/** * A per-column counter that goes up on every change to that column's filter value. Unlike * the value itself it never returns to an earlier state, so a filter cleared and then set * back to the same value still reads as changed (the ABA case a value comparison misses). */ export class FilterRevisions { // Raw record replaced on change: reads in templates/deriveds stay reactive. #revisions = $state.raw({}); get(columnId) { return this.#revisions[columnId] ?? 0; } bump(columnIds) { if (columnIds.length === 0) return; const next = { ...this.#revisions }; for (const id of columnIds) next[id] = (next[id] ?? 0) + 1; this.#revisions = next; } } /** Ids of the columns whose filter value differs (by reference) between two filter states. */ export function changedFilterIds(prev, next) { if (prev === next) return []; const before = Object.create(null); const after = Object.create(null); for (const filter of prev) before[filter.id] = filter.value; for (const filter of next) after[filter.id] = filter.value; const ids = Object.keys({ ...before, ...after }); return ids.filter((id) => !Object.is(before[id], after[id])); } const registry = new WeakMap(); /** Links a table to its filter revisions (done by `createTable`). */ export function registerFilterRevisions(table, revisions) { registry.set(table, revisions); } /** The filter revisions of a table made by `createTable`; undefined for any other table. */ export function getFilterRevisions(table) { return registry.get(table); }