( records: readonly T[], isActive: (record: T) => boolean, getTimestamp: (record: T) => number, max: number, )
| 15 | * consume it directly without re-sorting. |
| 16 | */ |
| 17 | export function retainActiveFirst<T>( |
| 18 | records: readonly T[], |
| 19 | isActive: (record: T) => boolean, |
| 20 | getTimestamp: (record: T) => number, |
| 21 | max: number, |
| 22 | ): T[] { |
| 23 | const sortDesc = (left: T, right: T) => |
| 24 | getTimestamp(right) - getTimestamp(left) |
| 25 | const active = records.filter(isActive).slice().sort(sortDesc) |
| 26 | const history = records |
| 27 | .filter(record => !isActive(record)) |
| 28 | .slice() |
| 29 | .sort(sortDesc) |
| 30 | .slice(0, Math.max(0, max - active.length)) |
| 31 | return [...active, ...history].sort(sortDesc) |
| 32 | } |
| 33 | |
| 34 | export function getAutonomyPersistenceLockCountForTests(): number { |
| 35 | if (process.env.NODE_ENV !== 'test') { |
no test coverage detected