(map: Map<string, Promise<void>>, key: string, task: () => Promise<void>)
| 83 | } |
| 84 | |
| 85 | function runInflight(map: Map<string, Promise<void>>, key: string, task: () => Promise<void>) { |
| 86 | const pending = map.get(key) |
| 87 | if (pending) return pending |
| 88 | const promise = task().finally(() => { |
| 89 | if (map.get(key) === promise) map.delete(key) |
| 90 | }) |
| 91 | map.set(key, promise) |
| 92 | return promise |
| 93 | } |
| 94 | |
| 95 | function merge<T extends { id: string }>(a: readonly T[], b: readonly T[]) { |
| 96 | const items = new Map(a.map((item) => [item.id, item] as const)) |