Coalesce coupled items into units, preserving first-appearance order.
(items: WorkItem<T>[])
| 68 | |
| 69 | /** Coalesce coupled items into units, preserving first-appearance order. */ |
| 70 | function buildUnits<T>(items: WorkItem<T>[]): Unit<T>[] { |
| 71 | const byKey = new Map<string, Unit<T>>(); |
| 72 | const units: Unit<T>[] = []; |
| 73 | items.forEach((item, i) => { |
| 74 | const w = sanitizeWeight(item.weight); |
| 75 | const key = item.coupledKey && item.coupledKey.length > 0 ? item.coupledKey : null; |
| 76 | if (key === null) { |
| 77 | units.push({ key: item.id, weight: w, members: [item], order: i }); |
| 78 | return; |
| 79 | } |
| 80 | const existing = byKey.get(key); |
| 81 | if (existing) { |
| 82 | existing.weight += w; |
| 83 | existing.members.push(item); |
| 84 | } else { |
| 85 | const unit: Unit<T> = { key, weight: w, members: [item], order: i }; |
| 86 | byKey.set(key, unit); |
| 87 | units.push(unit); |
| 88 | } |
| 89 | }); |
| 90 | return units; |
| 91 | } |
| 92 | |
| 93 | /** Decide how many partitions to create, clamped to every relevant bound. */ |
| 94 | function chooseK<T>(units: Unit<T>[], totalWeight: number, opts: PartitionOptions): number { |
no test coverage detected