import { type PrimativeKey, type ReadKeyFunction, type ReadRequiredKeyFunction } from '../key'; import { type MapFunction } from '../value/map'; import { type Maybe } from '../value/maybe.type'; /** * Configuration of a {@link PruneTreesFunction}: how to read the items of a flat list of trees. * * @template T The item type. * @template K The id type. */ export interface PruneTreesFunctionConfig { /** * Reads an item's id. Ids are unique. */ readonly readId: ReadRequiredKeyFunction; /** * Reads an item's parent id. An item with no parent id, or whose parent is not in the list, is a root. */ readonly readParentId: ReadKeyFunction; /** * Reads the value items are aged by, such as a creation time. Smaller is older. Ties keep the list order: a later item counts as newer. */ readonly readOrder: MapFunction; /** * Groups the roots. Each group keeps its own maxRoots roots, and the trees of each group keep their own maxLeaves leaves. Defaults to a single * group. */ readonly readRootGroup?: Maybe>; /** * Whether an item is preferred as the leaf a kept root protects, such as a successful result over a failed one. Each kept root protects the * path to its newest preferred leaf, or to its newest leaf when it has no preferred one. Defaults to preferring every leaf. */ readonly isPreferredLeaf?: Maybe>; } /** * Input of a {@link PruneTreesFunction}. * * @template T The item type. * @template K The id type. */ export interface PruneTreesParams { /** * The items: a flat list of trees, linked by parent id. */ readonly items: readonly T[]; /** * Max roots kept per root group. Unlimited when unset. */ readonly maxRoots?: Maybe; /** * Max leaves (items with no kept children) kept in the trees of each root group. Unlimited when unset. */ readonly maxLeaves?: Maybe; /** * Ids of the items that are always kept, with their ancestors, whatever the limits. */ readonly pinnedIds?: Maybe>; } /** * Result of a {@link PruneTreesFunction}. Both lists keep the input order. * * @template T The item type. */ export interface PruneTreesResult { readonly keep: T[]; readonly remove: T[]; } /** * Prunes a flat list of trees (items linked by parent id) to a root limit and a leaf limit. See {@link pruneTreesFunction}. * * @template T The item type. * @template K The id type. */ export type PruneTreesFunction = (params: PruneTreesParams) => PruneTreesResult; /** * Creates a {@link PruneTreesFunction}, which prunes a flat list of trees (items linked by parent id), oldest first, such as a history of * generations where each one derives from a parent generation. * * - Roots are capped per root group. The oldest roots past maxRoots are removed with their descendants. * - Leaves are capped per root group's trees. Past maxLeaves the oldest leaf is removed first. Removing a leaf can leave its parent as a leaf, * which is then removed in turn, so only leaves are ever removed and no kept item loses its parent. * - Each kept root protects the path to its newest (preferred) leaf, even past maxLeaves, so every kept root keeps its latest result. * - Pinned items are always kept with their ancestors. * * @param config - How to read the items. * @returns The prune function. * * @dbxUtil * @dbxUtilCategory tree * @dbxUtilKind factory * @dbxUtilTags tree, prune, history, limit, leaf, root, parent, lineage, retention, oldest * @dbxUtilRelated expand-trees, flatten-tree-to-array-function * * @example * ```ts * const pruneRuns = pruneTreesFunction({ readId: (x) => x.id, readParentId: (x) => x.parentId, readOrder: (x) => x.createdAt.getTime() }); * const { keep } = pruneRuns({ items: runs, maxRoots: 25, maxLeaves: 25, pinnedIds: [selectedRunId] }); * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function pruneTreesFunction(config: PruneTreesFunctionConfig): PruneTreesFunction;